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
34 #include <sys/types.h>
50 void* operator new(size_t size
)
56 void operator delete(void* ptr
, size_t size
)
61 JackLockMem():fSize(gSize
)
64 virtual ~JackLockMem()
76 VirtualLock(this, fSize
);
87 VirtualUnlock(this, fSize
);
95 \brief The base class for shared memory management.
97 A class which objects need to be allocated in shared memory derives from this class.
105 jack_shm_info_t fInfo
;
106 static unsigned long fSegmentNum
;
107 static unsigned long fSegmentCount
;
108 static jack_shm_info_t gInfo
;
112 void* operator new(size_t size
);
113 void operator delete(void* p
, size_t size
);
117 fInfo
.index
= gInfo
.index
;
118 fInfo
.attached_at
= gInfo
.attached_at
;
121 virtual ~JackShmMem()
129 char* GetShmAddress()
131 return (char*)fInfo
.attached_at
;
137 \brief Pointer on shared memory segment in the client side.
141 class JackShmReadWritePtr
146 jack_shm_info_t fInfo
;
150 if (fInfo
.index
< 0 && index
>= 0) {
151 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index
, fInfo
.index
);
152 if (jack_initialize_shm_client() < 0)
155 if (jack_attach_shm(&fInfo
)) {
156 //jack_error("cannot attach shared memory segment", strerror(errno));
164 JackShmReadWritePtr()
167 fInfo
.attached_at
= NULL
;
170 JackShmReadWritePtr(int index
)
175 virtual ~JackShmReadWritePtr()
177 if (fInfo
.index
>= 0) {
178 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo
.index
);
179 jack_release_shm(&fInfo
);
184 T
* operator->() const
186 return (T
*)fInfo
.attached_at
;
191 return (T
*)fInfo
.attached_at
;
194 JackShmReadWritePtr
& operator=(int index
)
207 return (T
*)fInfo
.attached_at
;
212 \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
216 class JackShmReadWritePtr1
221 jack_shm_info_t fInfo
;
225 if (fInfo
.index
< 0 && index
>= 0) {
226 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index
, fInfo
.index
);
227 if (jack_initialize_shm_client() < 0)
230 if (jack_attach_shm(&fInfo
)) {
231 //jack_error("cannot attach shared memory segment", strerror(errno));
235 nobody else needs to access this shared memory any more, so
236 destroy it. because we have our own attachment to it, it won't
237 vanish till we exit (and release it).
239 jack_destroy_shm(&fInfo
);
245 JackShmReadWritePtr1()
248 fInfo
.attached_at
= NULL
;
251 JackShmReadWritePtr1(int index
)
256 virtual ~JackShmReadWritePtr1()
258 if (fInfo
.index
>= 0) {
259 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo
.index
);
260 jack_release_shm(&fInfo
);
265 T
* operator->() const
267 return (T
*)fInfo
.attached_at
;
272 return (T
*)fInfo
.attached_at
;
275 JackShmReadWritePtr1
& operator=(int index
)
288 return (T
*)fInfo
.attached_at
;
293 \brief Pointer on shared memory segment in the client side.
302 jack_shm_info_t fInfo
;
306 if (fInfo
.index
< 0 && index
>= 0) {
307 JackLog("JackShmPtrRead::Init %ld %ld\n", index
, fInfo
.index
);
308 if (jack_initialize_shm_client() < 0)
311 if (jack_attach_shm_read(&fInfo
)) {
312 //jack_error("cannot attach shared memory segment", strerror(errno));
323 fInfo
.attached_at
= NULL
;
326 JackShmReadPtr(int index
)
331 virtual ~JackShmReadPtr()
333 if (fInfo
.index
>= 0) {
334 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo
.index
);
335 jack_release_shm(&fInfo
);
340 T
* operator->() const
342 return (T
*)fInfo
.attached_at
;
347 return (T
*)fInfo
.attached_at
;
350 JackShmReadPtr
& operator=(int index
)
363 return (T
*)fInfo
.attached_at
;
368 } // end of namespace