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.
33#define HX_C_BEGIN_ extern "C" {
34#define HX_C_END_ }
36#elif defined __cplusplus
37#define HX_CPLUSPLUS __cplusplus
38#define HX_C_BEGIN_ extern "C" {
39#define HX_C_END_ }
40#else
41// This is C. Providing 0 here avoids warnings about using undefined variables.
42#define HX_CPLUSPLUS 0
43#define HX_C_BEGIN_
44#define HX_C_END_
45#endif
46
47#if !defined HX_USE_INLINING_ATTR
50#define HX_USE_INLINING_ATTR 1
51#endif
52
53// -- Target settings for Doxygen ----------------------------------------------
54// See the Doxyfile. Run doxygen with no args.
55#if defined HX_DOXYGEN_PARSER
56
59#define HX_USE_THREADS 11
60
65#define HX_USE_LIBCXX 1
66
70#define hxbreakpoint() true
71
75#define hxrestrict
76
79#define hxattr_allocator(...)
80
83#define hxattr_assume(...) (void)0
84
86#define hxattr_cold
87
90#define hxattr_flatten
91
94#define hxattr_hot
95
98#define hxattr_nodiscard
99
102#define hxattr_noexcept
103
106#define hxattr_nonnull(...)
107
109#define hxattr_noinline
110
113#define hxattr_noreturn
114
117#define hxattr_printf(...)
118
121#define hxattr_weak
122
125#define hxinline inline
126
127// -- Target settings for MSVC -------------------------------------------------
128// MSVC doesn't support C++'s feature test macros very well.
129#elif defined _MSC_VER
130
131#if defined __clang__
132#error Clang detected masquerading as MSVC - not supported due to intrinsic use.
133#endif
134
135#if !defined __cpp_exceptions && !defined _HAS_EXCEPTIONS
136#define _HAS_EXCEPTIONS 0
137#endif
138
139#if !defined HX_USE_THREADS
140#define HX_USE_THREADS 11
141#endif
142
143#if !defined HX_USE_LIBCXX
144#define HX_USE_LIBCXX 1
145#endif
146
147#define hxbreakpoint() (__debugbreak(),true)
148#define hxrestrict __restrict
149#define hxattr_allocator(...)
150#define hxattr_assume(condition_) __assume(condition_)
151#define hxattr_cold
152#define hxattr_flatten
153#define hxattr_hot
154#if HX_CPLUSPLUS
155#define hxattr_nodiscard [[nodiscard]]
156#else
157#define hxattr_nodiscard
158#endif
159#define hxattr_noexcept
160#define hxattr_nonnull(...)
161#define hxattr_noinline __declspec(noinline)
162#if HX_CPLUSPLUS
163#define hxattr_noreturn [[noreturn]]
164#else
165#define hxattr_noreturn
166#endif
167#define hxattr_printf(...)
168// #define hxattr_weak __declspec(selectany) is not used as MSVC treats all
169// library objects as weak.
170#define hxattr_weak
171
172#if HX_USE_INLINING_ATTR
173#define hxinline inline __forceinline
174#else
175#define hxinline inline
176#endif
177
178// -- Target settings for Clang and GCC ----------------------------------------
179// Further compilers will require customization.
180#else // Assume gcc/clang.
181
182// hxthreads.hpp should work in C++11 with pthread.h. _POSIX_THREADS is the
183// correct way to observe the -pthread compiler flag.
184#if !defined HX_USE_THREADS
185#if __has_include(<threads.h>)
186#define HX_USE_THREADS 11
187#elif defined _POSIX_THREADS
188#define HX_USE_THREADS 1
189#else
190#define HX_USE_THREADS 0
191#endif
192#endif
193
194#if !defined HX_USE_LIBCXX
195#define HX_USE_LIBCXX 1
196#elif !(HX_USE_LIBCXX) && !defined HX_SKIP_LIBCXX_CHECK && __has_include(<new>)
197#error Use -nostdinc++ to use the C headers instead of the C++ ones.
198#endif
199
200// HX_USE_SIGTRAP is not always defined like the other HX_USE_* macros.
201#if !defined HX_USE_SIGTRAP && defined __has_builtin && __has_builtin(__builtin_debugtrap)
202#define hxbreakpoint() (__builtin_debugtrap(),true)
203#else
204#define hxbreakpoint() (raise(SIGTRAP),true)
205#endif
206
207#define hxrestrict __restrict
208
209// hxattr_allocator - Collection of attributes for allocators.
210#if defined __clang__
211#define hxattr_allocator(...) \
212 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
213#elif __GNUC__ >= 11
214#define hxattr_allocator(...) __attribute__((malloc(__VA_ARGS__))) \
215 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
216#else
217#define hxattr_allocator(...) __attribute__((malloc)) \
218 __attribute__((returns_nonnull)) __attribute__((warn_unused_result))
219#endif
220
221#if defined __clang__
222#define hxattr_assume(condition_) __builtin_assume(condition_)
223#else
224// The solution for gcc may have side effects. Use with caution.
225// #define hxattr_assume(condition_) (void)((bool)(condition_) || (__builtin_unreachable(),0))
226#define hxattr_assume(...) (void)0
227#endif
228
229// __attribute__ is used instead of [[...]] because it works in an extern "C"
230// block.
231
232#define hxattr_cold __attribute__((cold))
233#if HX_USE_INLINING_ATTR && (defined __clang__ || __GNUC__ >= 11)
234#define hxattr_flatten __attribute__((flatten))
235#else
236#define hxattr_flatten
237#endif
238#if HX_USE_INLINING_ATTR
239#define hxattr_hot __attribute__((hot))
240#else
241#define hxattr_hot
242#endif
243#define hxattr_nodiscard __attribute__((warn_unused_result))
244#define hxattr_noexcept __attribute__((nothrow))
245#define hxattr_nonnull(...)__attribute__((nonnull(__VA_ARGS__)))
246#define hxattr_noinline __attribute__((noinline))
247#define hxattr_noreturn __attribute__((noreturn))
248#define hxattr_printf(pos_, start_) __attribute__((format(printf, pos_, start_)))
249#define hxattr_weak __attribute__((weak))
250
251#ifndef hxinline
252#if HX_USE_INLINING_ATTR
253#define hxinline inline __attribute__((always_inline))
254#else
255#define hxinline inline
256#endif
257#endif
258
259#endif // target specific settings
260
261// -- Target independent -------------------------------------------------------
262#if HX_CPLUSPLUS >= 202302L
266#define hxconstexpr constexpr
267#else
268#define hxconstexpr
269#endif // HX_CPLUSPLUS < 202302L
270
271#if HX_CPLUSPLUS >= 201703L
275#define hxinline_constexpr inline constexpr
277#define hxif_constexpr if constexpr
278#else
279#define hxinline_constexpr constexpr
280#define hxif_constexpr if
281#endif
282
283#if !defined HX_USE_MACROS_WITH_MODULE
289#define HX_USE_MACROS_WITH_MODULE 0
290#endif
291
292#if !defined HX_USE_CONSOLE
297#define HX_USE_CONSOLE ((HX_CPLUSPLUS >= 202002L) ? 1 : 0)
298#elif (HX_USE_CONSOLE) && HX_CPLUSPLUS && HX_CPLUSPLUS < 202002L
299#error The console requires C++20 or later.
300#endif
301#if (HX_USE_CONSOLE) > 1 && defined __wasm__
302// This warning is primarily for security and sanity checking reasons.
303#error The debug console is not designed for use with WASM.
304#endif
305
306#if !defined HX_USE_LOGGING
311#define HX_USE_LOGGING 2
312#endif
313
314#if !defined HX_USE_FILE_IO
318#define HX_USE_FILE_IO 1
319#endif
320
321#if !defined HX_USE_PROFILER
324#define HX_USE_PROFILER 0
325#endif
326
327#if !defined HX_CYCLES_PER_SECOND
330#define HX_CYCLES_PER_SECOND 3.0e+9
331#endif
332
333#if !defined HX_PROFILER_MAX_RECORDS
336#define HX_PROFILER_MAX_RECORDS 4096
337#endif
338
339#if !defined HX_USE_MEMORY_MANAGER
344#define HX_USE_MEMORY_MANAGER 1
345#endif
346
347#if !defined HX_PROVIDE_NEW_DELETE
351#define HX_PROVIDE_NEW_DELETE !(HX_USE_LIBCXX)
352#endif
353
355#define HX_KIB (1 << 10)
356
358#define HX_MIB (1 << 20)
359
361#define HX_GIB (1 << 30)
362
363#if !defined HX_MEMORY_BUDGET_PERMANENT
366#define HX_MEMORY_BUDGET_PERMANENT (4u * HX_KIB)
367#endif
368
369#if !defined HX_MEMORY_MAX_STACKS
373#define HX_MEMORY_MAX_STACKS 3u
374#endif
375
376#if !defined HX_RADIX_SORT_MIN_SIZE
379#define HX_RADIX_SORT_MIN_SIZE 32
380#endif
381
382#if !defined HX_MAX_LINE
385#define HX_MAX_LINE (2 * HX_KIB)
386#endif
387
388#if !defined HX_USE_GOOGLE_TEST
391#define HX_USE_GOOGLE_TEST 0
392#endif
393
394#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG && defined __GLIBC__ && !defined __FAST_MATH__
395#if !defined HX_USE_FLOATING_POINT_TRAPS
399#define HX_USE_FLOATING_POINT_TRAPS 1
400#endif
401#else
402#undef HX_USE_FLOATING_POINT_TRAPS
403#define HX_USE_FLOATING_POINT_TRAPS 0
404#endif
405
407#define HX_QUOTE(x_) #x_
408
410// `HX_TEST_ERROR_HANDLING` - Tests that the failure of tests is handled
411// correctly. Set to `0` if not defined. Set by `testerrorhandling.sh` and
412// `coverage.sh`.
413#if !defined HX_TEST_ERROR_HANDLING
414#define HX_TEST_ERROR_HANDLING 0
415#endif
416
417#if HX_CPLUSPLUS
418// HX_USE_* feature test flags must not be empty as that is evaluated as 0.
419#define HX_CHECK_USE_(x_) static_assert(HX_QUOTE(x_)[0] != 0, #x_ " must not be empty");
420HX_CHECK_USE_(HX_PROVIDE_NEW_DELETE)
421HX_CHECK_USE_(HX_TEST_ERROR_HANDLING)
422HX_CHECK_USE_(HX_USE_CONSOLE)
423HX_CHECK_USE_(HX_USE_FILE_IO)
424HX_CHECK_USE_(HX_USE_FLOATING_POINT_TRAPS)
425HX_CHECK_USE_(HX_USE_GOOGLE_TEST)
426HX_CHECK_USE_(HX_USE_INLINING_ATTR)
427HX_CHECK_USE_(HX_USE_LIBCXX)
428HX_CHECK_USE_(HX_USE_LOGGING)
429HX_CHECK_USE_(HX_USE_MEMORY_MANAGER)
430HX_CHECK_USE_(HX_USE_MACROS_WITH_MODULE)
431HX_CHECK_USE_(HX_USE_PROFILER)
432HX_CHECK_USE_(HX_USE_THREADS)
433#endif
434
435// HX_APPEND_COUNTER - Used to generate unique identifiers. This is weird
436// because the ## operator happens before macro arg evaluation and both happen
437// before general macro evaluation.
438#define HX_APPEND_COUNTER2_(x_, y_) x_ ## y_
439#define HX_APPEND_COUNTER_(x_, y_) HX_APPEND_COUNTER2_(x_, y_)
440#define HX_APPEND_COUNTER(x_) HX_APPEND_COUNTER_(x_, __COUNTER__)
441
442// HX_INL_BEGIN_/HX_INL_END_ - These allow excluding .inl files from the module
443// export block. See hxmodule.cppm for details.
444#if !defined HX_INL_BEGIN_
445#define HX_INL_BEGIN_
446#define HX_INL_END_
447#endif
448
449// HX_USE_NAMESPACE - Wraps the entire library in a namespace when HX_USE_NAMESPACE is
450// defined as a valid namespace identifier.
451#if HX_CPLUSPLUS && defined HX_USE_NAMESPACE
452HX_CHECK_USE_(HX_USE_NAMESPACE)
453#define HX_NS_BEGIN_ namespace HX_USE_NAMESPACE {
454#define HX_NS_END_ }
455#define HX_NS_PREFIX_ HX_USE_NAMESPACE::
456#define HX_NS_USE using namespace HX_USE_NAMESPACE;
457#else
458#define HX_NS_BEGIN_
459#define HX_NS_END_
460#define HX_NS_PREFIX_
461#define HX_NS_USE
462#endif
464
465#if !(HX_USE_MACROS_WITH_MODULE)
466HX_C_BEGIN_
467
473 uint8_t log_level;
474
478
482
485 const char* test_filter;
486};
487
489extern struct hxsettings_t hxg_settings;
490
493
494HX_C_END_
495#endif // HX_USE_MACROS_WITH_MODULE
#define HX_USE_FLOATING_POINT_TRAPS
Definition hxsettings.h:403
#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:344
#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:289
#define HX_USE_CONSOLE
HX_USE_CONSOLE - Control whether the console is included in the build.
Definition hxsettings.h:297
#define HX_USE_LIBCXX
HX_USE_LIBCXX: Indicates whether libstdc++/libc++ are present.
Definition hxsettings.h:65
#define HX_USE_THREADS
HX_USE_THREADS - 11 indicates C11 threads are in use.
Definition hxsettings.h:59
#define HX_USE_INLINING_ATTR
HX_USE_INLINING_ATTR - Controls whether hxinline, hxattr_flatten, and hxattr_hot are enabled.
Definition hxsettings.h:50
#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:351
#define HX_USE_GOOGLE_TEST
HX_USE_GOOGLE_TEST - Switch to using the real Google Test.
Definition hxsettings.h:391
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:311
#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:318
struct hxsettings_t hxg_settings
hxg_settings - Global class constructed by hxinit.
#define HX_USE_PROFILER
HX_USE_PROFILER - Enable this to use the profiler.
Definition hxsettings.h:324
hxsettings_t - Constructed by first call to hxinit which happens when on or before the system memory ...
Definition hxsettings.h:470
bool test_break_on_failure
test_break_on_failure - Triggers a breakpoint when an hxassert, ASSERT_* or EXPECT_* condition fails ...
Definition hxsettings.h:481
const char * test_filter
test_filter - Used by hxtest to support simple Google Test style filtering.
Definition hxsettings.h:485
bool deallocate_permanent
deallocate_permanent - Allows deallocation of permanent resources at system shut down.
Definition hxsettings.h:477
uint8_t log_level
log_level - Logging level for the application (e.g., verbosity of logs).
Definition hxsettings.h:473