OSDN Git Service

* rtl.h (addr_diff_vec_flags): New typedef.
[pf3gnuchains/gcc-fork.git] / gcc / config / m68k / aux-exit.c
1 /* Generic atexit()
2    Copyright (C) 1996 Free Software Foundation, Inc.
3
4 This file is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 In addition to the permissions in the GNU General Public License, the
10 Free Software Foundation gives you unlimited permission to link the
11 compiled version of this file with other programs, and to distribute
12 those programs without any restriction coming from the use of this
13 file.  (The General Public License restrictions do apply in other
14 respects; for example, they cover modification of the file, and
15 distribution when not linked into another program.)
16
17 This file is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; see the file COPYING.  If not, write to
24 the Free Software Foundation, 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.  */
26
27 /* As a special exception, if you link this library with files
28    compiled with GCC to produce an executable, this does not cause
29    the resulting executable to be covered by the GNU General Public License.
30    This exception does not however invalidate any other reasons why
31    the executable file might be covered by the GNU General Public License.  */
32
33 /* Rather than come up with some ugly hack to make mcrt1 work, it is
34    better to just go ahead and provide atexit().  */
35
36
37 #include <stdlib.h>
38
39
40 void exit(int) __attribute__((noreturn));
41 void _exit(int) __attribute__((noreturn));
42 void _cleanup(void);
43
44
45 #define FNS_PER_BLOCK   32
46
47 struct atexit_fn_block
48 {
49   struct atexit_fn_block *next;
50   void (*fns[FNS_PER_BLOCK])(void);
51   short used;
52 };
53
54
55 /* staticly allocate the first block */
56 static struct atexit_fn_block atexit_fns;
57 static struct atexit_fn_block *current_block = &atexit_fns;
58
59
60 int atexit(void (*fn)(void))
61 {
62   if (current_block->used >= FNS_PER_BLOCK)
63     {
64       struct atexit_fn_block *new_block = 
65         (struct atexit_fn_block *)malloc(sizeof(struct atexit_fn_block));
66       if (new_block == NULL)
67         return -1;
68
69       new_block->used = 0;
70       new_block->next = current_block;
71       current_block = new_block;
72     }
73
74   current_block->fns[current_block->used++] = fn;
75
76   return 0;
77 }
78
79
80 void exit(int status)
81 {
82   struct atexit_fn_block *block = current_block, *old_block;
83   short i;
84
85   while (1)
86     {
87       for (i = block->used; --i >= 0 ;)
88         (*block->fns[i])();
89       if (block == &atexit_fns)
90         break;
91       /* I know what you are thinking -- we are about to exit, why free?
92          Because it is friendly to memory leak detectors, that's why. */
93       old_block = block;
94       block = block->next;
95       free(old_block);
96     }
97
98   _exit(status);
99 }