Xenomai  3.1
compiler.h
1 /*
2  * Copyright (C) 2013 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _BOILERPLATE_COMPILER_H
19 #define _BOILERPLATE_COMPILER_H
20 
21 #include <stddef.h>
22 
23 #define container_of(ptr, type, member) \
24  ({ \
25  const __typeof__(((type *)0)->member) *__mptr = (ptr); \
26  (type *)((char *)__mptr - offsetof(type, member)); \
27  })
28 
29 #define __stringify_1(x...) #x
30 #define __stringify(x...) __stringify_1(x)
31 
32 #ifndef likely
33 #define likely(x) __builtin_expect(!!(x), 1)
34 #define unlikely(x) __builtin_expect(!!(x), 0)
35 #endif
36 
37 #ifndef __noreturn
38 #define __noreturn __attribute__((__noreturn__))
39 #endif
40 
41 #ifndef __must_check
42 #define __must_check __attribute__((__warn_unused_result__))
43 #endif
44 
45 #ifndef __weak
46 #define __weak __attribute__((__weak__))
47 #endif
48 
49 #ifndef __maybe_unused
50 #define __maybe_unused __attribute__((__unused__))
51 #endif
52 
53 #ifndef __aligned
54 #define __aligned(__n) __attribute__((aligned (__n)))
55 #endif
56 
57 #ifndef __deprecated
58 #define __deprecated __attribute__((__deprecated__))
59 #endif
60 
61 #ifndef __packed
62 #define __packed __attribute__((__packed__))
63 #endif
64 
65 #ifndef __alloc_size
66 #define __alloc_size(__args) __attribute__((__alloc_size__(__args)))
67 #endif
68 
69 #define __align_to(__size, __al) (((__size) + (__al) - 1) & (~((__al) - 1)))
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 #define xenomai_count_trailing_zeros(x) \
76  ((x) == 0 ? (int)(sizeof(x) * __CHAR_BIT__) \
77  : sizeof(x) <= sizeof(unsigned int) ? \
78  __builtin_ctz((unsigned int)x) \
79  : sizeof(x) <= sizeof(unsigned long) ? \
80  __builtin_ctzl((unsigned long)x) \
81  : __builtin_ctzll(x))
82 
83 #define xenomai_count_leading_zeros(x) \
84  ((x) == 0 ? (int)(sizeof(x) * __CHAR_BIT__) \
85  : sizeof(x) <= sizeof(unsigned int) ? \
86  __builtin_clz((unsigned int)x) + \
87  (int)(sizeof(unsigned int) - sizeof(x)) \
88  : sizeof(x) <= sizeof(unsigned long) ? \
89  __builtin_clzl((unsigned long)x) \
90  : __builtin_clzll(x))
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif /* _BOILERPLATE_COMPILER_H */