Split libc/stdlib/on_exit.cpp into multiple files.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-10-21 20:11:34 +02:00
parent 8018a85a30
commit b9e463965b
5 changed files with 60 additions and 34 deletions

View File

@ -328,6 +328,7 @@ stdio/tmpnam.o \
stdio/vfprintf.o \
stdio/vprintf.o \
stdio/vscanf.o \
stdlib/atexit.o \
stdlib/canonicalize_file_name_at.o \
stdlib/canonicalize_file_name.o \
stdlib/env.o \

View File

@ -128,6 +128,16 @@ char* getenv(const char*);
int clearenv(void);
#endif
#if __is_sortix_libc
struct exit_handler
{
void (*hook)(int, void*);
void* param;
struct exit_handler* next;
};
extern struct exit_handler* __exit_handler_stack;
#endif
/* TODO: These are not implemented in sortix libc yet. */
#if 0
long a64l(const char* s);

35
libc/stdlib/atexit.cpp Normal file
View File

@ -0,0 +1,35 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
stdlib/atexit.cpp
Hooks that are called upon process exit.
*******************************************************************************/
#include <stdlib.h>
static void atexit_adapter(int /*status*/, void* user)
{
((void (*)(void)) user)();
}
extern "C" int atexit(void (*hook)(void))
{
return on_exit(atexit_adapter, (void*) hook);
}

View File

@ -26,11 +26,15 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" void call_exit_handlers(int status);
extern "C" { struct exit_handler* __exit_handler_stack = NULL; }
extern "C" void exit(int status)
{
call_exit_handlers(status);
while ( __exit_handler_stack )
{
__exit_handler_stack->hook(status, __exit_handler_stack->param);
__exit_handler_stack = __exit_handler_stack->next;
}
dcloseall();
fcloseall();
_Exit(status);

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
@ -18,45 +18,21 @@
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
stdlib/on_exit.cpp
Hooks that is called upon process exit.
Hooks that are called upon process exit.
*******************************************************************************/
#include <stdlib.h>
struct exithandler
{
void (*hook)(int, void*);
void* param;
struct exithandler* next;
}* exit_handler_stack = NULL;
extern "C" int on_exit(void (*hook)(int, void*), void* param)
{
struct exithandler* handler = (struct exithandler*) malloc(sizeof(struct exithandler));
if ( !handler ) { return -1; }
struct exit_handler* handler =
(struct exit_handler*) malloc(sizeof(struct exit_handler));
if ( !handler )
return -1;
handler->hook = hook;
handler->param = param;
handler->next = exit_handler_stack;
exit_handler_stack = handler;
handler->next = __exit_handler_stack;
__exit_handler_stack = handler;
return 0;
}
static void atexit_adapter(int /*status*/, void* user)
{
((void (*)(void)) user)();
}
extern "C" int atexit(void (*hook)(void))
{
return on_exit(atexit_adapter, (void*) hook);
}
extern "C" void call_exit_handlers(int status)
{
while ( exit_handler_stack )
{
exit_handler_stack->hook(status, exit_handler_stack->param);
exit_handler_stack = exit_handler_stack->next;
}
}