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 |
1d498196 | 22 | char *_shmp(struct shm_object_table *table, struct shm_ref *ref) |
a6352fd4 | 23 | { |
1d498196 MD |
24 | struct shm_object *obj; |
25 | size_t index, offset; | |
a6352fd4 | 26 | |
1d498196 MD |
27 | index = (size_t) ref->index; |
28 | if (unlikely(index >= table->allocated_len)) | |
29 | return NULL; | |
30 | obj = &table->objects[index]; | |
31 | offset = (size_t) ref->offset; | |
32 | if (unlikely(offset >= obj->memory_map_size)) | |
a6352fd4 | 33 | return NULL; |
1d498196 | 34 | return &obj->memory_map[offset]; |
a6352fd4 MD |
35 | } |
36 | ||
1d498196 MD |
37 | #define shmp(handle, ref) \ |
38 | ({ \ | |
39 | __typeof__((ref)._type) ____ptr_ret; \ | |
40 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp((handle)->table, &(ref)._ref); \ | |
41 | ____ptr_ret; \ | |
42 | }) | |
43 | ||
431d5cf0 | 44 | static inline |
1d498196 | 45 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 46 | { |
1d498196 | 47 | *ref = src; |
431d5cf0 MD |
48 | } |
49 | ||
1d498196 MD |
50 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
51 | ||
52 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); | |
53 | void shm_object_table_destroy(struct shm_object_table *table); | |
54 | struct shm_object *shm_object_table_append(struct shm_object_table *table, | |
55 | size_t memory_map_size); | |
56 | ||
57 | /* | |
58 | * zalloc_shm - allocate memory within a shm object. | |
59 | * | |
60 | * Shared memory is already zeroed by shmget. | |
61 | * *NOT* multithread-safe (should be protected by mutex). | |
62 | * Returns a -1, -1 tuple on error. | |
63 | */ | |
64 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); | |
65 | void align_shm(struct shm_object *obj, size_t align); | |
66 | ||
5d61a504 MD |
67 | static inline |
68 | int shm_get_wakeup_fd(struct shm_handle *handle, struct shm_ref *ref) | |
69 | { | |
70 | struct shm_object_table *table = handle->table; | |
71 | struct shm_object *obj; | |
72 | size_t index; | |
73 | ||
74 | index = (size_t) ref->index; | |
75 | if (unlikely(index >= table->allocated_len)) | |
76 | return -EPERM; | |
77 | obj = &table->objects[index]; | |
78 | return obj->wait_fd[1]; | |
79 | ||
80 | } | |
81 | ||
82 | static inline | |
83 | int shm_get_wait_fd(struct shm_handle *handle, struct shm_ref *ref) | |
84 | { | |
85 | struct shm_object_table *table = handle->table; | |
86 | struct shm_object *obj; | |
87 | size_t index; | |
88 | ||
89 | index = (size_t) ref->index; | |
90 | if (unlikely(index >= table->allocated_len)) | |
91 | return -EPERM; | |
92 | obj = &table->objects[index]; | |
93 | return obj->wait_fd[0]; | |
94 | } | |
95 | ||
a6352fd4 | 96 | #endif /* _LIBRINGBUFFER_SHM_H */ |