2024-03-26 02:27:38 +02:00
|
|
|
#include <BAN/Assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2023-06-02 17:49:21 +03:00
|
|
|
|
2023-10-30 11:10:08 +02:00
|
|
|
#define ATEXIT_MAX_FUNCS 128
|
2023-06-02 17:49:21 +03:00
|
|
|
|
|
|
|
struct atexit_func_entry_t
|
|
|
|
{
|
2024-03-26 02:27:38 +02:00
|
|
|
void(*func)(void*);
|
|
|
|
void* arg;
|
2023-10-30 11:10:08 +02:00
|
|
|
void* dso_handle;
|
2023-06-02 17:49:21 +03:00
|
|
|
};
|
|
|
|
|
2023-10-30 11:10:08 +02:00
|
|
|
static atexit_func_entry_t __atexit_funcs[ATEXIT_MAX_FUNCS];
|
2024-03-26 02:27:38 +02:00
|
|
|
static size_t __atexit_func_count = 0;
|
2023-06-02 17:49:21 +03:00
|
|
|
|
2024-03-26 02:27:38 +02:00
|
|
|
extern "C" int __cxa_atexit(void(*func)(void*), void* arg, void* dso_handle)
|
2023-06-02 17:49:21 +03:00
|
|
|
{
|
2023-10-30 11:10:08 +02:00
|
|
|
if (__atexit_func_count >= ATEXIT_MAX_FUNCS)
|
2024-03-26 02:27:38 +02:00
|
|
|
return -1;
|
|
|
|
auto& atexit_func = __atexit_funcs[__atexit_func_count++];
|
|
|
|
atexit_func.func = func;
|
|
|
|
atexit_func.arg = arg;
|
|
|
|
atexit_func.dso_handle = dso_handle;
|
2023-10-30 11:10:08 +02:00
|
|
|
return 0;
|
2023-06-02 17:49:21 +03:00
|
|
|
};
|
|
|
|
|
2024-03-26 02:27:38 +02:00
|
|
|
extern "C" void __cxa_finalize(void* f)
|
2023-10-30 11:10:08 +02:00
|
|
|
{
|
2024-03-26 02:27:38 +02:00
|
|
|
for (size_t i = __atexit_func_count; i > 0; i--)
|
2023-10-30 11:10:08 +02:00
|
|
|
{
|
2024-03-26 02:27:38 +02:00
|
|
|
auto& atexit_func = __atexit_funcs[i - 1];
|
|
|
|
if (atexit_func.func == nullptr)
|
2023-10-30 11:10:08 +02:00
|
|
|
continue;
|
2024-03-26 02:27:38 +02:00
|
|
|
if (f == nullptr || f == atexit_func.func)
|
|
|
|
{
|
|
|
|
atexit_func.func(atexit_func.arg);
|
|
|
|
atexit_func.func = nullptr;
|
|
|
|
}
|
2023-10-30 11:10:08 +02:00
|
|
|
}
|
2024-03-26 02:27:38 +02:00
|
|
|
};
|