Common Trace Format 2 generation
[lttng-tools.git] / src / common / macros.hpp
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>
7532fa3b 15#include <common/compat/string.hpp>
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
54c90d10
DG
60#ifndef LTTNG_PACKED
61#define LTTNG_PACKED __attribute__((__packed__))
62#endif
63
1405051a
FD
64#ifndef LTTNG_NO_SANITIZE_ADDRESS
65#if defined(__clang__) || defined (__GNUC__)
66#define LTTNG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
67#else
68#define LTTNG_NO_SANITIZE_ADDRESS
69#endif
70#endif
71
f8f3885c
MD
72#define member_sizeof(type, field) sizeof(((type *) 0)->field)
73
a0377dfe 74#define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
5e5c14ce 75
d22ad5f8
SM
76/* Attribute suitable to tag functions as having printf()-like arguments. */
77#define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
78 __attribute__((format(printf, _string_index, _first_to_check)))
79
411b3154
SM
80/* Attribute suitable to tag functions as having strftime()-like arguments. */
81#define ATTR_FORMAT_STRFTIME(_string_index) \
82 __attribute__((format(strftime, _string_index, 0)))
83
d22ad5f8
SM
84/* Macros used to ignore specific compiler diagnostics. */
85
86#define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
87#define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
88
89#if defined(__clang__)
90 /* Clang */
91# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
411b3154
SM
92# define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
93 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
d22ad5f8
SM
94#else
95 /* GCC */
96# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
97 _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
411b3154
SM
98# define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
99 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
d22ad5f8
SM
100#endif
101
d50d200a
SM
102/* Used to make specific C++ functions to C code. */
103#ifdef __cplusplus
104#define C_LINKAGE extern "C"
105#else
106#define C_LINKAGE
107#endif
108
f6835b82
MD
109/*
110 * lttng_strncpy returns 0 on success, or nonzero on failure.
111 * It checks that the @src string fits into @dst_len before performing
112 * the copy. On failure, no copy has been performed.
113 *
114 * dst_len includes the string's trailing NULL.
115 */
116static inline
117int lttng_strncpy(char *dst, const char *src, size_t dst_len)
118{
c3ef76cd 119 if (lttng_strnlen(src, dst_len) >= dst_len) {
f6835b82
MD
120 /* Fail since copying would result in truncation. */
121 return -1;
122 }
c3ef76cd 123 strcpy(dst, src);
f6835b82
MD
124 return 0;
125}
126
a0377dfe
FD
127#ifdef NDEBUG
128/*
129* Force usage of the assertion condition to prevent unused variable warnings
130* when `assert()` are disabled by the `NDEBUG` definition.
131*/
132# define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0))
133#else
134# include <assert.h>
135# define LTTNG_ASSERT(_cond) assert(_cond)
136#endif
137
7532fa3b
MD
138#define DISABLE_COPY_AND_ASSIGN(name) \
139 name (const name &) = delete; \
140 name &operator=(const name &) = delete
141
990570ed 142#endif /* _MACROS_H */
This page took 0.097028 seconds and 5 git commands to generate.