e9dc190aac3f00c0235d272d375c950632013a26
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/compat/string.h>
18 * Takes a pointer x and transform it so we can use it to access members
19 * without a function call. Here an example:
21 * #define GET_SIZE(x) LTTNG_REF(x)->size
23 * struct { int size; } s;
25 * printf("size : %d\n", GET_SIZE(&s));
27 * For this example we can't use something like this for compatibility purpose
28 * since this will fail:
30 * #define GET_SIZE(x) x->size;
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.
37 #define LTTNG_REF(x) ((typeof(*x) *)(x))
40 * Memory allocation zeroed
43 void *zmalloc(size_t len
)
45 return calloc(1, len
);
49 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
53 #define container_of(ptr, type, member) \
55 const typeof(((type *)NULL)->member) * __ptr = (ptr); \
56 (type *)((char *)__ptr - offsetof(type, member)); \
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.
69 # define max(a, b) ((a) > (b) ? (a) : (b))
73 # define min(a, b) ((a) < (b) ? (a) : (b))
77 # define max_t(type, a, b) max((type) a, (type) b)
81 # define min_t(type, a, b) min((type) a, (type) b)
87 #define LTTNG_PACKED __attribute__((__packed__))
90 #ifndef LTTNG_NO_SANITIZE_ADDRESS
91 #if defined(__clang__) || defined (__GNUC__)
92 #define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
94 #define LTTNG_NO_SANITIZE_ADDRESS
98 #define is_signed(type) (((type) -1) < (type) 1)
101 * Align value to the next multiple of align. Returns val if it already is a
102 * multiple of align. Align must be a power of two.
104 #define ALIGN_TO(value, align) ((value + (align - 1)) & ~(align - 1))
106 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
108 #define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
111 * Get an aligned pointer to a value. This is meant
112 * as a helper to pass an aligned pointer to a member in a packed structure
115 #define ALIGNED_CONST_PTR(value) (((const typeof(value) []) { value }))
118 * lttng_strncpy returns 0 on success, or nonzero on failure.
119 * It checks that the @src string fits into @dst_len before performing
120 * the copy. On failure, no copy has been performed.
122 * dst_len includes the string's trailing NULL.
125 int lttng_strncpy(char *dst
, const char *src
, size_t dst_len
)
127 if (lttng_strnlen(src
, dst_len
) >= dst_len
) {
128 /* Fail since copying would result in truncation. */
137 * Force usage of the assertion condition to prevent unused variable warnings
138 * when `assert()` are disabled by the `NDEBUG` definition.
140 # define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
143 # define LTTNG_ASSERT(_cond) assert(_cond)
146 #endif /* _MACROS_H */
This page took 0.042707 seconds and 5 git commands to generate.