00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __JackShmMem__
00022 #define __JackShmMem__
00023
00024 #include "shm.h"
00025 #include "JackError.h"
00026
00027 #include <new>
00028 #include <errno.h>
00029 #include <stdlib.h>
00030
00031 #ifdef WIN32
00032 #include <windows.h>
00033 #else
00034 #include <sys/types.h>
00035 #include <sys/mman.h>
00036 #endif
00037
00038 namespace Jack
00039 {
00040
00041 class JackLockMem
00042 {
00043 private:
00044
00045 size_t fSize;
00046 static size_t gSize;
00047
00048 public:
00049
00050 void* operator new(size_t size)
00051 {
00052 gSize = size;
00053 return malloc(size);
00054 }
00055
00056 void operator delete(void* ptr, size_t size)
00057 {
00058 free(ptr);
00059 }
00060
00061 JackLockMem():fSize(gSize)
00062 {}
00063
00064 virtual ~JackLockMem()
00065 {
00066 UnlockMemory();
00067 }
00068
00069 void LockMemory()
00070 {
00071 #ifdef __APPLE__
00072 mlock(this, fSize);
00073 #elif linux_
00074 mlock(this, fSize);
00075 #elif WIN32
00076 VirtualLock(this, fSize);
00077 #endif
00078 }
00079
00080 void UnlockMemory()
00081 {
00082 #ifdef __APPLE__
00083 munlock(this, fSize);
00084 #elif linux_
00085 munlock(this, fSize);
00086 #elif WIN32
00087 VirtualUnlock(this, fSize);
00088 #endif
00089 }
00090
00091 };
00092
00093
00100 class JackShmMem
00101 {
00102
00103 private:
00104
00105 jack_shm_info_t fInfo;
00106 static unsigned long fSegmentNum;
00107 static unsigned long fSegmentCount;
00108 static jack_shm_info_t gInfo;
00109
00110 public:
00111
00112 void* operator new(size_t size);
00113 void operator delete(void* p, size_t size);
00114
00115 JackShmMem()
00116 {
00117 fInfo.index = gInfo.index;
00118 fInfo.attached_at = gInfo.attached_at;
00119 }
00120
00121 virtual ~JackShmMem()
00122 {}
00123
00124 int GetShmIndex()
00125 {
00126 return fInfo.index;
00127 }
00128
00129 char* GetShmAddress()
00130 {
00131 return (char*)fInfo.attached_at;
00132 }
00133
00134 };
00135
00140 template <class T>
00141 class JackShmReadWritePtr
00142 {
00143
00144 private:
00145
00146 jack_shm_info_t fInfo;
00147
00148 void Init(int index)
00149 {
00150 if (fInfo.index < 0 && index >= 0) {
00151 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
00152 if (jack_initialize_shm_client() < 0)
00153 throw - 1;
00154 fInfo.index = index;
00155 if (jack_attach_shm(&fInfo)) {
00156
00157 throw - 2;
00158 }
00159 }
00160 }
00161
00162 public:
00163
00164 JackShmReadWritePtr()
00165 {
00166 fInfo.index = -1;
00167 fInfo.attached_at = NULL;
00168 }
00169
00170 JackShmReadWritePtr(int index)
00171 {
00172 Init(index);
00173 }
00174
00175 virtual ~JackShmReadWritePtr()
00176 {
00177 if (fInfo.index >= 0) {
00178 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
00179 jack_release_shm(&fInfo);
00180 fInfo.index = -1;
00181 }
00182 }
00183
00184 T* operator->() const
00185 {
00186 return (T*)fInfo.attached_at;
00187 }
00188
00189 operator T*() const
00190 {
00191 return (T*)fInfo.attached_at;
00192 }
00193
00194 JackShmReadWritePtr& operator=(int index)
00195 {
00196 Init(index);
00197 return *this;
00198 }
00199
00200 int GetShmIndex()
00201 {
00202 return fInfo.index;
00203 }
00204
00205 T* GetShmAddress()
00206 {
00207 return (T*)fInfo.attached_at;
00208 }
00209 };
00210
00215 template <class T>
00216 class JackShmReadWritePtr1
00217 {
00218
00219 private:
00220
00221 jack_shm_info_t fInfo;
00222
00223 void Init(int index)
00224 {
00225 if (fInfo.index < 0 && index >= 0) {
00226 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
00227 if (jack_initialize_shm_client() < 0)
00228 throw - 1;
00229 fInfo.index = index;
00230 if (jack_attach_shm(&fInfo)) {
00231
00232 throw - 2;
00233 }
00234
00235
00236
00237
00238
00239 jack_destroy_shm(&fInfo);
00240 }
00241 }
00242
00243 public:
00244
00245 JackShmReadWritePtr1()
00246 {
00247 fInfo.index = -1;
00248 fInfo.attached_at = NULL;
00249 }
00250
00251 JackShmReadWritePtr1(int index)
00252 {
00253 Init(index);
00254 }
00255
00256 virtual ~JackShmReadWritePtr1()
00257 {
00258 if (fInfo.index >= 0) {
00259 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
00260 jack_release_shm(&fInfo);
00261 fInfo.index = -1;
00262 }
00263 }
00264
00265 T* operator->() const
00266 {
00267 return (T*)fInfo.attached_at;
00268 }
00269
00270 operator T*() const
00271 {
00272 return (T*)fInfo.attached_at;
00273 }
00274
00275 JackShmReadWritePtr1& operator=(int index)
00276 {
00277 Init(index);
00278 return *this;
00279 }
00280
00281 int GetShmIndex()
00282 {
00283 return fInfo.index;
00284 }
00285
00286 T* GetShmAddress()
00287 {
00288 return (T*)fInfo.attached_at;
00289 }
00290 };
00291
00296 template <class T>
00297 class JackShmReadPtr
00298 {
00299
00300 private:
00301
00302 jack_shm_info_t fInfo;
00303
00304 void Init(int index)
00305 {
00306 if (fInfo.index < 0 && index >= 0) {
00307 JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
00308 if (jack_initialize_shm_client() < 0)
00309 throw - 1;
00310 fInfo.index = index;
00311 if (jack_attach_shm_read(&fInfo)) {
00312
00313 throw - 2;
00314 }
00315 }
00316 }
00317
00318 public:
00319
00320 JackShmReadPtr()
00321 {
00322 fInfo.index = -1;
00323 fInfo.attached_at = NULL;
00324 }
00325
00326 JackShmReadPtr(int index)
00327 {
00328 Init(index);
00329 }
00330
00331 virtual ~JackShmReadPtr()
00332 {
00333 if (fInfo.index >= 0) {
00334 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
00335 jack_release_shm(&fInfo);
00336 fInfo.index = -1;
00337 }
00338 }
00339
00340 T* operator->() const
00341 {
00342 return (T*)fInfo.attached_at;
00343 }
00344
00345 operator T*() const
00346 {
00347 return (T*)fInfo.attached_at;
00348 }
00349
00350 JackShmReadPtr& operator=(int index)
00351 {
00352 Init(index);
00353 return *this;
00354 }
00355
00356 int GetShmIndex()
00357 {
00358 return fInfo.index;
00359 }
00360
00361 T* GetShmAddress()
00362 {
00363 return (T*)fInfo.attached_at;
00364 }
00365
00366 };
00367
00368 }
00369
00370 #endif