00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackShmMem.h"
00022 #include "JackError.h"
00023 #include <stdio.h>
00024
00025 namespace Jack
00026 {
00027
00028 unsigned long JackShmMem::fSegmentNum = 0;
00029 unsigned long JackShmMem::fSegmentCount = 0;
00030
00031 jack_shm_info_t JackShmMem::gInfo;
00032 size_t JackMem::gSize = 0;
00033
00034 void* JackShmMem::operator new(size_t size)
00035 {
00036 jack_shm_info_t info;
00037 JackShmMem* obj;
00038 char name[64];
00039
00040 snprintf(name, sizeof(name), "/jack_shared%ld", JackShmMem::fSegmentNum++);
00041
00042 if (JackShmMem::fSegmentCount++ == 0) {
00043 JackLog("jack_initialize_shm\n");
00044 if (jack_initialize_shm_server() < 0) {
00045 jack_error("cannot initialize shm", strerror(errno));
00046 goto error;
00047 }
00048 }
00049
00050 if (jack_shmalloc(name, size, &info)) {
00051 jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
00052 goto error;
00053 }
00054
00055 if (jack_attach_shm(&info)) {
00056 jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
00057 jack_destroy_shm(&info);
00058 goto error;
00059 }
00060
00061 obj = (JackShmMem*)jack_shm_addr(&info);
00062
00063
00064 gInfo.index = info.index;
00065 gInfo.size = size;
00066 gInfo.attached_at = info.attached_at;
00067
00068 JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size);
00069 return obj;
00070
00071 error:
00072 jack_error("JackShmMem::new bad alloc", size);
00073 throw new std::bad_alloc;
00074 }
00075
00076 void JackShmMem::operator delete(void* p, size_t size)
00077 {
00078 jack_shm_info_t info;
00079 JackShmMem* obj = (JackShmMem*)p;
00080 info.index = obj->fInfo.index;
00081 info.attached_at = obj->fInfo.attached_at;
00082
00083 JackLog("JackShmMem::delete size = %ld index = %ld\n", size, info.index);
00084
00085 jack_release_shm(&info);
00086 jack_destroy_shm(&info);
00087
00088 if (--JackShmMem::fSegmentCount == 0) {
00089 JackLog("jack_cleanup_shm\n");
00090 jack_cleanup_shm();
00091 }
00092 }
00093
00094 void LockMemoryImp(void* ptr, size_t size)
00095 {
00096 if (CHECK_MLOCK(ptr, size)) {
00097 JackLog("Succeeded in locking %u byte memory area\n", size);
00098 } else {
00099 jack_error("Cannot lock down memory area (%s)", strerror(errno));
00100 }
00101 }
00102
00103 void UnlockMemoryImp(void* ptr, size_t size)
00104 {
00105 if (CHECK_MUNLOCK(ptr, size)) {
00106 JackLog("Succeeded in unlocking %u byte memory area\n", size);
00107 } else {
00108 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
00109 }
00110 }
00111
00112 void LockAllMemory()
00113 {
00114 if (CHECK_MLOCKALL()) {
00115 JackLog("Succeeded in locking all memory\n");
00116 } else {
00117 jack_error("Cannot lock down memory area (%s)", strerror(errno));
00118 }
00119 }
00120
00121 void UnlockAllMemory()
00122 {
00123 if (CHECK_MUNLOCKALL()) {
00124 JackLog("Succeeded in unlocking all memory\n");
00125 } else {
00126 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
00127 }
00128 }
00129
00130
00131 }
00132