Clean-up: move global sessiond symbols out of main.o
[lttng-tools.git] / src / common / macros.h
CommitLineData
990570ed
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
990570ed
DG
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d14d33bf 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
990570ed
DG
12 * more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
990570ed
DG
17 */
18
19#ifndef _MACROS_H
20#define _MACROS_H
21
22#include <stdlib.h>
93375aa6 23#include <stddef.h>
f6835b82 24#include <string.h>
639082d2 25#include <common/compat/string.h>
990570ed
DG
26
27/*
28 * Takes a pointer x and transform it so we can use it to access members
29 * without a function call. Here an example:
30 *
31 * #define GET_SIZE(x) LTTNG_REF(x)->size
32 *
33 * struct { int size; } s;
34 *
35 * printf("size : %d\n", GET_SIZE(&s));
36 *
37 * For this example we can't use something like this for compatibility purpose
38 * since this will fail:
39 *
40 * #define GET_SIZE(x) x->size;
41 *
42 * This is mostly use for the compatibility layer of lttng-tools. See
43 * poll/epoll for a good example. Since x can be on the stack or allocated
44 * memory using malloc(), we must use generic accessors for compat in order to
45 * *not* use a function to access members and not the variable name.
46 */
47#define LTTNG_REF(x) ((typeof(*x) *)(x))
48
49/*
50 * Memory allocation zeroed
51 */
4616a46c
MD
52static inline
53void *zmalloc(size_t len)
54{
55 return calloc(1, len);
56}
990570ed
DG
57
58#ifndef ARRAY_SIZE
59#define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
60#endif
61
93375aa6
JG
62#ifndef container_of
63#define container_of(ptr, type, member) \
64 ({ \
65 const typeof(((type *)NULL)->member) * __ptr = (ptr); \
66 (type *)((char *)__ptr - offsetof(type, member)); \
67 })
68#endif
69
cf3f19ae
SM
70#ifndef max
71#define max(a, b) ((a) > (b) ? (a) : (b))
72#endif
73
331744e3
JD
74#ifndef max_t
75#define max_t(type, a, b) ((type) max(a, b))
76#endif
77
cf3f19ae
SM
78#ifndef min
79#define min(a, b) ((a) < (b) ? (a) : (b))
80#endif
81
d0927b41
FD
82#ifndef min_t
83#define min_t(type, a, b) ((type) min(a, b))
84#endif
85
54c90d10
DG
86#ifndef LTTNG_PACKED
87#define LTTNG_PACKED __attribute__((__packed__))
88#endif
89
eac6ee11
JG
90/*
91 * Align value to the next multiple of align. Returns val if it already is a
92 * multiple of align. Align must be a power of two.
93 */
94#define ALIGN_TO(value, align) ((value + (align - 1)) & ~(align - 1))
95
70c6956b
MJ
96/*
97 * LTTNG_HIDDEN: set the hidden attribute for internal functions
98 * On Windows, symbols are local unless explicitly exported,
99 * see https://gcc.gnu.org/wiki/Visibility
100 */
101#if defined(_WIN32) || defined(__CYGWIN__)
102#define LTTNG_HIDDEN
103#else
99c815c1
DG
104#define LTTNG_HIDDEN __attribute__((visibility("hidden")))
105#endif
106
f8f3885c
MD
107#define member_sizeof(type, field) sizeof(((type *) 0)->field)
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
990570ed 127#endif /* _MACROS_H */
This page took 0.073377 seconds and 4 git commands to generate.