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 int JackShmMem::fSegmentNum = 0;
00029 jack_shm_info_t JackShmMem::gInfo;
00030 size_t JackMem::gSize = 0;
00031
00032 void* JackShmMem::operator new(size_t size)
00033 {
00034 jack_shm_info_t info;
00035 JackShmMem* obj;
00036 char name[64];
00037
00038 snprintf(name, sizeof(name), "/jack_shared%ld", JackShmMem::fSegmentNum++);
00039
00040 if (jack_shmalloc(name, size, &info)) {
00041 jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
00042 goto error;
00043 }
00044
00045 if (jack_attach_shm(&info)) {
00046 jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
00047 jack_destroy_shm(&info);
00048 goto error;
00049 }
00050
00051 obj = (JackShmMem*)jack_shm_addr(&info);
00052
00053
00054 gInfo.index = info.index;
00055 gInfo.size = size;
00056 gInfo.attached_at = info.attached_at;
00057
00058 JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size);
00059 return obj;
00060
00061 error:
00062 jack_error("JackShmMem::new bad alloc", size);
00063 throw new std::bad_alloc;
00064 }
00065
00066 void JackShmMem::operator delete(void* p, size_t size)
00067 {
00068 jack_shm_info_t info;
00069 JackShmMem* obj = (JackShmMem*)p;
00070 info.index = obj->fInfo.index;
00071 info.attached_at = obj->fInfo.attached_at;
00072
00073 JackLog("JackShmMem::delete size = %ld index = %ld\n", size, info.index);
00074
00075 jack_release_shm(&info);
00076 jack_destroy_shm(&info);
00077 }
00078
00079 void LockMemoryImp(void* ptr, size_t size)
00080 {
00081 if (CHECK_MLOCK(ptr, size)) {
00082 JackLog("Succeeded in locking %u byte memory area\n", size);
00083 } else {
00084 jack_error("Cannot lock down memory area (%s)", strerror(errno));
00085 }
00086 }
00087
00088 void UnlockMemoryImp(void* ptr, size_t size)
00089 {
00090 if (CHECK_MUNLOCK(ptr, size)) {
00091 JackLog("Succeeded in unlocking %u byte memory area\n", size);
00092 } else {
00093 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
00094 }
00095 }
00096
00097 void LockAllMemory()
00098 {
00099 if (CHECK_MLOCKALL()) {
00100 JackLog("Succeeded in locking all memory\n");
00101 } else {
00102 jack_error("Cannot lock down memory area (%s)", strerror(errno));
00103 }
00104 }
00105
00106 void UnlockAllMemory()
00107 {
00108 if (CHECK_MUNLOCKALL()) {
00109 JackLog("Succeeded in unlocking all memory\n");
00110 } else {
00111 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
00112 }
00113 }
00114
00115
00116 }
00117