master
 1#include <stdlib.h>
 2#include <stdint.h>
 3#include "libc.h"
 4#include "lock.h"
 5#include "fork_impl.h"
 6
 7#define malloc __libc_malloc
 8#define calloc __libc_calloc
 9#define realloc undef
10#define free undef
11
12/* Ensure that at least 32 atexit handlers can be registered without malloc */
13#define COUNT 32
14
15static struct fl
16{
17	struct fl *next;
18	void (*f[COUNT])(void *);
19	void *a[COUNT];
20} builtin, *head;
21
22static int slot;
23
24#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
25static volatile int lock[1];
26volatile int *const __atexit_lockptr = lock;
27#endif
28
29void __funcs_on_exit()
30{
31	void (*func)(void *), *arg;
32	LOCK(lock);
33	for (; head; head=head->next, slot=COUNT) while(slot-->0) {
34		func = head->f[slot];
35		arg = head->a[slot];
36		UNLOCK(lock);
37		func(arg);
38		LOCK(lock);
39	}
40}
41
42void __cxa_finalize(void *dso)
43{
44}
45
46int __cxa_atexit(void (*func)(void *), void *arg, void *dso)
47{
48	LOCK(lock);
49
50	/* Defer initialization of head so it can be in BSS */
51	if (!head) head = &builtin;
52
53	/* If the current function list is full, add a new one */
54	if (slot==COUNT) {
55		struct fl *new_fl = calloc(sizeof(struct fl), 1);
56		if (!new_fl) {
57			UNLOCK(lock);
58			return -1;
59		}
60		new_fl->next = head;
61		head = new_fl;
62		slot = 0;
63	}
64
65	/* Append function to the list. */
66	head->f[slot] = func;
67	head->a[slot] = arg;
68	slot++;
69
70	UNLOCK(lock);
71	return 0;
72}
73
74static void call(void *p)
75{
76	((void (*)(void))(uintptr_t)p)();
77}
78
79int atexit(void (*func)(void))
80{
81	return __cxa_atexit(call, (void *)(uintptr_t)func, 0);
82}