4e9ced198755809cdf63f8140180eeecfb4ff5bd
1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
7 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Dual LGPL v2.1/GPL v2 license.
14 #include "usterr_signal_safe.h"
16 #define SHM_MAGIC 0x54335433
21 * Defining a max shm offset, for debugging purposes.
23 #if (CAA_BITS_PER_LONG == 32)
24 /* Define the maximum shared memory size to 128MB on 32-bit machines */
25 #define MAX_SHM_SIZE 134217728
27 /* Define the maximum shared memory size to 8GB on 64-bit machines */
28 #define MAX_SHM_SIZE 8589934592
31 #define DECLARE_SHMP(type, name) type *****name
37 uint8_t bits_per_long
;
38 size_t shm_size
, shm_allocated
;
40 DECLARE_SHMP(struct channel
, chan
);
44 struct shm_header
*header
; /* beginning of mapping */
45 int shmfd
; /* process-local file descriptor */
48 #define shmp(shm_offset) \
49 ((__typeof__(****(shm_offset))) (((char *) &(shm_offset)) + (ptrdiff_t) (shm_offset)))
51 #define _shmp_abs(a) ((a < 0) ? -(a) : (a))
54 void _set_shmp(ptrdiff_t *shm_offset
, void *ptr
)
56 *shm_offset
= (((char *) ptr
) - ((char *) shm_offset
));
57 assert(_shmp_abs(*shm_offset
) < MAX_SHM_SIZE
);
60 #define set_shmp(shm_offset, ptr) \
61 _set_shmp((ptrdiff_t *) ****(shm_offset), ptr)
63 /* Shared memory is already zeroed by shmget */
64 /* *NOT* multithread-safe (should be protected by mutex) */
66 void *zalloc_shm(struct shm_header
*shm_header
, size_t len
)
70 if (shm_header
->shm_size
- shm_header
->shm_allocated
< len
)
72 ret
= (char *) shm_header
+ shm_header
->shm_allocated
;
73 shm_header
->shm_allocated
+= len
;
78 void align_shm(struct shm_header
*shm_header
, size_t align
)
80 size_t offset_len
= offset_align(shm_header
->shm_allocated
, align
);
81 shm_header
->shm_allocated
+= offset_len
;
84 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.04334 seconds and 4 git commands to generate.