| 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/usterr-signal-safe.h> |
| 14 | #include "ust/core.h" |
| 15 | #include "shm_types.h" |
| 16 | |
| 17 | #define SHM_MAGIC 0x54335433 |
| 18 | #define SHM_MAJOR 0 |
| 19 | #define SHM_MINOR 1 |
| 20 | |
| 21 | /* |
| 22 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
| 23 | * both the index and offset with known boundaries. |
| 24 | */ |
| 25 | static inline |
| 26 | char *_shmp(struct shm_object_table *table, struct shm_ref *ref) |
| 27 | { |
| 28 | struct shm_object *obj; |
| 29 | size_t index, offset; |
| 30 | |
| 31 | index = (size_t) ref->index; |
| 32 | if (unlikely(index >= table->allocated_len)) |
| 33 | return NULL; |
| 34 | obj = &table->objects[index]; |
| 35 | offset = (size_t) ref->offset; |
| 36 | if (unlikely(offset >= obj->memory_map_size)) |
| 37 | return NULL; |
| 38 | return &obj->memory_map[offset]; |
| 39 | } |
| 40 | |
| 41 | #define shmp(handle, ref) \ |
| 42 | ({ \ |
| 43 | __typeof__((ref)._type) ____ptr_ret; \ |
| 44 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp((handle)->table, &(ref)._ref); \ |
| 45 | ____ptr_ret; \ |
| 46 | }) |
| 47 | |
| 48 | static inline |
| 49 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
| 50 | { |
| 51 | *ref = src; |
| 52 | } |
| 53 | |
| 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 | |
| 71 | #endif /* _LIBRINGBUFFER_SHM_H */ |