2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 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
137 void* operator new(size_t size
);
138 void* operator new(size_t size
, void* memory
);
140 void operator delete(void* p
, size_t size
);
141 void operator delete(void* p
);
146 \brief Pointer on shared memory segment in the client side.
150 class JackShmReadWritePtr
155 jack_shm_info_t fInfo
;
157 void Init(int index
, const char* server_name
= "default")
159 if (fInfo
.index
< 0 && index
>= 0) {
160 jack_log("JackShmReadWritePtr::Init %ld %ld", index
, fInfo
.index
);
161 if (jack_initialize_shm(server_name
) < 0)
164 if (jack_attach_shm(&fInfo
)) {
165 //jack_error("cannot attach shared memory segment", strerror(errno));
173 JackShmReadWritePtr()
176 fInfo
.ptr
.attached_at
= (char*)NULL
;
179 JackShmReadWritePtr(int index
, const char* server_name
)
181 Init(index
, server_name
);
184 ~JackShmReadWritePtr()
186 if (fInfo
.index
>= 0) {
187 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo
.index
);
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
)) {
245 //jack_error("cannot attach shared memory segment", strerror(errno));
249 nobody else needs to access this shared memory any more, so
250 destroy it. because we have our own attachment to it, it won't
251 vanish till we exit (and release it).
253 jack_destroy_shm(&fInfo
);
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 jack_release_shm(&fInfo
);
279 T
* operator->() const
281 return (T
*)fInfo
.ptr
.attached_at
;
286 return (T
*)fInfo
.ptr
.attached_at
;
289 JackShmReadWritePtr1
& operator=(int index
)
295 void SetShmIndex(int index
, const char* server_name
)
297 Init(index
, server_name
);
307 return (T
*)fInfo
.ptr
.attached_at
;
312 \brief Pointer on shared memory segment in the client side.
321 jack_shm_info_t fInfo
;
323 void Init(int index
, const char* server_name
= "default")
325 if (fInfo
.index
< 0 && index
>= 0) {
326 jack_log("JackShmPtrRead::Init %ld %ld", index
, fInfo
.index
);
327 if (jack_initialize_shm(server_name
) < 0)
330 if (jack_attach_shm_read(&fInfo
)) {
331 //jack_error("cannot attach shared memory segment", strerror(errno));
342 fInfo
.ptr
.attached_at
= NULL
;
345 JackShmReadPtr(int index
, const char* server_name
)
347 Init(index
, server_name
);
352 if (fInfo
.index
>= 0) {
353 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo
.index
);
354 jack_release_shm(&fInfo
);
359 T
* operator->() const
361 return (T
*)fInfo
.ptr
.attached_at
;
366 return (T
*)fInfo
.ptr
.attached_at
;
369 JackShmReadPtr
& operator=(int index
)
375 void SetShmIndex(int index
, const char* server_name
)
377 Init(index
, server_name
);
387 return (T
*)fInfo
.ptr
.attached_at
;
392 } // end of namespace