jack: remove unnecessary GPL include from LGPL code
[jack2.git] / common / JackShmMem.cpp
blob4651ecf698d4a9ebe66bff7f484117c4f5cf9973
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;
29 size_t JackMem::gSize = 0;
31 JackShmMem::JackShmMem()
33 JackShmMemAble::Init();
34 LockMemory();
37 JackShmMem::~JackShmMem()
39 UnlockMemory();
42 void JackShmMemAble::Init()
44 fInfo.index = gInfo.index;
45 fInfo.ptr.attached_at = gInfo.ptr.attached_at;
46 fInfo.size = gInfo.size;
49 void* JackShmMem::operator new(size_t size, void* memory)
51 jack_log("JackShmMem::new placement size = %ld", size);
52 return memory;
55 void* JackShmMem::operator new(size_t size)
57 jack_shm_info_t info;
58 JackShmMem* obj;
59 char name[64];
61 snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
63 if (jack_shmalloc(name, size, &info)) {
64 jack_error("Cannot create shared memory segment of size = %d", size, strerror(errno));
65 goto error;
68 if (jack_attach_shm(&info)) {
69 jack_error("Cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
70 jack_destroy_shm(&info);
71 goto error;
74 obj = (JackShmMem*)jack_shm_addr(&info);
75 // It is unsafe to set object fields directly (may be overwritten during object initialization),
76 // so use an intermediate global data
77 gInfo.index = info.index;
78 gInfo.size = size;
79 gInfo.ptr.attached_at = info.ptr.attached_at;
81 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
82 return obj;
84 error:
85 jack_error("JackShmMem::new bad alloc", size);
86 throw std::bad_alloc();
89 void JackShmMem::operator delete(void* p, size_t size)
91 jack_shm_info_t info;
92 JackShmMem* obj = (JackShmMem*)p;
93 info.index = obj->fInfo.index;
94 info.ptr.attached_at = obj->fInfo.ptr.attached_at;
96 jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
98 jack_release_shm(&info);
99 jack_destroy_shm(&info);
102 void JackShmMem::operator delete(void* obj)
104 if (obj) {
105 JackShmMem::operator delete(obj, 0);
109 void LockMemoryImp(void* ptr, size_t size)
111 if (CHECK_MLOCK((char*)ptr, size)) {
112 jack_log("Succeeded in locking %u byte memory area", size);
113 } else {
114 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
118 void InitLockMemoryImp(void* ptr, size_t size)
120 if (CHECK_MLOCK((char*)ptr, size)) {
121 memset(ptr, 0, size);
122 jack_log("Succeeded in locking %u byte memory area", size);
123 } else {
124 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
128 void UnlockMemoryImp(void* ptr, size_t size)
130 if (CHECK_MUNLOCK((char*)ptr, size)) {
131 jack_log("Succeeded in unlocking %u byte memory area", size);
132 } else {
133 jack_error("Cannot unlock down %u byte memory area (%s)", size, strerror(errno));
137 void LockAllMemory()
139 if (CHECK_MLOCKALL()) {
140 jack_log("Succeeded in locking all memory");
141 } else {
142 jack_error("Cannot lock all memory (%s)", strerror(errno));
146 void UnlockAllMemory()
148 if (CHECK_MUNLOCKALL()) {
149 jack_log("Succeeded in unlocking all memory");
150 } else {
151 jack_error("Cannot unlock all memory (%s)", strerror(errno));
156 } // end of namespace