Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / macros.h
CommitLineData
990570ed 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
990570ed 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
990570ed 6 *
990570ed
DG
7 */
8
9#ifndef _MACROS_H
10#define _MACROS_H
11
12#include <stdlib.h>
93375aa6 13#include <stddef.h>
f6835b82 14#include <string.h>
639082d2 15#include <common/compat/string.h>
990570ed
DG
16
17/*
18 * Takes a pointer x and transform it so we can use it to access members
19 * without a function call. Here an example:
20 *
21 * #define GET_SIZE(x) LTTNG_REF(x)->size
22 *
23 * struct { int size; } s;
24 *
25 * printf("size : %d\n", GET_SIZE(&s));
26 *
27 * For this example we can't use something like this for compatibility purpose
28 * since this will fail:
29 *
30 * #define GET_SIZE(x) x->size;
31 *
32 * This is mostly use for the compatibility layer of lttng-tools. See
33 * poll/epoll for a good example. Since x can be on the stack or allocated
34 * memory using malloc(), we must use generic accessors for compat in order to
35 * *not* use a function to access members and not the variable name.
36 */
37#define LTTNG_REF(x) ((typeof(*x) *)(x))
38
39/*
40 * Memory allocation zeroed
41 */
4616a46c
MD
42static inline
43void *zmalloc(size_t len)
44{
45 return calloc(1, len);
46}
990570ed
DG
47
48#ifndef ARRAY_SIZE
49#define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
50#endif
51
93375aa6
JG
52#ifndef container_of
53#define container_of(ptr, type, member) \
54 ({ \
55 const typeof(((type *)NULL)->member) * __ptr = (ptr); \
56 (type *)((char *)__ptr - offsetof(type, member)); \
57 })
58#endif
59
cf3f19ae
SM
60#ifndef max
61#define max(a, b) ((a) > (b) ? (a) : (b))
62#endif
63
331744e3 64#ifndef max_t
46e3b92d 65#define max_t(type, a, b) max((type) a, (type) b)
331744e3
JD
66#endif
67
cf3f19ae
SM
68#ifndef min
69#define min(a, b) ((a) < (b) ? (a) : (b))
70#endif
71
d0927b41 72#ifndef min_t
46e3b92d 73#define min_t(type, a, b) min((type) a, (type) b)
d0927b41
FD
74#endif
75
54c90d10
DG
76#ifndef LTTNG_PACKED
77#define LTTNG_PACKED __attribute__((__packed__))
78#endif
79
1405051a
FD
80#ifndef LTTNG_NO_SANITIZE_ADDRESS
81#if defined(__clang__) || defined (__GNUC__)
82#define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
83#else
84#define LTTNG_NO_SANITIZE_ADDRESS
85#endif
86#endif
87
0c634962 88#define is_signed(type) (((type) -1) < (type) 1)
159b042f 89
eac6ee11
JG
90/*
91 * Align value to the next multiple of align. Returns val if it already is a
92 * multiple of align. Align must be a power of two.
93 */
94#define ALIGN_TO(value, align) ((value + (align - 1)) & ~(align - 1))
95
70c6956b
MJ
96/*
97 * LTTNG_HIDDEN: set the hidden attribute for internal functions
98 * On Windows, symbols are local unless explicitly exported,
99 * see https://gcc.gnu.org/wiki/Visibility
100 */
101#if defined(_WIN32) || defined(__CYGWIN__)
102#define LTTNG_HIDDEN
103#else
99c815c1
DG
104#define LTTNG_HIDDEN __attribute__((visibility("hidden")))
105#endif
106
f8f3885c
MD
107#define member_sizeof(type, field) sizeof(((type *) 0)->field)
108
a0377dfe 109#define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
5e5c14ce 110
1a65bbb8
JG
111/*
112 * Get an aligned pointer to a value. This is meant
113 * as a helper to pass an aligned pointer to a member in a packed structure
114 * to a function.
115 */
116#define ALIGNED_CONST_PTR(value) (((const typeof(value) []) { value }))
117
f6835b82
MD
118/*
119 * lttng_strncpy returns 0 on success, or nonzero on failure.
120 * It checks that the @src string fits into @dst_len before performing
121 * the copy. On failure, no copy has been performed.
122 *
123 * dst_len includes the string's trailing NULL.
124 */
125static inline
126int lttng_strncpy(char *dst, const char *src, size_t dst_len)
127{
c3ef76cd 128 if (lttng_strnlen(src, dst_len) >= dst_len) {
f6835b82
MD
129 /* Fail since copying would result in truncation. */
130 return -1;
131 }
c3ef76cd 132 strcpy(dst, src);
f6835b82
MD
133 return 0;
134}
135
a0377dfe
FD
136#ifdef NDEBUG
137/*
138* Force usage of the assertion condition to prevent unused variable warnings
139* when `assert()` are disabled by the `NDEBUG` definition.
140*/
141# define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
142#else
143# include <assert.h>
144# define LTTNG_ASSERT(_cond) assert(_cond)
145#endif
146
990570ed 147#endif /* _MACROS_H */
This page took 0.071358 seconds and 4 git commands to generate.