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__
25 #include "JackError.h"
27 #include <new> // GCC 4.0
33 #define CHECK_MLOCK(ptr, size) (VirtualLock((ptr), (size)) != 0)
34 #define CHECK_MUNLOCK(ptr, size) (VirtualUnlock((ptr), (size)) != 0)
35 #define CHECK_MLOCKALL()(false)
36 #define CHECK_MUNLOCKALL()(false)
38 #include <sys/types.h>
40 #define CHECK_MLOCK(ptr, size) (mlock((ptr), (size)) == 0)
41 #define CHECK_MUNLOCK(ptr, size) (munlock((ptr), (size)) == 0)
42 #define CHECK_MLOCKALL() (mlockall(MCL_CURRENT | MCL_FUTURE) == 0)
43 #define CHECK_MUNLOCKALL() (munlockall() == 0)
49 void LockMemoryImp(void* ptr
, size_t size
);
50 void UnlockMemoryImp(void* ptr
, size_t size
);
61 void* operator new(size_t size
)
64 return calloc(1, size
);
67 void operator delete(void* ptr
, size_t size
)
72 JackMem():fSize(gSize
)
77 UnlockMemoryImp(this, fSize
);
82 LockMemoryImp(this, fSize
);
87 UnlockMemoryImp(this, fSize
);
93 \brief The base class for shared memory management.
95 A class which objects need to be allocated in shared memory derives from this class.
103 jack_shm_info_t fInfo
;
104 static unsigned long fSegmentNum
;
105 static unsigned long fSegmentCount
;
106 static jack_shm_info_t gInfo
;
110 void* operator new(size_t size
);
111 void operator delete(void* p
, size_t size
);
115 fInfo
.index
= gInfo
.index
;
116 fInfo
.attached_at
= gInfo
.attached_at
;
117 fInfo
.size
= gInfo
.size
;
120 virtual ~JackShmMem()
122 UnlockMemoryImp(this, fInfo
.size
);
130 char* GetShmAddress()
132 return (char*)fInfo
.attached_at
;
137 LockMemoryImp(this, fInfo
.size
);
142 UnlockMemoryImp(this, fInfo
.size
);
148 \brief Pointer on shared memory segment in the client side.
152 class JackShmReadWritePtr
157 jack_shm_info_t fInfo
;
161 if (fInfo
.index
< 0 && index
>= 0) {
162 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index
, fInfo
.index
);
163 if (jack_initialize_shm_client() < 0)
166 if (jack_attach_shm(&fInfo
)) {
167 //jack_error("cannot attach shared memory segment", strerror(errno));
175 JackShmReadWritePtr()
178 fInfo
.attached_at
= NULL
;
181 JackShmReadWritePtr(int index
)
186 virtual ~JackShmReadWritePtr()
188 if (fInfo
.index
>= 0) {
189 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo
.index
);
190 jack_release_shm(&fInfo
);
195 T
* operator->() const
197 return (T
*)fInfo
.attached_at
;
202 return (T
*)fInfo
.attached_at
;
205 JackShmReadWritePtr
& operator=(int index
)
218 return (T
*)fInfo
.attached_at
;
223 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
227 class JackShmReadWritePtr1
232 jack_shm_info_t fInfo
;
236 if (fInfo
.index
< 0 && index
>= 0) {
237 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index
, fInfo
.index
);
238 if (jack_initialize_shm_client() < 0)
241 if (jack_attach_shm(&fInfo
)) {
242 //jack_error("cannot attach shared memory segment", strerror(errno));
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
);
256 JackShmReadWritePtr1()
259 fInfo
.attached_at
= NULL
;
262 JackShmReadWritePtr1(int index
)
267 virtual ~JackShmReadWritePtr1()
269 if (fInfo
.index
>= 0) {
270 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo
.index
);
271 jack_release_shm(&fInfo
);
276 T
* operator->() const
278 return (T
*)fInfo
.attached_at
;
283 return (T
*)fInfo
.attached_at
;
286 JackShmReadWritePtr1
& operator=(int index
)
299 return (T
*)fInfo
.attached_at
;
304 \brief Pointer on shared memory segment in the client side.
313 jack_shm_info_t fInfo
;
317 if (fInfo
.index
< 0 && index
>= 0) {
318 JackLog("JackShmPtrRead::Init %ld %ld\n", index
, fInfo
.index
);
319 if (jack_initialize_shm_client() < 0)
322 if (jack_attach_shm_read(&fInfo
)) {
323 //jack_error("cannot attach shared memory segment", strerror(errno));
334 fInfo
.attached_at
= NULL
;
337 JackShmReadPtr(int index
)
342 virtual ~JackShmReadPtr()
344 if (fInfo
.index
>= 0) {
345 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo
.index
);
346 jack_release_shm(&fInfo
);
351 T
* operator->() const
353 return (T
*)fInfo
.attached_at
;
358 return (T
*)fInfo
.attached_at
;
361 JackShmReadPtr
& operator=(int index
)
374 return (T
*)fInfo
.attached_at
;
379 } // end of namespace