Zth (libzth)
Loading...
Searching...
No Matches
init.h
Go to the documentation of this file.
1#ifndef ZTH_INIT_H
2#define ZTH_INIT_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
9#include <libzth/macros.h>
10
11#include <stdlib.h>
12
14#ifdef __cplusplus
15 zth_init_entry(void (*f_)(void), zth_init_entry const* next_) noexcept
16 : f(f_)
17 , next(next_)
18 {}
19#endif // C++
20 void (*f)(void);
21 struct zth_init_entry const* next;
22};
23
24extern struct zth_init_entry const* zth_init_head;
25extern struct zth_init_entry* zth_init_tail;
26EXTERN_C ZTH_EXPORT void zth_init();
27EXTERN_C ZTH_EXPORT void zth_preinit();
28EXTERN_C ZTH_EXPORT int zth_postdeinit();
29
30#ifdef __cplusplus
31# ifndef ZTH_INIT_CALL
35# define ZTH_INIT_CALL_(f, ...) \
36 struct f##__init : public zth_init_entry { \
37 f##__init() noexcept \
38 : zth_init_entry(&exec, nullptr) \
39 { \
40 if(zth_init_tail) \
41 zth_init_tail->next = this; \
42 zth_init_tail = this; \
43 if(!zth_init_head) \
44 zth_init_head = this; \
45 } \
46 static void exec() { __VA_ARGS__ }; \
47 };
48# define ZTH_INIT_CALL(f) \
49 ZTH_INIT_CALL_(f, f();) \
50 static f##__init const f##__init_;
51# endif
52
53# ifndef ZTH_DEINIT_CALL
54# ifdef ZTH_OS_BAREMETAL
55# define ZTH_DEINIT_CALL(...) // Don't bother doing any cleanup during shutdown.
56# else
60# define ZTH_DEINIT_CALL(f) \
61 ZTH_INIT_CALL_(f, atexit(f);) \
62 static f##__init f##__deinit_;
63# endif
64# endif
65
66#endif // __cplusplus
67#endif // ZTH_INIT_H
struct zth_init_entry * zth_init_tail
Definition init.cpp:15
void zth_init()
Perform one-time global initialization of the Zth library.
Definition init.cpp:25
struct zth_init_entry const * zth_init_head
Definition init.cpp:13
int zth_postdeinit()
Initialization function to be called by the default-supplied main(), just before shutting down.
Definition main.cpp:39
void zth_preinit()
Initialization function to be called by the default-supplied main(), before doing anything else.
Definition main.cpp:27
Definition init.h:13
void(* f)(void)
Definition init.h:20
struct zth_init_entry const * next
Definition init.h:21
zth_init_entry(void(*f_)(void), zth_init_entry const *next_) noexcept
Definition init.h:15