CS350 COS
COS
Loading...
Searching...
No Matches
exit.c
Go to the documentation of this file.
1
2#include <stdint.h>
3#include <stdlib.h>
4
5#include <syscall.h>
6
7struct atexit_cb {
8 struct atexit_cb *next;
9 void (*cb)(void);
10};
11
13static struct atexit_cb _atexits[32]; // POSIX requires at least 32 atexit functions
14static struct atexit_cb *_atexit_last = NULL;
15
16int
17atexit(void (*function)(void))
18{
19 if (_atexit_count < 32) {
20 struct atexit_cb *prev = _atexit_last;
21
22 _atexits[_atexit_count].cb = function;
24
27 } else {
28 // XXX: Support malloc
29 return -1;
30 }
31
32 return 0;
33}
34
35void
36exit(int status)
37{
38 while (_atexit_last != NULL) {
39 (_atexit_last->cb)();
41 }
42
43 OSExit(status & 0x00ff);
44
45 __builtin_unreachable();
46}
47
void exit(int status)
Definition: exit.c:36
static uint64_t _atexit_count
Definition: exit.c:12
static struct atexit_cb _atexits[32]
Definition: exit.c:13
int atexit(void(*function)(void))
Definition: exit.c:17
static struct atexit_cb * _atexit_last
Definition: exit.c:14
void OSExit(int status)
Definition: syscall.c:17
#define NULL
Definition: stddef.h:6
Definition: exit.c:7
void(* cb)(void)
Definition: exit.c:9
struct atexit_cb * next
Definition: exit.c:8
unsigned long uint64_t
Definition: types.h:13