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