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 Definitions for builtin attributes and compiler directives
13 \ingroup system_macros
14
15 This file contains definitions of various __attribute__ directives in
16 shorter forms for use in programs. These typically aid in optimizations
17 or provide the compiler with extra information about a symbol.
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/** \defgroup system_macros Macros
30 \brief Various common macros used throughout the codebase
31 \ingroup system
32
33 @{
34*/
35
36/* Check GCC version */
37#if __GNUC__ <= 3
38# warning Your GCC is too old. This will probably not work right.
39#endif
40
41/* Special function/variable attributes */
42
43#ifndef __noreturn
44/** \brief Identify a function that will never return. */
45#define __noreturn __attribute__((__noreturn__))
46#endif
47
48#ifndef __pure
49/** \brief Identify a function that has no side effects other than its return,
50 and only uses its arguments for any work. */
51#define __pure __attribute__((__const__))
52#endif
53
54#ifndef __unused
55/** \brief Identify a function or variable that may be unused. */
56#define __unused __attribute__((__unused__))
57#endif
58
59#ifndef __used
60/** \brief Prevent a symbol from being removed from the binary. */
61#define __used __attribute__((used))
62#endif
63
64#ifndef __weak
65/** \brief Identify a function or variable that may be overridden by another symbol. */
66#define __weak __attribute__((weak))
67#endif
68
69#ifndef __packed
70/** \brief Force a structure, enum, or other type to be packed as small as possible. */
71#define __packed __attribute__((packed))
72#endif
73
74#ifndef __dead2
75/** \brief Alias for \ref __noreturn. For BSD compatibility. */
76#define __dead2 __noreturn /* BSD compat */
77#endif
78
79#ifndef __pure2
80/** \brief Alias for \ref __pure. For BSD compatibility. */
81#define __pure2 __pure /* ditto */
82#endif
83
84#ifndef __likely
85/** \brief Directive to inform the compiler the condition is in the likely path.
86
87 This can be used around conditionals or loops to help inform the
88 compiler which path to optimize for as the common-case.
89
90 \param exp Boolean expression which expected to be true.
91
92 \sa __unlikely()
93*/
94#define __likely(exp) __builtin_expect(!!(exp), 1)
95#endif
96
97#ifndef __unlikely
98/** \brief Directive to inform the compiler the condition is in the unlikely path.
99
100 This can be used around conditionals or loops to help inform the
101 compiler which path to optimize against as the infrequent-case.
102
103 \param exp Boolean expression which is expected to be false.
104
105 \sa __likely()
106*/
107#define __unlikely(exp) __builtin_expect(!!(exp), 0)
108#endif
109
110#ifndef __deprecated
111/** \brief Mark something as deprecated.
112 This should be used to warn users that a function/type/etc will be removed
113 in a future version of KOS. */
114#define __deprecated __attribute__((deprecated))
115#endif
116
117#ifndef __depr
118/** \brief Mark something as deprecated, with an informative message.
119 This should be used to warn users that a function/type/etc will be removed
120 in a future version of KOS and to suggest an alternative that they can use
121 instead.
122 \param m A string literal that is included with the warning message
123 at compile time. */
124#define __depr(m) __attribute__((deprecated(m)))
125#endif
126
127/* Printf/Scanf-like declaration */
128#ifndef __printflike
129/** \brief Identify a function as accepting formatting like printf().
130
131 Using this macro allows GCC to typecheck calls to printf-like functions,
132 which can aid in finding mistakes.
133
134 \param fmtarg The argument number (1-based) of the format string.
135 \param firstvararg The argument number of the first vararg (the ...).
136*/
137#define __printflike(fmtarg, firstvararg) \
138 __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
139#endif
140
141#ifndef __scanflike
142/** \brief Identify a function as accepting formatting like scanf().
143
144 Using this macro allows GCC to typecheck calls to scanf-like functions,
145 which can aid in finding mistakes.
146
147 \param fmtarg The argument number (1-based) of the format string.
148 \param firstvararg The argument number of the first vararg (the ...).
149*/
150#define __scanflike(fmtarg, firstvararg) \
151 __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
152#endif
153
154#if __GNUC__ >= 7
155/** \brief Identify a case statement that is expected to fall through to the
156 statement underneath it. */
157#define __fallthrough __attribute__((__fallthrough__))
158#else
159#define __fallthrough /* Fall through */
160#endif
161
162#ifndef __always_inline
163/** \brief Ask the compiler to \a always inline a given function. */
164#define __always_inline inline __attribute__((__always_inline__))
165#endif
166
167#ifndef __no_inline
168/** \brief Ask the compiler to \a never inline a given function. */
169#define __no_inline __attribute__((__noinline__))
170#endif
171
172/* GCC macros for special cases */
173/* #if __GNUC__ == */
174
175#ifndef __RESTRICT
176#if (__STDC_VERSION__ >= 199901L)
177#define __RESTRICT restrict
178#elif defined(__GNUC__) || defined(__GNUG__)
179#define __RESTRICT __restrict__
180#else /* < C99 and not GCC */
181#define __RESTRICT
182#endif
183#endif /* !__RESTRICT */
184
185#ifndef __GNUC__
186#define __extension__
187#endif
188
189#ifndef __GNUC_STDC_INLINE__
190#define inline __inline__
191#endif
192
193/** @} */
194
195#endif /* __KOS_CDEFS_H */