2
Copyright (C) 2001 Paul Davis
3
Copyright (C) 2004-2009 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"
26
#include "JackCompilerDeps.h"
28
#include <new> // GCC 4.0
32
#include "JackShmMem_os.h"
37
SERVER_EXPORT void LockMemoryImp(void* ptr, size_t size);
38
SERVER_EXPORT void InitLockMemoryImp(void* ptr, size_t size);
39
SERVER_EXPORT void UnlockMemoryImp(void* ptr, size_t size);
40
SERVER_EXPORT void LockAllMemory();
41
SERVER_EXPORT void UnlockAllMemory();
52
JackMem(): fSize(gSize)
59
void* operator new(size_t size)
62
return calloc(1, size);
65
void operator delete(void* ptr, size_t size)
72
LockMemoryImp(this, fSize);
77
UnlockMemoryImp(this, fSize);
85
A class which objects possibly want to be allocated in shared memory derives from this class.
92
jack_shm_info_t fInfo;
103
char* GetShmAddress()
105
return (char*)fInfo.ptr.attached_at;
110
LockMemoryImp(this, fInfo.size);
115
UnlockMemoryImp(this, fInfo.size);
121
\brief The base class for shared memory management.
123
A class which objects need to be allocated in shared memory derives from this class.
126
class SERVER_EXPORT JackShmMem : public JackShmMemAble
136
void* operator new(size_t size);
137
void* operator new(size_t size, void* memory);
139
void operator delete(void* p, size_t size);
140
void operator delete(void* p);
145
\brief Pointer on shared memory segment in the client side.
149
class JackShmReadWritePtr
154
jack_shm_info_t fInfo;
156
void Init(int index, const char* server_name = "default")
158
if (fInfo.index < 0 && index >= 0) {
159
jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
160
if (jack_initialize_shm(server_name) < 0)
163
if (jack_attach_shm(&fInfo)) {
166
GetShmAddress()->LockMemory();
172
JackShmReadWritePtr()
175
fInfo.ptr.attached_at = (char*)NULL;
178
JackShmReadWritePtr(int index, const char* server_name)
180
Init(index, server_name);
183
~JackShmReadWritePtr()
185
if (fInfo.index >= 0) {
186
jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
187
GetShmAddress()->UnlockMemory();
188
jack_release_shm(&fInfo);
193
T* operator->() const
195
return (T*)fInfo.ptr.attached_at;
200
return (T*)fInfo.ptr.attached_at;
203
JackShmReadWritePtr& operator=(int index)
209
void SetShmIndex(int index, const char* server_name)
211
Init(index, server_name);
221
return (T*)fInfo.ptr.attached_at;
226
\brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
230
class JackShmReadWritePtr1
235
jack_shm_info_t fInfo;
237
void Init(int index, const char* server_name = "default")
239
if (fInfo.index < 0 && index >= 0) {
240
jack_log("JackShmReadWritePtr1::Init %ld %ld", index, fInfo.index);
241
if (jack_initialize_shm(server_name) < 0)
244
if (jack_attach_shm(&fInfo)) {
248
nobody else needs to access this shared memory any more, so
249
destroy it. because we have our own attachment to it, it won't
250
vanish till we exit (and release it).
252
jack_destroy_shm(&fInfo);
253
GetShmAddress()->LockMemory();
259
JackShmReadWritePtr1()
262
fInfo.ptr.attached_at = NULL;
265
JackShmReadWritePtr1(int index, const char* server_name)
267
Init(index, server_name);
270
~JackShmReadWritePtr1()
272
if (fInfo.index >= 0) {
273
jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
274
GetShmAddress()->UnlockMemory();
275
jack_release_shm(&fInfo);
280
T* operator->() const
282
return (T*)fInfo.ptr.attached_at;
287
return (T*)fInfo.ptr.attached_at;
290
JackShmReadWritePtr1& operator=(int index)
296
void SetShmIndex(int index, const char* server_name)
298
Init(index, server_name);
308
return (T*)fInfo.ptr.attached_at;
313
\brief Pointer on shared memory segment in the client side.
322
jack_shm_info_t fInfo;
324
void Init(int index, const char* server_name = "default")
326
if (fInfo.index < 0 && index >= 0) {
327
jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
328
if (jack_initialize_shm(server_name) < 0)
331
if (jack_attach_shm_read(&fInfo)) {
334
GetShmAddress()->LockMemory();
343
fInfo.ptr.attached_at = NULL;
346
JackShmReadPtr(int index, const char* server_name)
348
Init(index, server_name);
353
if (fInfo.index >= 0) {
354
jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
355
GetShmAddress()->UnlockMemory();
356
jack_release_shm(&fInfo);
361
T* operator->() const
363
return (T*)fInfo.ptr.attached_at;
368
return (T*)fInfo.ptr.attached_at;
371
JackShmReadPtr& operator=(int index)
377
void SetShmIndex(int index, const char* server_name)
379
Init(index, server_name);
389
return (T*)fInfo.ptr.attached_at;
394
} // end of namespace