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 UnlockMemoryImp(void* ptr
, size_t size
);
49 JackMem(): fSize(gSize
)
56 void* operator new(size_t size
)
59 return calloc(1, size
);
62 void operator delete(void* ptr
, size_t size
)
69 LockMemoryImp(this, fSize
);
74 UnlockMemoryImp(this, fSize
);
82 A class which objects possibly want to be allocated in shared memory derives from this class.
89 jack_shm_info_t fInfo
;
100 char* GetShmAddress()
102 return (char*)fInfo
.attached_at
;
107 LockMemoryImp(this, fInfo
.size
);
112 UnlockMemoryImp(this, fInfo
.size
);
118 \brief The base class for shared memory management.
120 A class which objects need to be allocated in shared memory derives from this class.
123 class SERVER_EXPORT JackShmMem
: public JackShmMemAble
134 void* operator new(size_t size
);
135 void* operator new(size_t size
, void* memory
);
137 void operator delete(void* p
, size_t size
);
138 void operator delete(void* p
);
143 \brief Pointer on shared memory segment in the client side.
147 class JackShmReadWritePtr
152 jack_shm_info_t fInfo
;
154 void Init(int index
, const char* server_name
= "default")
156 if (fInfo
.index
< 0 && index
>= 0) {
157 jack_log("JackShmReadWritePtr::Init %ld %ld", index
, fInfo
.index
);
158 if (jack_initialize_shm(server_name
) < 0)
161 if (jack_attach_shm(&fInfo
)) {
162 //jack_error("cannot attach shared memory segment", strerror(errno));
170 JackShmReadWritePtr()
173 fInfo
.attached_at
= NULL
;
176 JackShmReadWritePtr(int index
, const char* server_name
)
178 Init(index
, server_name
);
181 ~JackShmReadWritePtr()
183 if (fInfo
.index
>= 0) {
184 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo
.index
);
185 jack_release_shm(&fInfo
);
190 T
* operator->() const
192 return (T
*)fInfo
.attached_at
;
197 return (T
*)fInfo
.attached_at
;
200 JackShmReadWritePtr
& operator=(int index
)
206 void SetShmIndex(int index
, const char* server_name
)
208 Init(index
, server_name
);
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
;
234 void Init(int index
, const char* server_name
= "default")
236 if (fInfo
.index
< 0 && index
>= 0) {
237 jack_log("JackShmReadWritePtr1::Init %ld %ld", index
, fInfo
.index
);
238 if (jack_initialize_shm(server_name
) < 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
, const char* server_name
)
264 Init(index
, server_name
);
267 ~JackShmReadWritePtr1()
269 if (fInfo
.index
>= 0) {
270 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", 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
)
292 void SetShmIndex(int index
, const char* server_name
)
294 Init(index
, server_name
);
304 return (T
*)fInfo
.attached_at
;
309 \brief Pointer on shared memory segment in the client side.
318 jack_shm_info_t fInfo
;
320 void Init(int index
, const char* server_name
= "default")
322 if (fInfo
.index
< 0 && index
>= 0) {
323 jack_log("JackShmPtrRead::Init %ld %ld", index
, fInfo
.index
);
324 if (jack_initialize_shm(server_name
) < 0)
327 if (jack_attach_shm_read(&fInfo
)) {
328 //jack_error("cannot attach shared memory segment", strerror(errno));
339 fInfo
.attached_at
= NULL
;
342 JackShmReadPtr(int index
, const char* server_name
)
344 Init(index
, server_name
);
349 if (fInfo
.index
>= 0) {
350 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo
.index
);
351 jack_release_shm(&fInfo
);
356 T
* operator->() const
358 return (T
*)fInfo
.attached_at
;
363 return (T
*)fInfo
.attached_at
;
366 JackShmReadPtr
& operator=(int index
)
372 void SetShmIndex(int index
, const char* server_name
)
374 Init(index
, server_name
);
384 return (T
*)fInfo
.attached_at
;
389 } // end of namespace