Skip to content

KLibc: idea for supporting atexit #18

@Deek

Description

@Deek

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)();

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions