metal-cos/lib/libc/exit.c

48 lines
802 B
C
Raw Normal View History

2014-07-28 00:09:31 +00:00
#include <stdint.h>
2014-07-28 00:09:31 +00:00
#include <stdlib.h>
#include <syscall.h>
2014-09-05 01:22:04 +00:00
struct atexit_cb {
struct atexit_cb *next;
void (*cb)(void);
};
static uint64_t _atexit_count = 0;
static struct atexit_cb _atexits[32]; // POSIX requires at least 32 atexit functions
static struct atexit_cb *_atexit_last = NULL;
2014-07-28 00:09:31 +00:00
int
atexit(void (*function)(void))
{
2014-09-05 01:22:04 +00:00
if (_atexit_count < 32) {
struct atexit_cb *prev = _atexit_last;
_atexits[_atexit_count].cb = function;
_atexits[_atexit_count].next = prev;
_atexit_last = &_atexits[_atexit_count];
_atexit_count++;
} else {
// XXX: Support malloc
return -1;
}
return 0;
2014-07-28 00:09:31 +00:00
}
void
exit(int status)
{
2014-09-05 01:22:04 +00:00
while (_atexit_last != NULL) {
(_atexit_last->cb)();
_atexit_last = _atexit_last->next;
}
2014-07-28 00:09:31 +00:00
2015-01-02 22:04:14 +00:00
OSExit(status);
2014-07-28 00:09:31 +00:00
__builtin_unreachable();
}