Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / common / JackShmMem.h
bloba00cd1901900cb35db8973991528f556d0af5f8b
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 UnlockMemoryImp(void* ptr, size_t size);
40 class JackMem
42 private:
44 size_t fSize;
45 static size_t gSize;
47 protected:
49 JackMem(): fSize(gSize)
51 ~JackMem()
54 public:
56 void* operator new(size_t size)
58 gSize = size;
59 return calloc(1, size);
62 void operator delete(void* ptr, size_t size)
64 free(ptr);
67 void LockMemory()
69 LockMemoryImp(this, fSize);
72 void UnlockMemory()
74 UnlockMemoryImp(this, fSize);
79 /*!
80 \brief
82 A class which objects possibly want to be allocated in shared memory derives from this class.
85 class JackShmMemAble
87 protected:
89 jack_shm_info_t fInfo;
91 public:
93 void Init();
95 int GetShmIndex()
97 return fInfo.index;
100 char* GetShmAddress()
102 return (char*)fInfo.attached_at;
105 void LockMemory()
107 LockMemoryImp(this, fInfo.size);
110 void UnlockMemory()
112 UnlockMemoryImp(this, fInfo.size);
118 \brief The base class for shared memory management.
120 A class which objects need to be allocated in shared memory derives from this class.
123 class SERVER_EXPORT JackShmMem : public JackShmMemAble
126 protected:
128 JackShmMem();
129 ~JackShmMem()
132 public:
134 void* operator new(size_t size);
135 void* operator new(size_t size, void* memory);
137 void operator delete(void* p, size_t size);
138 void operator delete(void* p);
143 \brief Pointer on shared memory segment in the client side.
146 template <class T>
147 class JackShmReadWritePtr
150 private:
152 jack_shm_info_t fInfo;
154 void Init(int index, const char* server_name = "default")
156 if (fInfo.index < 0 && index >= 0) {
157 jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
158 if (jack_initialize_shm(server_name) < 0)
159 throw - 1;
160 fInfo.index = index;
161 if (jack_attach_shm(&fInfo)) {
162 //jack_error("cannot attach shared memory segment", strerror(errno));
163 throw - 2;
168 public:
170 JackShmReadWritePtr()
172 fInfo.index = -1;
173 fInfo.attached_at = NULL;
176 JackShmReadWritePtr(int index, const char* server_name)
178 Init(index, server_name);
181 ~JackShmReadWritePtr()
183 if (fInfo.index >= 0) {
184 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
185 jack_release_shm(&fInfo);
186 fInfo.index = -1;
190 T* operator->() const
192 return (T*)fInfo.attached_at;
195 operator T*() const
197 return (T*)fInfo.attached_at;
200 JackShmReadWritePtr& operator=(int index)
202 Init(index);
203 return *this;
206 void SetShmIndex(int index, const char* server_name)
208 Init(index, server_name);
211 int GetShmIndex()
213 return fInfo.index;
216 T* GetShmAddress()
218 return (T*)fInfo.attached_at;
223 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
226 template <class T>
227 class JackShmReadWritePtr1
230 private:
232 jack_shm_info_t fInfo;
234 void Init(int index, const char* server_name = "default")
236 if (fInfo.index < 0 && index >= 0) {
237 jack_log("JackShmReadWritePtr1::Init %ld %ld", index, fInfo.index);
238 if (jack_initialize_shm(server_name) < 0)
239 throw - 1;
240 fInfo.index = index;
241 if (jack_attach_shm(&fInfo)) {
242 //jack_error("cannot attach shared memory segment", strerror(errno));
243 throw - 2;
246 nobody else needs to access this shared memory any more, so
247 destroy it. because we have our own attachment to it, it won't
248 vanish till we exit (and release it).
250 jack_destroy_shm(&fInfo);
254 public:
256 JackShmReadWritePtr1()
258 fInfo.index = -1;
259 fInfo.attached_at = NULL;
262 JackShmReadWritePtr1(int index, const char* server_name)
264 Init(index, server_name);
267 ~JackShmReadWritePtr1()
269 if (fInfo.index >= 0) {
270 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
271 jack_release_shm(&fInfo);
272 fInfo.index = -1;
276 T* operator->() const
278 return (T*)fInfo.attached_at;
281 operator T*() const
283 return (T*)fInfo.attached_at;
286 JackShmReadWritePtr1& operator=(int index)
288 Init(index);
289 return *this;
292 void SetShmIndex(int index, const char* server_name)
294 Init(index, server_name);
297 int GetShmIndex()
299 return fInfo.index;
302 T* GetShmAddress()
304 return (T*)fInfo.attached_at;
309 \brief Pointer on shared memory segment in the client side.
312 template <class T>
313 class JackShmReadPtr
316 private:
318 jack_shm_info_t fInfo;
320 void Init(int index, const char* server_name = "default")
322 if (fInfo.index < 0 && index >= 0) {
323 jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
324 if (jack_initialize_shm(server_name) < 0)
325 throw - 1;
326 fInfo.index = index;
327 if (jack_attach_shm_read(&fInfo)) {
328 //jack_error("cannot attach shared memory segment", strerror(errno));
329 throw - 2;
334 public:
336 JackShmReadPtr()
338 fInfo.index = -1;
339 fInfo.attached_at = NULL;
342 JackShmReadPtr(int index, const char* server_name)
344 Init(index, server_name);
347 ~JackShmReadPtr()
349 if (fInfo.index >= 0) {
350 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
351 jack_release_shm(&fInfo);
352 fInfo.index = -1;
356 T* operator->() const
358 return (T*)fInfo.attached_at;
361 operator T*() const
363 return (T*)fInfo.attached_at;
366 JackShmReadPtr& operator=(int index)
368 Init(index);
369 return *this;
372 void SetShmIndex(int index, const char* server_name)
374 Init(index, server_name);
377 int GetShmIndex()
379 return fInfo.index;
382 T* GetShmAddress()
384 return (T*)fInfo.attached_at;
389 } // end of namespace
391 #endif