*** empty log message ***
[deliverable/binutils-gdb.git] / libiberty / xatexit.c
CommitLineData
252b5132
RH
1/*
2 * Copyright (c) 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
39423523
DD
8
9/*
10
11@deftypefun int xatexit (void (*@var{fn}) (void))
12
13Behaves as the standard @code{atexit} function, but with no limit on
99b58139 14the number of registered functions. Returns 0 on success, or @minus{}1 on
39423523
DD
15failure. If you use @code{xatexit} to register functions, you must use
16@code{xexit} to terminate your program.
17
18@end deftypefun
19
20*/
21
252b5132
RH
22/* Adapted from newlib/libc/stdlib/{,at}exit.[ch].
23 If you use xatexit, you must call xexit instead of exit. */
24
25#include "ansidecl.h"
26#include "libiberty.h"
27
28#include <stdio.h>
29
252b5132 30#include <stddef.h>
252b5132 31
dabc64ea
DD
32#if VMS
33#include <stdlib.h>
34#include <unixlib.h>
35#else
252b5132 36/* For systems with larger pointers than ints, this must be declared. */
49b1fae4 37PTR malloc (size_t);
dabc64ea 38#endif
252b5132 39
49b1fae4 40static void xatexit_cleanup (void);
252b5132
RH
41
42/* Pointer to function run by xexit. */
49b1fae4 43extern void (*_xexit_cleanup) (void);
252b5132
RH
44
45#define XATEXIT_SIZE 32
46
47struct xatexit {
48 struct xatexit *next; /* next in list */
49 int ind; /* next index in this table */
49b1fae4 50 void (*fns[XATEXIT_SIZE]) (void); /* the table itself */
252b5132
RH
51};
52
53/* Allocate one struct statically to guarantee that we can register
54 at least a few handlers. */
55static struct xatexit xatexit_first;
56
57/* Points to head of LIFO stack. */
58static struct xatexit *xatexit_head = &xatexit_first;
59
60/* Register function FN to be run by xexit.
61 Return 0 if successful, -1 if not. */
62
63int
49b1fae4 64xatexit (void (*fn) (void))
252b5132
RH
65{
66 register struct xatexit *p;
67
68 /* Tell xexit to call xatexit_cleanup. */
69 if (!_xexit_cleanup)
70 _xexit_cleanup = xatexit_cleanup;
71
72 p = xatexit_head;
73 if (p->ind >= XATEXIT_SIZE)
74 {
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;
80 }
81 p->fns[p->ind++] = fn;
82 return 0;
83}
84
85/* Call any cleanup functions. */
86
87static void
49b1fae4 88xatexit_cleanup (void)
252b5132
RH
89{
90 register struct xatexit *p;
91 register int n;
92
93 for (p = xatexit_head; p; p = p->next)
94 for (n = p->ind; --n >= 0;)
95 (*p->fns[n]) ();
96}
This page took 0.274841 seconds and 4 git commands to generate.