JackShmMem.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2008 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 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     // It is unsafe to set object fields directly (may be overwritten during object initialization),
00053     // so use an intermediate global data
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 } // end of namespace
00117 

Generated on Thu Feb 14 11:16:02 2008 for Jackdmp by  doxygen 1.5.1