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 JackLockMem::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.attached_at = info.attached_at;
00066 JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size);
00067 return obj;
00068
00069 error:
00070 jack_error("JackShmMem::new bad alloc", size);
00071 throw new std::bad_alloc;
00072 }
00073
00074 void JackShmMem::operator delete(void* p, size_t size)
00075 {
00076 jack_shm_info_t info;
00077 JackShmMem* obj = (JackShmMem*)p;
00078 info.index = obj->fInfo.index;
00079 info.attached_at = obj->fInfo.attached_at;
00080
00081 JackLog("JackShmMem::delete size = %ld index = %ld\n", size, info.index);
00082
00083 jack_release_shm(&info);
00084 jack_destroy_shm(&info);
00085
00086 if (--JackShmMem::fSegmentCount == 0) {
00087 JackLog("jack_cleanup_shm\n");
00088 jack_cleanup_shm();
00089 }
00090 }
00091
00092 }
00093