Merge branch 'master' into develop
[jack2.git] / common / JackShmMem.cpp
blobe144c225efe36667a2c064a009e9b72326042621
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackError.h"
21 #include "JackShmMem.h"
22 #include <stdio.h>
24 namespace Jack
27 static unsigned int fSegmentNum = 0;
28 static jack_shm_info_t gInfo;
30 JackShmMem::JackShmMem()
32 JackShmMemAble::Init();
33 LockMemory();
36 JackShmMem::~JackShmMem()
38 UnlockMemory();
41 void JackShmMemAble::Init()
43 fInfo.index = gInfo.index;
44 fInfo.ptr.attached_at = gInfo.ptr.attached_at;
45 fInfo.size = gInfo.size;
48 void* JackShmMem::operator new(size_t size, void* memory)
50 jack_log("JackShmMem::new placement size = %ld", size);
51 return memory;
54 void* JackShmMem::operator new(size_t size)
56 jack_shm_info_t info;
57 JackShmMem* obj;
58 char name[64];
60 snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
62 if (jack_shmalloc(name, size, &info)) {
63 jack_error("Cannot create shared memory segment of size = %d", size, strerror(errno));
64 goto error;
67 if (jack_attach_shm(&info)) {
68 jack_error("Cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
69 jack_destroy_shm(&info);
70 goto error;
73 obj = (JackShmMem*)jack_shm_addr(&info);
74 // It is unsafe to set object fields directly (may be overwritten during object initialization),
75 // so use an intermediate global data
76 gInfo.index = info.index;
77 gInfo.size = size;
78 gInfo.ptr.attached_at = info.ptr.attached_at;
80 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
81 return obj;
83 error:
84 jack_error("JackShmMem::new bad alloc", size);
85 throw std::bad_alloc();
88 void JackShmMem::operator delete(void* p, size_t size)
90 jack_shm_info_t info;
91 JackShmMem* obj = (JackShmMem*)p;
92 info.index = obj->fInfo.index;
93 info.ptr.attached_at = obj->fInfo.ptr.attached_at;
95 jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
97 jack_release_shm(&info);
98 jack_destroy_shm(&info);
101 void JackShmMem::operator delete(void* obj)
103 if (obj) {
104 JackShmMem::operator delete(obj, 0);
108 void LockMemoryImp(void* ptr, size_t size)
110 if (CHECK_MLOCK((char*)ptr, size)) {
111 jack_log("Succeeded in locking %u byte memory area", size);
112 } else {
113 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
117 void InitLockMemoryImp(void* ptr, size_t size)
119 if (CHECK_MLOCK((char*)ptr, size)) {
120 memset(ptr, 0, size);
121 jack_log("Succeeded in locking %u byte memory area", size);
122 } else {
123 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
127 void UnlockMemoryImp(void* ptr, size_t size)
129 if (CHECK_MUNLOCK((char*)ptr, size)) {
130 jack_log("Succeeded in unlocking %u byte memory area", size);
131 } else {
132 jack_error("Cannot unlock down %u byte memory area (%s)", size, strerror(errno));
136 void LockAllMemory()
138 if (CHECK_MLOCKALL()) {
139 jack_log("Succeeded in locking all memory");
140 } else {
141 jack_error("Cannot lock all memory (%s)", strerror(errno));
145 void UnlockAllMemory()
147 if (CHECK_MUNLOCKALL()) {
148 jack_log("Succeeded in unlocking all memory");
149 } else {
150 jack_error("Cannot unlock all memory (%s)", strerror(errno));
155 } // end of namespace