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.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
00135 public:
00136
00137 void* operator new(size_t size);
00138 void* operator new(size_t size, void* memory);
00139
00140 void operator delete(void* p, size_t size);
00141 void operator delete(void* p);
00142
00143 };
00144
00149 template <class T>
00150 class JackShmReadWritePtr
00151 {
00152
00153 private:
00154
00155 jack_shm_info_t fInfo;
00156
00157 void Init(int index, const char* server_name = "default")
00158 {
00159 if (fInfo.index < 0 && index >= 0) {
00160 jack_log("JackShmReadWritePtr::Init %ld %ld", index, fInfo.index);
00161 if (jack_initialize_shm(server_name) < 0)
00162 throw - 1;
00163 fInfo.index = index;
00164 if (jack_attach_shm(&fInfo)) {
00165
00166 throw - 2;
00167 }
00168 }
00169 }
00170
00171 public:
00172
00173 JackShmReadWritePtr()
00174 {
00175 fInfo.index = -1;
00176 fInfo.attached_at = NULL;
00177 }
00178
00179 JackShmReadWritePtr(int index, const char* server_name)
00180 {
00181 Init(index, server_name);
00182 }
00183
00184 ~JackShmReadWritePtr()
00185 {
00186 if (fInfo.index >= 0) {
00187 jack_log("JackShmReadWritePtr::~JackShmReadWritePtr %ld", fInfo.index);
00188 jack_release_shm(&fInfo);
00189 fInfo.index = -1;
00190 }
00191 }
00192
00193 T* operator->() const
00194 {
00195 return (T*)fInfo.attached_at;
00196 }
00197
00198 operator T*() const
00199 {
00200 return (T*)fInfo.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.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
00246 throw - 2;
00247 }
00248
00249
00250
00251
00252
00253 jack_destroy_shm(&fInfo);
00254 }
00255 }
00256
00257 public:
00258
00259 JackShmReadWritePtr1()
00260 {
00261 fInfo.index = -1;
00262 fInfo.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 jack_release_shm(&fInfo);
00275 fInfo.index = -1;
00276 }
00277 }
00278
00279 T* operator->() const
00280 {
00281 return (T*)fInfo.attached_at;
00282 }
00283
00284 operator T*() const
00285 {
00286 return (T*)fInfo.attached_at;
00287 }
00288
00289 JackShmReadWritePtr1& operator=(int index)
00290 {
00291 Init(index);
00292 return *this;
00293 }
00294
00295 void SetShmIndex(int index, const char* server_name)
00296 {
00297 Init(index, server_name);
00298 }
00299
00300 int GetShmIndex()
00301 {
00302 return fInfo.index;
00303 }
00304
00305 T* GetShmAddress()
00306 {
00307 return (T*)fInfo.attached_at;
00308 }
00309 };
00310
00315 template <class T>
00316 class JackShmReadPtr
00317 {
00318
00319 private:
00320
00321 jack_shm_info_t fInfo;
00322
00323 void Init(int index, const char* server_name = "default")
00324 {
00325 if (fInfo.index < 0 && index >= 0) {
00326 jack_log("JackShmPtrRead::Init %ld %ld", index, fInfo.index);
00327 if (jack_initialize_shm(server_name) < 0)
00328 throw - 1;
00329 fInfo.index = index;
00330 if (jack_attach_shm_read(&fInfo)) {
00331
00332 throw - 2;
00333 }
00334 }
00335 }
00336
00337 public:
00338
00339 JackShmReadPtr()
00340 {
00341 fInfo.index = -1;
00342 fInfo.attached_at = NULL;
00343 }
00344
00345 JackShmReadPtr(int index, const char* server_name)
00346 {
00347 Init(index, server_name);
00348 }
00349
00350 ~JackShmReadPtr()
00351 {
00352 if (fInfo.index >= 0) {
00353 jack_log("JackShmPtrRead::~JackShmPtrRead %ld", fInfo.index);
00354 jack_release_shm(&fInfo);
00355 fInfo.index = -1;
00356 }
00357 }
00358
00359 T* operator->() const
00360 {
00361 return (T*)fInfo.attached_at;
00362 }
00363
00364 operator T*() const
00365 {
00366 return (T*)fInfo.attached_at;
00367 }
00368
00369 JackShmReadPtr& operator=(int index)
00370 {
00371 Init(index);
00372 return *this;
00373 }
00374
00375 void SetShmIndex(int index, const char* server_name)
00376 {
00377 Init(index, server_name);
00378 }
00379
00380 int GetShmIndex()
00381 {
00382 return fInfo.index;
00383 }
00384
00385 T* GetShmAddress()
00386 {
00387 return (T*)fInfo.attached_at;
00388 }
00389
00390 };
00391
00392 }
00393
00394 #endif