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 #define CHECK_MLOCK(ptr, size) (VirtualLock((ptr), (size)) != 0)
00034 #define CHECK_MUNLOCK(ptr, size) (VirtualUnlock((ptr), (size)) != 0)
00035 #define CHECK_MLOCKALL()(false)
00036 #define CHECK_MUNLOCKALL()(false)
00037 #else
00038 #include <sys/types.h>
00039 #include <sys/mman.h>
00040 #define CHECK_MLOCK(ptr, size) (mlock((ptr), (size)) == 0)
00041 #define CHECK_MUNLOCK(ptr, size) (munlock((ptr), (size)) == 0)
00042 #define CHECK_MLOCKALL() (mlockall(MCL_CURRENT | MCL_FUTURE) == 0)
00043 #define CHECK_MUNLOCKALL() (munlockall() == 0)
00044 #endif
00045
00046 namespace Jack
00047 {
00048
00049 void LockMemoryImp(void* ptr, size_t size);
00050 void UnlockMemoryImp(void* ptr, size_t size);
00051
00052 class JackMem
00053 {
00054 private:
00055
00056 size_t fSize;
00057 static size_t gSize;
00058
00059 public:
00060
00061 void* operator new(size_t size)
00062 {
00063 gSize = size;
00064 return calloc(1, size);
00065 }
00066
00067 void operator delete(void* ptr, size_t size)
00068 {
00069 free(ptr);
00070 }
00071
00072 JackMem():fSize(gSize)
00073 {}
00074
00075 virtual ~JackMem()
00076 {}
00077
00078 void LockMemory()
00079 {
00080 LockMemoryImp(this, fSize);
00081 }
00082
00083 void UnlockMemory()
00084 {
00085 UnlockMemoryImp(this, fSize);
00086 }
00087
00088 };
00089
00096 class JackShmMem
00097 {
00098
00099 protected:
00100
00101 jack_shm_info_t fInfo;
00102 static unsigned long fSegmentNum;
00103 static unsigned long fSegmentCount;
00104 static jack_shm_info_t gInfo;
00105
00106 public:
00107
00108 void* operator new(size_t size);
00109 void operator delete(void* p, size_t size);
00110
00111 JackShmMem()
00112 {
00113 fInfo.index = gInfo.index;
00114 fInfo.attached_at = gInfo.attached_at;
00115 fInfo.size = gInfo.size;
00116 }
00117
00118 virtual ~JackShmMem()
00119 {}
00120
00121 int GetShmIndex()
00122 {
00123 return fInfo.index;
00124 }
00125
00126 char* GetShmAddress()
00127 {
00128 return (char*)fInfo.attached_at;
00129 }
00130
00131 void LockMemory()
00132 {
00133 LockMemoryImp(this, fInfo.size);
00134 }
00135
00136 void UnlockMemory()
00137 {
00138 UnlockMemoryImp(this, fInfo.size);
00139 }
00140
00141 };
00142
00147 template <class T>
00148 class JackShmReadWritePtr
00149 {
00150
00151 private:
00152
00153 jack_shm_info_t fInfo;
00154
00155 void Init(int index)
00156 {
00157 if (fInfo.index < 0 && index >= 0) {
00158 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
00159 if (jack_initialize_shm_client() < 0)
00160 throw - 1;
00161 fInfo.index = index;
00162 if (jack_attach_shm(&fInfo)) {
00163
00164 throw - 2;
00165 }
00166 }
00167 }
00168
00169 public:
00170
00171 JackShmReadWritePtr()
00172 {
00173 fInfo.index = -1;
00174 fInfo.attached_at = NULL;
00175 }
00176
00177 JackShmReadWritePtr(int index)
00178 {
00179 Init(index);
00180 }
00181
00182 virtual ~JackShmReadWritePtr()
00183 {
00184 if (fInfo.index >= 0) {
00185 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
00186 jack_release_shm(&fInfo);
00187 fInfo.index = -1;
00188 }
00189 }
00190
00191 T* operator->() const
00192 {
00193 return (T*)fInfo.attached_at;
00194 }
00195
00196 operator T*() const
00197 {
00198 return (T*)fInfo.attached_at;
00199 }
00200
00201 JackShmReadWritePtr& operator=(int index)
00202 {
00203 Init(index);
00204 return *this;
00205 }
00206
00207 int GetShmIndex()
00208 {
00209 return fInfo.index;
00210 }
00211
00212 T* GetShmAddress()
00213 {
00214 return (T*)fInfo.attached_at;
00215 }
00216 };
00217
00222 template <class T>
00223 class JackShmReadWritePtr1
00224 {
00225
00226 private:
00227
00228 jack_shm_info_t fInfo;
00229
00230 void Init(int index)
00231 {
00232 if (fInfo.index < 0 && index >= 0) {
00233 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
00234 if (jack_initialize_shm_client() < 0)
00235 throw - 1;
00236 fInfo.index = index;
00237 if (jack_attach_shm(&fInfo)) {
00238
00239 throw - 2;
00240 }
00241
00242
00243
00244
00245
00246 jack_destroy_shm(&fInfo);
00247 }
00248 }
00249
00250 public:
00251
00252 JackShmReadWritePtr1()
00253 {
00254 fInfo.index = -1;
00255 fInfo.attached_at = NULL;
00256 }
00257
00258 JackShmReadWritePtr1(int index)
00259 {
00260 Init(index);
00261 }
00262
00263 virtual ~JackShmReadWritePtr1()
00264 {
00265 if (fInfo.index >= 0) {
00266 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
00267 jack_release_shm(&fInfo);
00268 fInfo.index = -1;
00269 }
00270 }
00271
00272 T* operator->() const
00273 {
00274 return (T*)fInfo.attached_at;
00275 }
00276
00277 operator T*() const
00278 {
00279 return (T*)fInfo.attached_at;
00280 }
00281
00282 JackShmReadWritePtr1& operator=(int index)
00283 {
00284 Init(index);
00285 return *this;
00286 }
00287
00288 int GetShmIndex()
00289 {
00290 return fInfo.index;
00291 }
00292
00293 T* GetShmAddress()
00294 {
00295 return (T*)fInfo.attached_at;
00296 }
00297 };
00298
00303 template <class T>
00304 class JackShmReadPtr
00305 {
00306
00307 private:
00308
00309 jack_shm_info_t fInfo;
00310
00311 void Init(int index)
00312 {
00313 if (fInfo.index < 0 && index >= 0) {
00314 JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
00315 if (jack_initialize_shm_client() < 0)
00316 throw - 1;
00317 fInfo.index = index;
00318 if (jack_attach_shm_read(&fInfo)) {
00319
00320 throw - 2;
00321 }
00322 }
00323 }
00324
00325 public:
00326
00327 JackShmReadPtr()
00328 {
00329 fInfo.index = -1;
00330 fInfo.attached_at = NULL;
00331 }
00332
00333 JackShmReadPtr(int index)
00334 {
00335 Init(index);
00336 }
00337
00338 virtual ~JackShmReadPtr()
00339 {
00340 if (fInfo.index >= 0) {
00341 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
00342 jack_release_shm(&fInfo);
00343 fInfo.index = -1;
00344 }
00345 }
00346
00347 T* operator->() const
00348 {
00349 return (T*)fInfo.attached_at;
00350 }
00351
00352 operator T*() const
00353 {
00354 return (T*)fInfo.attached_at;
00355 }
00356
00357 JackShmReadPtr& operator=(int index)
00358 {
00359 Init(index);
00360 return *this;
00361 }
00362
00363 int GetShmIndex()
00364 {
00365 return fInfo.index;
00366 }
00367
00368 T* GetShmAddress()
00369 {
00370 return (T*)fInfo.attached_at;
00371 }
00372
00373 };
00374
00375 }
00376
00377 #endif