Jack2: JackShmMem.cpp Source File

Jack2 1.9.12

JackShmMem.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackError.h"
21 #include "JackShmMem.h"
22 #include <stdio.h>
23
24 namespace Jack
25 {
26
27 static unsigned int fSegmentNum = 0;
28 static jack_shm_info_t gInfo;
29 size_t JackMem::gSize = 0;
30
31 JackShmMem::JackShmMem()
32 {
33 JackShmMemAble::Init();
34 LockMemory();
35 }
36
37 JackShmMem::~JackShmMem()
38 {
39 UnlockMemory();
40 }
41
42 void JackShmMemAble::Init()
43 {
44 fInfo.index = gInfo.index;
45 fInfo.ptr.attached_at = gInfo.ptr.attached_at;
46 fInfo.size = gInfo.size;
47 }
48
49 void* JackShmMem::operator new(size_t size, void* memory)
50 {
51 jack_log("JackShmMem::new placement size = %ld", size);
52 return memory;
53 }
54
55 void* JackShmMem::operator new(size_t size)
56 {
57 jack_shm_info_t info;
58 JackShmMem* obj;
59 char name[64];
60
61 snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
62
63 if (jack_shmalloc(name, size, &info)) {
64 jack_error("Cannot create shared memory segment of size = %d", size, strerror(errno));
65 goto error;
66 }
67
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);
71 goto error;
72 }
73
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;
78 gInfo.size = size;
79 gInfo.ptr.attached_at = info.ptr.attached_at;
80
81 jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
82 return obj;
83
84 error:
85 jack_error("JackShmMem::new bad alloc", size);
86 throw std::bad_alloc();
87 }
88
89 void JackShmMem::operator delete(void* p, size_t size)
90 {
91 jack_shm_info_t info;
92 JackShmMem* obj = (JackShmMem*)p;
93 info.index = obj->fInfo.index;
94 info.ptr.attached_at = obj->fInfo.ptr.attached_at;
95
96 jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
97
98 jack_release_shm(&info);
99 jack_destroy_shm(&info);
100 }
101
102 void JackShmMem::operator delete(void* obj)
103 {
104 if (obj) {
105 JackShmMem::operator delete(obj, 0);
106 }
107 }
108
109 void LockMemoryImp(void* ptr, size_t size)
110 {
111 if (CHECK_MLOCK((char*)ptr, size)) {
112 jack_log("Succeeded in locking %u byte memory area", size);
113 } else {
114 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
115 }
116 }
117
118 void InitLockMemoryImp(void* ptr, size_t size)
119 {
120 if (CHECK_MLOCK((char*)ptr, size)) {
121 memset(ptr, 0, size);
122 jack_log("Succeeded in locking %u byte memory area", size);
123 } else {
124 jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
125 }
126 }
127
128 void UnlockMemoryImp(void* ptr, size_t size)
129 {
130 if (CHECK_MUNLOCK((char*)ptr, size)) {
131 jack_log("Succeeded in unlocking %u byte memory area", size);
132 } else {
133 jack_error("Cannot unlock down %u byte memory area (%s)", size, strerror(errno));
134 }
135 }
136
137 void LockAllMemory()
138 {
139 if (CHECK_MLOCKALL()) {
140 jack_log("Succeeded in locking all memory");
141 } else {
142 jack_error("Cannot lock all memory (%s)", strerror(errno));
143 }
144 }
145
146 void UnlockAllMemory()
147 {
148 if (CHECK_MUNLOCKALL()) {
149 jack_log("Succeeded in unlocking all memory");
150 } else {
151 jack_error("Cannot unlock all memory (%s)", strerror(errno));
152 }
153 }
154
155
156 } // end of namespace
157
_jack_shm_info
Definition: shm.h:170
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108