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__
24 #include "JackError.h"
25 #include "JackCompilerDeps.h"
27 #include <new> // GCC 4.0
31 #include "JackShmMem_os.h"
36 void LockMemoryImp(void* ptr
, size_t size
);
37 void InitLockMemoryImp(void* ptr
, size_t size
);
38 void UnlockMemoryImp(void* ptr
, size_t size
);
40 void UnlockAllMemory();
51 JackMem(): fSize(gSize
)
58 void* operator new(size_t size
)
61 return calloc(1, size
);
64 void operator delete(void* ptr
, size_t size
)
71 LockMemoryImp(this, fSize
);
76 UnlockMemoryImp(this, fSize
);
84 A class which objects possibly want to be allocated in shared memory derives from this class.
91 jack_shm_info_t fInfo
;
102 char* GetShmAddress()
104 return (char*)fInfo
.ptr
.attached_at
;
109 LockMemoryImp(this, fInfo
.size
);
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
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.
148 class JackShmReadWritePtr
153 jack_shm_info_t fInfo
;
156 void Init(int index
, const char* server_name
= JACK_DEFAULT_SERVER_NAME
)
158 if (fInfo
.index
< 0 && index
>= 0) {
159 jack_log("JackShmReadWritePtr::Init %ld %d", index
, fInfo
.index
);
160 if (jack_initialize_shm(server_name
) < 0) {
161 throw std::bad_alloc();
164 if (jack_attach_lib_shm(&fInfo
)) {
165 throw std::bad_alloc();
167 GetShmAddress()->LockMemory();
174 JackShmReadWritePtr()
178 fInfo
.ptr
.attached_at
= (char*)NULL
;
181 JackShmReadWritePtr(int index
, const char* server_name
)
183 Init(index
, server_name
);
186 ~JackShmReadWritePtr()
189 jack_error("JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for %d, skipping unlock", fInfo
.index
);
192 if (fInfo
.index
>= 0) {
193 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %d", fInfo
.index
);
194 GetShmAddress()->UnlockMemory();
195 jack_release_lib_shm(&fInfo
);
200 T
* operator->() const
202 return (T
*)fInfo
.ptr
.attached_at
;
207 return (T
*)fInfo
.ptr
.attached_at
;
210 JackShmReadWritePtr
& operator=(int index
)
216 void SetShmIndex(int index
, const char* server_name
)
218 Init(index
, server_name
);
228 return (T
*)fInfo
.ptr
.attached_at
;
233 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
237 class JackShmReadWritePtr1
242 jack_shm_info_t fInfo
;
245 void Init(int index
, const char* server_name
= JACK_DEFAULT_SERVER_NAME
)
247 if (fInfo
.index
< 0 && index
>= 0) {
248 jack_log("JackShmReadWritePtr1::Init %ld %d", index
, fInfo
.index
);
249 if (jack_initialize_shm(server_name
) < 0) {
250 throw std::bad_alloc();
253 if (jack_attach_lib_shm(&fInfo
)) {
254 throw std::bad_alloc();
256 GetShmAddress()->LockMemory();
259 nobody else needs to access this shared memory any more, so
260 destroy it. because we have our own attachment to it, it won't
261 vanish till we exit (and release it).
263 jack_destroy_shm(&fInfo
);
269 JackShmReadWritePtr1()
273 fInfo
.ptr
.attached_at
= NULL
;
276 JackShmReadWritePtr1(int index
, const char* server_name
)
278 Init(index
, server_name
);
281 ~JackShmReadWritePtr1()
284 jack_error("JackShmReadWritePtr1::~JackShmReadWritePtr1 - Init not done for %d, skipping unlock", fInfo
.index
);
287 if (fInfo
.index
>= 0) {
288 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %d", fInfo
.index
);
289 GetShmAddress()->UnlockMemory();
290 jack_release_lib_shm(&fInfo
);
295 T
* operator->() const
297 return (T
*)fInfo
.ptr
.attached_at
;
302 return (T
*)fInfo
.ptr
.attached_at
;
305 JackShmReadWritePtr1
& operator=(int index
)
311 void SetShmIndex(int index
, const char* server_name
)
313 Init(index
, server_name
);
323 return (T
*)fInfo
.ptr
.attached_at
;
328 \brief Pointer on shared memory segment in the client side.
337 jack_shm_info_t fInfo
;
340 void Init(int index
, const char* server_name
= JACK_DEFAULT_SERVER_NAME
)
342 if (fInfo
.index
< 0 && index
>= 0) {
343 jack_log("JackShmPtrRead::Init %ld %d", index
, fInfo
.index
);
344 if (jack_initialize_shm(server_name
) < 0) {
345 throw std::bad_alloc();
348 if (jack_attach_lib_shm_read(&fInfo
)) {
349 throw std::bad_alloc();
351 GetShmAddress()->LockMemory();
362 fInfo
.ptr
.attached_at
= NULL
;
365 JackShmReadPtr(int index
, const char* server_name
)
367 Init(index
, server_name
);
373 jack_error("JackShmReadPtr::~JackShmReadPtr - Init not done for %ld, skipping unlock", fInfo
.index
);
376 if (fInfo
.index
>= 0) {
377 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo
.index
);
378 GetShmAddress()->UnlockMemory();
379 jack_release_lib_shm(&fInfo
);
384 T
* operator->() const
386 return (T
*)fInfo
.ptr
.attached_at
;
391 return (T
*)fInfo
.ptr
.attached_at
;
394 JackShmReadPtr
& operator=(int index
)
400 void SetShmIndex(int index
, const char* server_name
)
402 Init(index
, server_name
);
412 return (T
*)fInfo
.ptr
.attached_at
;
417 } // end of namespace