CoCoC icon indicating copy to clipboard operation
CoCoC copied to clipboard

KLibc: idea for supporting atexit

Open Deek opened this issue 1 year ago • 0 comments

exit() calls the internal function _tidyup() to clean up, this should be equivalent to modern C's atexit list. We should be able to refactor this code so that there is a linked list of exit functions, something like

struct _exitfun {
    _VOIDPTR _ex_fptr;
    struct _exitfun *_ex_next;
};

by default, there is a single function pointer pointing to the original _tidyup() function, and with a "next" pointer of NULL. Calling atexit(myfunc) allocates a new structure (two pointers) with ibrk(); assigns the head of the exit func list to the address of 'myfunc', then sets the 'next' pointer to the old head.

Inside exit(), we can then do the equivalent of

for (func = top->_ex_fptr; func; func = top = top->_ex_next)
    (*func)();

Deek avatar Jan 16 '23 10:01 Deek