1/* Generic atexit() 2 Copyright (C) 1996 Free Software Foundation, Inc. 3 4This file is free software; you can redistribute it and/or modify it 5under the terms of the GNU General Public License as published by the 6Free Software Foundation; either version 2, or (at your option) any 7later version. 8 9In addition to the permissions in the GNU General Public License, the 10Free Software Foundation gives you unlimited permission to link the 11compiled version of this file with other programs, and to distribute 12those programs without any restriction coming from the use of this 13file. (The General Public License restrictions do apply in other 14respects; for example, they cover modification of the file, and 15distribution when not linked into another program.) 16 17This file is distributed in the hope that it will be useful, but 18WITHOUT ANY WARRANTY; without even the implied warranty of 19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20General Public License for more details. 21 22You should have received a copy of the GNU General Public License 23along with this program; see the file COPYING. If not, write to 24the Free Software Foundation, 59 Temple Place - Suite 330, 25Boston, 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 40void exit(int) __attribute__((noreturn)); 41void _exit(int) __attribute__((noreturn)); 42void _cleanup(void); 43 44 45#define FNS_PER_BLOCK 32 46 47struct 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 */ 56static struct atexit_fn_block atexit_fns; 57static struct atexit_fn_block *current_block = &atexit_fns; 58 59 60int 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 80void 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} 100