Contiki-NG
Loading...
Searching...
No Matches
cc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop OS
31 *
32 *
33 */
34
35/**
36 * \file
37 * Default definitions of C compiler quirk work-arounds.
38 * \author Adam Dunkels <adam@dunkels.com>
39 *
40 * This file is used for making use of extra functionality of some C
41 * compilers used for Contiki, and defining work-arounds for various
42 * quirks and problems with some other C compilers.
43 */
44
45#ifndef CC_H_
46#define CC_H_
47
48#include "contiki.h"
49
50#ifdef __GNUC__
51
52#ifndef CC_CONF_ASSUME
53 #ifdef __clang__
54 #define CC_CONF_ASSUME(c) do { __builtin_assume((c)); } while(0)
55 #elif __GNUC__ >= 13
56 #define CC_CONF_ASSUME(c) __attribute__((__assume__(c)))
57 #else
58 #define CC_CONF_ASSUME(c) do { if (!(c)) __builtin_unreachable(); } while(0)
59 #endif
60#endif
61
62#define CC_CONF_ALIGN(n) __attribute__((__aligned__(n)))
63
64#ifndef CC_CONF_CONSTRUCTOR
65#define CC_CONF_CONSTRUCTOR(prio) __attribute__((constructor(prio)))
66#endif /* CC_CONF_CONSTRUCTOR */
67
68#ifndef CC_CONF_DESTRUCTOR
69#define CC_CONF_DESTRUCTOR(prio) __attribute__((destructor(prio)))
70#endif /* CC_CONF_DESTRUCTOR */
71
72#define CC_CONF_DEPRECATED(msg) __attribute__((deprecated(msg)))
73
74#define CC_CONF_NORETURN __attribute__((__noreturn__))
75
76#define CC_CONF_MUST_BE_ARRAY(array) \
77 (sizeof(struct {_Static_assert( \
78 (!__builtin_types_compatible_p(__typeof(array), \
79 __typeof(&(array)[0]))), \
80 "not an array");}))
81
82#endif /* __GNUC__ */
83
84/**
85 * Configure if the C compiler supports taking hints from the user about
86 * invariants, e.g. with __attribute__((__assume__(hint))).
87 */
88#ifdef CC_CONF_ASSUME
89#define ASSUME(c) CC_CONF_ASSUME(c)
90#else
91#define ASSUME(c)
92#endif /* CC_CONF_ASSUME */
93
94#ifdef CC_CONF_ALIGN
95#define CC_ALIGN(n) CC_CONF_ALIGN(n)
96#endif /* CC_CONF_ALIGN */
97
98/**
99 * Configure if the C compiler supports functions that are not meant to return
100 * e.g. with __attribute__((__noreturn__))
101 */
102#ifdef CC_CONF_NORETURN
103#define CC_NORETURN CC_CONF_NORETURN
104#else
105#define CC_NORETURN
106#endif /* CC_CONF_NORETURN */
107
108/**
109 * Returns 0 if the provided variable is an array and raises an error if not.
110 */
111#ifdef CC_CONF_MUST_BE_ARRAY
112#define CC_MUST_BE_ARRAY(array) CC_CONF_MUST_BE_ARRAY(array)
113#else /* CC_CONF_MUST_BE_ARRAY */
114#define CC_MUST_BE_ARRAY(array) 0
115#endif /* CC_CONF_MUST_BE_ARRAY */
116
117/**
118 * \brief Counts the number of elements of an array.
119 * \param array The array.
120 * \return The number of elements of the array.
121 */
122#define CC_ARRAY_LENGTH(array) ((sizeof(array) / sizeof((array)[0])) \
123 + CC_MUST_BE_ARRAY(array))
124
125/**
126 * Configure if the C compiler supports marking functions as constructors
127 * e.g. with __attribute__((constructor(prio))).
128 *
129 * Lower priority runs before higher priority. Priorities 0-100 are reserved.
130 */
131#ifdef CC_CONF_CONSTRUCTOR
132#define CC_CONSTRUCTOR(prio) CC_CONF_CONSTRUCTOR(prio)
133#else
134#define CC_CONSTRUCTOR(prio)
135#endif /* CC_CONF_CONSTRUCTOR */
136
137/**
138 * Configure if the C compiler supports marking functions as destructors
139 * e.g. with __attribute__((destructor(prio))).
140 *
141 * Lower priority runs before higher priority. Priorities 0-100 are reserved.
142 */
143#ifdef CC_CONF_DESTRUCTOR
144#define CC_DESTRUCTOR(prio) CC_CONF_DESTRUCTOR(prio)
145#else
146#define CC_DESTRUCTOR(prio)
147#endif /* CC_CONF_DESTRUCTOR */
148
149/**
150 * Configure if the C compiler supports marking functions as deprecated
151 * e.g. with __attribute__((deprecated))
152 */
153#ifdef CC_CONF_DEPRECATED
154#define CC_DEPRECATED(msg) CC_CONF_DEPRECATED(msg)
155#else
156#define CC_DEPRECATED(msg)
157#endif /* CC_CONF_DEPRECATED */
158
159/** \def CC_ACCESS_NOW(x)
160 * This macro ensures that the access to a non-volatile variable can
161 * not be reordered or optimized by the compiler.
162 * See also https://lwn.net/Articles/508991/ - In Linux the macro is
163 * called ACCESS_ONCE
164 * The type must be passed, because the typeof-operator is a gcc
165 * extension
166 */
167
168#define CC_ACCESS_NOW(type, variable) (*(volatile type *)&(variable))
169
170#ifndef NULL
171#define NULL 0
172#endif /* NULL */
173
174#ifndef MAX
175#define MAX(n, m) (((n) < (m)) ? (m) : (n))
176#endif
177
178#ifndef MIN
179#define MIN(n, m) (((n) < (m)) ? (n) : (m))
180#endif
181
182#ifndef ABS
183#define ABS(n) (((n) < 0) ? -(n) : (n))
184#endif
185
186#ifndef BOUND
187#define BOUND(a, minimum, maximum) MIN(MAX(a, minimum), maximum)
188#endif
189
190
191#define CC_CONCAT2(s1, s2) s1##s2
192/**
193 * A C preprocessing macro for concatenating two preprocessor tokens.
194 *
195 * We need use two macros (CC_CONCAT and CC_CONCAT2) in order to allow
196 * concatenation of two \#defined macros.
197 */
198#define CC_CONCAT(s1, s2) CC_CONCAT2(s1, s2)
199#define CC_CONCAT_EXT_2(s1, s2) CC_CONCAT2(s1, s2)
200
201/**
202 * A C preprocessing macro for concatenating three preprocessor tokens.
203 */
204#define CC_CONCAT3(s1, s2, s3) s1##s2##s3
205#define CC_CONCAT_EXT_3(s1, s2, s3) CC_CONCAT3(s1, s2, s3)
206
207/* Provide static_assert macro in C11-C17. */
208#if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L && \
209 !defined __cplusplus
210#ifndef static_assert
211#define static_assert _Static_assert
212#endif
213#endif
214
215#endif /* CC_H_ */