]>
git.proxmox.com Git - mirror_frr.git/blob - lib/compiler.h
2 * Copyright (c) 2015-2017 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _FRR_COMPILER_H
18 #define _FRR_COMPILER_H
24 /* function attributes, use like
25 * void prototype(void) __attribute__((_CONSTRUCTOR(100)));
27 #if defined(__clang__)
28 #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 5)
29 # define _RET_NONNULL , returns_nonnull
31 #if __has_attribute(fallthrough)
32 # define _FALLTHROUGH __attribute__((fallthrough));
34 # define _CONSTRUCTOR(x) constructor(x)
35 # define _DEPRECATED(x) deprecated(x)
36 #elif defined(__GNUC__)
37 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
38 # define _RET_NONNULL , returns_nonnull
40 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
41 # define _CONSTRUCTOR(x) constructor(x)
42 # define _DESTRUCTOR(x) destructor(x)
43 # define _ALLOC_SIZE(x) alloc_size(x)
45 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
46 # define _DEPRECATED(x) deprecated(x)
49 # define _FALLTHROUGH __attribute__((fallthrough));
54 /* Solaris doesn't do constructor priorities due to linker restrictions */
59 /* fallback versions */
64 # define _CONSTRUCTOR(x) constructor
67 # define _DESTRUCTOR(x) destructor
70 # define _ALLOC_SIZE(x)
76 #define _DEPRECATED(x) deprecated
79 /* for helper functions defined inside macros */
80 #define macro_inline static inline __attribute__((unused))
81 #define macro_pure static inline __attribute__((unused, pure))
84 * for warnings on macros, put in the macro content like this:
85 * #define MACRO BLA CPP_WARN("MACRO has been deprecated")
90 #define CPP_NOTICE(text) _Pragma(CPP_STR(message __FILE__ ": " text))
91 #define CPP_WARN(text) CPP_NOTICE(text)
93 #elif (defined(__GNUC__) \
94 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) \
95 || (defined(__clang__) \
96 && (__clang_major__ >= 4 \
97 || (__clang_major__ == 3 && __clang_minor__ >= 5)))
98 #define CPP_WARN(text) _Pragma(CPP_STR(GCC warning text))
99 #define CPP_NOTICE(text) _Pragma(CPP_STR(message text))
102 #define CPP_WARN(text)
103 #define CPP_NOTICE(text)
106 /* MAX / MIN are not commonly defined, but useful */
107 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
113 typeof(a) _max_a = (a); \
114 typeof(b) _max_b = (b); \
115 _max_a > _max_b ? _max_a : _max_b; \
122 typeof(a) _min_a = (a); \
123 typeof(b) _min_b = (b); \
124 _min_a < _min_b ? _min_a : _min_b; \
128 #ifdef __compiler_offsetof
129 #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
131 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
135 /* this variant of container_of() retains 'const' on pointers without needing
136 * to be told to do so. The following will all work without warning:
139 * const struct member *cp;
141 * const struct cont *x = container_of(cp, struct cont, member);
142 * const struct cont *x = container_of(cp, const struct cont, member);
143 * const struct cont *x = container_of(p, struct cont, member);
144 * const struct cont *x = container_of(p, const struct cont, member);
145 * struct cont *x = container_of(p, struct cont, member);
147 * but the following will generate warnings about stripping const:
149 * struct cont *x = container_of(cp, struct cont, member);
150 * struct cont *x = container_of(cp, const struct cont, member);
151 * struct cont *x = container_of(p, const struct cont, member);
156 #define container_of(ptr, type, member) \
157 (__builtin_choose_expr( \
158 __builtin_types_compatible_p(typeof(&((type *)0)->member), \
160 || __builtin_types_compatible_p(void *, typeof(ptr)), \
162 typeof(((type *)0)->member) *__mptr = (void *)(ptr); \
163 (type *)((char *)__mptr - offsetof(type, member)); \
166 typeof(((const type *)0)->member) *__mptr = (ptr); \
167 (const type *)((const char *)__mptr - \
168 offsetof(type, member)); \
172 #define container_of_null(ptr, type, member) \
174 typeof(ptr) _tmp = (ptr); \
175 _tmp ? container_of(_tmp, type, member) : NULL; \
178 #define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
184 #endif /* _FRR_COMPILER_H */