#include #include #define COUNT 32 static struct cleaner { struct cleaner *next; void (*clean[COUNT])(void *); void *arg[COUNT]; } arena, *head; static int slot; void weaklink exit(int code){}; void rt·clean(void) { void (*clean)(void *), *arg; /*LOCK*/ for(; head; head=head->next, slot=COUNT) { while(slot-- > 0){ arg = head->arg[slot]; clean = head->clean[slot]; /* UNLOCK */ clean(arg); /* LOCK */ } } } int rt·atexit(void (*clean)(void *), void *arg) { /* LOCK */ if(!head) head = &arena; /* we need malloc... */ if(slot==COUNT) return -1; head->arg[slot] = arg; head->clean[slot] = clean; slot++; /* UNLOCK */ return 0; }