Commit | Line | Data |
---|---|---|
a6352fd4 MD |
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> | |
1dbfff0c | 13 | #include <ust/usterr-signal-safe.h> |
a6352fd4 | 14 | #include "ust/core.h" |
1d498196 | 15 | #include "shm_types.h" |
a6352fd4 | 16 | |
a6352fd4 | 17 | /* |
1d498196 MD |
18 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
19 | * both the index and offset with known boundaries. | |
a6352fd4 | 20 | */ |
a6352fd4 | 21 | static inline |
4746ae29 MD |
22 | char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref, |
23 | size_t offset) | |
a6352fd4 | 24 | { |
1d498196 | 25 | struct shm_object *obj; |
4746ae29 | 26 | size_t index, ref_offset; |
a6352fd4 | 27 | |
1d498196 MD |
28 | index = (size_t) ref->index; |
29 | if (unlikely(index >= table->allocated_len)) | |
30 | return NULL; | |
31 | obj = &table->objects[index]; | |
4746ae29 MD |
32 | ref_offset = (size_t) ref->offset; |
33 | ref_offset += offset; | |
34 | if (unlikely(ref_offset >= obj->memory_map_size)) | |
a6352fd4 | 35 | return NULL; |
4746ae29 | 36 | return &obj->memory_map[ref_offset]; |
a6352fd4 MD |
37 | } |
38 | ||
4746ae29 | 39 | #define shmp_index(handle, ref, offset) \ |
1d498196 MD |
40 | ({ \ |
41 | __typeof__((ref)._type) ____ptr_ret; \ | |
4746ae29 | 42 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, ((offset) * sizeof(*____ptr_ret))); \ |
1d498196 MD |
43 | ____ptr_ret; \ |
44 | }) | |
45 | ||
4746ae29 MD |
46 | #define shmp(handle, ref) shmp_index(handle, ref, 0) |
47 | ||
431d5cf0 | 48 | static inline |
1d498196 | 49 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 50 | { |
1d498196 | 51 | *ref = src; |
431d5cf0 MD |
52 | } |
53 | ||
1d498196 MD |
54 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
55 | ||
56 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); | |
57 | void shm_object_table_destroy(struct shm_object_table *table); | |
58 | struct shm_object *shm_object_table_append(struct shm_object_table *table, | |
59 | size_t memory_map_size); | |
60 | ||
61 | /* | |
62 | * zalloc_shm - allocate memory within a shm object. | |
63 | * | |
64 | * Shared memory is already zeroed by shmget. | |
65 | * *NOT* multithread-safe (should be protected by mutex). | |
66 | * Returns a -1, -1 tuple on error. | |
67 | */ | |
68 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); | |
69 | void align_shm(struct shm_object *obj, size_t align); | |
70 | ||
5d61a504 MD |
71 | static inline |
72 | int shm_get_wakeup_fd(struct shm_handle *handle, struct shm_ref *ref) | |
73 | { | |
74 | struct shm_object_table *table = handle->table; | |
75 | struct shm_object *obj; | |
76 | size_t index; | |
77 | ||
78 | index = (size_t) ref->index; | |
79 | if (unlikely(index >= table->allocated_len)) | |
80 | return -EPERM; | |
81 | obj = &table->objects[index]; | |
82 | return obj->wait_fd[1]; | |
83 | ||
84 | } | |
85 | ||
86 | static inline | |
87 | int shm_get_wait_fd(struct shm_handle *handle, struct shm_ref *ref) | |
88 | { | |
89 | struct shm_object_table *table = handle->table; | |
90 | struct shm_object *obj; | |
91 | size_t index; | |
92 | ||
93 | index = (size_t) ref->index; | |
94 | if (unlikely(index >= table->allocated_len)) | |
95 | return -EPERM; | |
96 | obj = &table->objects[index]; | |
97 | return obj->wait_fd[0]; | |
98 | } | |
99 | ||
a6352fd4 | 100 | #endif /* _LIBRINGBUFFER_SHM_H */ |