1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
Copyright (C) 2004-2008 Grame
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "JackError.h"
#include "JackShmMem.h"
#include <stdio.h>
namespace Jack
{
static unsigned int fSegmentNum = 0;
static jack_shm_info_t gInfo;
size_t JackMem::gSize = 0;
JackShmMem::JackShmMem()
{
JackShmMemAble::Init();
LockMemory();
}
JackShmMem::~JackShmMem()
{
UnlockMemory();
}
void JackShmMemAble::Init()
{
fInfo.index = gInfo.index;
fInfo.ptr.attached_at = gInfo.ptr.attached_at;
fInfo.size = gInfo.size;
}
void* JackShmMem::operator new(size_t size, void* memory)
{
jack_log("JackShmMem::new placement size = %ld", size);
return memory;
}
void* JackShmMem::operator new(size_t size)
{
jack_shm_info_t info;
JackShmMem* obj;
char name[64];
snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
if (jack_shmalloc(name, size, &info)) {
jack_error("Cannot create shared memory segment of size = %d", size, strerror(errno));
goto error;
}
if (jack_attach_shm(&info)) {
jack_error("Cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
jack_destroy_shm(&info);
goto error;
}
obj = (JackShmMem*)jack_shm_addr(&info);
// It is unsafe to set object fields directly (may be overwritten during object initialization),
// so use an intermediate global data
gInfo.index = info.index;
gInfo.size = size;
gInfo.ptr.attached_at = info.ptr.attached_at;
jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
return obj;
error:
jack_error("JackShmMem::new bad alloc", size);
throw std::bad_alloc();
}
void JackShmMem::operator delete(void* p, size_t size)
{
jack_shm_info_t info;
JackShmMem* obj = (JackShmMem*)p;
info.index = obj->fInfo.index;
info.ptr.attached_at = obj->fInfo.ptr.attached_at;
jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
jack_release_shm(&info);
jack_destroy_shm(&info);
}
void JackShmMem::operator delete(void* obj)
{
if (obj) {
JackShmMem::operator delete(obj, 0);
}
}
void LockMemoryImp(void* ptr, size_t size)
{
if (CHECK_MLOCK((char*)ptr, size)) {
jack_log("Succeeded in locking %u byte memory area", size);
} else {
jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
}
}
void InitLockMemoryImp(void* ptr, size_t size)
{
if (CHECK_MLOCK((char*)ptr, size)) {
memset(ptr, 0, size);
jack_log("Succeeded in locking %u byte memory area", size);
} else {
jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
}
}
void UnlockMemoryImp(void* ptr, size_t size)
{
if (CHECK_MUNLOCK((char*)ptr, size)) {
jack_log("Succeeded in unlocking %u byte memory area", size);
} else {
jack_error("Cannot unlock down %u byte memory area (%s)", size, strerror(errno));
}
}
void LockAllMemory()
{
if (CHECK_MLOCKALL()) {
jack_log("Succeeded in locking all memory");
} else {
jack_error("Cannot lock all memory (%s)", strerror(errno));
}
}
void UnlockAllMemory()
{
if (CHECK_MUNLOCKALL()) {
jack_log("Succeeded in unlocking all memory");
} else {
jack_error("Cannot unlock all memory (%s)", strerror(errno));
}
}
} // end of namespace
|