Jack2: JackShmMem.h Source File

Jack2 1.9.12

JackShmMem.h
1 /*
2 Copyright (C) 2004-2008 Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 */
19
20 #ifndef __JackShmMem__
21 #define __JackShmMem__
22
23 #include "shm.h"
24 #include "JackError.h"
25 #include "JackCompilerDeps.h"
26
27 #include <new> // GCC 4.0
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include "JackShmMem_os.h"
32
33 namespace Jack
34 {
35
36 void LockMemoryImp(void* ptr, size_t size);
37 void InitLockMemoryImp(void* ptr, size_t size);
38 void UnlockMemoryImp(void* ptr, size_t size);
39 void LockAllMemory();
40 void UnlockAllMemory();
41
42 class JackMem
43 {
44 private:
45
46 size_t fSize;
47 static size_t gSize;
48
49 protected:
50
51 JackMem(): fSize(gSize)
52 {}
53 ~JackMem()
54 {}
55
56 public:
57
58 void* operator new(size_t size)
59 {
60 gSize = size;
61 return calloc(1, size);
62 }
63
64 void operator delete(void* ptr, size_t size)
65 {
66 free(ptr);
67 }
68
69 void LockMemory()
70 {
71 LockMemoryImp(this, fSize);
72 }
73
74 void UnlockMemory()
75 {
76 UnlockMemoryImp(this, fSize);
77 }
78
79 };
80
87 class JackShmMemAble
88 {
89 protected:
90
91 jack_shm_info_t fInfo;
92
93 public:
94
95 void Init();
96
97 int GetShmIndex()
98 {
99 return fInfo.index;
100 }
101
102 char* GetShmAddress()
103 {
104 return (char*)fInfo.ptr.attached_at;
105 }
106
107 void LockMemory()
108 {
109 LockMemoryImp(this, fInfo.size);
110 }
111
112 void UnlockMemory()
113 {
114 UnlockMemoryImp(this, fInfo.size);
115 }
116
117 };
118
125 class SERVER_EXPORT JackShmMem : public JackShmMemAble
126 {
127
128 protected:
129
130 JackShmMem();
131 ~JackShmMem();
132
133 public:
134
135 void* operator new(size_t size);
136 void* operator new(size_t size, void* memory);
137
138 void operator delete(void* p, size_t size);
139 void operator delete(void* p);
140
141 };
142
147 template <class T>
148 class JackShmReadWritePtr
149 {
150
151 private:
152
153 jack_shm_info_t fInfo;
154 bool fInitDone;
155
156 void Init(int index, const char* server_name = JACK_DEFAULT_SERVER_NAME)
157 {
158 if (fInfo.index < 0 && index >= 0) {
159 jack_log("JackShmReadWritePtr::Init %ld %d", index, fInfo.index);
160 if (jack_initialize_shm(server_name) < 0) {
161 throw std::bad_alloc();
162 }
163 fInfo.index = index;
164 if (jack_attach_lib_shm(&fInfo)) {
165 throw std::bad_alloc();
166 }
167 GetShmAddress()->LockMemory();
168 fInitDone = true;
169 }
170 }
171
172 public:
173
174 JackShmReadWritePtr()
175 {
176 fInfo.index = -1;
177 fInitDone = false;
178 fInfo.ptr.attached_at = (char*)NULL;
179 }
180
181 JackShmReadWritePtr(int index, const char* server_name)
182 {
183 Init(index, server_name);
184 }
185
186 ~JackShmReadWritePtr()
187 {
188 if (!fInitDone) {
189 jack_error("JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for %d, skipping unlock", fInfo.index);
190 return;
191 }
192 if (fInfo.index >= 0) {
193 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %d", fInfo.index);
194 GetShmAddress()->UnlockMemory();
195 jack_release_lib_shm(&fInfo);
196 fInfo.index = -1;
197 }
198 }
199
200 T* operator->() const
201 {
202 return (T*)fInfo.ptr.attached_at;
203 }
204
205 operator T*() const
206 {
207 return (T*)fInfo.ptr.attached_at;
208 }
209
210 JackShmReadWritePtr& operator=(int index)
211 {
212 Init(index);
213 return *this;
214 }
215
216 void SetShmIndex(int index, const char* server_name)
217 {
218 Init(index, server_name);
219 }
220
221 int GetShmIndex()
222 {
223 return fInfo.index;
224 }
225
226 T* GetShmAddress()
227 {
228 return (T*)fInfo.ptr.attached_at;
229 }
230 };
231
236 template <class T>
237 class JackShmReadWritePtr1
238 {
239
240 private:
241
242 jack_shm_info_t fInfo;
243 bool fInitDone;
244
245 void Init(int index, const char* server_name = JACK_DEFAULT_SERVER_NAME)
246 {
247 if (fInfo.index < 0 && index >= 0) {
248 jack_log("JackShmReadWritePtr1::Init %ld %d", index, fInfo.index);
249 if (jack_initialize_shm(server_name) < 0) {
250 throw std::bad_alloc();
251 }
252 fInfo.index = index;
253 if (jack_attach_lib_shm(&fInfo)) {
254 throw std::bad_alloc();
255 }
256 GetShmAddress()->LockMemory();
257 fInitDone = true;
258 /*
259 nobody else needs to access this shared memory any more, so
260 destroy it. because we have our own attachment to it, it won't
261 vanish till we exit (and release it).
262 */
263 jack_destroy_shm(&fInfo);
264 }
265 }
266
267 public:
268
269 JackShmReadWritePtr1()
270 {
271 fInfo.index = -1;
272 fInitDone = false;
273 fInfo.ptr.attached_at = NULL;
274 }
275
276 JackShmReadWritePtr1(int index, const char* server_name)
277 {
278 Init(index, server_name);
279 }
280
281 ~JackShmReadWritePtr1()
282 {
283 if (!fInitDone) {
284 jack_error("JackShmReadWritePtr1::~JackShmReadWritePtr1 - Init not done for %d, skipping unlock", fInfo.index);
285 return;
286 }
287 if (fInfo.index >= 0) {
288 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %d", fInfo.index);
289 GetShmAddress()->UnlockMemory();
290 jack_release_lib_shm(&fInfo);
291 fInfo.index = -1;
292 }
293 }
294
295 T* operator->() const
296 {
297 return (T*)fInfo.ptr.attached_at;
298 }
299
300 operator T*() const
301 {
302 return (T*)fInfo.ptr.attached_at;
303 }
304
305 JackShmReadWritePtr1& operator=(int index)
306 {
307 Init(index);
308 return *this;
309 }
310
311 void SetShmIndex(int index, const char* server_name)
312 {
313 Init(index, server_name);
314 }
315
316 int GetShmIndex()
317 {
318 return fInfo.index;
319 }
320
321 T* GetShmAddress()
322 {
323 return (T*)fInfo.ptr.attached_at;
324 }
325 };
326
331 template <class T>
332 class JackShmReadPtr
333 {
334
335 private:
336
337 jack_shm_info_t fInfo;
338 bool fInitDone;
339
340 void Init(int index, const char* server_name = JACK_DEFAULT_SERVER_NAME)
341 {
342 if (fInfo.index < 0 && index >= 0) {
343 jack_log("JackShmPtrRead::Init %ld %d", index, fInfo.index);
344 if (jack_initialize_shm(server_name) < 0) {
345 throw std::bad_alloc();
346 }
347 fInfo.index = index;
348 if (jack_attach_lib_shm_read(&fInfo)) {
349 throw std::bad_alloc();
350 }
351 GetShmAddress()->LockMemory();
352 fInitDone = true;
353 }
354 }
355
356 public:
357
358 JackShmReadPtr()
359 {
360 fInfo.index = -1;
361 fInitDone = false;
362 fInfo.ptr.attached_at = NULL;
363 }
364
365 JackShmReadPtr(int index, const char* server_name)
366 {
367 Init(index, server_name);
368 }
369
370 ~JackShmReadPtr()
371 {
372 if (!fInitDone) {
373 jack_error("JackShmReadPtr::~JackShmReadPtr - Init not done for %ld, skipping unlock", fInfo.index);
374 return;
375 }
376 if (fInfo.index >= 0) {
377 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
378 GetShmAddress()->UnlockMemory();
379 jack_release_lib_shm(&fInfo);
380 fInfo.index = -1;
381 }
382 }
383
384 T* operator->() const
385 {
386 return (T*)fInfo.ptr.attached_at;
387 }
388
389 operator T*() const
390 {
391 return (T*)fInfo.ptr.attached_at;
392 }
393
394 JackShmReadPtr& operator=(int index)
395 {
396 Init(index);
397 return *this;
398 }
399
400 void SetShmIndex(int index, const char* server_name)
401 {
402 Init(index, server_name);
403 }
404
405 int GetShmIndex()
406 {
407 return fInfo.index;
408 }
409
410 T* GetShmAddress()
411 {
412 return (T*)fInfo.ptr.attached_at;
413 }
414
415 };
416
417 } // end of namespace
418
419 #endif
_jack_shm_info
Definition: shm.h:170
Jack::JackShmReadPtr
Pointer on shared memory segment in the client side.
Definition: JackShmMem.h:332
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
Jack::JackMem
Definition: JackShmMem.h:42
Jack::JackShmMemAble
A class which objects possibly want to be allocated in shared memory derives from this class.
Definition: JackShmMem.h:87
Jack::JackShmReadWritePtr
Pointer on shared memory segment in the client side.
Definition: JackShmMem.h:148
Jack::JackShmReadWritePtr1
Pointer on shared memory segment in the client side: destroy the segment (used client control)
Definition: JackShmMem.h:237
Jack::JackShmMem
The base class for shared memory management.
Definition: JackShmMem.h:125
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108