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 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     // 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.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 } // end of namespace
00132 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1