Cleanup: namespace 'align' macros
[lttng-tools.git] / src / common / macros.h
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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 <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <common/compat/string.h>
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 */
42 static inline
43 void *zmalloc(size_t len)
44 {
45 return calloc(1, len);
46 }
47
48 #ifndef ARRAY_SIZE
49 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
50 #endif
51
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
60
61 /*
62 * The min and max macros are not needed in C++ (std::min and std::max are
63 * preferred) and they conflict with some C++ header file. Don't define them
64 * when compiling C++ source.
65 */
66 #ifndef __cplusplus
67
68 # ifndef max
69 # define max(a, b) ((a) > (b) ? (a) : (b))
70 # endif
71
72 # ifndef min
73 # define min(a, b) ((a) < (b) ? (a) : (b))
74 # endif
75
76 # ifndef max_t
77 # define max_t(type, a, b) max((type) a, (type) b)
78 # endif
79
80 # ifndef min_t
81 # define min_t(type, a, b) min((type) a, (type) b)
82 # endif
83
84 #endif
85
86 #ifndef LTTNG_PACKED
87 #define LTTNG_PACKED __attribute__((__packed__))
88 #endif
89
90 #ifndef LTTNG_NO_SANITIZE_ADDRESS
91 #if defined(__clang__) || defined (__GNUC__)
92 #define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
93 #else
94 #define LTTNG_NO_SANITIZE_ADDRESS
95 #endif
96 #endif
97
98 #define is_signed(type) (((type) -1) < (type) 1)
99
100 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
101
102 #define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
103
104 /*
105 * Get an aligned pointer to a value. This is meant
106 * as a helper to pass an aligned pointer to a member in a packed structure
107 * to a function.
108 */
109 #define ALIGNED_CONST_PTR(value) (((const typeof(value) []) { value }))
110
111 /*
112 * lttng_strncpy returns 0 on success, or nonzero on failure.
113 * It checks that the @src string fits into @dst_len before performing
114 * the copy. On failure, no copy has been performed.
115 *
116 * dst_len includes the string's trailing NULL.
117 */
118 static inline
119 int lttng_strncpy(char *dst, const char *src, size_t dst_len)
120 {
121 if (lttng_strnlen(src, dst_len) >= dst_len) {
122 /* Fail since copying would result in truncation. */
123 return -1;
124 }
125 strcpy(dst, src);
126 return 0;
127 }
128
129 #ifdef NDEBUG
130 /*
131 * Force usage of the assertion condition to prevent unused variable warnings
132 * when `assert()` are disabled by the `NDEBUG` definition.
133 */
134 # define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
135 #else
136 # include <assert.h>
137 # define LTTNG_ASSERT(_cond) assert(_cond)
138 #endif
139
140 #endif /* _MACROS_H */
This page took 0.045805 seconds and 4 git commands to generate.