Jack2  1.9.9
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 
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>
149 {
150 
151  private:
152 
153  jack_shm_info_t fInfo;
154 
155  void Init(int index, const char* server_name = "default")
156  {
157  if (fInfo.index < 0 && index >= 0) {
158  jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
159  if (jack_initialize_shm(server_name) < 0) {
160  throw std::bad_alloc();
161  }
162  fInfo.index = index;
163  if (jack_attach_lib_shm(&fInfo)) {
164  throw std::bad_alloc();
165  }
166  GetShmAddress()->LockMemory();
167  }
168  }
169 
170  public:
171 
173  {
174  fInfo.index = -1;
175  fInfo.ptr.attached_at = (char*)NULL;
176  }
177 
178  JackShmReadWritePtr(int index, const char* server_name)
179  {
180  Init(index, server_name);
181  }
182 
184  {
185  if (fInfo.index >= 0) {
186  jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
187  GetShmAddress()->UnlockMemory();
188  jack_release_lib_shm(&fInfo);
189  fInfo.index = -1;
190  }
191  }
192 
193  T* operator->() const
194  {
195  return (T*)fInfo.ptr.attached_at;
196  }
197 
198  operator T*() const
199  {
200  return (T*)fInfo.ptr.attached_at;
201  }
202 
203  JackShmReadWritePtr& operator=(int index)
204  {
205  Init(index);
206  return *this;
207  }
208 
209  void SetShmIndex(int index, const char* server_name)
210  {
211  Init(index, server_name);
212  }
213 
214  int GetShmIndex()
215  {
216  return fInfo.index;
217  }
218 
219  T* GetShmAddress()
220  {
221  return (T*)fInfo.ptr.attached_at;
222  }
223 };
224 
229 template <class T>
231 {
232 
233  private:
234 
235  jack_shm_info_t fInfo;
236 
237  void Init(int index, const char* server_name = "default")
238  {
239  if (fInfo.index < 0 && index >= 0) {
240  jack_log("JackShmReadWritePtr1::Init %ld %ld", index, fInfo.index);
241  if (jack_initialize_shm(server_name) < 0) {
242  throw std::bad_alloc();
243  }
244  fInfo.index = index;
245  if (jack_attach_lib_shm(&fInfo)) {
246  throw std::bad_alloc();
247  }
248  GetShmAddress()->LockMemory();
249  /*
250  nobody else needs to access this shared memory any more, so
251  destroy it. because we have our own attachment to it, it won't
252  vanish till we exit (and release it).
253  */
254  jack_destroy_shm(&fInfo);
255  }
256  }
257 
258  public:
259 
261  {
262  fInfo.index = -1;
263  fInfo.ptr.attached_at = NULL;
264  }
265 
266  JackShmReadWritePtr1(int index, const char* server_name)
267  {
268  Init(index, server_name);
269  }
270 
272  {
273  if (fInfo.index >= 0) {
274  jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
275  GetShmAddress()->UnlockMemory();
276  jack_release_lib_shm(&fInfo);
277  fInfo.index = -1;
278  }
279  }
280 
281  T* operator->() const
282  {
283  return (T*)fInfo.ptr.attached_at;
284  }
285 
286  operator T*() const
287  {
288  return (T*)fInfo.ptr.attached_at;
289  }
290 
291  JackShmReadWritePtr1& operator=(int index)
292  {
293  Init(index);
294  return *this;
295  }
296 
297  void SetShmIndex(int index, const char* server_name)
298  {
299  Init(index, server_name);
300  }
301 
302  int GetShmIndex()
303  {
304  return fInfo.index;
305  }
306 
307  T* GetShmAddress()
308  {
309  return (T*)fInfo.ptr.attached_at;
310  }
311 };
312 
317 template <class T>
319 {
320 
321  private:
322 
323  jack_shm_info_t fInfo;
324 
325  void Init(int index, const char* server_name = "default")
326  {
327  if (fInfo.index < 0 && index >= 0) {
328  jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
329  if (jack_initialize_shm(server_name) < 0) {
330  throw std::bad_alloc();
331  }
332  fInfo.index = index;
333  if (jack_attach_lib_shm_read(&fInfo)) {
334  throw std::bad_alloc();
335  }
336  GetShmAddress()->LockMemory();
337  }
338  }
339 
340  public:
341 
343  {
344  fInfo.index = -1;
345  fInfo.ptr.attached_at = NULL;
346  }
347 
348  JackShmReadPtr(int index, const char* server_name)
349  {
350  Init(index, server_name);
351  }
352 
353  ~JackShmReadPtr()
354  {
355  if (fInfo.index >= 0) {
356  jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
357  GetShmAddress()->UnlockMemory();
358  jack_release_lib_shm(&fInfo);
359  fInfo.index = -1;
360  }
361  }
362 
363  T* operator->() const
364  {
365  return (T*)fInfo.ptr.attached_at;
366  }
367 
368  operator T*() const
369  {
370  return (T*)fInfo.ptr.attached_at;
371  }
372 
373  JackShmReadPtr& operator=(int index)
374  {
375  Init(index);
376  return *this;
377  }
378 
379  void SetShmIndex(int index, const char* server_name)
380  {
381  Init(index, server_name);
382  }
383 
384  int GetShmIndex()
385  {
386  return fInfo.index;
387  }
388 
389  T* GetShmAddress()
390  {
391  return (T*)fInfo.ptr.attached_at;
392  }
393 
394 };
395 
396 } // end of namespace
397 
398 #endif
Pointer on shared memory segment in the client side.
Definition: JackShmMem.h:318
A class which objects possibly want to be allocated in shared memory derives from this class...
Definition: JackShmMem.h:87
Pointer on shared memory segment in the client side.
Definition: JackShmMem.h:148
Pointer on shared memory segment in the client side: destroy the segment (used client control) ...
Definition: JackShmMem.h:230
The base class for shared memory management.
Definition: JackShmMem.h:125
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107