Unused variable.
[jack2.git] / common / JackShmMem.cpp
blob90d42e7be18f0bad26eb5f98ce66c9b7e58a9b4d
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2009 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackError.h"
22 #include "JackShmMem.h"
23 #include <stdio.h>
25 namespace Jack
28 static unsigned int fSegmentNum = 0;
29 static jack_shm_info_t gInfo;
30 size_t JackMem::gSize = 0;
32 JackShmMem::JackShmMem()
34 JackShmMemAble::Init();
35 LockMemory();
38 JackShmMem::~JackShmMem()
40 UnlockMemory();
43 void JackShmMemAble::Init()
45 fInfo.index = gInfo.index;
46 fInfo.ptr.attached_at = gInfo.ptr.attached_at;
47 fInfo.size = gInfo.size;
50 void* JackShmMem::operator new(size_t size, void* memory)
52 jack_log("JackShmMem::new placement size = %ld", size);
53 return memory;
56 void* JackShmMem::operator new(size_t size)
58 jack_shm_info_t info;
59 JackShmMem* obj;
60 char name[64];
62 snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
64 if (jack_shmalloc(name, size, &info)) {
65 jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
66 goto error;
69 if (jack_attach_shm(&info)) {
70 jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
71 jack_destroy_shm(&info);
72 goto error;
75 obj = (JackShmMem*)jack_shm_addr(&info);
76 // It is unsafe to set object fields directly (may be overwritten during object initialization),
77 // so use an intermediate global data
78 gInfo.index = info.index;
79 gInfo.size = size;
80 gInfo.ptr.attached_at = info.ptr.attached_at;
82 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
83 return obj;
85 error:
86 jack_error("JackShmMem::new bad alloc", size);
87 throw std::bad_alloc();
90 void JackShmMem::operator delete(void* p, size_t size)
92 jack_shm_info_t info;
93 JackShmMem* obj = (JackShmMem*)p;
94 info.index = obj->fInfo.index;
95 info.ptr.attached_at = obj->fInfo.ptr.attached_at;
97 jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
99 jack_release_shm(&info);
100 jack_destroy_shm(&info);
103 void JackShmMem::operator delete(void* obj)
105 if (obj) {
106 JackShmMem::operator delete(obj, 0);
110 void LockMemoryImp(void* ptr, size_t size)
112 if (CHECK_MLOCK((char*)ptr, size)) {
113 jack_log("Succeeded in locking %u byte memory area", size);
114 } else {
115 jack_error("Cannot lock down memory area (%s)", strerror(errno));
119 void InitLockMemoryImp(void* ptr, size_t size)
121 if (CHECK_MLOCK((char*)ptr, size)) {
122 memset(ptr, 0, size);
123 jack_log("Succeeded in locking %u byte memory area", size);
124 } else {
125 jack_error("Cannot lock down memory area (%s)", strerror(errno));
129 void UnlockMemoryImp(void* ptr, size_t size)
131 if (CHECK_MUNLOCK((char*)ptr, size)) {
132 jack_log("Succeeded in unlocking %u byte memory area", size);
133 } else {
134 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
138 void LockAllMemory()
140 if (CHECK_MLOCKALL()) {
141 jack_log("Succeeded in locking all memory");
142 } else {
143 jack_error("Cannot lock all memory (%s)", strerror(errno));
147 void UnlockAllMemory()
149 if (CHECK_MUNLOCKALL()) {
150 jack_log("Succeeded in unlocking all memory");
151 } else {
152 jack_error("Cannot unlock all memory (%s)", strerror(errno));
157 } // end of namespace