Move to kernel style SPDX license identifiers
[lttng-ust.git] / libcounter / shm.h
CommitLineData
ebabbf58 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
ebabbf58
MD
3 *
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
ebabbf58
MD
5 */
6
c0c0989a
MJ
7#ifndef _LIBCOUNTER_SHM_H
8#define _LIBCOUNTER_SHM_H
9
ebabbf58
MD
10#include <stddef.h>
11#include <stdint.h>
12#include <unistd.h>
13#include <usterr-signal-safe.h>
14#include <urcu/compiler.h>
15#include "shm_types.h"
3b098ac1 16#include "helper.h"
ebabbf58
MD
17
18/* lttng_counter_handle_create - for UST. */
19extern
20struct lttng_counter_shm_handle *lttng_counter_handle_create(void *data,
21 uint64_t memory_map_size, int wakeup_fd);
22/* lttng_counter_handle_add_cpu - for UST. */
23extern
24int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle *handle,
25 int shm_fd, uint32_t cpu_nr,
26 uint64_t memory_map_size);
3b098ac1 27LTTNG_HIDDEN
ebabbf58
MD
28unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle *handle);
29
30/*
31 * Pointer dereferencing. We don't trust the shm_ref, so we validate
32 * both the index and offset with known boundaries.
33 *
34 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
35 * target type, even in the occurrence of shm_ref modification by an
36 * untrusted process having write access to the shm_ref. We return a
37 * NULL pointer if the ranges are invalid.
38 */
39static inline
40char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table *table,
41 struct lttng_counter_shm_ref *ref,
42 size_t idx, size_t elem_size)
43{
44 struct lttng_counter_shm_object *obj;
45 size_t objindex, ref_offset;
46
47 objindex = (size_t) ref->index;
48 if (caa_unlikely(objindex >= table->allocated_len))
49 return NULL;
50 obj = &table->objects[objindex];
51 ref_offset = (size_t) ref->offset;
52 ref_offset += idx * elem_size;
53 /* Check if part of the element returned would exceed the limits. */
54 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
55 return NULL;
56 return &obj->memory_map[ref_offset];
57}
58
59#define lttng_counter_shmp_index(handle, ref, index) \
60 ({ \
61 __typeof__((ref)._type) ____ptr_ret; \
62 ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
63 ____ptr_ret; \
64 })
65
66#define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0)
67
68static inline
69void _lttng_counter_set_shmp(struct lttng_counter_shm_ref *ref, struct lttng_counter_shm_ref src)
70{
71 *ref = src;
72}
73
74#define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src)
75
3b098ac1 76LTTNG_HIDDEN
ebabbf58 77struct lttng_counter_shm_object_table *lttng_counter_shm_object_table_create(size_t max_nb_obj);
3b098ac1 78LTTNG_HIDDEN
ebabbf58
MD
79struct lttng_counter_shm_object *lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table *table,
80 size_t memory_map_size,
81 enum lttng_counter_shm_object_type type,
82 const int cpu_fd,
83 int cpu);
3b098ac1 84LTTNG_HIDDEN
ebabbf58
MD
85struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table *table,
86 int shm_fd, size_t memory_map_size);
87/* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */
3b098ac1 88LTTNG_HIDDEN
ebabbf58
MD
89struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table *table,
90 void *mem, size_t memory_map_size);
3b098ac1 91LTTNG_HIDDEN
ebabbf58
MD
92void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table *table, int consumer);
93
94/*
95 * lttng_counter_zalloc_shm - allocate memory within a shm object.
96 *
97 * Shared memory is already zeroed by shmget.
98 * *NOT* multithread-safe (should be protected by mutex).
99 * Returns a -1, -1 tuple on error.
100 */
3b098ac1 101LTTNG_HIDDEN
ebabbf58 102struct lttng_counter_shm_ref lttng_counter_zalloc_shm(struct lttng_counter_shm_object *obj, size_t len);
3b098ac1 103LTTNG_HIDDEN
ebabbf58
MD
104void lttng_counter_align_shm(struct lttng_counter_shm_object *obj, size_t align);
105
106static inline
107int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref)
108{
109 struct lttng_counter_shm_object_table *table = handle->table;
110 struct lttng_counter_shm_object *obj;
111 size_t index;
112
113 index = (size_t) ref->index;
114 if (caa_unlikely(index >= table->allocated_len))
115 return -EPERM;
116 obj = &table->objects[index];
117 return obj->shm_fd;
118}
119
120
121static inline
122int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref,
123 uint64_t *size)
124{
125 struct lttng_counter_shm_object_table *table = handle->table;
126 struct lttng_counter_shm_object *obj;
127 size_t index;
128
129 index = (size_t) ref->index;
130 if (caa_unlikely(index >= table->allocated_len))
131 return -EPERM;
132 obj = &table->objects[index];
133 *size = obj->memory_map_size;
134 return 0;
135}
136
137#endif /* _LIBCOUNTER_SHM_H */
This page took 0.027897 seconds and 4 git commands to generate.