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)
36 #include <sys/types.h>
38 #define CHECK_MLOCK(ptr, size) (munlock((ptr), (size)) == 0)
39 #define CHECK_MUNLOCK(ptr, size) (munlock((ptr), (size)) == 0)
45 void LockMemoryImp(void* ptr
, size_t size
);
46 void UnlockMemoryImp(void* ptr
, size_t size
);
57 void* operator new(size_t size
)
60 return calloc(1, size
);
63 void operator delete(void* ptr
, size_t size
)
68 JackMem():fSize(gSize
)
73 UnlockMemoryImp(this, fSize
);
78 LockMemoryImp(this, fSize
);
83 UnlockMemoryImp(this, fSize
);
89 \brief The base class for shared memory management.
91 A class which objects need to be allocated in shared memory derives from this class.
99 jack_shm_info_t fInfo
;
100 static unsigned long fSegmentNum
;
101 static unsigned long fSegmentCount
;
102 static jack_shm_info_t gInfo
;
106 void* operator new(size_t size
);
107 void operator delete(void* p
, size_t size
);
111 fInfo
.index
= gInfo
.index
;
112 fInfo
.attached_at
= gInfo
.attached_at
;
113 fInfo
.size
= gInfo
.size
;
116 virtual ~JackShmMem()
118 UnlockMemoryImp(this, fInfo
.size
);
126 char* GetShmAddress()
128 return (char*)fInfo
.attached_at
;
133 LockMemoryImp(this, fInfo
.size
);
138 UnlockMemoryImp(this, fInfo
.size
);
144 \brief Pointer on shared memory segment in the client side.
148 class JackShmReadWritePtr
153 jack_shm_info_t fInfo
;
157 if (fInfo
.index
< 0 && index
>= 0) {
158 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index
, fInfo
.index
);
159 if (jack_initialize_shm_client() < 0)
162 if (jack_attach_shm(&fInfo
)) {
163 //jack_error("cannot attach shared memory segment", strerror(errno));
171 JackShmReadWritePtr()
174 fInfo
.attached_at
= NULL
;
177 JackShmReadWritePtr(int index
)
182 virtual ~JackShmReadWritePtr()
184 if (fInfo
.index
>= 0) {
185 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo
.index
);
186 jack_release_shm(&fInfo
);
191 T
* operator->() const
193 return (T
*)fInfo
.attached_at
;
198 return (T
*)fInfo
.attached_at
;
201 JackShmReadWritePtr
& operator=(int index
)
214 return (T
*)fInfo
.attached_at
;
219 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
223 class JackShmReadWritePtr1
228 jack_shm_info_t fInfo
;
232 if (fInfo
.index
< 0 && index
>= 0) {
233 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index
, fInfo
.index
);
234 if (jack_initialize_shm_client() < 0)
237 if (jack_attach_shm(&fInfo
)) {
238 //jack_error("cannot attach shared memory segment", strerror(errno));
242 nobody else needs to access this shared memory any more, so
243 destroy it. because we have our own attachment to it, it won't
244 vanish till we exit (and release it).
246 jack_destroy_shm(&fInfo
);
252 JackShmReadWritePtr1()
255 fInfo
.attached_at
= NULL
;
258 JackShmReadWritePtr1(int index
)
263 virtual ~JackShmReadWritePtr1()
265 if (fInfo
.index
>= 0) {
266 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo
.index
);
267 jack_release_shm(&fInfo
);
272 T
* operator->() const
274 return (T
*)fInfo
.attached_at
;
279 return (T
*)fInfo
.attached_at
;
282 JackShmReadWritePtr1
& operator=(int index
)
295 return (T
*)fInfo
.attached_at
;
300 \brief Pointer on shared memory segment in the client side.
309 jack_shm_info_t fInfo
;
313 if (fInfo
.index
< 0 && index
>= 0) {
314 JackLog("JackShmPtrRead::Init %ld %ld\n", index
, fInfo
.index
);
315 if (jack_initialize_shm_client() < 0)
318 if (jack_attach_shm_read(&fInfo
)) {
319 //jack_error("cannot attach shared memory segment", strerror(errno));
330 fInfo
.attached_at
= NULL
;
333 JackShmReadPtr(int index
)
338 virtual ~JackShmReadPtr()
340 if (fInfo
.index
>= 0) {
341 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo
.index
);
342 jack_release_shm(&fInfo
);
347 T
* operator->() const
349 return (T
*)fInfo
.attached_at
;
354 return (T
*)fInfo
.attached_at
;
357 JackShmReadPtr
& operator=(int index
)
370 return (T
*)fInfo
.attached_at
;
375 } // end of namespace