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
Copyright (C) 2024, 2025 Donald Haase
8
9
Based loosely around some stuff in BSD's sys/cdefs.h
10
*/
11
12
/** \file kos/cdefs.h
13
\brief Various common macros used throughout the codebase
14
\ingroup system
15
16
This file contains various convenience macros. Mostly compiler
17
__attribute__ directives, as well as other language defines, and
18
useful language extensions.
19
20
\author Megan Potter
21
\author Lawrence Sebald
22
\author Falco Girgis
23
\author Donald Haase
24
*/
25
26
#ifndef __KOS_CDEFS_H
27
#define __KOS_CDEFS_H
28
29
#include <sys/cdefs.h>
30
31
/* Check GCC version */
32
#if __GNUC__ < 9
33
# warning Your GCC is too old. This will probably not work right.
34
#endif
35
36
/** \defgroup system_attributes Function Attribute Defines
37
\brief Definitions for builtin attributes and compiler directives
38
\ingroup system
39
40
This group contains definitions of various __attribute__ directives in
41
shorter forms for use in programs. These typically aid in optimizations
42
or provide the compiler with extra information about a symbol.
43
44
@{
45
*/
46
47
#ifndef __noreturn
48
/** \brief Identify a function that will never return. */
49
#define __noreturn __attribute__((__noreturn__))
50
#endif
51
52
#ifndef __deprecated
53
/** \brief Mark something as deprecated.
54
This should be used to warn users that a function/type/etc will be removed
55
in a future version of KOS. */
56
#define __deprecated __attribute__((deprecated))
57
#endif
58
59
#ifndef __depr
60
/** \brief Mark something as deprecated, with an informative message.
61
This should be used to warn users that a function/type/etc will be removed
62
in a future version of KOS and to suggest an alternative that they can use
63
instead.
64
\param m A string literal that is included with the warning message
65
at compile time. */
66
#define __depr(m) __attribute__((deprecated(m)))
67
#endif
68
69
#if __GNUC__ >= 7
70
/** \brief Identify a case statement that is expected to fall through to the
71
statement underneath it. */
72
#define __fallthrough __attribute__((__fallthrough__))
73
#else
74
#define __fallthrough
/* Fall through */
75
#endif
76
77
/** @} */
78
79
/** \defgroup system_compat Language Compatibility Defines
80
\brief Definitions for language features
81
\ingroup system
82
83
This group contains definitions to help retain some older language
84
backwards compatibility for external software linking into KOS.
85
86
@{
87
*/
88
89
/* GCC macros for special cases */
90
/* #if __GNUC__ == */
91
92
#ifndef __RESTRICT
93
#if (__STDC_VERSION__ >= 199901L)
94
#define __RESTRICT restrict
95
#elif defined(__GNUC__) || defined(__GNUG__)
96
#define __RESTRICT __restrict__
97
#else
/* < C99 and not GCC */
98
#define __RESTRICT
99
#endif
100
#endif
/* !__RESTRICT */
101
102
#ifndef __GNUC__
103
#define __extension__
104
#endif
105
106
#ifndef __GNUC_STDC_INLINE__
107
#define inline __inline__
108
#endif
109
110
/** @} */
111
112
/** \defgroup system_helpers Helper Macros
113
\brief General useful language macros
114
\ingroup system
115
116
This group contains definitions to help give robust solutions
117
to common code patterns.
118
119
@{
120
*/
121
122
/** \brief Assert a build-time dependency.
123
124
Your compiler will fail if the condition isn't true, or can't be evaluated
125
by the compiler. This can only be used within a function.
126
127
Example:
128
\#include <stddef.h>
129
...
130
static char *foo_to_char(struct foo *foo)
131
{
132
// This code needs string to be at start of foo.
133
__build_assert(offsetof(struct foo, string) == 0);
134
return (char *)foo;
135
}
136
137
\param cond The compile-time condition which must be true.
138
139
\sa __build_assert_or_zero
140
*/
141
#define __build_assert(cond) \
142
do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
143
144
/** \brief Assert a build-time dependency.
145
146
Your compiler will fail if the condition isn't true, or can't be evaluated
147
by the compiler. This can be used in an expression: its value is "0".
148
149
Example:
150
\#define foo_to_char(foo) \
151
((char *)(foo) \
152
+ __build_assert_or_zero(offsetof(struct foo, string) == 0))
153
154
\param cond The compile-time condition which must be true.
155
156
\sa __build_assert
157
*/
158
#define __build_assert_or_zero(cond) \
159
(sizeof(char [1 - 2*!(cond)]) - 1)
160
161
/** \brief Get the number of elements in a visible array.
162
163
This does not work on pointers, or arrays declared as [], or
164
function parameters. With correct compiler support, such usage
165
will cause a build error (\see __build_assert).
166
167
\param arr The array whose size you want.
168
169
*/
170
#define __array_size(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
171
172
/* Two gcc extensions.
173
* &a[0] degrades to a pointer: a different type from an array */
174
#define _array_size_chk(arr) \
175
__build_assert_or_zero(!__builtin_types_compatible_p(typeof(arr), \
176
typeof(&(arr)[0])))
177
178
/** \brief Create a string from the argument.
179
180
\param arg The text to stringify.
181
*/
182
#define __stringify(arg) ""#arg
183
184
/** \brief Check if a macro is defined to 1.
185
186
\param macro The macro to check
187
\return 1 if the macro is defined to 1, 0 otherwise.
188
*/
189
#define __is_defined(macro) !__builtin_strcmp(__stringify(macro), "1")
190
191
/** @} */
192
193
#endif
/* __KOS_CDEFS_H */
include
kos
cdefs.h
Generated by
1.12.0