libhatchet
Loading...
Searching...
No Matches
libhatchet.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
19
20// Use minimal C style headers. The std:: namespace may not exist. "You can't
21// get there from here."
22#include <errno.h>
23#include <signal.h>
24#include <stdarg.h>
25#include <stddef.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29
30#ifndef __wasm__
31#if __has_include(<x86intrin.h>)
32#include <x86intrin.h>
33#elif __has_include(<intrin.h>)
34#include <intrin.h>
35#endif
36#endif
37
38#if defined __STDC_VERSION__ && __STDC_VERSION__ < 202311l
39#include <stdbool.h>
40#endif
41
44#define LIBHATCHET_VER 14904
45
47#define LIBHATCHET_TAG "v1.49.4"
48
49#if !defined HX_HARDENING_MODE
51#define HX_HARDENING_MODE HX_HARDENING_MODE_DEBUG
52#endif
53
55#define HX_HARDENING_MODE_NONE 0
58#define HX_HARDENING_MODE_STANDARD 1
61#define HX_HARDENING_MODE_VERBOSE 2
64#define HX_HARDENING_MODE_DEBUG 3
65
67#if (HX_HARDENING_MODE) < 0 || (HX_HARDENING_MODE) > 3
68#error HX_HARDENING_MODE must be [0..3]. See <hx/hxsettings.h>.
69#endif
70
71#include "hxsettings.h"
72#if !(HX_USE_MACROS_WITH_MODULE)
73#include "hxmemory_manager.h"
74#endif
75
81#define hxinit() (void)(hxg_init_ver_ == LIBHATCHET_VER || (hxinit_internal(LIBHATCHET_VER), 0))
82
83#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG // These are debug facilities.
84
90#define hxassert(x_) (void)((bool)(x_) /* THIS IS USED AS A COMPILE TIME ASSERT: */ \
91 || (hxlog_handler_(hxlog_level_assert, #x_), hxassert_handler(__FILE__, __LINE__)) \
92 || hxbreakpoint())
93
101#define hxassertmsg(x_, ...) (void)((bool)(x_) /* THIS IS USED AS A COMPILE TIME ASSERT: */ \
102 || (hxlog_handler_(hxlog_level_assert, __VA_ARGS__), hxassert_handler(__FILE__, __LINE__)) \
103 || hxbreakpoint())
104
111#define hxassert_hard(x_, ...) (void)((bool)(x_) /* THIS IS USED AS A COMPILE TIME ASSERT: */ \
112 || (hxlog_handler_(hxlog_level_assert, __VA_ARGS__), hxassert_handler(__FILE__, __LINE__)) \
113 || hxbreakpoint())
114
121#define hxassert_always(x_, ...) (void)((bool)(x_) /* THIS IS USED AS A COMPILE TIME ASSERT: */ \
122 || (hxlog_handler_(hxlog_level_assert, __VA_ARGS__), hxassert_handler(__FILE__, __LINE__)) \
123 || hxbreakpoint())
124
125#else // HX_HARDENING_MODE != HX_HARDENING_MODE_DEBUG
126#define hxassertmsg(x_, ...) ((void)0)
127#define hxassert(x_) ((void)0)
128#define hxassert_always(x_, ...) (void)((bool)(x_) \
129 || (hxassert_handler(), 0)) // THIS IS USED AS A COMPILE TIME ASSERT.
130
131#endif // HX_HARDENING_MODE != HX_HARDENING_MODE_DEBUG
132
133#if (HX_USE_LOGGING) > 1
137#define hxlog(...) hxlog_handler(hxlog_level_log, __VA_ARGS__)
138#else
139#define hxlog(...) ((void)0)
140#endif
141
142#if HX_USE_LOGGING
146#define hxlog_release(...) hxlog_handler(hxlog_level_log, __VA_ARGS__)
147
150#define hxlog_console(...) hxlog_handler(hxlog_level_console, __VA_ARGS__)
151
154#define hxlog_warning(...) hxlog_handler(hxlog_level_warning, __VA_ARGS__)
155
160#define hxwarn_msg(x_, ...) (void)((bool)(x_) \
161 || (hxlog_handler(hxlog_level_warning, __VA_ARGS__), 0))
162
163#else // !HX_USE_LOGGING
164#define hxlog_release(...) ((void)0)
165#define hxlog_console(...) ((void)0)
166#define hxlog_warning(...) ((void)0)
167#define hxwarn_msg(x_, ...) ((void)0)
168#endif // !HX_USE_LOGGING
169
170// hxassert_hard has 4 variations. See above. It is only evaluated when
171// HX_HARDENING_MODE != HX_HARDENING_MODE_NONE.
172#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_VERBOSE
173#define hxassert_hard(x_, ...) (void)((bool)(x_) \
174 || (hxlog_handler_(hxlog_level_assert, __VA_ARGS__), \
175 hxassert_handler(), 0)) // THIS IS USED AS A COMPILE TIME ASSERT.
176#elif (HX_HARDENING_MODE) == HX_HARDENING_MODE_STANDARD
177#define hxassert_hard(x_, ...) (void)((bool)(x_) \
178 || (hxassert_handler(), 0)) // THIS IS USED AS A COMPILE TIME ASSERT.
179#elif (HX_HARDENING_MODE) == HX_HARDENING_MODE_NONE
180#define hxassert_hard(x_, ...) ((void)0)
181#endif
182
183#if !(HX_USE_MACROS_WITH_MODULE)
184#if HX_CPLUSPLUS
185extern "C" {
186#endif
187
206
209typedef ptrdiff_t hxsize_t;
210
212typedef uint32_t hxhash_t;
213
215typedef uint64_t hxhandle_t;
216
218void hxinit_internal(int version_) hxattr_cold;
219
221// `hxg_init_ver_` - Internal. Set to the current library version by `hxinit`.
222// It is zero when the platform has not been initialized.
223extern int hxg_init_ver_;
225
229
230#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG
235bool hxassert_handler(const char* file_, size_t line_) hxattr_noexcept hxattr_nonnull(1) hxattr_cold;
236#else // HX_HARDENING_MODE != HX_HARDENING_MODE_DEBUG
237// Errors from consteval code calling this function are how you know a compile
238// time assert has been hit. hxattr_cold tells the compiler this call is
239// unlikely.
240// WARNING: THIS IS USED AS A COMPILE TIME ASSERT.
242#endif
243
248void hxset_assert_handler(bool (*handler_)(void)) hxattr_noexcept;
249
254void hxlog_handler(enum hxlog_level_t level_, const char* format_, ...) hxattr_noexcept hxattr_printf(2, 3);
255
260void hxlog_handler_v(enum hxlog_level_t level_, const char* format_, va_list args_) hxattr_noexcept hxattr_nonnull(2);
261
265
266#if HX_CPLUSPLUS
267} // extern "C"
268#endif
269#endif // !HX_USE_MACROS_WITH_MODULE
270
277#define hxnull 0
278
279#if HX_CPLUSPLUS
280#if !(HX_USE_MACROS_WITH_MODULE)
283
286
288template<typename T_> constexpr hxsize_t hxsizeof(void) { return static_cast<hxsize_t>(sizeof(T_)); }
289template<typename T_> constexpr hxsize_t hxsizeof(T_&) { return static_cast<hxsize_t>(sizeof(T_)); }
290#endif // !HX_USE_MACROS_WITH_MODULE
291#else
292#define hxhash_bits 32u
293#define hxnull_handle 0u
294#define hxsizeof(x) (hxsize_t)sizeof(x)
295#endif
296
298// WARNING: gcc version 13 was reporting the wrong __cplusplus for C++23.
299#if HX_CPLUSPLUS >= 202302L
300#if !(HX_USE_MACROS_WITH_MODULE)
301// This allows hxassert_handler and hxlog_handler to be used by compile time
302// asserts in C++23.
303hxattr_noexcept constexpr void hxlog_handler_(enum hxlog_level_t level_, const char* format_, ...) {
304 if !consteval {
305 va_list args_;
306 va_start(args_, format_);
307 hxlog_handler_v(level_, format_, args_);
308 va_end(args_);
309 }
310}
311#endif // !HX_USE_MACROS_WITH_MODULE
312#else // HX_CPLUSPLUS < 202302L
313// Disable the compile time version of hxlog_handler_.
314#define hxlog_handler_ hxlog_handler
315#endif // HX_CPLUSPLUS < 202302L
Memory Manager C/C++ API.
Compiler detection and target specific C++ feature polyfill.
#define hxattr_noreturn
hxattr_noreturn - Indicates that a function will never return.
Definition hxsettings.h:104
#define hxattr_noexcept
hxattr_noexcept - Use gcc/clang nothrow attribute.
Definition hxsettings.h:93
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:272
#define hxattr_printf(...)
hxattr_printf - Indicates to gcc that a function uses printf-style formatting so it can type-check th...
Definition hxsettings.h:108
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
#define hxattr_cold
hxattr_cold - Optimize a function for size.
Definition hxsettings.h:78
hxlog_level_t
hxlog_level_t - Runtime setting for verbosity of log messages.
Definition libhatchet.h:191
@ hxlog_level_warning
hxlog_level_warning - Written to hxerr.
Definition libhatchet.h:201
@ hxlog_level_console
hxlog_level_console - Written to hxerr.
Definition libhatchet.h:198
@ hxlog_level_log
hxlog_level_log - Written to hxout.
Definition libhatchet.h:194
@ hxlog_level_assert
hxlog_level_assert - Written to hxerr.
Definition libhatchet.h:204
constexpr hxhash_t hxhash_bits
hxhash_bits - Number of bits in hxhash_t.
Definition libhatchet.h:282
bool hxassert_handler(const char *file_, size_t line_)
hxassert_handler - Assert handler.
void hxlog_handler(enum hxlog_level_t level_, const char *format_,...)
hxlog_handler - Enters formatted messages in the system log.
constexpr hxhandle_t hxnull_handle
hxnull_handle - A handle that will never refer to a valid object.
Definition libhatchet.h:285
void hxlog_handler_v(enum hxlog_level_t level_, const char *format_, va_list args_)
hxlog_handler_v - A va_list version of hxlog_handler.
constexpr hxsize_t hxsizeof(void)
hxsizeof - Returns the size of a type or expression as hxsize_t.
Definition libhatchet.h:288
void hxexit(int status_)
hxexit(int status) - Flushes hxout and hxerr then calls _Exit.
void hxinit_internal(int version_)
hxinit_internal - Internal. Use hxinit instead. It checks hxg_init_ver_.
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:212
uint64_t hxhandle_t
hxhandle_t - An opaque 64-bit handle.
Definition libhatchet.h:215
void hxset_assert_handler(bool(*handler_)(void))
hxset_assert_handler - Installs a custom handler called when an assertion fails.
void hxshutdown(void)
hxshutdown - Terminates service.
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209