2004-09-10 03:46:54 +08:00
|
|
|
/*
|
|
|
|
* COmmon routine to call call registered atexit-like routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <reent.h>
|
|
|
|
#include "atexit.h"
|
|
|
|
|
2009-04-24 01:54:22 +08:00
|
|
|
/* Make this a weak reference to avoid pulling in free. */
|
|
|
|
void free(void *) _ATTRIBUTE((__weak__));
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
/*
|
|
|
|
* Call registered exit handlers. If D is null then all handlers are called,
|
|
|
|
* otherwise only the handlers from that DSO are called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
_DEFUN (__call_exitprocs, (code, d),
|
|
|
|
int code _AND _PTR d)
|
|
|
|
{
|
|
|
|
register struct _atexit *p;
|
|
|
|
struct _atexit **lastp;
|
|
|
|
register struct _on_exit_args * args;
|
|
|
|
register int n;
|
|
|
|
int i;
|
|
|
|
void (*fn) (void);
|
|
|
|
|
2007-04-06 00:47:38 +08:00
|
|
|
restart:
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
p = _GLOBAL_REENT->_atexit;
|
|
|
|
lastp = &_GLOBAL_REENT->_atexit;
|
|
|
|
while (p)
|
|
|
|
{
|
|
|
|
#ifdef _REENT_SMALL
|
|
|
|
args = p->_on_exit_args_ptr;
|
|
|
|
#else
|
|
|
|
args = &p->_on_exit_args;
|
|
|
|
#endif
|
|
|
|
for (n = p->_ind - 1; n >= 0; n--)
|
|
|
|
{
|
2007-04-06 00:47:38 +08:00
|
|
|
int ind;
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
i = 1 << n;
|
|
|
|
|
|
|
|
/* Skip functions not from this dso. */
|
|
|
|
if (d && (!args || args->_dso_handle[n] != d))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Remove the function now to protect against the
|
|
|
|
function calling exit recursively. */
|
|
|
|
fn = p->_fns[n];
|
|
|
|
if (n == p->_ind - 1)
|
|
|
|
p->_ind--;
|
|
|
|
else
|
|
|
|
p->_fns[n] = NULL;
|
|
|
|
|
|
|
|
/* Skip functions that have already been called. */
|
|
|
|
if (!fn)
|
|
|
|
continue;
|
|
|
|
|
2007-04-06 00:47:38 +08:00
|
|
|
ind = p->_ind;
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
/* Call the function. */
|
|
|
|
if (!args || (args->_fntypes & i) == 0)
|
|
|
|
fn ();
|
|
|
|
else if ((args->_is_cxa & i) == 0)
|
|
|
|
(*((void (*)(int, _PTR)) fn))(code, args->_fnargs[n]);
|
|
|
|
else
|
|
|
|
(*((void (*)(_PTR)) fn))(args->_fnargs[n]);
|
2007-04-06 00:47:38 +08:00
|
|
|
|
|
|
|
/* The function we called call atexit and registered another
|
|
|
|
function (or functions). Call these new functions before
|
|
|
|
continuing with the already registered functions. */
|
|
|
|
if (ind != p->_ind || *lastp != p)
|
|
|
|
goto restart;
|
2004-09-10 03:46:54 +08:00
|
|
|
}
|
|
|
|
|
2006-03-21 08:57:34 +08:00
|
|
|
#ifndef _ATEXIT_DYNAMIC_ALLOC
|
|
|
|
break;
|
|
|
|
#else
|
2009-04-24 01:54:22 +08:00
|
|
|
/* Don't dynamically free the atexit array if free is not
|
|
|
|
available. */
|
|
|
|
if (!free)
|
|
|
|
break;
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
/* Move to the next block. Free empty blocks except the last one,
|
|
|
|
which is part of _GLOBAL_REENT. */
|
|
|
|
if (p->_ind == 0 && p->_next)
|
|
|
|
{
|
|
|
|
/* Remove empty block from the list. */
|
|
|
|
*lastp = p->_next;
|
|
|
|
#ifdef _REENT_SMALL
|
|
|
|
if (args)
|
|
|
|
free (args);
|
|
|
|
#endif
|
|
|
|
free (p);
|
|
|
|
p = *lastp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastp = &p->_next;
|
|
|
|
p = p->_next;
|
|
|
|
}
|
2006-03-21 08:57:34 +08:00
|
|
|
#endif
|
2004-09-10 03:46:54 +08:00
|
|
|
}
|
|
|
|
}
|