2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _LTTNG_UST_UTILS_H
8 #define _LTTNG_UST_UTILS_H
12 #include <urcu/compiler.h>
14 /* For lttng_ust_is_integer_type */
15 #if defined (__cplusplus)
16 #include <type_traits>
21 * lttng_ust_stringify - convert a literal value to a C string
23 #define __lttng_ust_stringify1(x) #x
24 #define lttng_ust_stringify(x) __lttng_ust_stringify1(x)
27 * lttng_ust_is_signed_type - check if type is signed
29 * Returns true if the type of @type is signed.
31 #if defined(__cplusplus)
32 #define lttng_ust_is_signed_type(type) (std::is_signed<type>::value)
34 #define lttng_ust_is_signed_type(type) ((type) -1 < (type) 1)
39 * lttng_ust_is_integer_type - check if type is an integer
41 * Returns true if the type of @type is an integer.
43 #if defined(__cplusplus)
44 #define lttng_ust_is_integer_type(type) (std::is_integral<type>::value)
46 #define lttng_ust_is_integer_type(type) \
47 (__builtin_types_compatible_p(type, _Bool) || \
48 __builtin_types_compatible_p(type, char) || \
49 __builtin_types_compatible_p(type, signed char) || \
50 __builtin_types_compatible_p(type, unsigned char) || \
51 __builtin_types_compatible_p(type, short) || \
52 __builtin_types_compatible_p(type, unsigned short) || \
53 __builtin_types_compatible_p(type, int) || \
54 __builtin_types_compatible_p(type, unsigned int) || \
55 __builtin_types_compatible_p(type, long) || \
56 __builtin_types_compatible_p(type, unsigned long) || \
57 __builtin_types_compatible_p(type, long long) || \
58 __builtin_types_compatible_p(type, unsigned long long))
62 * lttng_ust_field_array_element_type_is_supported -
64 * Adds a compilation assertion that array and sequence fields declared by the
65 * user are of an integral type.
67 #define lttng_ust_field_array_element_type_is_supported(type, item) \
68 lttng_ust_static_assert(lttng_ust_is_integer_type(type), \
69 "Non-integer type `" #item "` not supported as element of LTTNG_UST_FIELD_ARRAY or LTTNG_UST_FIELD_SEQUENCE", \
70 Non_integer_type__##item##__not_supported_as_element_of_LTTNG_UST_FIELD_ARRAY_or_LTTNG_UST_FIELD_SEQUENCE)
74 * lttng_ust_runtime_bug_on - check condition at runtime
75 * @condition: the condition which should be false.
77 * If the condition is true, a BUG will be triggered at runtime.
79 #define lttng_ust_runtime_bug_on(condition) \
81 if (caa_unlikely(condition)) { \
83 "LTTng BUG in file %s, line %d.\n", \
84 __FILE__, __LINE__); \
91 * lttng_ust_build_bug_on - check condition at build
92 * @condition: the condition which should be false.
94 * If the condition is true, the compiler will generate a build error.
96 #define lttng_ust_build_bug_on(condition) \
97 ((void) sizeof(char[-!!(condition)]))
101 * lttng_ust_build_runtime_bug_on - check condition at build (if constant) or runtime
102 * @condition: the condition which should be false.
104 * If the condition is a constant and true, the compiler will generate a build
105 * error. If the condition is not constant, a BUG will be triggered at runtime
106 * if the condition is ever true. If the condition is constant and false, no
109 #define lttng_ust_build_runtime_bug_on(condition) \
111 if (__builtin_constant_p(condition)) \
112 lttng_ust_build_bug_on(condition); \
114 lttng_ust_runtime_bug_on(condition); \
119 * lttng_ust_offset_align - Calculate the offset needed to align an object on
120 * its natural alignment towards higher addresses.
121 * @align_drift: object offset from an "alignment"-aligned address.
122 * @alignment: natural object alignment. Must be non-zero, power of 2.
124 * Returns the offset that must be added to align towards higher
127 #define lttng_ust_offset_align(align_drift, alignment) \
129 lttng_ust_build_runtime_bug_on((alignment) == 0 \
130 || ((alignment) & ((alignment) - 1))); \
131 (((alignment) - (align_drift)) & ((alignment) - 1)); \
136 * lttng_ust_offset_align_floor - Calculate the offset needed to align an
137 * object on its natural alignment towards lower addresses.
138 * @align_drift: object offset from an "alignment"-aligned address.
139 * @alignment: natural object alignment. Must be non-zero, power of 2.
141 * Returns the offset that must be substracted to align towards lower addresses.
143 #define lttng_ust_offset_align_floor(align_drift, alignment) \
145 lttng_ust_build_runtime_bug_on((alignment) == 0 \
146 || ((alignment) & ((alignment) - 1))); \
147 (((align_drift) - (alignment)) & ((alignment) - 1)); \
150 #endif /* _LTTNG_UST_UTILS_H */
This page took 0.049589 seconds and 4 git commands to generate.