2
* Copyright (c) 1990 Regents of the University of California.
5
* %sccs.include.redist.c%
11
@deftypefun int xatexit (void (*@var{fn}) (void))
13
Behaves as the standard @code{atexit} function, but with no limit on
14
the number of registered functions. Returns 0 on success, or @minus{}1 on
15
failure. If you use @code{xatexit} to register functions, you must use
16
@code{xexit} to terminate your program.
22
/* Adapted from newlib/libc/stdlib/{,at}exit.[ch].
23
If you use xatexit, you must call xexit instead of exit. */
26
#include "libiberty.h"
33
#define size_t unsigned long
40
/* For systems with larger pointers than ints, this must be declared. */
41
PTR malloc PARAMS ((size_t));
44
static void xatexit_cleanup PARAMS ((void));
46
/* Pointer to function run by xexit. */
47
extern void (*_xexit_cleanup) PARAMS ((void));
49
#define XATEXIT_SIZE 32
52
struct xatexit *next; /* next in list */
53
int ind; /* next index in this table */
54
void (*fns[XATEXIT_SIZE]) PARAMS ((void)); /* the table itself */
57
/* Allocate one struct statically to guarantee that we can register
58
at least a few handlers. */
59
static struct xatexit xatexit_first;
61
/* Points to head of LIFO stack. */
62
static struct xatexit *xatexit_head = &xatexit_first;
64
/* Register function FN to be run by xexit.
65
Return 0 if successful, -1 if not. */
69
void (*fn) PARAMS ((void));
71
register struct xatexit *p;
73
/* Tell xexit to call xatexit_cleanup. */
75
_xexit_cleanup = xatexit_cleanup;
78
if (p->ind >= XATEXIT_SIZE)
80
if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL)
83
p->next = xatexit_head;
86
p->fns[p->ind++] = fn;
90
/* Call any cleanup functions. */
95
register struct xatexit *p;
98
for (p = xatexit_head; p; p = p->next)
99
for (n = p->ind; --n >= 0;)