* config/alpha/alpha.md, config/arm/arm.c, config/arm/arm.h,
[official-gcc.git] / gcc / config / m68k / aux-exit.c
blob1d7d6c0fd80bbe35735fd59e773268e390c5acc7
1 /* Generic atexit()
2 Copyright (C) 1996 Free Software Foundation, Inc.
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.
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 into combinations with other programs,
12 and to distribute those combinations without any restriction coming
13 from the use of this file. (The General Public License restrictions
14 do apply in other respects; for example, they cover modification of
15 the file, and distribution when not linked into a combine
16 executable.)
18 This file is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
28 /* Rather than come up with some ugly hack to make mcrt1 work, it is
29 better to just go ahead and provide atexit(). */
32 #include <stdlib.h>
35 void exit(int) __attribute__((noreturn));
36 void _exit(int) __attribute__((noreturn));
37 void _cleanup(void);
40 #define FNS_PER_BLOCK 32
42 struct atexit_fn_block
44 struct atexit_fn_block *next;
45 void (*fns[FNS_PER_BLOCK])(void);
46 short used;
50 /* statically allocate the first block */
51 static struct atexit_fn_block atexit_fns;
52 static struct atexit_fn_block *current_block = &atexit_fns;
55 int atexit(void (*fn)(void))
57 if (current_block->used >= FNS_PER_BLOCK)
59 struct atexit_fn_block *new_block =
60 (struct atexit_fn_block *)malloc(sizeof(struct atexit_fn_block));
61 if (new_block == NULL)
62 return -1;
64 new_block->used = 0;
65 new_block->next = current_block;
66 current_block = new_block;
69 current_block->fns[current_block->used++] = fn;
71 return 0;
75 void exit(int status)
77 struct atexit_fn_block *block = current_block, *old_block;
78 short i;
80 while (1)
82 for (i = block->used; --i >= 0 ;)
83 (*block->fns[i])();
84 if (block == &atexit_fns)
85 break;
86 /* I know what you are thinking -- we are about to exit, why free?
87 Because it is friendly to memory leak detectors, that's why. */
88 old_block = block;
89 block = block->next;
90 free(old_block);
93 _exit(status);