Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / libiberty / xatexit.c
blob728254b2c0d818bf54e6ae172ccf162d291716b8
1 /*
2 * Copyright (c) 1990 Regents of the University of California.
3 * All rights reserved.
5 * %sccs.include.redist.c%
6 */
9 /*
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.
18 @end deftypefun
22 /* Adapted from newlib/libc/stdlib/{,at}exit.[ch].
23 If you use xatexit, you must call xexit instead of exit. */
25 #include "ansidecl.h"
26 #include "libiberty.h"
28 #include <stdio.h>
30 #ifdef __STDC__
31 #include <stddef.h>
32 #else
33 #define size_t unsigned long
34 #endif
36 /* For systems with larger pointers than ints, this must be declared. */
37 PTR malloc PARAMS ((size_t));
39 static void xatexit_cleanup PARAMS ((void));
41 /* Pointer to function run by xexit. */
42 extern void (*_xexit_cleanup) PARAMS ((void));
44 #define XATEXIT_SIZE 32
46 struct xatexit {
47 struct xatexit *next; /* next in list */
48 int ind; /* next index in this table */
49 void (*fns[XATEXIT_SIZE]) PARAMS ((void)); /* the table itself */
52 /* Allocate one struct statically to guarantee that we can register
53 at least a few handlers. */
54 static struct xatexit xatexit_first;
56 /* Points to head of LIFO stack. */
57 static struct xatexit *xatexit_head = &xatexit_first;
59 /* Register function FN to be run by xexit.
60 Return 0 if successful, -1 if not. */
62 int
63 xatexit (fn)
64 void (*fn) PARAMS ((void));
66 register struct xatexit *p;
68 /* Tell xexit to call xatexit_cleanup. */
69 if (!_xexit_cleanup)
70 _xexit_cleanup = xatexit_cleanup;
72 p = xatexit_head;
73 if (p->ind >= XATEXIT_SIZE)
75 if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL)
76 return -1;
77 p->ind = 0;
78 p->next = xatexit_head;
79 xatexit_head = p;
81 p->fns[p->ind++] = fn;
82 return 0;
85 /* Call any cleanup functions. */
87 static void
88 xatexit_cleanup ()
90 register struct xatexit *p;
91 register int n;
93 for (p = xatexit_head; p; p = p->next)
94 for (n = p->ind; --n >= 0;)
95 (*p->fns[n]) ();