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 #include "JackCompilerDeps.h"
00027
00028 #include <new>
00029 #include <errno.h>
00030 #include <stdlib.h>
00031
00032 #include "JackShmMem_os.h"
00033
00034 namespace Jack
00035 {
00036
00037 SERVER_EXPORT void LockMemoryImp(void* ptr, size_t size);
00038 SERVER_EXPORT void InitLockMemoryImp(void* ptr, size_t size);
00039 SERVER_EXPORT void UnlockMemoryImp(void* ptr, size_t size);
00040 SERVER_EXPORT void LockAllMemory();
00041 SERVER_EXPORT void UnlockAllMemory();
00042
00043 class JackMem
00044 {
00045 private:
00046
00047 size_t fSize;
00048 static size_t gSize;
00049
00050 protected:
00051
00052 JackMem(): fSize(gSize)
00053 {}
00054 ~JackMem()
00055 {}
00056
00057 public:
00058
00059 void* operator new(size_t size)
00060 {
00061 gSize = size;
00062 return calloc(1, size);
00063 }
00064
00065 void operator delete(void* ptr, size_t size)
00066 {
00067 free(ptr);
00068 }
00069
00070 void LockMemory()
00071 {
00072 LockMemoryImp(this, fSize);
00073 }
00074
00075 void UnlockMemory()
00076 {
00077 UnlockMemoryImp(this, fSize);
00078 }
00079
00080 };
00081
00088 class JackShmMemAble
00089 {
00090 protected:
00091
00092 jack_shm_info_t fInfo;
00093
00094 public:
00095
00096 void Init();
00097
00098 int GetShmIndex()
00099 {
00100 return fInfo.index;
00101 }
00102
00103 char* GetShmAddress()
00104 {
00105 return (char*)fInfo.ptr.attached_at;
00106 }
00107
00108 void LockMemory()
00109 {
00110 LockMemoryImp(this, fInfo.size);
00111 }
00112
00113 void UnlockMemory()
00114 {
00115 UnlockMemoryImp(this, fInfo.size);
00116 }
00117
00118 };
00119
00126 class SERVER_EXPORT JackShmMem : public JackShmMemAble
00127 {
00128
00129 protected:
00130
00131 JackShmMem();
00132 ~JackShmMem();
00133
00134 public:
00135
00136 void* operator new(size_t size);
00137 void* operator new(size_t size, void* memory);
00138
00139 void operator delete(void* p, size_t size);
00140 void operator delete(void* p);
00141
00142 };
00143
00148 template <class T>
00149 class JackShmReadWritePtr
00150 {
00151
00152 private:
00153
00154 jack_shm_info_t fInfo;
00155
00156 void Init(int index, const char* server_name = "default")
00157 {
00158 if (fInfo.index < 0 && index >= 0) {
00159 jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
00160 if (jack_initialize_shm(server_name) < 0)
00161 throw - 1;
00162 fInfo.index = index;
00163 if (jack_attach_shm(&fInfo)) {
00164 throw - 2;
00165 }
00166 GetShmAddress()->LockMemory();
00167 }
00168 }
00169
00170 public:
00171
00172 JackShmReadWritePtr()
00173 {
00174 fInfo.index = -1;
00175 fInfo.ptr.attached_at = (char*)NULL;
00176 }
00177
00178 JackShmReadWritePtr(int index, const char* server_name)
00179 {
00180 Init(index, server_name);
00181 }
00182
00183 ~JackShmReadWritePtr()
00184 {
00185 if (fInfo.index >= 0) {
00186 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
00187 GetShmAddress()->UnlockMemory();
00188 jack_release_shm(&fInfo);
00189 fInfo.index = -1;
00190 }
00191 }
00192
00193 T* operator->() const
00194 {
00195 return (T*)fInfo.ptr.attached_at;
00196 }
00197
00198 operator T*() const
00199 {
00200 return (T*)fInfo.ptr.attached_at;
00201 }
00202
00203 JackShmReadWritePtr& operator=(int index)
00204 {
00205 Init(index);
00206 return *this;
00207 }
00208
00209 void SetShmIndex(int index, const char* server_name)
00210 {
00211 Init(index, server_name);
00212 }
00213
00214 int GetShmIndex()
00215 {
00216 return fInfo.index;
00217 }
00218
00219 T* GetShmAddress()
00220 {
00221 return (T*)fInfo.ptr.attached_at;
00222 }
00223 };
00224
00229 template <class T>
00230 class JackShmReadWritePtr1
00231 {
00232
00233 private:
00234
00235 jack_shm_info_t fInfo;
00236
00237 void Init(int index, const char* server_name = "default")
00238 {
00239 if (fInfo.index < 0 && index >= 0) {
00240 jack_log("JackShmReadWritePtr1::Init %ld %ld", index, fInfo.index);
00241 if (jack_initialize_shm(server_name) < 0)
00242 throw - 1;
00243 fInfo.index = index;
00244 if (jack_attach_shm(&fInfo)) {
00245 throw - 2;
00246 }
00247
00248
00249
00250
00251
00252 jack_destroy_shm(&fInfo);
00253 GetShmAddress()->LockMemory();
00254 }
00255 }
00256
00257 public:
00258
00259 JackShmReadWritePtr1()
00260 {
00261 fInfo.index = -1;
00262 fInfo.ptr.attached_at = NULL;
00263 }
00264
00265 JackShmReadWritePtr1(int index, const char* server_name)
00266 {
00267 Init(index, server_name);
00268 }
00269
00270 ~JackShmReadWritePtr1()
00271 {
00272 if (fInfo.index >= 0) {
00273 jack_log("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld", fInfo.index);
00274 GetShmAddress()->UnlockMemory();
00275 jack_release_shm(&fInfo);
00276 fInfo.index = -1;
00277 }
00278 }
00279
00280 T* operator->() const
00281 {
00282 return (T*)fInfo.ptr.attached_at;
00283 }
00284
00285 operator T*() const
00286 {
00287 return (T*)fInfo.ptr.attached_at;
00288 }
00289
00290 JackShmReadWritePtr1& operator=(int index)
00291 {
00292 Init(index);
00293 return *this;
00294 }
00295
00296 void SetShmIndex(int index, const char* server_name)
00297 {
00298 Init(index, server_name);
00299 }
00300
00301 int GetShmIndex()
00302 {
00303 return fInfo.index;
00304 }
00305
00306 T* GetShmAddress()
00307 {
00308 return (T*)fInfo.ptr.attached_at;
00309 }
00310 };
00311
00316 template <class T>
00317 class JackShmReadPtr
00318 {
00319
00320 private:
00321
00322 jack_shm_info_t fInfo;
00323
00324 void Init(int index, const char* server_name = "default")
00325 {
00326 if (fInfo.index < 0 && index >= 0) {
00327 jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
00328 if (jack_initialize_shm(server_name) < 0)
00329 throw - 1;
00330 fInfo.index = index;
00331 if (jack_attach_shm_read(&fInfo)) {
00332 throw - 2;
00333 }
00334 GetShmAddress()->LockMemory();
00335 }
00336 }
00337
00338 public:
00339
00340 JackShmReadPtr()
00341 {
00342 fInfo.index = -1;
00343 fInfo.ptr.attached_at = NULL;
00344 }
00345
00346 JackShmReadPtr(int index, const char* server_name)
00347 {
00348 Init(index, server_name);
00349 }
00350
00351 ~JackShmReadPtr()
00352 {
00353 if (fInfo.index >= 0) {
00354 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
00355 GetShmAddress()->UnlockMemory();
00356 jack_release_shm(&fInfo);
00357 fInfo.index = -1;
00358 }
00359 }
00360
00361 T* operator->() const
00362 {
00363 return (T*)fInfo.ptr.attached_at;
00364 }
00365
00366 operator T*() const
00367 {
00368 return (T*)fInfo.ptr.attached_at;
00369 }
00370
00371 JackShmReadPtr& operator=(int index)
00372 {
00373 Init(index);
00374 return *this;
00375 }
00376
00377 void SetShmIndex(int index, const char* server_name)
00378 {
00379 Init(index, server_name);
00380 }
00381
00382 int GetShmIndex()
00383 {
00384 return fInfo.index;
00385 }
00386
00387 T* GetShmAddress()
00388 {
00389 return (T*)fInfo.ptr.attached_at;
00390 }
00391
00392 };
00393
00394 }
00395
00396 #endif