Cleanup
[jack2.git] / common / JackShmMem.h
blob5ebb55d0e7c42cbcf57afdcfd45fc063aef1c81f
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2006 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"
27 #include <new> // GCC 4.0
28 #include <errno.h>
29 #include <stdlib.h>
31 #ifdef WIN32
32 #include <windows.h>
33 #else
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 #endif
38 namespace Jack
41 class JackLockMem
43 private:
45 size_t fSize;
46 static size_t gSize;
48 public:
50 void* operator new(size_t size)
52 gSize = size;
53 return malloc(size);
56 void operator delete(void* ptr, size_t size)
58 free(ptr);
61 JackLockMem():fSize(gSize)
64 virtual ~JackLockMem()
66 UnlockMemory();
69 void LockMemory()
71 #ifdef __APPLE__
72 mlock(this, fSize);
73 #elif linux_
74 mlock(this, fSize);
75 #elif WIN32
76 VirtualLock(this, fSize);
77 #endif
80 void UnlockMemory()
82 #ifdef __APPLE__
83 munlock(this, fSize);
84 #elif linux_
85 munlock(this, fSize);
86 #elif WIN32
87 VirtualUnlock(this, fSize);
88 #endif
94 /*!
95 \brief The base class for shared memory management.
97 A class which objects need to be allocated in shared memory derives from this class.
100 class JackShmMem
103 private:
105 jack_shm_info_t fInfo;
106 static unsigned long fSegmentNum;
107 static unsigned long fSegmentCount;
108 static jack_shm_info_t gInfo;
110 public:
112 void* operator new(size_t size);
113 void operator delete(void* p, size_t size);
115 JackShmMem()
117 fInfo.index = gInfo.index;
118 fInfo.attached_at = gInfo.attached_at;
121 virtual ~JackShmMem()
124 int GetShmIndex()
126 return fInfo.index;
129 char* GetShmAddress()
131 return (char*)fInfo.attached_at;
137 \brief Pointer on shared memory segment in the client side.
140 template <class T>
141 class JackShmReadWritePtr
144 private:
146 jack_shm_info_t fInfo;
148 void Init(int index)
150 if (fInfo.index < 0 && index >= 0) {
151 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
152 if (jack_initialize_shm_client() < 0)
153 throw - 1;
154 fInfo.index = index;
155 if (jack_attach_shm(&fInfo)) {
156 //jack_error("cannot attach shared memory segment", strerror(errno));
157 throw - 2;
162 public:
164 JackShmReadWritePtr()
166 fInfo.index = -1;
167 fInfo.attached_at = NULL;
170 JackShmReadWritePtr(int index)
172 Init(index);
175 virtual ~JackShmReadWritePtr()
177 if (fInfo.index >= 0) {
178 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
179 jack_release_shm(&fInfo);
180 fInfo.index = -1;
184 T* operator->() const
186 return (T*)fInfo.attached_at;
189 operator T*() const
191 return (T*)fInfo.attached_at;
194 JackShmReadWritePtr& operator=(int index)
196 Init(index);
197 return *this;
200 int GetShmIndex()
202 return fInfo.index;
205 T* GetShmAddress()
207 return (T*)fInfo.attached_at;
212 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
215 template <class T>
216 class JackShmReadWritePtr1
219 private:
221 jack_shm_info_t fInfo;
223 void Init(int index)
225 if (fInfo.index < 0 && index >= 0) {
226 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
227 if (jack_initialize_shm_client() < 0)
228 throw - 1;
229 fInfo.index = index;
230 if (jack_attach_shm(&fInfo)) {
231 //jack_error("cannot attach shared memory segment", strerror(errno));
232 throw - 2;
235 nobody else needs to access this shared memory any more, so
236 destroy it. because we have our own attachment to it, it won't
237 vanish till we exit (and release it).
239 jack_destroy_shm(&fInfo);
243 public:
245 JackShmReadWritePtr1()
247 fInfo.index = -1;
248 fInfo.attached_at = NULL;
251 JackShmReadWritePtr1(int index)
253 Init(index);
256 virtual ~JackShmReadWritePtr1()
258 if (fInfo.index >= 0) {
259 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
260 jack_release_shm(&fInfo);
261 fInfo.index = -1;
265 T* operator->() const
267 return (T*)fInfo.attached_at;
270 operator T*() const
272 return (T*)fInfo.attached_at;
275 JackShmReadWritePtr1& operator=(int index)
277 Init(index);
278 return *this;
281 int GetShmIndex()
283 return fInfo.index;
286 T* GetShmAddress()
288 return (T*)fInfo.attached_at;
293 \brief Pointer on shared memory segment in the client side.
296 template <class T>
297 class JackShmReadPtr
300 private:
302 jack_shm_info_t fInfo;
304 void Init(int index)
306 if (fInfo.index < 0 && index >= 0) {
307 JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
308 if (jack_initialize_shm_client() < 0)
309 throw - 1;
310 fInfo.index = index;
311 if (jack_attach_shm_read(&fInfo)) {
312 //jack_error("cannot attach shared memory segment", strerror(errno));
313 throw - 2;
318 public:
320 JackShmReadPtr()
322 fInfo.index = -1;
323 fInfo.attached_at = NULL;
326 JackShmReadPtr(int index)
328 Init(index);
331 virtual ~JackShmReadPtr()
333 if (fInfo.index >= 0) {
334 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
335 jack_release_shm(&fInfo);
336 fInfo.index = -1;
340 T* operator->() const
342 return (T*)fInfo.attached_at;
345 operator T*() const
347 return (T*)fInfo.attached_at;
350 JackShmReadPtr& operator=(int index)
352 Init(index);
353 return *this;
356 int GetShmIndex()
358 return fInfo.index;
361 T* GetShmAddress()
363 return (T*)fInfo.attached_at;
368 } // end of namespace
370 #endif