home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libiberty / xatexit.c < prev    next >
C/C++ Source or Header  |  1994-02-17  |  2KB  |  74 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * %sccs.include.redist.c%
  6.  */
  7.  
  8. /* Adapted from newlib/libc/stdlib/{,at}exit.[ch].
  9.    If you use xatexit, you must call xexit instead of exit.  */
  10.  
  11. #include "ansidecl.h"
  12. #include "libiberty.h"
  13.  
  14. #include <stdio.h>
  15.  
  16. static void xatexit_cleanup PARAMS ((void));
  17.  
  18. /* Pointer to function run by xexit.  */
  19. extern void (*_xexit_cleanup) ();
  20.  
  21. #define    XATEXIT_SIZE 32
  22.  
  23. struct xatexit {
  24.     struct    xatexit *next;        /* next in list */
  25.     int    ind;            /* next index in this table */
  26.     void    (*fns[XATEXIT_SIZE]) PARAMS ((void));    /* the table itself */
  27. };
  28.  
  29. /* Allocate one struct statically to guarantee that we can register
  30.    at least a few handlers.  */
  31. static struct xatexit xatexit_first;
  32.  
  33. /* Points to head of LIFO stack.  */
  34. static struct xatexit *xatexit_head = &xatexit_first;
  35.  
  36. /* Register function FN to be run by xexit.
  37.    Return 0 if successful, -1 if not.  */
  38.  
  39. int
  40. xatexit (fn)
  41.      void (*fn) PARAMS ((void));
  42. {
  43.   register struct xatexit *p;
  44.  
  45.   /* Tell xexit to call xatexit_cleanup.  */
  46.   if (!_xexit_cleanup)
  47.     _xexit_cleanup = xatexit_cleanup;
  48.  
  49.   p = xatexit_head;
  50.   if (p->ind >= XATEXIT_SIZE)
  51.     {
  52.       if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL)
  53.     return -1;
  54.       p->ind = 0;
  55.       p->next = xatexit_head;
  56.       xatexit_head = p;
  57.     }
  58.   p->fns[p->ind++] = fn;
  59.   return 0;
  60. }
  61.  
  62. /* Call any cleanup functions.  */
  63.  
  64. static void
  65. xatexit_cleanup ()
  66. {
  67.   register struct xatexit *p;
  68.   register int n;
  69.  
  70.   for (p = xatexit_head; p; p = p->next)
  71.     for (n = p->ind; --n >= 0;)
  72.       (*p->fns[n]) ();
  73. }
  74.