JackShmMem.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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     // It is unsafe to set object fields directly (may be overwritten during object initialization),
00063     // so use an intermediate global data
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 } // end of namespace
00093 

Generated on Wed Jan 10 11:42:46 2007 for Jackdmp by  doxygen 1.4.5