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#endif /* __GNUC__ */
77
78/**
79 * Configure if the C compiler supports taking hints from the user about
80 * invariants, e.g. with __attribute__((__assume__(hint))).
81 */
82#ifdef CC_CONF_ASSUME
83#define ASSUME(c) CC_CONF_ASSUME(c)
84#else
85#define ASSUME(c)
86#endif /* CC_CONF_ASSUME */
87
88#ifdef CC_CONF_ALIGN
89#define CC_ALIGN(n) CC_CONF_ALIGN(n)
90#endif /* CC_CONF_ALIGN */
91
92/**
93 * Configure if the C compiler supports functions that are not meant to return
94 * e.g. with __attribute__((__noreturn__))
95 */
96#ifdef CC_CONF_NORETURN
97#define CC_NORETURN CC_CONF_NORETURN
98#else
99#define CC_NORETURN
100#endif /* CC_CONF_NORETURN */
101
102/**
103 * Configure if the C compiler supports marking functions as constructors
104 * e.g. with __attribute__((constructor(prio))).
105 *
106 * Lower priority runs before higher priority. Priorities 0-100 are reserved.
107 */
108#ifdef CC_CONF_CONSTRUCTOR
109#define CC_CONSTRUCTOR(prio) CC_CONF_CONSTRUCTOR(prio)
110#else
111#define CC_CONSTRUCTOR(prio)
112#endif /* CC_CONF_CONSTRUCTOR */
113
114/**
115 * Configure if the C compiler supports marking functions as destructors
116 * e.g. with __attribute__((destructor(prio))).
117 *
118 * Lower priority runs before higher priority. Priorities 0-100 are reserved.
119 */
120#ifdef CC_CONF_DESTRUCTOR
121#define CC_DESTRUCTOR(prio) CC_CONF_DESTRUCTOR(prio)
122#else
123#define CC_DESTRUCTOR(prio)
124#endif /* CC_CONF_DESTRUCTOR */
125
126/**
127 * Configure if the C compiler supports marking functions as deprecated
128 * e.g. with __attribute__((deprecated))
129 */
130#ifdef CC_CONF_DEPRECATED
131#define CC_DEPRECATED(msg) CC_CONF_DEPRECATED(msg)
132#else
133#define CC_DEPRECATED(msg)
134#endif /* CC_CONF_DEPRECATED */
135
136/** \def CC_ACCESS_NOW(x)
137 * This macro ensures that the access to a non-volatile variable can
138 * not be reordered or optimized by the compiler.
139 * See also https://lwn.net/Articles/508991/ - In Linux the macro is
140 * called ACCESS_ONCE
141 * The type must be passed, because the typeof-operator is a gcc
142 * extension
143 */
144
145#define CC_ACCESS_NOW(type, variable) (*(volatile type *)&(variable))
146
147#ifndef NULL
148#define NULL 0
149#endif /* NULL */
150
151#ifndef MAX
152#define MAX(n, m) (((n) < (m)) ? (m) : (n))
153#endif
154
155#ifndef MIN
156#define MIN(n, m) (((n) < (m)) ? (n) : (m))
157#endif
158
159#ifndef ABS
160#define ABS(n) (((n) < 0) ? -(n) : (n))
161#endif
162
163#ifndef BOUND
164#define BOUND(a, minimum, maximum) MIN(MAX(a, minimum), maximum)
165#endif
166
167
168#define CC_CONCAT2(s1, s2) s1##s2
169/**
170 * A C preprocessing macro for concatenating two preprocessor tokens.
171 *
172 * We need use two macros (CC_CONCAT and CC_CONCAT2) in order to allow
173 * concatenation of two \#defined macros.
174 */
175#define CC_CONCAT(s1, s2) CC_CONCAT2(s1, s2)
176#define CC_CONCAT_EXT_2(s1, s2) CC_CONCAT2(s1, s2)
177
178/**
179 * A C preprocessing macro for concatenating three preprocessor tokens.
180 */
181#define CC_CONCAT3(s1, s2, s3) s1##s2##s3
182#define CC_CONCAT_EXT_3(s1, s2, s3) CC_CONCAT3(s1, s2, s3)
183
184/* Provide static_assert macro in C11-C17. */
185#if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L && \
186 !defined __cplusplus
187#ifndef static_assert
188#define static_assert _Static_assert
189#endif
190#endif
191
192#endif /* CC_H_ */