1 // SPDX-FileCopyrightText: 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
5 #ifndef _LTTNG_UST_UTILS_H
6 #define _LTTNG_UST_UTILS_H
10 #include <urcu/compiler.h>
12 /* For lttng_ust_is_integer_type */
13 #if defined (__cplusplus)
14 #include <type_traits>
19 * lttng_ust_stringify - convert a literal value to a C string
21 #define __lttng_ust_stringify1(x) #x
22 #define lttng_ust_stringify(x) __lttng_ust_stringify1(x)
25 * lttng_ust_is_signed_type - check if type is signed
27 * Returns true if the type of @type is signed.
29 #if defined(__cplusplus)
30 #define lttng_ust_is_signed_type(type) (std::is_signed<type>::value)
32 #define lttng_ust_is_signed_type(type) ((type) -1 < (type) 1)
37 * lttng_ust_is_integer_type - check if type is an integer
39 * Returns true if the type of @type is an integer.
41 #if defined(__cplusplus)
42 #define lttng_ust_is_integer_type(type) (std::is_integral<type>::value)
44 #define lttng_ust_is_integer_type(type) \
45 (__builtin_types_compatible_p(type, _Bool) || \
46 __builtin_types_compatible_p(type, char) || \
47 __builtin_types_compatible_p(type, signed char) || \
48 __builtin_types_compatible_p(type, unsigned char) || \
49 __builtin_types_compatible_p(type, short) || \
50 __builtin_types_compatible_p(type, unsigned short) || \
51 __builtin_types_compatible_p(type, int) || \
52 __builtin_types_compatible_p(type, unsigned int) || \
53 __builtin_types_compatible_p(type, long) || \
54 __builtin_types_compatible_p(type, unsigned long) || \
55 __builtin_types_compatible_p(type, long long) || \
56 __builtin_types_compatible_p(type, unsigned long long))
60 * lttng_ust_is_pointer_type - check if type is a pointer
62 * Returns true if the type of @type is a pointer.
64 * Note: The C implementation of lttng_ust_is_pointer_type uses pointer
65 * arithmetic, which does not work on opaque pointer types.
67 #if defined(__cplusplus)
68 #define lttng_ust_is_pointer_type(type) (std::is_pointer<type>::value)
70 /* The difference between two pointers is an integer. */
71 #define lttng_ust_is_pointer_type(type) \
72 (lttng_ust_is_integer_type(typeof(((type)1 - (type)1))) && !lttng_ust_is_integer_type(type))
77 * lttng_ust_field_array_element_type_is_supported -
79 * Adds a compilation assertion that array and sequence fields declared by the
80 * user are of an integral or pointer type.
82 #define lttng_ust_field_array_element_type_is_supported(type, item) \
83 lttng_ust_static_assert(lttng_ust_is_integer_type(type) || lttng_ust_is_pointer_type(type), \
84 "Non-integer, non-pointer type `" #item "` not supported as element of LTTNG_UST_FIELD_ARRAY or LTTNG_UST_FIELD_SEQUENCE", \
85 Non_integer_type__##item##__not_supported_as_element_of_LTTNG_UST_FIELD_ARRAY_or_LTTNG_UST_FIELD_SEQUENCE)
89 * lttng_ust_runtime_bug_on - check condition at runtime
90 * @condition: the condition which should be false.
92 * If the condition is true, a BUG will be triggered at runtime.
94 #define lttng_ust_runtime_bug_on(condition) \
96 if (caa_unlikely(condition)) { \
98 "LTTng BUG in file %s, line %d.\n", \
99 __FILE__, __LINE__); \
100 exit(EXIT_FAILURE); \
106 * lttng_ust_build_bug_on - check condition at build
107 * @condition: the condition which should be false.
109 * If the condition is true, the compiler will generate a build error.
111 #define lttng_ust_build_bug_on(condition) \
112 ((void) sizeof(char[-!!(condition)]))
116 * lttng_ust_build_runtime_bug_on - check condition at build (if constant) or runtime
117 * @condition: the condition which should be false.
119 * If the condition is a constant and true, the compiler will generate a build
120 * error. If the condition is not constant, a BUG will be triggered at runtime
121 * if the condition is ever true. If the condition is constant and false, no
124 #define lttng_ust_build_runtime_bug_on(condition) \
126 if (__builtin_constant_p(condition)) \
127 lttng_ust_build_bug_on(condition); \
129 lttng_ust_runtime_bug_on(condition); \
134 * lttng_ust_offset_align - Calculate the offset needed to align an object on
135 * its natural alignment towards higher addresses.
136 * @align_drift: object offset from an "alignment"-aligned address.
137 * @alignment: natural object alignment. Must be non-zero, power of 2.
139 * Returns the offset that must be added to align towards higher
142 #define lttng_ust_offset_align(align_drift, alignment) \
144 lttng_ust_build_runtime_bug_on((alignment) == 0 \
145 || ((alignment) & ((alignment) - 1))); \
146 (((alignment) - (align_drift)) & ((alignment) - 1)); \
151 * lttng_ust_offset_align_floor - Calculate the offset needed to align an
152 * object on its natural alignment towards lower addresses.
153 * @align_drift: object offset from an "alignment"-aligned address.
154 * @alignment: natural object alignment. Must be non-zero, power of 2.
156 * Returns the offset that must be subtracted to align towards lower addresses.
158 #define lttng_ust_offset_align_floor(align_drift, alignment) \
160 lttng_ust_build_runtime_bug_on((alignment) == 0 \
161 || ((alignment) & ((alignment) - 1))); \
162 (((align_drift) - (alignment)) & ((alignment) - 1)); \
165 #endif /* _LTTNG_UST_UTILS_H */
This page took 0.032535 seconds and 4 git commands to generate.