Luna/libs/libc/src/atexit.cpp

25 lines
558 B
C++
Raw Normal View History

#include <stdlib.h>
#include <unistd.h>
#define ATEXIT_MAX_FUNCS 32
typedef void (*atexit_func_t)(void);
atexit_func_t atexit_functions[ATEXIT_MAX_FUNCS];
int atexit_function_count = 0;
extern "C"
{
int atexit(atexit_func_t handler)
{
if (atexit_function_count >= ATEXIT_MAX_FUNCS) return -1;
atexit_functions[atexit_function_count++] = handler;
return 0;
}
__lc_noreturn void exit(int status)
{
for (int i = 0; i < atexit_function_count; i++) { atexit_functions[i](); }
_exit(status);
}
}