Apply srcfactor.diff patch for ticket #162.
[jack2.git] / common / JackShmMem.h
blobfdef507b39a70a35d0462d7d14efccb6292aa032
1 /*
2 Copyright (C) 2004-2008 Grame
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.
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.
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.
20 #ifndef __JackShmMem__
21 #define __JackShmMem__
23 #include "shm.h"
24 #include "JackError.h"
25 #include "JackCompilerDeps.h"
27 #include <new> // GCC 4.0
28 #include <errno.h>
29 #include <stdlib.h>
31 #include "JackShmMem_os.h"
33 namespace Jack
36 SERVER_EXPORT void LockMemoryImp(void* ptr, size_t size);
37 SERVER_EXPORT void InitLockMemoryImp(void* ptr, size_t size);
38 SERVER_EXPORT void UnlockMemoryImp(void* ptr, size_t size);
39 SERVER_EXPORT void LockAllMemory();
40 SERVER_EXPORT void UnlockAllMemory();
42 class JackMem
44 private:
46 size_t fSize;
47 static size_t gSize;
49 protected:
51 JackMem(): fSize(gSize)
53 ~JackMem()
56 public:
58 void* operator new(size_t size)
60 gSize = size;
61 return calloc(1, size);
64 void operator delete(void* ptr, size_t size)
66 free(ptr);
69 void LockMemory()
71 LockMemoryImp(this, fSize);
74 void UnlockMemory()
76 UnlockMemoryImp(this, fSize);
81 /*!
82 \brief
84 A class which objects possibly want to be allocated in shared memory derives from this class.
87 class JackShmMemAble
89 protected:
91 jack_shm_info_t fInfo;
93 public:
95 void Init();
97 int GetShmIndex()
99 return fInfo.index;
102 char* GetShmAddress()
104 return (char*)fInfo.ptr.attached_at;
107 void LockMemory()
109 LockMemoryImp(this, fInfo.size);
112 void UnlockMemory()
114 UnlockMemoryImp(this, fInfo.size);
120 \brief The base class for shared memory management.
122 A class which objects need to be allocated in shared memory derives from this class.
125 class SERVER_EXPORT JackShmMem : public JackShmMemAble
128 protected:
130 JackShmMem();
131 ~JackShmMem();
133 public:
135 void* operator new(size_t size);
136 void* operator new(size_t size, void* memory);
138 void operator delete(void* p, size_t size);
139 void operator delete(void* p);
144 \brief Pointer on shared memory segment in the client side.
147 template <class T>
148 class JackShmReadWritePtr
151 private:
153 jack_shm_info_t fInfo;
155 void Init(int index, const char* server_name = "default")
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 - 1;
161 fInfo.index = index;
162 if (jack_attach_shm(&fInfo)) {
163 throw - 2;
165 GetShmAddress()->LockMemory();
169 public:
171 JackShmReadWritePtr()
173 fInfo.index = -1;
174 fInfo.ptr.attached_at = (char*)NULL;
177 JackShmReadWritePtr(int index, const char* server_name)
179 Init(index, server_name);
182 ~JackShmReadWritePtr()
184 if (fInfo.index >= 0) {
185 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
186 GetShmAddress()->UnlockMemory();
187 jack_release_shm(&fInfo);
188 fInfo.index = -1;
192 T* operator->() const
194 return (T*)fInfo.ptr.attached_at;
197 operator T*() const
199 return (T*)fInfo.ptr.attached_at;
202 JackShmReadWritePtr& operator=(int index)
204 Init(index);
205 return *this;
208 void SetShmIndex(int index, const char* server_name)
210 Init(index, server_name);
213 int GetShmIndex()
215 return fInfo.index;
218 T* GetShmAddress()
220 return (T*)fInfo.ptr.attached_at;
225 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
228 template <class T>
229 class JackShmReadWritePtr1
232 private:
234 jack_shm_info_t fInfo;
236 void Init(int index, const char* server_name = "default")
238 if (fInfo.index < 0 && index >= 0) {
239 jack_log("JackShmReadWritePtr1::Init %ld %ld", index, fInfo.index);
240 if (jack_initialize_shm(server_name) < 0)
241 throw - 1;
242 fInfo.index = index;
243 if (jack_attach_shm(&fInfo)) {
244 throw - 2;
247 nobody else needs to access this shared memory any more, so
248 destroy it. because we have our own attachment to it, it won't
249 vanish till we exit (and release it).
251 jack_destroy_shm(&fInfo);
252 GetShmAddress()->LockMemory();
256 public:
258 JackShmReadWritePtr1()
260 fInfo.index = -1;
261 fInfo.ptr.attached_at = NULL;
264 JackShmReadWritePtr1(int index, const char* server_name)
266 Init(index, server_name);
269 ~JackShmReadWritePtr1()
271 if (fInfo.index >= 0) {
272 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
273 GetShmAddress()->UnlockMemory();
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 throw - 2;
333 GetShmAddress()->LockMemory();
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 GetShmAddress()->UnlockMemory();
355 jack_release_shm(&fInfo);
356 fInfo.index = -1;
360 T* operator->() const
362 return (T*)fInfo.ptr.attached_at;
365 operator T*() const
367 return (T*)fInfo.ptr.attached_at;
370 JackShmReadPtr& operator=(int index)
372 Init(index);
373 return *this;
376 void SetShmIndex(int index, const char* server_name)
378 Init(index, server_name);
381 int GetShmIndex()
383 return fInfo.index;
386 T* GetShmAddress()
388 return (T*)fInfo.ptr.attached_at;
393 } // end of namespace
395 #endif