Ring buffer: use shmp (shared-memory pointers) for per-channel shm structures
[lttng-ust.git] / libringbuffer / shm.h
1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
3
4 /*
5 * libringbuffer/shm.h
6 *
7 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Dual LGPL v2.1/GPL v2 license.
10 */
11
12 #include <stdint.h>
13 #include "ust/core.h"
14
15 #define SHM_MAGIC 0x54335433
16 #define SHM_MAJOR 0
17 #define SHM_MINOR 1
18
19 /*
20 * Defining a max shm offset, for debugging purposes.
21 */
22 #if (CAA_BITS_PER_LONG == 32)
23 /* Define the maximum shared memory size to 128MB on 32-bit machines */
24 #define MAX_SHM_SIZE 134217728
25 #else
26 /* Define the maximum shared memory size to 8GB on 64-bit machines */
27 #define MAX_SHM_SIZE 8589934592
28 #endif
29
30 #define DECLARE_SHMP(type, name) type *****name
31
32 struct shm_header {
33 uint32_t magic;
34 uint8_t major;
35 uint8_t minor;
36 uint8_t bits_per_long;
37 size_t shm_size, shm_allocated;
38
39 DECLARE_SHMP(struct channel, chan);
40 };
41
42 #define shmp(shm_offset) \
43 ((__typeof__(****(shm_offset))) (((char *) &(shm_offset)) + (ptrdiff_t) (shm_offset)))
44
45 #define _shmp_abs(a) ((a < 0) ? -(a) : (a))
46
47 static inline
48 void _set_shmp(ptrdiff_t *shm_offset, void *ptr)
49 {
50 *shm_offset = (((char *) ptr) - ((char *) shm_offset));
51 assert(_shmp_abs(*shm_offset) < MAX_SHM_SIZE);
52 }
53
54 #define set_shmp(shm_offset, ptr) \
55 _set_shmp((ptrdiff_t *) ****(shm_offset), ptr)
56
57 /* Shared memory is already zeroed by shmget */
58 /* *NOT* multithread-safe (should be protected by mutex) */
59 static inline
60 void *zalloc_shm(struct shm_header *shm_header, size_t len)
61 {
62 void *ret;
63
64 if (shm_header->shm_size - shm_header->shm_allocated < len)
65 return NULL;
66 ret = (char *) shm_header + shm_header->shm_allocated;
67 shm_header->shm_allocated += len;
68 return ret;
69 }
70
71 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.031748 seconds and 5 git commands to generate.