Fix: disable array/sequence compile-time type check in C
[lttng-ust.git] / include / lttng / ust-utils.h
CommitLineData
eae3c729
MJ
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7#ifndef _LTTNG_UST_UTILS_H
8#define _LTTNG_UST_UTILS_H
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <urcu/compiler.h>
13
14/* For lttng_ust_is_integer_type */
15#if defined (__cplusplus)
16#include <type_traits>
17#endif
18
19
20/**
21 * lttng_ust_stringify - convert a literal value to a C string
22 */
23#define __lttng_ust_stringify1(x) #x
24#define lttng_ust_stringify(x) __lttng_ust_stringify1(x)
25
26/**
27 * lttng_ust_is_signed_type - check if type is signed
28 *
29 * Returns true if the type of @type is signed.
30 */
cd933410
MJ
31#if defined(__cplusplus)
32#define lttng_ust_is_signed_type(type) (std::is_signed<type>::value)
33#else
34#define lttng_ust_is_signed_type(type) ((type) -1 < (type) 1)
35#endif
eae3c729
MJ
36
37
38/**
39 * lttng_ust_is_integer_type - check if type is an integer
40 *
41 * Returns true if the type of @type is an integer.
42 */
43#if defined(__cplusplus)
44#define lttng_ust_is_integer_type(type) (std::is_integral<type>::value)
45#else
46#define lttng_ust_is_integer_type(type) \
47 (__builtin_types_compatible_p(type, _Bool) || \
48 __builtin_types_compatible_p(type, char) || \
d465835d 49 __builtin_types_compatible_p(type, signed char) || \
eae3c729
MJ
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))
59#endif
60
62eb004c
MD
61/**
62 * lttng_ust_is_pointer_type - check if type is a pointer
63 *
64 * Returns true if the type of @type is a pointer.
f04f60a5
MD
65 *
66 * Note: The C implementation of lttng_ust_is_pointer_type uses pointer
67 * arithmetic, which does not work on opaque pointer types.
62eb004c
MD
68 */
69#if defined(__cplusplus)
70#define lttng_ust_is_pointer_type(type) (std::is_pointer<type>::value)
71#else
72/* The difference between two pointers is an integer. */
73#define lttng_ust_is_pointer_type(type) \
a9bd7249 74 (lttng_ust_is_integer_type(typeof(((type)1 - (type)1))) && !lttng_ust_is_integer_type(type))
62eb004c
MD
75#endif
76
77
eae3c729 78/**
10937ee5 79 * lttng_ust_field_array_element_type_is_supported -
eae3c729 80 *
10937ee5 81 * Adds a compilation assertion that array and sequence fields declared by the
62eb004c 82 * user are of an integral or pointer type.
eae3c729 83 */
10937ee5 84#define lttng_ust_field_array_element_type_is_supported(type, item) \
62eb004c
MD
85 lttng_ust_static_assert(lttng_ust_is_integer_type(type) || lttng_ust_is_pointer_type(type), \
86 "Non-integer, non-pointer type `" #item "` not supported as element of LTTNG_UST_FIELD_ARRAY or LTTNG_UST_FIELD_SEQUENCE", \
10937ee5 87 Non_integer_type__##item##__not_supported_as_element_of_LTTNG_UST_FIELD_ARRAY_or_LTTNG_UST_FIELD_SEQUENCE)
eae3c729
MJ
88
89
90/**
91 * lttng_ust_runtime_bug_on - check condition at runtime
92 * @condition: the condition which should be false.
93 *
94 * If the condition is true, a BUG will be triggered at runtime.
95 */
96#define lttng_ust_runtime_bug_on(condition) \
97 do { \
98 if (caa_unlikely(condition)) { \
99 fprintf(stderr, \
100 "LTTng BUG in file %s, line %d.\n", \
101 __FILE__, __LINE__); \
102 exit(EXIT_FAILURE); \
103 } \
104 } while (0)
105
106
107/**
108 * lttng_ust_build_bug_on - check condition at build
109 * @condition: the condition which should be false.
110 *
111 * If the condition is true, the compiler will generate a build error.
112 */
113#define lttng_ust_build_bug_on(condition) \
114 ((void) sizeof(char[-!!(condition)]))
115
116
117/**
118 * lttng_ust_build_runtime_bug_on - check condition at build (if constant) or runtime
119 * @condition: the condition which should be false.
120 *
121 * If the condition is a constant and true, the compiler will generate a build
122 * error. If the condition is not constant, a BUG will be triggered at runtime
123 * if the condition is ever true. If the condition is constant and false, no
124 * code is emitted.
125 */
126#define lttng_ust_build_runtime_bug_on(condition) \
127 do { \
128 if (__builtin_constant_p(condition)) \
129 lttng_ust_build_bug_on(condition); \
130 else \
131 lttng_ust_runtime_bug_on(condition); \
132 } while (0)
133
134
135/**
136 * lttng_ust_offset_align - Calculate the offset needed to align an object on
137 * its natural alignment towards higher addresses.
138 * @align_drift: object offset from an "alignment"-aligned address.
139 * @alignment: natural object alignment. Must be non-zero, power of 2.
140 *
141 * Returns the offset that must be added to align towards higher
142 * addresses.
143 */
144#define lttng_ust_offset_align(align_drift, alignment) \
145 ({ \
146 lttng_ust_build_runtime_bug_on((alignment) == 0 \
147 || ((alignment) & ((alignment) - 1))); \
148 (((alignment) - (align_drift)) & ((alignment) - 1)); \
149 })
150
151
152/**
153 * lttng_ust_offset_align_floor - Calculate the offset needed to align an
154 * object on its natural alignment towards lower addresses.
155 * @align_drift: object offset from an "alignment"-aligned address.
156 * @alignment: natural object alignment. Must be non-zero, power of 2.
157 *
2fbda51c 158 * Returns the offset that must be subtracted to align towards lower addresses.
eae3c729
MJ
159 */
160#define lttng_ust_offset_align_floor(align_drift, alignment) \
161 ({ \
162 lttng_ust_build_runtime_bug_on((alignment) == 0 \
163 || ((alignment) & ((alignment) - 1))); \
164 (((align_drift) - (alignment)) & ((alignment) - 1)); \
165 })
166
167#endif /* _LTTNG_UST_UTILS_H */
This page took 0.031982 seconds and 4 git commands to generate.