/
shm.h
191 lines (157 loc) · 4.58 KB
/
shm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef __SHM_H__
#define __SHM_H__
#ifdef HAVE_CONFIG_H
#include "gnashconfig.h"
#endif
#include <string>
//#include "impl.h"
#include "as_object.h" // for inheritance
#include <sys/types.h>
#if !defined(HAVE_WINSOCK_H) && !defined(__riscos__) && !defined(__OS2__)
# include <sys/ipc.h>
# include <sys/shm.h>
#elif !defined(__riscos__) && !defined(__OS2__)
# include <windows.h>
# include <process.h>
# include <fcntl.h>
# include <io.h>
#endif
// Forward declarations
namespace gnash {
class fn_call;
}
namespace gnash {
#ifndef MAP_INHERIT
const int MAP_INHERIT = 0;
#endif
#ifndef MAP_HASSEMAPHORE
const int MAP_HASSEMAPHORE = 0;
#endif
const int MAX_SHM_NAME_SIZE = 48;
class Shm {
public:
DSOEXPORT Shm();
DSOEXPORT ~Shm();
// Initialize the shared memory segment
bool attach();
bool DSOEXPORT attach(char const *filespec, bool nuke);
bool DSOEXPORT attach(key_t key, bool nuke);
// Resize the allocated memory segment
bool resize(int bytes);
bool resize();
// Allocate a memory from the shared memory segment
void *brk(int bytes);
// Close the memory segment. This removes it from the system.
bool DSOEXPORT closeMem();
Shm *cloneSelf(void);
// Accessors for testing
char *getAddr() { return _addr; };
char *getName() { return _filespec; };
size_t getSize() { return _size; };
int getAllocated() { return _alloced; };
bool exists();
protected:
char *_addr;
long _alloced;
size_t _size;
char _filespec[MAX_SHM_NAME_SIZE];
#if !defined(HAVE_WINSOCK_H) || defined(__OS2__)
key_t _shmkey;
#else
long _shmkey;
HANDLE _shmhandle;
#endif
int _shmfd;
};
// Custome memory allocator for the shared memory segment
template<typename _Tp>
class ShmAlloc
{
private:
Shm *mmptr;
Shm mem;
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
template<typename _Tp1>
struct rebind
{ typedef ShmAlloc<_Tp1> other; };
ShmAlloc() throw() { }
ShmAlloc(const ShmAlloc& other) throw()
: mem(other.mem)
{ }
template<typename _Tp1>
ShmAlloc(const ShmAlloc<_Tp1>& other) throw()
: mem(other.mem)
{ }
~ShmAlloc() throw() { }
pointer
address(reference __x) const { return &__x; }
const_pointer
address(const_reference __x) const { return &__x; }
// Allocate memory
_Tp*
allocate(size_type n, const void* p = 0) {
// If the memory manager has no blocks, it hasn't been
// initialized.
// if (mminit == false) {
// mmptr = mem.initMemManager(true);
// mminit = true;
// } else {
// mmptr = mem.initMemManager(false);
// }
_Tp* ret = 0;
if (n) {
ret = (_Tp*)mmptr->brk(n * sizeof(_Tp));
if (ret == 0)
throw std::bad_alloc();
}
return ret;
}
// Deallocate memory
void
deallocate(pointer __p, size_type __n) {
//mmptr->free(__p);
}
void construct(pointer __p, const _Tp& __val) {
new(__p) _Tp(__val);
}
void destroy(pointer __p) { __p->~_Tp(); }
};
class shm_as_object : public as_object
{
public:
Shm obj;
};
as_value shm_getname(const fn_call& fn);
as_value shm_getsize(const fn_call& fn);
as_value shm_getallocated(const fn_call& fn);
as_value shm_exists(const fn_call& fn);
} // end of gnash namespace
// end of __SHM_H__
#endif
// Local Variables:
// mode: C++
// indent-tabs-mode: t
// End: