Use jack_client_open instead of old jack_client_new.
[jack2.git] / common / JackShmMem.h
blob0a00daa5444def950ee1d955b8cc677dc5eb634b
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2009 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef __JackShmMem__
22 #define __JackShmMem__
24 #include "shm.h"
25 #include "JackError.h"
26 #include "JackCompilerDeps.h"
28 #include <new> // GCC 4.0
29 #include <errno.h>
30 #include <stdlib.h>
32 #include "JackShmMem_os.h"
34 namespace Jack
37 SERVER_EXPORT void LockMemoryImp(void* ptr, size_t size);
38 SERVER_EXPORT void InitLockMemoryImp(void* ptr, size_t size);
39 SERVER_EXPORT void UnlockMemoryImp(void* ptr, size_t size);
40 SERVER_EXPORT void LockAllMemory();
41 SERVER_EXPORT void UnlockAllMemory();
43 class JackMem
45 private:
47 size_t fSize;
48 static size_t gSize;
50 protected:
52 JackMem(): fSize(gSize)
54 ~JackMem()
57 public:
59 void* operator new(size_t size)
61 gSize = size;
62 return calloc(1, size);
65 void operator delete(void* ptr, size_t size)
67 free(ptr);
70 void LockMemory()
72 LockMemoryImp(this, fSize);
75 void UnlockMemory()
77 UnlockMemoryImp(this, fSize);
82 /*!
83 \brief
85 A class which objects possibly want to be allocated in shared memory derives from this class.
88 class JackShmMemAble
90 protected:
92 jack_shm_info_t fInfo;
94 public:
96 void Init();
98 int GetShmIndex()
100 return fInfo.index;
103 char* GetShmAddress()
105 return (char*)fInfo.ptr.attached_at;
108 void LockMemory()
110 LockMemoryImp(this, fInfo.size);
113 void UnlockMemory()
115 UnlockMemoryImp(this, fInfo.size);
121 \brief The base class for shared memory management.
123 A class which objects need to be allocated in shared memory derives from this class.
126 class SERVER_EXPORT JackShmMem : public JackShmMemAble
129 protected:
131 JackShmMem();
132 ~JackShmMem();
134 public:
136 void* operator new(size_t size);
137 void* operator new(size_t size, void* memory);
139 void operator delete(void* p, size_t size);
140 void operator delete(void* p);
145 \brief Pointer on shared memory segment in the client side.
148 template <class T>
149 class JackShmReadWritePtr
152 private:
154 jack_shm_info_t fInfo;
156 void Init(int index, const char* server_name = "default")
158 if (fInfo.index < 0 && index >= 0) {
159 jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
160 if (jack_initialize_shm(server_name) < 0)
161 throw - 1;
162 fInfo.index = index;
163 if (jack_attach_shm(&fInfo)) {
164 throw - 2;
166 GetShmAddress()->LockMemory();
170 public:
172 JackShmReadWritePtr()
174 fInfo.index = -1;
175 fInfo.ptr.attached_at = (char*)NULL;
178 JackShmReadWritePtr(int index, const char* server_name)
180 Init(index, server_name);
183 ~JackShmReadWritePtr()
185 if (fInfo.index >= 0) {
186 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
187 GetShmAddress()->UnlockMemory();
188 jack_release_shm(&fInfo);
189 fInfo.index = -1;
193 T* operator->() const
195 return (T*)fInfo.ptr.attached_at;
198 operator T*() const
200 return (T*)fInfo.ptr.attached_at;
203 JackShmReadWritePtr& operator=(int index)
205 Init(index);
206 return *this;
209 void SetShmIndex(int index, const char* server_name)
211 Init(index, server_name);
214 int GetShmIndex()
216 return fInfo.index;
219 T* GetShmAddress()
221 return (T*)fInfo.ptr.attached_at;
226 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
229 template <class T>
230 class JackShmReadWritePtr1
233 private:
235 jack_shm_info_t fInfo;
237 void Init(int index, const char* server_name = "default")
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 - 1;
243 fInfo.index = index;
244 if (jack_attach_shm(&fInfo)) {
245 throw - 2;
248 nobody else needs to access this shared memory any more, so
249 destroy it. because we have our own attachment to it, it won't
250 vanish till we exit (and release it).
252 jack_destroy_shm(&fInfo);
253 GetShmAddress()->LockMemory();
257 public:
259 JackShmReadWritePtr1()
261 fInfo.index = -1;
262 fInfo.ptr.attached_at = NULL;
265 JackShmReadWritePtr1(int index, const char* server_name)
267 Init(index, server_name);
270 ~JackShmReadWritePtr1()
272 if (fInfo.index >= 0) {
273 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
274 GetShmAddress()->UnlockMemory();
275 jack_release_shm(&fInfo);
276 fInfo.index = -1;
280 T* operator->() const
282 return (T*)fInfo.ptr.attached_at;
285 operator T*() const
287 return (T*)fInfo.ptr.attached_at;
290 JackShmReadWritePtr1& operator=(int index)
292 Init(index);
293 return *this;
296 void SetShmIndex(int index, const char* server_name)
298 Init(index, server_name);
301 int GetShmIndex()
303 return fInfo.index;
306 T* GetShmAddress()
308 return (T*)fInfo.ptr.attached_at;
313 \brief Pointer on shared memory segment in the client side.
316 template <class T>
317 class JackShmReadPtr
320 private:
322 jack_shm_info_t fInfo;
324 void Init(int index, const char* server_name = "default")
326 if (fInfo.index < 0 && index >= 0) {
327 jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
328 if (jack_initialize_shm(server_name) < 0)
329 throw - 1;
330 fInfo.index = index;
331 if (jack_attach_shm_read(&fInfo)) {
332 throw - 2;
334 GetShmAddress()->LockMemory();
338 public:
340 JackShmReadPtr()
342 fInfo.index = -1;
343 fInfo.ptr.attached_at = NULL;
346 JackShmReadPtr(int index, const char* server_name)
348 Init(index, server_name);
351 ~JackShmReadPtr()
353 if (fInfo.index >= 0) {
354 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
355 GetShmAddress()->UnlockMemory();
356 jack_release_shm(&fInfo);
357 fInfo.index = -1;
361 T* operator->() const
363 return (T*)fInfo.ptr.attached_at;
366 operator T*() const
368 return (T*)fInfo.ptr.attached_at;
371 JackShmReadPtr& operator=(int index)
373 Init(index);
374 return *this;
377 void SetShmIndex(int index, const char* server_name)
379 Init(index, server_name);
382 int GetShmIndex()
384 return fInfo.index;
387 T* GetShmAddress()
389 return (T*)fInfo.ptr.attached_at;
394 } // end of namespace
396 #endif