43a4d692b80fbad0a5a42f03f2ceffc3450c63a1
2 * Copyright (C) 2011 EfficiOS Inc.
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)); \
61 #define LTTNG_PACKED __attribute__((__packed__))
64 #ifndef LTTNG_NO_SANITIZE_ADDRESS
65 #if defined(__clang__) || defined (__GNUC__)
66 #define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
68 #define LTTNG_NO_SANITIZE_ADDRESS
72 #define is_signed(type) (((type) -1) < (type) 1)
74 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
76 #define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
77 #define ASSERT_RCU_READ_LOCKED(lock) LTTNG_ASSERT(rcu_read_ongoing())
79 /* Attribute suitable to tag functions as having printf()-like arguments. */
80 #define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
81 __attribute__((format(printf, _string_index, _first_to_check)))
83 /* Attribute suitable to tag functions as having strftime()-like arguments. */
84 #define ATTR_FORMAT_STRFTIME(_string_index) \
85 __attribute__((format(strftime, _string_index, 0)))
87 /* Macros used to ignore specific compiler diagnostics. */
89 #define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
90 #define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
92 #if defined(__clang__)
94 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
95 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
96 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
99 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
100 _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
101 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
102 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
105 /* Used to make specific C++ functions to C code. */
107 #define C_LINKAGE extern "C"
113 * lttng_strncpy returns 0 on success, or nonzero on failure.
114 * It checks that the @src string fits into @dst_len before performing
115 * the copy. On failure, no copy has been performed.
117 * dst_len includes the string's trailing NULL.
120 int lttng_strncpy(char *dst
, const char *src
, size_t dst_len
)
122 if (lttng_strnlen(src
, dst_len
) >= dst_len
) {
123 /* Fail since copying would result in truncation. */
132 * Force usage of the assertion condition to prevent unused variable warnings
133 * when `assert()` are disabled by the `NDEBUG` definition.
135 # define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
138 # define LTTNG_ASSERT(_cond) assert(_cond)
141 #endif /* _MACROS_H */
This page took 0.035046 seconds and 5 git commands to generate.