CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1#pragma once
2
5
157
158#if defined(_MSC_VER)
159
160#include <assert.h>
161#include <intrin.h>
162
163#define _COMPILER_MSVC 1
164
165// C11 thread local
166#define _Thread_local __declspec(thread)
167
168// C11 static assert
169#define _Static_assert static_assert
170
171// Nullability qualifiers
172// SAL is much more powerful than these
173#define _Nonnull
174#define _Nullable
175#define _Null_unspecified
176
177// Pure functions
178#define _Pure
179
180#define _Analysis_noreturn _Analysis_noreturn_
181
182// We use inline functions for metaprogramming and really, REALLY want them
183// to be inlined
184#define _meta_inline __forceinline
185#define _no_inline __declspec(noinline)
186#pragma warning(disable : 6255)
187#define stackAlloc(sz) _alloca(sz)
188
189// Disable "nonstandard extension used: zero-sized array in struct/union" warning.
190// We only use this in C99 code, where it is perfectly valid. But because test_runner
191// is linked with a C++ file for the compatibility tests, we get this incorrect
192// warning.
193#pragma warning(disable : 4200)
194
195// Sometimes things need to be aligned precisely
196#define alignMem(bytes) __declspec(align(bytes))
197
198// Hint that some functions never return
199#define _no_return __declspec(noreturn)
200
201#if defined(_WIN32)
202#define _PLATFORM_WIN 1
203#define _PLATFORM_STR "win"
204#else
205// this should be impossible
206#error Unsupported operating system ???
207#endif
208
209#if defined(_M_IX86)
210#define _ARCH_X86 1
211#define _ARCH_STR "x86"
212#define _32BIT 1
213#elif defined(_M_X64)
214#define _ARCH_X64 1
215#define _ARCH_STR "x64"
216#define _64BIT 1
217#else
218#error Unsupported architecture
219#endif
220
221#elif defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 4)
222
223#if defined(__clang__)
224#define _COMPILER_CLANG 1
225
226// Nullability qualifiers
227#pragma clang diagnostic ignored "-Wnullability-completeness"
228
229#elif defined(__GNUC__)
230#define _COMPILER_GCC 1
231
232// Nullability qualifiers
233#define _Nonnull
234#define _Nullable
235#define _Null_unspecified
236
237#endif
238
239// Pure functions
240#define _Pure __attribute__((pure))
241
242#if defined(__clang_analyzer__)
243#define _Analysis_noreturn __attribute__((noreturn))
244#else
245#define _Analysis_noreturn
246#endif
247
248// C11 thread local is already supported
249
250// We use inline functions for metaprogramming and really, REALLY want them
251// to be inlined
252#define _meta_inline __attribute__((always_inline)) inline
253#define _no_inline __attribute__((noinline))
254#define stackAlloc(sz) alloca(sz)
255
256// Sometimes things need to be aligned precisely
257#define alignMem(bytes) __attribute__((aligned(bytes)))
258
259// Hint that some functions never return
260#define _no_return __attribute__((noreturn))
261
262#if defined(__linux__)
263#include <alloca.h>
264#define _PLATFORM_UNIX 1
265#define _PLATFORM_LINUX 1
266#define _PLATFORM_STR "linux"
267#elif defined(__FreeBSD__)
268#define _PLATFORM_UNIX 1
269#define _PLATFORM_FBSD 1
270#define _PLATFORM_STR "fbsd"
271#elif defined(__EMSCRIPTEN__)
272#define _PLATFORM_WASM 1
273#define _PLATFORM_STR "wasm"
274#else
275#error Unsupported operating system
276#endif
277
278#if defined(__i386__) || defined(__i386) || defined(_X86_)
279#define _ARCH_X86 1
280#define _ARCH_STR "x86"
281#define _32BIT 1
282#elif defined(__x86_64__)
283#define _ARCH_X64 1
284#define _ARCH_STR "x64"
285#define _64BIT 1
286#elif defined(__aarch64__)
287#define _ARCH_ARM64 1
288#define _ARCH_STR "arm64"
289#define _64BIT 1
290#elif defined(__EMSCRIPTEN__)
291#define _ARCH_WASM 1
292#define _ARCH_STR "wasm32"
293#define _32BIT 1
294#else
295#error Unsupported architecture
296#endif
297
298#else
299#error Unsupported compiler
300#endif
301
302#if defined(_32BIT)
303#define _word_align alignMem(4)
304#elif defined(_64BIT)
305#define _word_align alignMem(8)
306#else
307#error Unsupported architecture
308#endif
309
310#define _PLATFORM_ARCH_STR (_PLATFORM_STR "-" _ARCH_STR)
311
Runtime assertion macros and failure handling.