Add pipe_release utils to the pipe wrapper
[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>
f6835b82 23#include <string.h>
639082d2 24#include <common/compat/string.h>
990570ed
DG
25
26/*
27 * Takes a pointer x and transform it so we can use it to access members
28 * without a function call. Here an example:
29 *
30 * #define GET_SIZE(x) LTTNG_REF(x)->size
31 *
32 * struct { int size; } s;
33 *
34 * printf("size : %d\n", GET_SIZE(&s));
35 *
36 * For this example we can't use something like this for compatibility purpose
37 * since this will fail:
38 *
39 * #define GET_SIZE(x) x->size;
40 *
41 * This is mostly use for the compatibility layer of lttng-tools. See
42 * poll/epoll for a good example. Since x can be on the stack or allocated
43 * memory using malloc(), we must use generic accessors for compat in order to
44 * *not* use a function to access members and not the variable name.
45 */
46#define LTTNG_REF(x) ((typeof(*x) *)(x))
47
48/*
49 * Memory allocation zeroed
50 */
4616a46c
MD
51static inline
52void *zmalloc(size_t len)
53{
54 return calloc(1, len);
55}
990570ed
DG
56
57#ifndef ARRAY_SIZE
58#define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
59#endif
60
cf3f19ae
SM
61#ifndef max
62#define max(a, b) ((a) > (b) ? (a) : (b))
63#endif
64
331744e3
JD
65#ifndef max_t
66#define max_t(type, a, b) ((type) max(a, b))
67#endif
68
cf3f19ae
SM
69#ifndef min
70#define min(a, b) ((a) < (b) ? (a) : (b))
71#endif
72
54c90d10
DG
73#ifndef LTTNG_PACKED
74#define LTTNG_PACKED __attribute__((__packed__))
75#endif
76
70c6956b
MJ
77/*
78 * LTTNG_HIDDEN: set the hidden attribute for internal functions
79 * On Windows, symbols are local unless explicitly exported,
80 * see https://gcc.gnu.org/wiki/Visibility
81 */
82#if defined(_WIN32) || defined(__CYGWIN__)
83#define LTTNG_HIDDEN
84#else
99c815c1
DG
85#define LTTNG_HIDDEN __attribute__((visibility("hidden")))
86#endif
87
f8f3885c
MD
88#define member_sizeof(type, field) sizeof(((type *) 0)->field)
89
f6835b82
MD
90/*
91 * lttng_strncpy returns 0 on success, or nonzero on failure.
92 * It checks that the @src string fits into @dst_len before performing
93 * the copy. On failure, no copy has been performed.
94 *
95 * dst_len includes the string's trailing NULL.
96 */
97static inline
98int lttng_strncpy(char *dst, const char *src, size_t dst_len)
99{
639082d2 100 if (lttng_strnlen(src, dst_len) == dst_len) {
f6835b82
MD
101 /* Fail since copying would result in truncation. */
102 return -1;
103 }
104 strncpy(dst, src, dst_len);
105 /*
106 * Be extra careful and put final \0 at the end after strncpy(),
107 * even though we checked the length before. This makes Coverity
108 * happy.
109 */
110 dst[dst_len - 1] = '\0';
111 return 0;
112}
113
990570ed 114#endif /* _MACROS_H */
This page took 0.064361 seconds and 4 git commands to generate.