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"
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
.attached_at
= gInfo
.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
);
50 void* JackShmMem::operator new(size_t size
)
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
));
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
);
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
;
74 gInfo
.attached_at
= info
.attached_at
;
76 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info
.index
, info
.attached_at
, size
);
80 jack_error("JackShmMem::new bad alloc", size
);
81 throw std::bad_alloc();
84 void JackShmMem::operator delete(void* p
, size_t size
)
87 JackShmMem
* obj
= (JackShmMem
*)p
;
88 info
.index
= obj
->fInfo
.index
;
89 info
.attached_at
= obj
->fInfo
.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
)
100 JackShmMem::operator delete(obj
, 0);
103 void LockMemoryImp(void* ptr
, size_t size
)
105 if (CHECK_MLOCK(ptr
, size
)) {
106 jack_log("Succeeded in locking %u byte memory area", size
);
108 jack_error("Cannot lock down memory area (%s)", strerror(errno
));
112 void UnlockMemoryImp(void* ptr
, size_t size
)
114 if (CHECK_MUNLOCK(ptr
, size
)) {
115 jack_log("Succeeded in unlocking %u byte memory area", size
);
117 jack_error("Cannot unlock down memory area (%s)", strerror(errno
));
123 if (CHECK_MLOCKALL()) {
124 jack_log("Succeeded in locking all memory");
126 jack_error("Cannot lock all memory (%s)", strerror(errno
));
130 void UnlockAllMemory()
132 if (CHECK_MUNLOCKALL()) {
133 jack_log("Succeeded in unlocking all memory");
135 jack_error("Cannot unlock all memory (%s)", strerror(errno
));
140 } // end of namespace