Replace explicit rcu_read_lock/unlock with lttng::urcu::read_lock_guard
[lttng-tools.git] / src / common / macros.hpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #ifndef _MACROS_H
10 #define _MACROS_H
11
12 #include <common/compat/string.hpp>
13
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <memory>
19 #include <pthread.h>
20 #include <type_traits>
21
22 /*
23 * Takes a pointer x and transform it so we can use it to access members
24 * without a function call. Here an example:
25 *
26 * #define GET_SIZE(x) LTTNG_REF(x)->size
27 *
28 * struct { int size; } s;
29 *
30 * printf("size : %d\n", GET_SIZE(&s));
31 *
32 * For this example we can't use something like this for compatibility purpose
33 * since this will fail:
34 *
35 * #define GET_SIZE(x) x->size;
36 *
37 * This is mostly use for the compatibility layer of lttng-tools. See
38 * poll/epoll for a good example. Since x can be on the stack or allocated
39 * memory using malloc(), we must use generic accessors for compat in order to
40 * *not* use a function to access members and not the variable name.
41 */
42 #define LTTNG_REF(x) ((typeof(*(x)) *) (x))
43
44 #ifdef NDEBUG
45 /*
46 * Force usage of the assertion condition to prevent unused variable warnings
47 * when `assert()` are disabled by the `NDEBUG` definition.
48 */
49 # define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
50 #else
51 # include <assert.h>
52 # define LTTNG_ASSERT(_cond) assert(_cond)
53 #endif
54
55 /*
56 * Memory allocation zeroed
57 */
58
59 static inline
60 void *zmalloc_internal(size_t size)
61 {
62 return calloc(1, size);
63 }
64
65 template <typename MallocableType>
66 struct can_malloc {
67 /*
68 * gcc versions before 5.0 lack some type traits defined in C++11.
69 * Since in this instance we use the trait to prevent misuses
70 * of malloc (and statically assert) and not to generate different
71 * code based on this property, simply set value to true and allow
72 * the code to compile. Anyone using a contemporary compiler will
73 * catch the error.
74 */
75 #if __GNUG__ && __GNUC__ < 5
76 static constexpr bool value = true;
77 #else
78 static constexpr bool value = std::is_trivially_constructible<MallocableType>::value;
79 #endif
80 };
81
82 /*
83 * Malloc and zero-initialize an object of type T, asserting that MallocableType can be
84 * safely malloc-ed (is trivially constructible).
85 */
86 template <typename MallocableType>
87 MallocableType *zmalloc()
88 {
89 static_assert(can_malloc<MallocableType>::value, "type can be malloc'ed");
90 return (MallocableType *) zmalloc_internal(sizeof(MallocableType)); /* NOLINT sizeof
91 potentially used on a
92 pointer. */
93 }
94
95 /*
96 * Malloc and zero-initialize a buffer of size `size`, asserting that type AllocatedType
97 * can be safely malloc-ed (is trivially constructible).
98 */
99 template <typename AllocatedType>
100 AllocatedType *zmalloc(size_t size)
101 {
102 static_assert(can_malloc<AllocatedType>::value, "type can be malloc'ed");
103 LTTNG_ASSERT(size >= sizeof(AllocatedType));
104 return (AllocatedType *) zmalloc_internal(size);
105 }
106
107 /*
108 * Malloc and zero-initialize an array of `nmemb` elements of type AllocatedType,
109 * asserting that AllocatedType can be safely malloc-ed (is trivially constructible).
110 */
111 template <typename AllocatedType>
112 AllocatedType *calloc(size_t nmemb)
113 {
114 static_assert(can_malloc<AllocatedType>::value, "type can be malloc'ed");
115 return (AllocatedType *) zmalloc_internal(nmemb * sizeof(AllocatedType)); /* NOLINT sizeof
116 potentially
117 used on a
118 pointer. */
119 }
120
121 /*
122 * Malloc an object of type AllocatedType, asserting that AllocatedType can be safely malloc-ed (is
123 * trivially constructible).
124 */
125 template <typename AllocatedType>
126 AllocatedType *malloc()
127 {
128 static_assert(can_malloc<AllocatedType>::value, "type can be malloc'ed");
129 return (AllocatedType *) malloc(sizeof(AllocatedType));
130 }
131
132 /*
133 * Malloc a buffer of size `size`, asserting that AllocatedType can be safely
134 * malloc-ed (is trivially constructible).
135 */
136 template<typename AllocatedType>
137 AllocatedType *malloc(size_t size)
138 {
139 static_assert (can_malloc<AllocatedType>::value, "type can be malloc'ed");
140 return (AllocatedType *) malloc(size);
141 }
142
143 /*
144 * Prevent using `free` on types that are non-POD.
145 *
146 * Declare a delete prototype of free if the parameter type is not safe to free
147 * (non-POD).
148 *
149 * If the parameter is a pointer to void, we can't check if what is pointed
150 * to is safe to free or not, as we don't know what is pointed to. Ideally,
151 * all calls to free would be with a typed pointer, but there are too many
152 * instances of passing a pointer to void to enforce that right now. So allow
153 * pointers to void, these will not be checked.
154 */
155
156 template<typename FreedType>
157 struct can_free
158 {
159 /*
160 * gcc versions before 5.0 lack some type traits defined in C++11.
161 * Since in this instance we use the trait to prevent misuses
162 * of free (and statically assert) and not to generate different
163 * code based on this property, simply set value to true and allow
164 * the code to compile. Anyone using a contemporary compiler will
165 * catch the error.
166 */
167 #if __GNUG__ && __GNUC__ < 5
168 static constexpr bool value = true;
169 #else
170 static constexpr bool value = std::is_trivially_destructible<FreedType>::value ||
171 std::is_void<FreedType>::value;
172 #endif
173 };
174
175 template <typename FreedType, typename = typename std::enable_if<!can_free<FreedType>::value>::type>
176 void free(FreedType *p) = delete;
177
178 template <typename InitializedType>
179 struct can_memset {
180 static constexpr bool value = std::is_pod<InitializedType>::value ||
181 std::is_void<InitializedType>::value;
182 };
183
184 template <typename InitializedType,
185 typename = typename std::enable_if<!can_memset<InitializedType>::value>::type>
186 void *memset(InitializedType *s, int c, size_t n) = delete;
187
188 template<typename T>
189 struct can_memcpy
190 {
191 /*
192 * gcc versions before 5.0 lack some type traits defined in C++11.
193 * Since in this instance we use the trait to prevent misuses
194 * of memcpy (and statically assert) and not to generate different
195 * code based on this property, simply set value to true and allow
196 * the code to compile. Anyone using a contemporary compiler will
197 * catch the error.
198 */
199 #if __GNUG__ && __GNUC__ < 5
200 static constexpr bool value = true;
201 #else
202 static constexpr bool value = std::is_trivially_copyable<T>::value;
203 #endif
204 };
205
206 template <typename DestinationType,
207 typename SourceType,
208 typename = typename std::enable_if<!can_memcpy<DestinationType>::value>::type,
209 typename = typename std::enable_if<!can_memcpy<SourceType>::value>::type>
210 void *memcpy(DestinationType *d, const SourceType *s, size_t n) = delete;
211
212 template <typename MovedType>
213 struct can_memmove {
214 /*
215 * gcc versions before 5.0 lack some type traits defined in C++11.
216 * Since in this instance we use the trait to prevent misuses
217 * of memmove (and statically assert) and not to generate different
218 * code based on this property, simply set value to true and allow
219 * the code to compile. Anyone using a contemporary compiler will
220 * catch the error.
221 */
222 #if __GNUG__ && __GNUC__ < 5
223 static constexpr bool value = true;
224 #else
225 static constexpr bool value = std::is_trivially_copyable<MovedType>::value;
226 #endif
227 };
228
229 template <typename DestinationType,
230 typename SourceType,
231 typename = typename std::enable_if<!can_memmove<DestinationType>::value>::type,
232 typename = typename std::enable_if<!can_memmove<SourceType>::value>::type>
233 void *memmove(DestinationType *d, const SourceType *s, size_t n) = delete;
234
235 #ifndef ARRAY_SIZE
236 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
237 #endif
238
239 #ifndef LTTNG_PACKED
240 #define LTTNG_PACKED __attribute__((__packed__))
241 #endif
242
243 #ifndef LTTNG_NO_SANITIZE_ADDRESS
244 #if defined(__clang__) || defined (__GNUC__)
245 #define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
246 #else
247 #define LTTNG_NO_SANITIZE_ADDRESS
248 #endif
249 #endif
250
251 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
252
253 #define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&(lock)))
254 #define ASSERT_RCU_READ_LOCKED() LTTNG_ASSERT(rcu_read_ongoing())
255 #define ASSERT_RCU_READ_UNLOCKED() LTTNG_ASSERT(!rcu_read_ongoing())
256
257 /* Attribute suitable to tag functions as having printf()-like arguments. */
258 #define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
259 __attribute__((format(printf, _string_index, _first_to_check)))
260
261 /* Attribute suitable to tag functions as having strftime()-like arguments. */
262 #define ATTR_FORMAT_STRFTIME(_string_index) \
263 __attribute__((format(strftime, _string_index, 0)))
264
265 /* Macros used to ignore specific compiler diagnostics. */
266
267 #define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
268 #define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
269
270 #if defined(__clang__)
271 /* Clang */
272 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
273 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
274 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
275 # define DIAGNOSTIC_IGNORE_LOGICAL_OP
276 # define DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
277 # define DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
278 _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
279 #else
280 /* GCC */
281 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
282 _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
283 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
284 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
285 # define DIAGNOSTIC_IGNORE_LOGICAL_OP \
286 _Pragma("GCC diagnostic ignored \"-Wlogical-op\"")
287 #if __GNUG__ && __GNUC__ >= 7
288 # define DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES \
289 _Pragma("GCC diagnostic ignored \"-Wduplicated-branches\"")
290 #else
291 # define DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
292 #endif /* __GNUG__ && __GNUC__ >= 7 */
293 # define DIAGNOSTIC_IGNORE_INVALID_OFFSETOF \
294 _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
295 #endif
296
297 /* Used to make specific C++ functions to C code. */
298 #ifdef __cplusplus
299 #define C_LINKAGE extern "C"
300 #else
301 #define C_LINKAGE
302 #endif
303
304 /*
305 * lttng_strncpy returns 0 on success, or nonzero on failure.
306 * It checks that the @src string fits into @dst_len before performing
307 * the copy. On failure, no copy has been performed.
308 *
309 * Assumes that 'src' is null-terminated.
310 *
311 * dst_len includes the string's trailing NULL.
312 */
313 static inline
314 int lttng_strncpy(char *dst, const char *src, size_t dst_len)
315 {
316 if (strlen(src) >= dst_len) {
317 /* Fail since copying would result in truncation. */
318 return -1;
319 }
320 strcpy(dst, src);
321 return 0;
322 }
323
324 namespace lttng {
325 namespace utils {
326 template <class ParentType, class MemberType>
327 ParentType *container_of(const MemberType *member, const MemberType ParentType::*ptr_to_member)
328 {
329 const ParentType *dummy_parent = nullptr;
330 auto *offset_of_member = reinterpret_cast<const char *>(&(dummy_parent->*ptr_to_member));
331 auto address_of_parent = reinterpret_cast<const char *>(member) - offset_of_member;
332
333 return reinterpret_cast<ParentType *>(address_of_parent);
334 }
335 } /* namespace utils */
336 } /* namespace lttng */
337
338 #endif /* _MACROS_H */
This page took 0.037102 seconds and 5 git commands to generate.