Hide new 'lttng_counter_*' private symbols
[lttng-ust.git] / libcounter / shm.h
CommitLineData
ebabbf58
MD
1#ifndef _LIBCOUNTER_SHM_H
2#define _LIBCOUNTER_SHM_H
3
4/*
5 * libcounter/shm.h
6 *
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <stddef.h>
25#include <stdint.h>
26#include <unistd.h>
27#include <usterr-signal-safe.h>
28#include <urcu/compiler.h>
29#include "shm_types.h"
3b098ac1 30#include "helper.h"
ebabbf58
MD
31
32/* lttng_counter_handle_create - for UST. */
33extern
34struct lttng_counter_shm_handle *lttng_counter_handle_create(void *data,
35 uint64_t memory_map_size, int wakeup_fd);
36/* lttng_counter_handle_add_cpu - for UST. */
37extern
38int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle *handle,
39 int shm_fd, uint32_t cpu_nr,
40 uint64_t memory_map_size);
3b098ac1 41LTTNG_HIDDEN
ebabbf58
MD
42unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle *handle);
43
44/*
45 * Pointer dereferencing. We don't trust the shm_ref, so we validate
46 * both the index and offset with known boundaries.
47 *
48 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
49 * target type, even in the occurrence of shm_ref modification by an
50 * untrusted process having write access to the shm_ref. We return a
51 * NULL pointer if the ranges are invalid.
52 */
53static inline
54char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table *table,
55 struct lttng_counter_shm_ref *ref,
56 size_t idx, size_t elem_size)
57{
58 struct lttng_counter_shm_object *obj;
59 size_t objindex, ref_offset;
60
61 objindex = (size_t) ref->index;
62 if (caa_unlikely(objindex >= table->allocated_len))
63 return NULL;
64 obj = &table->objects[objindex];
65 ref_offset = (size_t) ref->offset;
66 ref_offset += idx * elem_size;
67 /* Check if part of the element returned would exceed the limits. */
68 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
69 return NULL;
70 return &obj->memory_map[ref_offset];
71}
72
73#define lttng_counter_shmp_index(handle, ref, index) \
74 ({ \
75 __typeof__((ref)._type) ____ptr_ret; \
76 ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
77 ____ptr_ret; \
78 })
79
80#define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0)
81
82static inline
83void _lttng_counter_set_shmp(struct lttng_counter_shm_ref *ref, struct lttng_counter_shm_ref src)
84{
85 *ref = src;
86}
87
88#define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src)
89
3b098ac1 90LTTNG_HIDDEN
ebabbf58 91struct lttng_counter_shm_object_table *lttng_counter_shm_object_table_create(size_t max_nb_obj);
3b098ac1 92LTTNG_HIDDEN
ebabbf58
MD
93struct lttng_counter_shm_object *lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table *table,
94 size_t memory_map_size,
95 enum lttng_counter_shm_object_type type,
96 const int cpu_fd,
97 int cpu);
3b098ac1 98LTTNG_HIDDEN
ebabbf58
MD
99struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table *table,
100 int shm_fd, size_t memory_map_size);
101/* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */
3b098ac1 102LTTNG_HIDDEN
ebabbf58
MD
103struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table *table,
104 void *mem, size_t memory_map_size);
3b098ac1 105LTTNG_HIDDEN
ebabbf58
MD
106void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table *table, int consumer);
107
108/*
109 * lttng_counter_zalloc_shm - allocate memory within a shm object.
110 *
111 * Shared memory is already zeroed by shmget.
112 * *NOT* multithread-safe (should be protected by mutex).
113 * Returns a -1, -1 tuple on error.
114 */
3b098ac1 115LTTNG_HIDDEN
ebabbf58 116struct lttng_counter_shm_ref lttng_counter_zalloc_shm(struct lttng_counter_shm_object *obj, size_t len);
3b098ac1 117LTTNG_HIDDEN
ebabbf58
MD
118void lttng_counter_align_shm(struct lttng_counter_shm_object *obj, size_t align);
119
120static inline
121int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref)
122{
123 struct lttng_counter_shm_object_table *table = handle->table;
124 struct lttng_counter_shm_object *obj;
125 size_t index;
126
127 index = (size_t) ref->index;
128 if (caa_unlikely(index >= table->allocated_len))
129 return -EPERM;
130 obj = &table->objects[index];
131 return obj->shm_fd;
132}
133
134
135static inline
136int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref,
137 uint64_t *size)
138{
139 struct lttng_counter_shm_object_table *table = handle->table;
140 struct lttng_counter_shm_object *obj;
141 size_t index;
142
143 index = (size_t) ref->index;
144 if (caa_unlikely(index >= table->allocated_len))
145 return -EPERM;
146 obj = &table->objects[index];
147 *size = obj->memory_map_size;
148 return 0;
149}
150
151#endif /* _LIBCOUNTER_SHM_H */
This page took 0.027693 seconds and 4 git commands to generate.