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
;
29 size_t JackMem::gSize
= 0;
31 JackShmMem::JackShmMem()
33 JackShmMemAble::Init();
37 JackShmMem::~JackShmMem()
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
);
55 void* JackShmMem::operator new(size_t size
)
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
));
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
);
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
;
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
);
85 jack_error("JackShmMem::new bad alloc", size
);
86 throw std::bad_alloc();
89 void JackShmMem::operator delete(void* p
, size_t size
)
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
)
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
);
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
);
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
);
133 jack_error("Cannot unlock down %u byte memory area (%s)", size
, strerror(errno
));
139 if (CHECK_MLOCKALL()) {
140 jack_log("Succeeded in locking all memory");
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");
151 jack_error("Cannot unlock all memory (%s)", strerror(errno
));
156 } // end of namespace