Cleanup thread code.
[jack2.git] / common / JackShmMem.cpp
blob5d68b86edd9fe288894d71c4fc411d5aeac008c3
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 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();
37 void JackShmMemAble::Init()
39 fInfo.index = gInfo.index;
40 fInfo.ptr.attached_at = gInfo.ptr.attached_at;
41 fInfo.size = gInfo.size;
44 void* JackShmMem::operator new(size_t size, void* memory)
46 jack_log("JackShmMem::new placement size = %ld", size);
47 return memory;
50 void* JackShmMem::operator new(size_t size)
52 jack_shm_info_t info;
53 JackShmMem* obj;
54 char name[64];
56 snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
58 if (jack_shmalloc(name, size, &info)) {
59 jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
60 goto error;
63 if (jack_attach_shm(&info)) {
64 jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
65 jack_destroy_shm(&info);
66 goto error;
69 obj = (JackShmMem*)jack_shm_addr(&info);
70 // It is unsafe to set object fields directly (may be overwritten during object initialization),
71 // so use an intermediate global data
72 gInfo.index = info.index;
73 gInfo.size = size;
74 gInfo.ptr.attached_at = info.ptr.attached_at;
76 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
77 return obj;
79 error:
80 jack_error("JackShmMem::new bad alloc", size);
81 throw std::bad_alloc();
84 void JackShmMem::operator delete(void* p, size_t size)
86 jack_shm_info_t info;
87 JackShmMem* obj = (JackShmMem*)p;
88 info.index = obj->fInfo.index;
89 info.ptr.attached_at = obj->fInfo.ptr.attached_at;
91 jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
93 jack_release_shm(&info);
94 jack_destroy_shm(&info);
97 void JackShmMem::operator delete(void* obj)
99 if (obj)
100 JackShmMem::operator delete(obj, 0);
103 void LockMemoryImp(void* ptr, size_t size)
105 if (CHECK_MLOCK((char*)ptr, size)) {
106 jack_log("Succeeded in locking %u byte memory area", size);
107 } else {
108 jack_error("Cannot lock down memory area (%s)", strerror(errno));
112 void InitLockMemoryImp(void* ptr, size_t size)
114 if (CHECK_MLOCK((char*)ptr, size)) {
115 memset(ptr, 0, size);
116 jack_log("Succeeded in locking %u byte memory area", size);
117 } else {
118 jack_error("Cannot lock down memory area (%s)", strerror(errno));
122 void UnlockMemoryImp(void* ptr, size_t size)
124 if (CHECK_MUNLOCK((char*)ptr, size)) {
125 jack_log("Succeeded in unlocking %u byte memory area", size);
126 } else {
127 jack_error("Cannot unlock down memory area (%s)", strerror(errno));
131 void LockAllMemory()
133 if (CHECK_MLOCKALL()) {
134 jack_log("Succeeded in locking all memory");
135 } else {
136 jack_error("Cannot lock all memory (%s)", strerror(errno));
140 void UnlockAllMemory()
142 if (CHECK_MUNLOCKALL()) {
143 jack_log("Succeeded in unlocking all memory");
144 } else {
145 jack_error("Cannot unlock all memory (%s)", strerror(errno));
150 } // end of namespace