Update OSX install script.
[jack2.git] / common / JackShmMem.h
blob5976c96d0d78215ded8c0d3fa11c6c48e63897ac
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 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()
135 public:
137 void* operator new(size_t size);
138 void* operator new(size_t size, void* memory);
140 void operator delete(void* p, size_t size);
141 void operator delete(void* p);
146 \brief Pointer on shared memory segment in the client side.
149 template <class T>
150 class JackShmReadWritePtr
153 private:
155 jack_shm_info_t fInfo;
157 void Init(int index, const char* server_name = "default")
159 if (fInfo.index < 0 && index >= 0) {
160 jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
161 if (jack_initialize_shm(server_name) < 0)
162 throw - 1;
163 fInfo.index = index;
164 if (jack_attach_shm(&fInfo)) {
165 //jack_error("cannot attach shared memory segment", strerror(errno));
166 throw - 2;
171 public:
173 JackShmReadWritePtr()
175 fInfo.index = -1;
176 fInfo.ptr.attached_at = (char*)NULL;
179 JackShmReadWritePtr(int index, const char* server_name)
181 Init(index, server_name);
184 ~JackShmReadWritePtr()
186 if (fInfo.index >= 0) {
187 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
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 //jack_error("cannot attach shared memory segment", strerror(errno));
246 throw - 2;
249 nobody else needs to access this shared memory any more, so
250 destroy it. because we have our own attachment to it, it won't
251 vanish till we exit (and release it).
253 jack_destroy_shm(&fInfo);
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 jack_release_shm(&fInfo);
275 fInfo.index = -1;
279 T* operator->() const
281 return (T*)fInfo.ptr.attached_at;
284 operator T*() const
286 return (T*)fInfo.ptr.attached_at;
289 JackShmReadWritePtr1& operator=(int index)
291 Init(index);
292 return *this;
295 void SetShmIndex(int index, const char* server_name)
297 Init(index, server_name);
300 int GetShmIndex()
302 return fInfo.index;
305 T* GetShmAddress()
307 return (T*)fInfo.ptr.attached_at;
312 \brief Pointer on shared memory segment in the client side.
315 template <class T>
316 class JackShmReadPtr
319 private:
321 jack_shm_info_t fInfo;
323 void Init(int index, const char* server_name = "default")
325 if (fInfo.index < 0 && index >= 0) {
326 jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
327 if (jack_initialize_shm(server_name) < 0)
328 throw - 1;
329 fInfo.index = index;
330 if (jack_attach_shm_read(&fInfo)) {
331 //jack_error("cannot attach shared memory segment", strerror(errno));
332 throw - 2;
337 public:
339 JackShmReadPtr()
341 fInfo.index = -1;
342 fInfo.ptr.attached_at = NULL;
345 JackShmReadPtr(int index, const char* server_name)
347 Init(index, server_name);
350 ~JackShmReadPtr()
352 if (fInfo.index >= 0) {
353 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
354 jack_release_shm(&fInfo);
355 fInfo.index = -1;
359 T* operator->() const
361 return (T*)fInfo.ptr.attached_at;
364 operator T*() const
366 return (T*)fInfo.ptr.attached_at;
369 JackShmReadPtr& operator=(int index)
371 Init(index);
372 return *this;
375 void SetShmIndex(int index, const char* server_name)
377 Init(index, server_name);
380 int GetShmIndex()
382 return fInfo.index;
385 T* GetShmAddress()
387 return (T*)fInfo.ptr.attached_at;
392 } // end of namespace
394 #endif