[0255a3]: common / mapped.cpp  Maximize  Restore  History

Download this file

471 lines (388 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (C) 2006-2007 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include <sipwitch/mapped.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#include <sys/types.h>
#endif
#ifdef HAVE_FTOK
#include <sys/ipc.h>
#include <sys/shm.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#if _POSIX_PRIORITY_SCHEDULING > 0
#include <sched.h>
#endif
#if defined(__APPLE__) && defined(__MACH__)
#define INSERT_OFFSET 16
#endif
#ifndef INSERT_OFFSET
#define INSERT_OFFSET 0
#endif
#if defined(__FreeBSD__)
#undef HAVE_SHM_OPEN
#endif
#if defined(HAVE_FTOK) && !defined(HAVE_SHM_OPEN)
static void ftok_name(const char *name, char *buf, size_t max)
{
assert(name != NULL && *name != 0);
assert(buf != NULL);
assert(max > 0);
struct stat ino;
if(*name == '/')
++name;
if(!stat("/var/run/ipc", &ino) && S_ISDIR(ino.st_mode))
snprintf(buf, max, "/var/run/ipc/%s", name);
else
snprintf(buf, max, "/tmp/.%s.ipc", name);
}
static key_t createipc(const char *name, char mode)
{
assert(name != NULL && *name != 0);
char buf[65];
int fd;
ftok_name(name, buf, sizeof(buf));
fd = open(buf, O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd > -1)
close(fd);
return ftok(buf, mode);
}
static key_t accessipc(const char *name, char mode)
{
assert(name != NULL && *name != 0);
char buf[65];
ftok_name(name, buf, sizeof(buf));
return ftok(buf, mode);
}
#endif
using namespace SIPWITCH_NAMESPACE;
using namespace UCOMMON_NAMESPACE;
MappedMemory::MappedMemory(const char *fn, size_t len)
{
assert(fn != NULL && *fn != 0);
assert(len > 0);
create(fn, size);
}
MappedMemory::MappedMemory(const char *fn)
{
assert(fn != NULL && *fn != 0);
create(fn, 0);
}
MappedMemory::MappedMemory()
{
size = 0;
used = 0;
map = NULL;
}
#if defined(_MSWINDOWS_)
void MappedMemory::create(const char *fn, size_t len)
{
assert(fn != NULL && *fn != 0);
int share = FILE_SHARE_READ;
int prot = FILE_MAP_READ;
int mode = GENERIC_READ;
struct stat ino;
size = 0;
used = 0;
map = NULL;
if(*fn == '/')
++fn;
if(len) {
prot = FILE_MAP_WRITE;
mode |= GENERIC_WRITE;
share |= FILE_SHARE_WRITE;
fd = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, len, fn);
}
else
fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fn);
if(fd == INVALID_HANDLE_VALUE || fd == NULL)
return;
map = (caddr_t)MapViewOfFile(fd, FILE_MAP_ALL_ACCESS, 0, 0, len);
if(map) {
size = len;
VirtualLock(map, size);
}
}
MappedMemory::~MappedMemory()
{
release();
}
void MappedMemory::remove(const char *id)
{
assert(id != NULL && *id != 0);
}
void MappedMemory::release(void)
{
if(map) {
VirtualUnlock(map, size);
UnmapViewOfFile(fd);
CloseHandle(fd);
map = NULL;
fd = INVALID_HANDLE_VALUE;
}
}
#elif defined(HAVE_SHM_OPEN)
void MappedMemory::create(const char *fn, size_t len)
{
assert(fn != NULL && *fn != 0);
int prot = PROT_READ;
struct stat ino;
char fbuf[80];
size = 0;
used = 0;
if(*fn != '/') {
snprintf(fbuf, sizeof(fbuf), "/%s", fn);
fn = fbuf;
}
if(len) {
len += INSERT_OFFSET;
prot |= PROT_WRITE;
fd = shm_open(fn, O_RDWR | O_CREAT, 0660);
if(fd > -1)
ftruncate(fd, len);
}
else {
fd = shm_open(fn, O_RDONLY, 0660);
if(fd > -1) {
fstat(fd, &ino);
len = ino.st_size;
}
}
if(fd < 0)
return;
map = (caddr_t)mmap(NULL, len, prot, MAP_SHARED, fd, 0);
close(fd);
if(map != (caddr_t)MAP_FAILED) {
size = mapsize = len;
mlock(map, mapsize);
#if INSERT_OFFSET > 0
if(prot & PROT_WRITE) {
size -= INSERT_OFFSET;
snprintf(map, INSERT_OFFSET, "%ld", size);
}
else
size = atol(map);
map += INSERT_OFFSET;
#endif
}
}
MappedMemory::~MappedMemory()
{
release();
}
void MappedMemory::release()
{
if(size) {
map -= INSERT_OFFSET;
munlock(map, mapsize);
munmap(map, mapsize);
size = 0;
}
}
void MappedMemory::remove(const char *fn)
{
assert(fn != NULL && *fn != 0);
struct stat ino;
char fbuf[80];
if(*fn != '/') {
snprintf(fbuf, sizeof(fbuf), "/%s", fn);
fn = fbuf;
}
shm_unlink(fn);
}
#else
void MappedMemory::remove(const char *name)
{
assert(name != NULL && *name != 0);
key_t key;
fd_t fd;
key = accessipc(name, 'S');
if(key) {
fd = shmget(key, 0, 0);
if(fd > -1)
shmctl(fd, IPC_RMID, NULL);
}
}
void MappedMemory::create(const char *name, size_t len)
{
assert(name != NULL && *name != 0);
struct shmid_ds stat;
size = 0;
used = 0;
key_t key;
if(len) {
key = createipc(name, 'S');
remake:
fd = shmget(key, len, IPC_CREAT | IPC_EXCL | 0660);
if(fd == -1 && errno == EEXIST) {
fd = shmget(key, 0, 0);
if(fd > -1) {
shmctl(fd, IPC_RMID, NULL);
goto remake;
}
}
}
else {
key = accessipc(name, 'S');
fd = shmget(key, 0, 0);
}
if(fd > -1) {
if(len)
size = len;
else if(shmctl(fd, IPC_STAT, &stat) == 0)
size = stat.shm_segsz;
else
fd = -1;
}
map = (caddr_t)shmat(fd, NULL, 0);
#ifdef SHM_LOCK
if(fd > -1)
shmctl(fd, SHM_LOCK, NULL);
#endif
}
MappedMemory::~MappedMemory()
{
release();
}
void MappedMemory::release(void)
{
if(size > 0) {
#ifdef SHM_UNLOCK
shmctl(fd, SHM_UNLOCK, NULL);
#endif
shmdt(map);
fd = -1;
size = 0;
}
}
#endif
void MappedMemory::fault(void)
{
abort();
}
void *MappedMemory::sbrk(size_t len)
{
assert(len > 0);
void *mp = (void *)(map + used);
if(used + len > size)
fault();
used += len;
return mp;
}
void *MappedMemory::offset(size_t offset)
{
if(offset >= size)
fault();
return (void *)(map + offset);
}
MappedReuse::MappedReuse(const char *name, size_t osize, unsigned count) :
ReusableAllocator(), MappedMemory(name, osize * count)
{
assert(name != NULL && *name != 0);
assert(osize > 0 && count > 0);
objsize = osize;
reading = 0;
}
MappedReuse::MappedReuse(size_t osize) :
ReusableAllocator(), MappedMemory()
{
assert(osize > 0);
objsize = osize;
reading = 0;
}
bool MappedReuse::avail(void)
{
bool rtn = false;
lock();
if(freelist || used < size)
rtn = true;
unlock();
return rtn;
}
ReusableObject *MappedReuse::request(void)
{
ReusableObject *obj = NULL;
lock();
if(freelist) {
obj = freelist;
freelist = next(obj);
}
else if(used + objsize <= size)
obj = (ReusableObject *)sbrk(objsize);
unlock();
return obj;
}
ReusableObject *MappedReuse::get(void)
{
return getTimed(Timer::inf);
}
void MappedReuse::removeLocked(ReusableObject *obj)
{
assert(obj != NULL);
LinkedObject **ru = (LinkedObject **)freelist;
obj->retain();
obj->enlist(ru);
}
ReusableObject *MappedReuse::getLocked(void)
{
ReusableObject *obj = NULL;
if(freelist) {
obj = freelist;
freelist = next(obj);
}
else if(used + objsize <= size)
obj = (ReusableObject *)sbrk(objsize);
return obj;
}
ReusableObject *MappedReuse::getTimed(timeout_t timeout)
{
bool rtn = true;
struct timespec ts;
ReusableObject *obj = NULL;
if(timeout && timeout != Timer::inf)
gettimeout(timeout, &ts);
lock();
while(rtn && (!freelist || freelist && reading) && used >= size) {
++waiting;
if(timeout == Timer::inf)
wait();
else if(timeout)
rtn = wait(&ts);
else
rtn = false;
--waiting;
}
if(!rtn) {
unlock();
return NULL;
}
if(freelist) {
obj = freelist;
freelist = next(obj);
}
else if(used + objsize <= size)
obj = (ReusableObject *)sbrk(objsize);
unlock();
return obj;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks