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"
36 /* For systems with larger pointers than ints, this must be declared. */
40 static void xatexit_cleanup (void);
42 /* Pointer to function run by xexit. */
43 extern void (*_xexit_cleanup) (void);
45 #define XATEXIT_SIZE 32
48 struct xatexit *next; /* next in list */
49 int ind; /* next index in this table */
50 void (*fns[XATEXIT_SIZE]) (void); /* the table itself */
53 /* Allocate one struct statically to guarantee that we can register
54 at least a few handlers. */
55 static struct xatexit xatexit_first;
57 /* Points to head of LIFO stack. */
58 static struct xatexit *xatexit_head = &xatexit_first;
60 /* Register function FN to be run by xexit.
61 Return 0 if successful, -1 if not. */
64 xatexit (void (*fn) (void))
66 register struct xatexit *p;
68 /* Tell xexit to call xatexit_cleanup. */
70 _xexit_cleanup = xatexit_cleanup;
73 if (p->ind >= XATEXIT_SIZE)
75 if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL)
78 p->next = xatexit_head;
81 p->fns[p->ind++] = fn;
85 /* Call any cleanup functions. */
88 xatexit_cleanup (void)
90 register struct xatexit *p;
93 for (p = xatexit_head; p; p = p->next)
94 for (n = p->ind; --n >= 0;)