KallistiOS
git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
cdefs.h
Go to the documentation of this file.
1
/* KallistiOS ##version##
2
3
kos/cdefs.h
4
Copyright (C) 2002, 2004 Megan Potter
5
Copyright (C) 2020, 2023 Lawrence Sebald
6
Copyright (C) 2023 Falco Girgis
7
8
Based loosely around some stuff in BSD's sys/cdefs.h
9
*/
10
11
/** \file kos/cdefs.h
12
\brief Various common macros used throughout the codebase
13
\ingroup system
14
15
This file contains various convenience macros. Mostly compiler
16
__attribute__ directives, as well as other language defines, and
17
useful language extensions.
18
19
\author Megan Potter
20
\author Lawrence Sebald
21
\author Falco Girgis
22
*/
23
24
#ifndef __KOS_CDEFS_H
25
#define __KOS_CDEFS_H
26
27
#include <sys/cdefs.h>
28
29
/* Check GCC version */
30
#if __GNUC__ <= 3
31
# warning Your GCC is too old. This will probably not work right.
32
#endif
33
34
/** \defgroup system_attributes Compiler Attributes
35
\brief Definitions for builtin attributes and compiler directives
36
\ingroup system
37
38
This group contains definitions of various __attribute__ directives in
39
shorter forms for use in programs. These typically aid in optimizations
40
or provide the compiler with extra information about a symbol.
41
42
@{
43
*/
44
45
#ifndef __noreturn
46
/** \brief Identify a function that will never return. */
47
#define __noreturn __attribute__((__noreturn__))
48
#endif
49
50
#ifndef __pure
51
/** \brief Identify a function that has no side effects other than its return,
52
and only uses its arguments for any work. */
53
#define __pure __attribute__((__const__))
54
#endif
55
56
#ifndef __unused
57
/** \brief Identify a function or variable that may be unused. */
58
#define __unused __attribute__((__unused__))
59
#endif
60
61
#ifndef __used
62
/** \brief Prevent a symbol from being removed from the binary. */
63
#define __used __attribute__((used))
64
#endif
65
66
#ifndef __weak
67
/** \brief Identify a function or variable that may be overridden by another symbol. */
68
#define __weak __attribute__((weak))
69
#endif
70
71
#ifndef __packed
72
/** \brief Force a structure, enum, or other type to be packed as small as possible. */
73
#define __packed __attribute__((packed))
74
#endif
75
76
#ifndef __dead2
77
/** \brief Alias for \ref __noreturn. For BSD compatibility. */
78
#define __dead2 __noreturn
/* BSD compat */
79
#endif
80
81
#ifndef __pure2
82
/** \brief Alias for \ref __pure. For BSD compatibility. */
83
#define __pure2 __pure
/* ditto */
84
#endif
85
86
#ifndef __likely
87
/** \brief Directive to inform the compiler the condition is in the likely path.
88
89
This can be used around conditionals or loops to help inform the
90
compiler which path to optimize for as the common-case.
91
92
\param exp Boolean expression which expected to be true.
93
94
\sa __unlikely()
95
*/
96
#define __likely(exp) __builtin_expect(!!(exp), 1)
97
#endif
98
99
#ifndef __unlikely
100
/** \brief Directive to inform the compiler the condition is in the unlikely path.
101
102
This can be used around conditionals or loops to help inform the
103
compiler which path to optimize against as the infrequent-case.
104
105
\param exp Boolean expression which is expected to be false.
106
107
\sa __likely()
108
*/
109
#define __unlikely(exp) __builtin_expect(!!(exp), 0)
110
#endif
111
112
#ifndef __deprecated
113
/** \brief Mark something as deprecated.
114
This should be used to warn users that a function/type/etc will be removed
115
in a future version of KOS. */
116
#define __deprecated __attribute__((deprecated))
117
#endif
118
119
#ifndef __depr
120
/** \brief Mark something as deprecated, with an informative message.
121
This should be used to warn users that a function/type/etc will be removed
122
in a future version of KOS and to suggest an alternative that they can use
123
instead.
124
\param m A string literal that is included with the warning message
125
at compile time. */
126
#define __depr(m) __attribute__((deprecated(m)))
127
#endif
128
129
/* Printf/Scanf-like declaration */
130
#ifndef __printflike
131
/** \brief Identify a function as accepting formatting like printf().
132
133
Using this macro allows GCC to typecheck calls to printf-like functions,
134
which can aid in finding mistakes.
135
136
\param fmtarg The argument number (1-based) of the format string.
137
\param firstvararg The argument number of the first vararg (the ...).
138
*/
139
#define __printflike(fmtarg, firstvararg) \
140
__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
141
#endif
142
143
#ifndef __scanflike
144
/** \brief Identify a function as accepting formatting like scanf().
145
146
Using this macro allows GCC to typecheck calls to scanf-like functions,
147
which can aid in finding mistakes.
148
149
\param fmtarg The argument number (1-based) of the format string.
150
\param firstvararg The argument number of the first vararg (the ...).
151
*/
152
#define __scanflike(fmtarg, firstvararg) \
153
__attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
154
#endif
155
156
#if __GNUC__ >= 7
157
/** \brief Identify a case statement that is expected to fall through to the
158
statement underneath it. */
159
#define __fallthrough __attribute__((__fallthrough__))
160
#else
161
#define __fallthrough
/* Fall through */
162
#endif
163
164
#ifndef __always_inline
165
/** \brief Ask the compiler to \a always inline a given function. */
166
#define __always_inline inline __attribute__((__always_inline__))
167
#endif
168
169
#ifndef __no_inline
170
/** \brief Ask the compiler to \a never inline a given function. */
171
#define __no_inline __attribute__((__noinline__))
172
#endif
173
174
/** @} */
175
176
/** \defgroup system_compat Language Compat
177
\brief Definitions for language features
178
\ingroup system
179
180
This group contains definitions to help retain some older language
181
backwards compatibility for external software linking into KOS.
182
183
@{
184
*/
185
186
/* GCC macros for special cases */
187
/* #if __GNUC__ == */
188
189
#ifndef __RESTRICT
190
#if (__STDC_VERSION__ >= 199901L)
191
#define __RESTRICT restrict
192
#elif defined(__GNUC__) || defined(__GNUG__)
193
#define __RESTRICT __restrict__
194
#else
/* < C99 and not GCC */
195
#define __RESTRICT
196
#endif
197
#endif
/* !__RESTRICT */
198
199
#ifndef __GNUC__
200
#define __extension__
201
#endif
202
203
#ifndef __GNUC_STDC_INLINE__
204
#define inline __inline__
205
#endif
206
207
/** @} */
208
209
/** \defgroup system_helpers Utility Macros
210
\brief General useful language macros
211
\ingroup system
212
213
This group contains definitions to help give robust solutions
214
to common code patterns.
215
216
@{
217
*/
218
219
/** \brief Assert a build-time dependency.
220
221
Your compiler will fail if the condition isn't true, or can't be evaluated
222
by the compiler. This can only be used within a function.
223
224
Example:
225
\code{.c}
226
#include <stddef.h>
227
...
228
static char *foo_to_char(struct foo *foo)
229
{
230
// This code needs string to be at start of foo.
231
__build_assert(offsetof(struct foo, string) == 0);
232
return (char *)foo;
233
}
234
\endcode
235
236
\param cond The compile-time condition which must be true.
237
238
\sa __build_assert_or_zero
239
*/
240
#define __build_assert(cond) \
241
do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
242
243
/** \brief Assert a build-time dependency.
244
245
Your compiler will fail if the condition isn't true, or can't be evaluated
246
by the compiler. This can be used in an expression: its value is "0".
247
248
Example:
249
\code{.c}
250
#define foo_to_char(foo) \
251
((char *)(foo) \
252
+ __build_assert_or_zero(offsetof(struct foo, string) == 0))
253
\endcode
254
255
\param cond The compile-time condition which must be true.
256
257
\sa __build_assert
258
*/
259
#define __build_assert_or_zero(cond) \
260
(sizeof(char [1 - 2*!(cond)]) - 1)
261
262
/** \brief Get the number of elements in a visible array.
263
264
This does not work on pointers, or arrays declared as [], or
265
function parameters. With correct compiler support, such usage
266
will cause a build error (\see __build_assert).
267
268
\param arr The array whose size you want.
269
270
*/
271
#define __array_size(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
272
273
/* Helper for __array_size's type check */
274
#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
275
/* Two gcc extensions.
276
* &a[0] degrades to a pointer: a different type from an array */
277
#define _array_size_chk(arr) \
278
__build_assert_or_zero(!__builtin_types_compatible_p(typeof(arr), \
279
typeof(&(arr)[0])))
280
#else
281
#define _array_size_chk(arr) 0
282
#endif
283
284
/** @} */
285
286
#endif
/* __KOS_CDEFS_H */
include
kos
cdefs.h
Generated by
1.11.0