Debian Codegraph @

.linked/mbsdcc.h

raw symbols SCIP
1 /*-
2 * MirBSD ISO C compiler toolkit (macros for mbsdint.h and others)
3 *
4 * © mirabilos Ⓕ CC0 or The MirOS Licence (MirBSD)
5 */
6
7 #ifndef SYSKERN_MBSDCC_H
8 #define SYSKERN_MBSDCC_H "$MirOS: src/bin/pax/.linked/mbsdcc.h,v 1.10 2024/07/25 02:29:43 tg Exp $"
9
10 /*
11 * Note: this header uses the SIZE_MAX (or similar) definitions
12 * from <basetsd.h> or <limits.h> if present, so either include
13 * these before this header or include "mbsdint.h", which fixes
14 * up the corresponding definition.
15 *
16 * Place the inclusion of this after <sys/cdefs.h> or something
17 * that pulls it, for example <sys/types.h>.
18 *
19 * Furthermore, either include <stdlib.h> for abort(3), or pro‐
20 * vide an mbccABEND macro (preferred).
21 */
22
23 #if !defined(_KERNEL) && !defined(_STANDALONE)
24 #include <stddef.h>
25 #else
26 #include <sys/stddef.h>
27 #endif
28
29 /* mbccABEND should be provided by the user, called by mbsd*.h only */
30 #ifndef mbccABEND
31 #define mbccABEND(reasonstr) abort()
32 #endif
33
34 /* monkey-patch known-bad offsetof versions */
35 #if (defined(__KLIBC__) || defined(__dietlibc__)) && \
36 ((defined(__GNUC__) && (__GNUC__ > 3)) || defined(__NWCC__))
37 #undef offsetof
38 #define offsetof(struc,memb) __builtin_offsetof(struc, memb)
39 #endif
40
41 /* should be defined by <sys/cdefs.h> already */
42 #ifndef __predict_true
43 #if defined(__GNUC__) && (__GNUC__ >= 3) /* 2.96, but keep it simple here */
44 #define __predict_true(exp) __builtin_expect(!!(exp), 1)
45 #define __predict_false(exp) __builtin_expect(!!(exp), 0)
46 #else
47 #define __predict_true(exp) (!!(exp))
48 #define __predict_false(exp) (!!(exp))
49 #endif /* !GCC 3.x */
50 #endif /* ndef(__predict_true) */
51
52 /* mbsdint.h replaces this with mbiSIZE_MAX */
53 #undef mbccSIZE_MAX
54 #if defined(SIZE_MAX)
55 #define mbccSIZE_MAX ((size_t)(SIZE_MAX)
56 #elif defined(SIZE_T_MAX)
57 #define mbccSIZE_MAX ((size_t)(SIZE_T_MAX)
58 #elif defined(MAXSIZE_T)
59 #define mbccSIZE_MAX ((size_t)(MAXSIZE_T)
60 #else
61 #define mbccSIZE_MAX ((size_t)~(size_t)0)
62 #endif
63
64 #ifdef __cplusplus
65 #define mbcextern extern "C"
66 /* for headers (BSD __BEGIN_DECLS/__END_DECLS) */
67 #define mbcextern_beg extern "C" {
68 #define mbcextern_end }
69 #else
70 #define mbcextern extern
71 #define mbcextern_beg
72 #define mbcextern_end
73 #endif
74
75 /* compiler warning pragmas, NOP for other compilers */
76 #ifdef _MSC_VER
77 /* push, suppress space-separated listed warning numbers */
78 #define mbmscWd(d) __pragma(warning(push)) \
79 __pragma(warning(disable: d))
80 /* suppress for only the next line */
81 #define mbmscWs(d) __pragma(warning(suppress: d))
82 /* pop from mbmscWd */
83 #define mbmscWpop __pragma(warning(pop))
84 #else
85 #define mbmscWd(d)
86 #define mbmscWs(d)
87 #define mbmscWpop
88 #endif
89
90 /* in-expression compile-time check evaluating to 0 */
91 #ifdef __cplusplus
92 template<bool> struct mbccChkExpr_sa;
93 template<> struct mbccChkExpr_sa<true> { typedef int Type; };
94 #define mbccChkExpr(test) (static_cast<mbccChkExpr_sa<!!(0+(test))>::Type>(0))
95 #else
96 #define mbccChkExpr(test) mbmscWs(4116) \
97 (sizeof(struct { unsigned int (mbccChkExpr):((0+(test)) ? 1 : -1); }) * 0)
98 #endif
99
100 /* ensure value x is a constant expression */
101 #ifdef __cplusplus
102 template<bool,bool> struct mbccCEX_sa;
103 template<> struct mbccCEX_sa<true,false> { typedef int Type; };
104 template<> struct mbccCEX_sa<false,true> { typedef int Type; };
105 #define mbccCEX(x) (static_cast<mbccCEX_sa<!!(0+(x)), !(0+(x))>::Type>(0) + (x))
106 #else
107 #define mbccCEX(x) mbmscWs(4116) \
108 (sizeof(struct { unsigned int (mbccCEX):(((0+(x)) && 1) + 1); }) * 0 + (x))
109 #endif
110
111 /* flexible array member */
112 #if defined(__cplusplus)
113 #ifdef __cpp_flexible_array_members
114 #define mbccFAMslot(type,memb) type memb[]
115 #elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
116 #define mbccFAMslot(type,memb) type memb[0] mbmscWs(4200 4820)
117 #else
118 #define mbccFAMslot(type,memb) type memb[1] mbmscWs(4820)
119 #endif
120 #elif (defined(__STDC_VERSION__) && ((__STDC_VERSION__) >= 199901L))
121 #define mbccFAMslot(type,memb) type memb[]
122 #elif defined(__GNUC__)
123 #define mbccFAMslot(type,memb) type memb[0] mbmscWs(4200 4820)
124 #else
125 #define mbccFAMslot(type,memb) type memb[1] mbmscWs(4820) /* SOL */
126 #endif
127 #define mbccFAMsz_i(t,memb,sz) /*private*/ (size_t)(offsetof(t, memb) + (size_t)sz)
128 /* compile-time constant */
129 #define mbccFAMSZ(struc,memb,sz) ((size_t)( \
130 mbccChkExpr(mbccSIZE_MAX - sizeof(struc) > mbccCEX((size_t)(sz))) + \
131 (mbccFAMsz_i(struc, memb, (sz)) > sizeof(struc) ? \
132 mbccFAMsz_i(struc, memb, (sz)) : sizeof(struc))))
133 /* run-time */
134 #define mbccFAMsz(struc,memb,sz) ((size_t)( \
135 !(mbccSIZE_MAX - sizeof(struc) > (size_t)(sz)) ? \
136 (mbccABEND("mbccFAMsz: " mbccS(sz) " too large for " mbccS(struc) "." mbccS(memb)), 0) : \
137 (mbccFAMsz_i(struc, memb, (sz)) > sizeof(struc) ? \
138 mbccFAMsz_i(struc, memb, (sz)) : sizeof(struc))))
139 /* example:
140 *
141 * struct s {
142 * int type;
143 * mbccFAMslot(char, label); // like char label[…];
144 * };
145 * struct t {
146 * int count;
147 * mbccFAMslot(time_t, value);
148 * };
149 * union fixed_s {
150 * struct s s;
151 * char storage[mbccFAMSZ(struct s, label, 28)];
152 * };
153 * struct s *sp = malloc(mbccFAMsz(struct s, label, strlen(labelvar) + 1U));
154 * struct t *tp = malloc(mbccFAMsz(struct t, value, sizeof(time_t[cnt])));
155 * tp->count = cnt;
156 * struct s *np = malloc(mbccFAMSZ(struct s, label, sizeof("myname")));
157 * memcpy(np->label, "myname", sizeof("myname"));
158 * static union fixed_s preallocated;
159 * preallocated.s.label[0] = '\0';
160 * somefunc((struct s *)&preallocated);
161 */
162
163 /* field sizeof */
164 #if defined(__cplusplus)
165 /* TODO: add better way on CFrustFrust once it has one */
166 #define mbccFSZ(struc,memb) (sizeof(((struc *)0)->memb))
167 #elif (defined(__STDC_VERSION__) && ((__STDC_VERSION__) >= 199901L))
168 #define mbccFSZ(struc,memb) (sizeof(((struc){0}).memb))
169 #else
170 #define mbccFSZ(struc,memb) (sizeof(((struc *)0)->memb))
171 #endif
172
173 /* stringification with expansion */
174 #define mbccS(x) #x
175 #define mbccS2(x) mbccS(x)
176
177 /* compile-time assertions */
178 #undef mbccCTA
179 #if defined(__WATCOMC__) && !defined(__WATCOM_CPLUSPLUS__)
180 /* “Comparison result always 0” */
181 #define mbccCTA_wb _Pragma("warning 124 5")
182 /*XXX no pop so set to visible info */
183 #define mbccCTA_we _Pragma("warning 124 4")
184 #else
185 #define mbccCTA_wb /* nothing */
186 #define mbccCTA_we /* nothing */
187 #endif
188 #if defined(__cplusplus)
189 #ifdef __cpp_static_assert
190 #define mbccCTA(fldn,cond) static_assert(cond, mbccS(fldn))
191 #endif
192 #elif (defined(__STDC_VERSION__) && ((__STDC_VERSION__) >= 202311L))
193 #define mbccCTA(fldn,cond) static_assert(cond, mbccS(fldn))
194 #elif (defined(__STDC_VERSION__) && ((__STDC_VERSION__) >= 201112L))
195 #define mbccCTA(fldn,cond) _Static_assert(cond, mbccS(fldn))
196 #elif defined(__GNUC__) && \
197 ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
198 #define mbccCTA(fldn,cond) __extension__ _Static_assert(cond, mbccS(fldn))
199 #endif
200 #ifndef mbccCTA
201 /* single assertion for macros (with fldn prefixed, cond parenthesised) */
202 #define mbccCTA(fldn,cond) unsigned int fldn:(cond ? 1 : -1)
203 /* begin/end assertion block */
204 #define mbCTA_BEG(name) struct ctassert_ ## name { mbccCTA_wb \
205 int _ctabeg /* plus user semicolon */
206 #define mbCTA_END(name) int _ctaend; \
207 }; \
208 struct ctassert2 ## name { \
209 char t[sizeof(struct ctassert_ ## name) > 1U ? 1 : -1]; \
210 mbccCTA_we } /* semicolon provided by user */
211 #else
212 /* nothing, but syntax-check equally to manual compile-time assert macro */
213 #define mbCTA_BEG(name) struct ctassert_ ## name { mbccCTA_wb \
214 char t[2]; \
215 } /* semicolon provided by user */
216 #define mbCTA_END(name) struct ctassert2 ## name { \
217 char t[sizeof(struct ctassert_ ## name) > 1U ? 1 : -1]; \
218 mbccCTA_we } /* semicolon provided by user */
219 #endif
220 /* single assertion */
221 #define mbCTA(name,cond) mbccCTA(cta_ ## name, (cond))
222
223 /* nil pointer constant */
224 #if (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
225 (defined(__STDC_VERSION__) && ((__STDC_VERSION__) >= 202311L))
226 #define mbnil nullptr
227 #elif defined(__GNUG__)
228 #define mbnil __null
229 #elif defined(__cplusplus)
230 #define mbnil (size_t)0UL /* wtf, why? */
231 #else
232 #define mbnil (void *)0UL
233 #endif
234
235 /* præfix to avoid -Wunused-result when it really cannot be helped */
236 #ifdef _FORTIFY_SOURCE
237 /* workaround via https://stackoverflow.com/a/64407070/2171120 */
238 #define SHIKATANAI (void)!
239 #else
240 #define SHIKATANAI (void)
241 #endif
242
243 #endif /* !SYSKERN_MBSDCC_H */