libhatchet
Loading...
Searching...
No Matches
hxsettings.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
10
11#if !LIBHATCHET_VER
12#error #include <hx/libhatchet.h> instead.
13#endif
14
16#ifndef __has_include
17#define __has_include(x) 0
18#endif
20
21#if defined HX_DOXYGEN_PARSER
31#define HX_CPLUSPLUS 202302L // See Doxyfile.
32#elif defined __cplusplus
33#define HX_CPLUSPLUS __cplusplus
34#else
35// This is C. Using 0 here avoids warnings.
36#define HX_CPLUSPLUS 0
37#endif
38
39#if !defined HX_USE_INLINING_ATTR
42#define HX_USE_INLINING_ATTR 1
43#endif
44
45// -- Target settings for Doxygen ----------------------------------------------
46// See the Doxyfile. Run doxygen with no args.
47#if defined HX_DOXYGEN_PARSER
48
51#define HX_USE_THREADS 11
52
57#define HX_USE_LIBCXX 1
58
62#define hxbreakpoint() true
63
67#define hxrestrict
68
71#define hxattr_allocator(...)
72
75#define hxattr_assume(...) (void)0
76
78#define hxattr_cold
79
82#define hxattr_flatten
83
86#define hxattr_hot
87
89#define hxattr_nodiscard
90
93#define hxattr_noexcept
94
97#define hxattr_nonnull(...)
98
100#define hxattr_noinline
101
104#define hxattr_noreturn
105
108#define hxattr_printf(...)
109
112#define hxattr_weak
113
116#define hxattr_scanf(...)
117
120#define hxinline inline
121
122// -- Target settings for MSVC -------------------------------------------------
123// MSVC doesn't support C++'s feature test macros very well.
124#elif defined _MSC_VER
125
126#if defined __clang__
127#error Clang detected masquerading as MSVC - not supported due to intrinsic use.
128#endif
129
130#if !defined __cpp_exceptions && !defined _HAS_EXCEPTIONS
131#define _HAS_EXCEPTIONS 0
132#endif
133
134#if !defined HX_USE_THREADS
135#define HX_USE_THREADS 11
136#endif
137
138#if !defined HX_USE_LIBCXX
139#define HX_USE_LIBCXX 1
140#endif
141
142#define hxbreakpoint() (__debugbreak(),true)
143#define hxrestrict __restrict
144#define hxattr_allocator(...)
145#define hxattr_assume(condition_) __assume(condition_)
146#define hxattr_cold
147#define hxattr_flatten
148#define hxattr_hot
149#if HX_CPLUSPLUS
150#define hxattr_nodiscard [[nodiscard]]
151#else
152#define hxattr_nodiscard
153#endif
154#define hxattr_noexcept
155#define hxattr_nonnull(...)
156#define hxattr_noinline __declspec(noinline)
157#if HX_CPLUSPLUS
158#define hxattr_noreturn [[noreturn]]
159#else
160#define hxattr_noreturn
161#endif
162#define hxattr_printf(...)
163#define hxattr_scanf(...)
164// #define hxattr_weak __declspec(selectany) is not used as MSVC treats all
165// library objects as weak.
166#define hxattr_weak
167
168#if HX_USE_INLINING_ATTR
169#define hxinline inline __forceinline
170#else
171#define hxinline inline
172#endif
173
174// -- Target settings for Clang and GCC ----------------------------------------
175// Further compilers will require customization.
176#else // Assume gcc/clang.
177
178// hxthreads.hpp should work in C++11 with pthread.h. _POSIX_THREADS is the
179// correct way to observe the -pthread compiler flag.
180#if !defined HX_USE_THREADS
181#if __has_include(<threads.h>)
182#define HX_USE_THREADS 11
183#elif defined _POSIX_THREADS
184#define HX_USE_THREADS 1
185#else
186#define HX_USE_THREADS 0
187#endif
188#endif
189
190#if !defined HX_USE_LIBCXX
191#define HX_USE_LIBCXX 1
192#elif !(HX_USE_LIBCXX) && !defined HX_SKIP_LIBCXX_CHECK && __has_include(<new>)
193#error Use -nostdinc++ to use the C headers instead of the C++ ones.
194#endif
195
196// HX_USE_SIGTRAP is not always defined like the other HX_USE_* macros.
197#if !defined HX_USE_SIGTRAP && defined __has_builtin && __has_builtin(__builtin_debugtrap)
198#define hxbreakpoint() (__builtin_debugtrap(),true)
199#else
200#define hxbreakpoint() (raise(SIGTRAP),true)
201#endif
202
203#define hxrestrict __restrict
204
205// hxattr_allocator - Collection of attributes for allocators.
206#if defined __clang__
207#define hxattr_allocator(...) \
208 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
209#elif __GNUC__ >= 11
210#define hxattr_allocator(...) __attribute__((malloc(__VA_ARGS__))) \
211 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
212#else
213#define hxattr_allocator(...) __attribute__((malloc)) \
214 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
215#endif
216
217#if defined __clang__
218#define hxattr_assume(condition_) __builtin_assume(condition_)
219#else
220// The solution for gcc may have side effects. Use with caution.
221// #define hxattr_assume(condition_) (void)((bool)(condition_) || (__builtin_unreachable(),0))
222#define hxattr_assume(...) (void)0
223#endif
224
225// __attribute__ is used instead of [[...]] because it works in an extern "C"
226// block.
227
228#define hxattr_cold __attribute__((cold))
229#if HX_USE_INLINING_ATTR && (defined __clang__ || __GNUC__ >= 11)
230#define hxattr_flatten __attribute__((flatten))
231#else
232#define hxattr_flatten
233#endif
234#if HX_USE_INLINING_ATTR
235#define hxattr_hot __attribute__((hot))
236#else
237#define hxattr_hot
238#endif
239#define hxattr_nodiscard __attribute__((warn_unused_result))
240#define hxattr_noexcept __attribute__((nothrow))
241#define hxattr_nonnull(...)__attribute__((nonnull(__VA_ARGS__)))
242#define hxattr_noinline __attribute__((noinline))
243#define hxattr_noreturn __attribute__((noreturn))
244#define hxattr_printf(pos_, start_) __attribute__((format(printf, pos_, start_)))
245#define hxattr_scanf(pos_, start_) __attribute__((format(scanf, pos_, start_)))
246#define hxattr_weak __attribute__((weak))
247
248#ifndef hxinline
249#if HX_USE_INLINING_ATTR
250#define hxinline inline __attribute__((always_inline))
251#else
252#define hxinline inline
253#endif
254#endif
255
256#endif // target specific settings
257
258// -- Target independent -------------------------------------------------------
259#if HX_CPLUSPLUS >= 202302L
263#define hxconstexpr constexpr
264#else
265#define hxconstexpr
266#endif // HX_CPLUSPLUS < 202302L
267
268#if HX_CPLUSPLUS >= 201703L
272#define hxinline_constexpr inline constexpr
274#define hxif_constexpr if constexpr
275#else
276#define hxinline_constexpr constexpr
277#define hxif_constexpr if
278#endif
279
280#if !defined HX_USE_MACROS_WITH_MODULE
286#define HX_USE_MACROS_WITH_MODULE 0
287#endif
288
289#if !defined HX_USE_CONSOLE
294#define HX_USE_CONSOLE ((HX_CPLUSPLUS >= 202002L) ? 1 : 0)
295#elif (HX_USE_CONSOLE) && HX_CPLUSPLUS && HX_CPLUSPLUS < 202002L
296#error The console requires C++20 or later.
297#endif
298#if (HX_USE_CONSOLE) > 1 && defined __wasm__
299// This warning is primarily for security and sanity checking reasons.
300#error The debug console is not designed for use with WASM.
301#endif
302
303#if !defined HX_USE_LOGGING
308#define HX_USE_LOGGING 2
309#endif
310
311#if !defined HX_USE_FILE_IO
315#define HX_USE_FILE_IO 1
316#endif
317
318#if !defined HX_USE_PROFILER
321#define HX_USE_PROFILER 0
322#endif
323
324#if !defined HX_CYCLES_PER_SECOND
327#define HX_CYCLES_PER_SECOND 3.0e+9
328#endif
329
330#if !defined HX_PROFILER_MAX_RECORDS
333#define HX_PROFILER_MAX_RECORDS 4096
334#endif
335
336#if !defined HX_USE_MEMORY_MANAGER
341#define HX_USE_MEMORY_MANAGER 1
342#endif
343
344#if !defined HX_PROVIDE_NEW_DELETE
348#define HX_PROVIDE_NEW_DELETE !(HX_USE_LIBCXX)
349#endif
350
352#define HX_KIB (1 << 10)
353
355#define HX_MIB (1 << 20)
356
358#define HX_GIB (1 << 30)
359
360#if !defined HX_MEMORY_BUDGET_PERMANENT
363#define HX_MEMORY_BUDGET_PERMANENT (4u * HX_KIB)
364#endif
365
366#if !defined HX_MEMORY_MAX_STACKS
370#define HX_MEMORY_MAX_STACKS 3u
371#endif
372
373#if !defined HX_RADIX_SORT_MIN_SIZE
376#define HX_RADIX_SORT_MIN_SIZE 32
377#endif
378
379#if !defined HX_MAX_LINE
382#define HX_MAX_LINE (2 * HX_KIB)
383#endif
384
385#if !defined HX_USE_GOOGLE_TEST
387#define HX_USE_GOOGLE_TEST 0
388#endif
389
390#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG && defined __GLIBC__ && !defined __FAST_MATH__
391#if !defined HX_USE_FLOATING_POINT_TRAPS
394#define HX_USE_FLOATING_POINT_TRAPS 1
395#endif
396#else
397#undef HX_USE_FLOATING_POINT_TRAPS
398#define HX_USE_FLOATING_POINT_TRAPS 0
399#endif
400
402#define HX_QUOTE(x_) #x_
403
405// `HX_TEST_ERROR_HANDLING` - Tests that the failure of tests is handled
406// correctly. Set to `0` if not defined. Set by `testerrorhandling.sh` and
407// `coverage.sh`.
408#if !defined HX_TEST_ERROR_HANDLING
409#define HX_TEST_ERROR_HANDLING 0
410#endif
411
412#if HX_CPLUSPLUS
413// HX_USE_* feature test flags must not be empty as that is evaluated as 0.
414#define HX_CHECK_USE_(x_) static_assert(HX_QUOTE(x_)[0] != 0, #x_ " must not be empty");
415HX_CHECK_USE_(HX_PROVIDE_NEW_DELETE)
416HX_CHECK_USE_(HX_TEST_ERROR_HANDLING)
417HX_CHECK_USE_(HX_USE_CONSOLE)
418HX_CHECK_USE_(HX_USE_FILE_IO)
419HX_CHECK_USE_(HX_USE_FLOATING_POINT_TRAPS)
420HX_CHECK_USE_(HX_USE_GOOGLE_TEST)
421HX_CHECK_USE_(HX_USE_INLINING_ATTR)
422HX_CHECK_USE_(HX_USE_LIBCXX)
423HX_CHECK_USE_(HX_USE_LOGGING)
424HX_CHECK_USE_(HX_USE_MEMORY_MANAGER)
425HX_CHECK_USE_(HX_USE_MACROS_WITH_MODULE)
426HX_CHECK_USE_(HX_USE_PROFILER)
427HX_CHECK_USE_(HX_USE_THREADS)
428#endif
429
430// HX_APPEND_COUNTER - Used to generate unique identifiers. This is weird
431// because the ## operator happens before macro arg evaluation and both happen
432// before general macro evaluation.
433#define HX_APPEND_COUNTER2_(x_, y_) x_ ## y_
434#define HX_APPEND_COUNTER_(x_, y_) HX_APPEND_COUNTER2_(x_, y_)
435#define HX_APPEND_COUNTER(x_) HX_APPEND_COUNTER_(x_, __COUNTER__)
436
437// HX_BEGIN_INL_/HX_END_INL_ - These allow excluding .inl files from the module
438// export block. See hxmodule.cppm for details.
439#if !defined HX_BEGIN_INL_
440#define HX_BEGIN_INL_
441#define HX_END_INL_
442#endif
443
444// HX_USE_NAMESPACE - Wraps the entire library in a namespace when HX_USE_NAMESPACE is
445// defined as a valid namespace identifier.
446#if HX_CPLUSPLUS && defined HX_USE_NAMESPACE
447HX_CHECK_USE_(HX_USE_NAMESPACE)
448#define HX_NS_BEGIN_ namespace HX_USE_NAMESPACE {
449#define HX_NS_END_ }
450#define HX_NS_PREFIX_ HX_USE_NAMESPACE::
451#define HX_NS_USE using namespace HX_USE_NAMESPACE;
452#else
453#define HX_NS_BEGIN_
454#define HX_NS_END_
455#define HX_NS_PREFIX_
456#define HX_NS_USE
457#endif
459
460#if !(HX_USE_MACROS_WITH_MODULE)
461#if HX_CPLUSPLUS
462extern "C" {
463#endif
464
470 uint8_t log_level;
471
475};
476
478extern struct hxsettings hxg_settings;
479
482
483#if HX_CPLUSPLUS
484} // extern "C"
485#endif
486#endif // HX_USE_MACROS_WITH_MODULE
#define HX_USE_FLOATING_POINT_TRAPS
Definition hxsettings.h:398
#define HX_USE_MEMORY_MANAGER
HX_USE_MEMORY_MANAGER - Used to disable memory management for debugging and for platforms like wasm w...
Definition hxsettings.h:341
#define HX_USE_MACROS_WITH_MODULE
HX_USE_MACROS_WITH_MODULE - Setting -DHX_USE_MACROS_WITH_MODULE=1 when using modules (e....
Definition hxsettings.h:286
#define HX_USE_CONSOLE
HX_USE_CONSOLE - Control whether the console is included in the build.
Definition hxsettings.h:294
#define HX_USE_LIBCXX
HX_USE_LIBCXX: Indicates whether libstdc++/libc++ are present.
Definition hxsettings.h:57
#define HX_USE_THREADS
HX_USE_THREADS - 11 indicates C11 threads are in use.
Definition hxsettings.h:51
#define HX_USE_INLINING_ATTR
HX_USE_INLINING_ATTR - Controls whether hxinline, hxattr_flatten, and hxattr_hot are enabled.
Definition hxsettings.h:42
struct hxsettings hxg_settings
hxg_settings - Global class constructed by hxinit.
#define HX_PROVIDE_NEW_DELETE
HX_PROVIDE_NEW_DELETE - Provides a new/delete implementation when the std library is absent unless ov...
Definition hxsettings.h:348
#define HX_USE_GOOGLE_TEST
HX_USE_GOOGLE_TEST - Switch to using the real Google Test. Defaults to 0.
Definition hxsettings.h:387
void hxsettings_construct_(void)
hxsettings_construct - Internal. Used to reset settings at startup.
#define HX_USE_LOGGING
HX_USE_LOGGING - Control whether logging statements are included in the build.
Definition hxsettings.h:308
#define HX_USE_FILE_IO
HX_USE_FILE_IO - Select if hxfile exists and if it uses C file I/O or POSIX I/O.
Definition hxsettings.h:315
#define HX_USE_PROFILER
HX_USE_PROFILER - Enable this to use the profiler.
Definition hxsettings.h:321
hxsettings - Constructed by first call to hxinit which happens when on or before the system memory al...
Definition hxsettings.h:467
uint8_t log_level
log_level - Logging level for the application (e.g., verbosity of logs).
Definition hxsettings.h:470
bool deallocate_permanent
deallocate_permanent - Allows deallocation of permanent resources at system shut down.
Definition hxsettings.h:474