2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
12 #include <common/compat/string.hpp>
20 #include <type_traits>
23 * Takes a pointer x and transform it so we can use it to access members
24 * without a function call. Here an example:
26 * #define GET_SIZE(x) LTTNG_REF(x)->size
28 * struct { int size; } s;
30 * printf("size : %d\n", GET_SIZE(&s));
32 * For this example we can't use something like this for compatibility purpose
33 * since this will fail:
35 * #define GET_SIZE(x) x->size;
37 * This is mostly use for the compatibility layer of lttng-tools. See
38 * poll/epoll for a good example. Since x can be on the stack or allocated
39 * memory using malloc(), we must use generic accessors for compat in order to
40 * *not* use a function to access members and not the variable name.
42 #define LTTNG_REF(x) ((typeof(*x) *)(x))
46 * Force usage of the assertion condition to prevent unused variable warnings
47 * when `assert()` are disabled by the `NDEBUG` definition.
49 # define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
52 # define LTTNG_ASSERT(_cond) assert(_cond)
56 * Memory allocation zeroed
60 void *zmalloc_internal(size_t size)
62 return calloc(1, size);
69 * gcc versions before 5.0 lack some type traits defined in C++11.
70 * Since in this instance we use the trait to prevent misuses
71 * of malloc (and statically assert) and not to generate different
72 * code based on this property, simply set value to true and allow
73 * the code to compile. Anyone using a contemporary compiler will
76 #if __GNUG__ && __GNUC__ < 5
77 static constexpr bool value = true;
79 static constexpr bool value = std::is_trivially_constructible<T>::value;
84 * Malloc and zero-initialize an object of type T, asserting that T can be
85 * safely malloc-ed (is trivially constructible).
90 static_assert (can_malloc<T>::value, "type can be malloc'ed");
91 return (T *) zmalloc_internal(sizeof(T));
95 * Malloc and zero-initialize a buffer of size `size`, asserting that type T
96 * can be safely malloc-ed (is trivially constructible).
99 T *zmalloc(size_t size)
101 static_assert (can_malloc<T>::value, "type can be malloc'ed");
102 LTTNG_ASSERT(size >= sizeof(T));
103 return (T *) zmalloc_internal(size);
107 * Malloc and zero-initialize an array of `nmemb` elements of type T,
108 * asserting that T can be safely malloc-ed (is trivially constructible).
111 T *calloc(size_t nmemb)
113 static_assert (can_malloc<T>::value, "type can be malloc'ed");
114 return (T *) zmalloc_internal(nmemb * sizeof(T));
118 * Malloc an object of type T, asserting that T can be safely malloc-ed (is
119 * trivially constructible).
124 static_assert (can_malloc<T>::value, "type can be malloc'ed");
125 return (T *) malloc(sizeof(T));
129 * Malloc a buffer of size `size`, asserting that type T can be safely
130 * malloc-ed (is trivially constructible).
133 T *malloc(size_t size)
135 static_assert (can_malloc<T>::value, "type can be malloc'ed");
136 return (T *) malloc(size);
140 * Prevent using `free` on types that are non-POD.
142 * Declare a delete prototype of free if the parameter type is not safe to free
145 * If the parameter is a pointer to void, we can't check if what is pointed
146 * to is safe to free or not, as we don't know what is pointed to. Ideally,
147 * all calls to free would be with a typed pointer, but there are too many
148 * instances of passing a pointer to void to enforce that right now. So allow
149 * pointers to void, these will not be checked.
156 * gcc versions before 5.0 lack some type traits defined in C++11.
157 * Since in this instance we use the trait to prevent misuses
158 * of free (and statically assert) and not to generate different
159 * code based on this property, simply set value to true and allow
160 * the code to compile. Anyone using a contemporary compiler will
163 #if __GNUG__ && __GNUC__ < 5
164 static constexpr bool value = true;
166 static constexpr bool value = std::is_trivially_destructible<T>::value || std::is_void<T>::value;
170 template<typename T, typename = typename std::enable_if<!can_free<T>::value>::type>
171 void free(T *p) = delete;
176 static constexpr bool value = std::is_pod<T>::value || std::is_void<T>::value;
179 template <typename T, typename = typename std::enable_if<!can_memset<T>::value>::type>
180 void *memset(T *s, int c, size_t n) = delete;
186 * gcc versions before 5.0 lack some type traits defined in C++11.
187 * Since in this instance we use the trait to prevent misuses
188 * of memcpy (and statically assert) and not to generate different
189 * code based on this property, simply set value to true and allow
190 * the code to compile. Anyone using a contemporary compiler will
193 #if __GNUG__ && __GNUC__ < 5
194 static constexpr bool value = true;
196 static constexpr bool value = std::is_trivially_copyable<T>::value;
200 template <typename T, typename U,
201 typename = typename std::enable_if<!can_memcpy<T>::value>::type,
202 typename = typename std::enable_if<!can_memcpy<U>::value>::type>
203 void *memcpy(T *d, const U *s, size_t n) = delete;
209 * gcc versions before 5.0 lack some type traits defined in C++11.
210 * Since in this instance we use the trait to prevent misuses
211 * of memmove (and statically assert) and not to generate different
212 * code based on this property, simply set value to true and allow
213 * the code to compile. Anyone using a contemporary compiler will
216 #if __GNUG__ && __GNUC__ < 5
217 static constexpr bool value = true;
219 static constexpr bool value = std::is_trivially_copyable<T>::value;
223 template <typename T, typename U,
224 typename = typename std::enable_if<!can_memmove<T>::value>::type,
225 typename = typename std::enable_if<!can_memmove<U>::value>::type>
226 void *memmove(T *d, const U *s, size_t n) = delete;
229 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
233 #define container_of(ptr, type, member) \
235 const typeof(((type *)NULL)->member) * __ptr = (ptr); \
236 (type *)((char *)__ptr - offsetof(type, member)); \
241 #define LTTNG_PACKED __attribute__((__packed__))
244 #ifndef LTTNG_NO_SANITIZE_ADDRESS
245 #if defined(__clang__) || defined (__GNUC__)
246 #define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
248 #define LTTNG_NO_SANITIZE_ADDRESS
252 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
254 #define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
255 #define ASSERT_RCU_READ_LOCKED(lock) LTTNG_ASSERT(rcu_read_ongoing())
257 /* Attribute suitable to tag functions as having printf()-like arguments. */
258 #define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
259 __attribute__((format(printf, _string_index, _first_to_check)))
261 /* Attribute suitable to tag functions as having strftime()-like arguments. */
262 #define ATTR_FORMAT_STRFTIME(_string_index) \
263 __attribute__((format(strftime, _string_index, 0)))
265 /* Macros used to ignore specific compiler diagnostics. */
267 #define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
268 #define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
270 #if defined(__clang__)
272 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
273 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
274 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
275 # define DIAGNOSTIC_IGNORE_LOGICAL_OP
276 # define DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
277 # define DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
278 _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
281 # define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
282 _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
283 # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
284 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
285 # define DIAGNOSTIC_IGNORE_LOGICAL_OP \
286 _Pragma("GCC diagnostic ignored \"-Wlogical-op\"")
287 # define DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES \
288 _Pragma("GCC diagnostic ignored \"-Wduplicated-branches\"")
289 # define DIAGNOSTIC_IGNORE_INVALID_OFFSETOF \
290 _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
293 /* Used to make specific C++ functions to C code. */
295 #define C_LINKAGE extern "C"
301 * lttng_strncpy returns 0 on success, or nonzero on failure.
302 * It checks that the @src string fits into @dst_len before performing
303 * the copy. On failure, no copy has been performed.
305 * Assumes that 'src' is null-terminated.
307 * dst_len includes the string's trailing NULL.
310 int lttng_strncpy(char *dst, const char *src, size_t dst_len)
312 if (strlen(src) >= dst_len) {
313 /* Fail since copying would result in truncation. */
320 #endif /* _MACROS_H */