2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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.
24 #include <common/compat/string.h>
27 * Takes a pointer x and transform it so we can use it to access members
28 * without a function call. Here an example:
30 * #define GET_SIZE(x) LTTNG_REF(x)->size
32 * struct { int size; } s;
34 * printf("size : %d\n", GET_SIZE(&s));
36 * For this example we can't use something like this for compatibility purpose
37 * since this will fail:
39 * #define GET_SIZE(x) x->size;
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.
46 #define LTTNG_REF(x) ((typeof(*x) *)(x))
49 * Memory allocation zeroed
52 void *zmalloc(size_t len
)
54 return calloc(1, len
);
58 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
62 #define max(a, b) ((a) > (b) ? (a) : (b))
66 #define max_t(type, a, b) ((type) max(a, b))
70 #define min(a, b) ((a) < (b) ? (a) : (b))
74 #define LTTNG_PACKED __attribute__((__packed__))
78 #define LTTNG_HIDDEN __attribute__((visibility("hidden")))
81 #define member_sizeof(type, field) sizeof(((type *) 0)->field)
84 * lttng_strncpy returns 0 on success, or nonzero on failure.
85 * It checks that the @src string fits into @dst_len before performing
86 * the copy. On failure, no copy has been performed.
88 * dst_len includes the string's trailing NULL.
91 int lttng_strncpy(char *dst
, const char *src
, size_t dst_len
)
93 if (lttng_strnlen(src
, dst_len
) == dst_len
) {
94 /* Fail since copying would result in truncation. */
97 strncpy(dst
, src
, dst_len
);
99 * Be extra careful and put final \0 at the end after strncpy(),
100 * even though we checked the length before. This makes Coverity
103 dst
[dst_len
- 1] = '\0';
107 #endif /* _MACROS_H */
This page took 0.034795 seconds and 4 git commands to generate.