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"
27 static unsigned int fSegmentNum
= 0;
28 static jack_shm_info_t gInfo
;
30 JackShmMem::JackShmMem()
32 JackShmMemAble::Init();
36 JackShmMem::~JackShmMem()
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
);
54 void* JackShmMem::operator new(size_t size
)
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
));
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
);
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
;
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
);
84 jack_error("JackShmMem::new bad alloc", size
);
85 throw std::bad_alloc();
88 void JackShmMem::operator delete(void* p
, size_t size
)
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
)
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
);
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
);
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
);
132 jack_error("Cannot unlock down %u byte memory area (%s)", size
, strerror(errno
));
138 if (CHECK_MLOCKALL()) {
139 jack_log("Succeeded in locking all memory");
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");
150 jack_error("Cannot unlock all memory (%s)", strerror(errno
));
155 } // end of namespace