Blame stdlib/cxa_atexit.c

Packit 6c4009
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include "exit.h"
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
#undef __cxa_atexit
Packit 6c4009
Packit 6c4009
/* See concurrency notes in stdlib/exit.h where this lock is declared.  */
Packit 6c4009
__libc_lock_define_initialized (, __exit_funcs_lock)
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
attribute_hidden
Packit 6c4009
__internal_atexit (void (*func) (void *), void *arg, void *d,
Packit 6c4009
		   struct exit_function_list **listp)
Packit 6c4009
{
Packit 6c4009
  struct exit_function *new;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (__exit_funcs_lock);
Packit 6c4009
  new = __new_exitfn (listp);
Packit 6c4009
Packit 6c4009
  if (new == NULL)
Packit 6c4009
    {
Packit 6c4009
      __libc_lock_unlock (__exit_funcs_lock);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef PTR_MANGLE
Packit 6c4009
  PTR_MANGLE (func);
Packit 6c4009
#endif
Packit 6c4009
  new->func.cxa.fn = (void (*) (void *, int)) func;
Packit 6c4009
  new->func.cxa.arg = arg;
Packit 6c4009
  new->func.cxa.dso_handle = d;
Packit 6c4009
  new->flavor = ef_cxa;
Packit 6c4009
  __libc_lock_unlock (__exit_funcs_lock);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Register a function to be called by exit or when a shared library
Packit 6c4009
   is unloaded.  This function is only called from code generated by
Packit 6c4009
   the C++ compiler.  */
Packit 6c4009
int
Packit 6c4009
__cxa_atexit (void (*func) (void *), void *arg, void *d)
Packit 6c4009
{
Packit 6c4009
  return __internal_atexit (func, arg, d, &__exit_funcs);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__cxa_atexit)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static struct exit_function_list initial;
Packit 6c4009
struct exit_function_list *__exit_funcs = &initial;
Packit 6c4009
uint64_t __new_exitfn_called;
Packit 6c4009
Packit 6c4009
/* Must be called with __exit_funcs_lock held.  */
Packit 6c4009
struct exit_function *
Packit 6c4009
__new_exitfn (struct exit_function_list **listp)
Packit 6c4009
{
Packit 6c4009
  struct exit_function_list *p = NULL;
Packit 6c4009
  struct exit_function_list *l;
Packit 6c4009
  struct exit_function *r = NULL;
Packit 6c4009
  size_t i = 0;
Packit 6c4009
Packit 6c4009
  if (__exit_funcs_done)
Packit 6c4009
    /* Exit code is finished processing all registered exit functions,
Packit 6c4009
       therefore we fail this registration.  */
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  for (l = *listp; l != NULL; p = l, l = l->next)
Packit 6c4009
    {
Packit 6c4009
      for (i = l->idx; i > 0; --i)
Packit 6c4009
	if (l->fns[i - 1].flavor != ef_free)
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
      if (i > 0)
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      /* This block is completely unused.  */
Packit 6c4009
      l->idx = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (l == NULL || i == sizeof (l->fns) / sizeof (l->fns[0]))
Packit 6c4009
    {
Packit 6c4009
      /* The last entry in a block is used.  Use the first entry in
Packit 6c4009
	 the previous block if it exists.  Otherwise create a new one.  */
Packit 6c4009
      if (p == NULL)
Packit 6c4009
	{
Packit 6c4009
	  assert (l != NULL);
Packit 6c4009
	  p = (struct exit_function_list *)
Packit 6c4009
	    calloc (1, sizeof (struct exit_function_list));
Packit 6c4009
	  if (p != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      p->next = *listp;
Packit 6c4009
	      *listp = p;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (p != NULL)
Packit 6c4009
	{
Packit 6c4009
	  r = &p->fns[0];
Packit 6c4009
	  p->idx = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* There is more room in the block.  */
Packit 6c4009
      r = &l->fns[i];
Packit 6c4009
      l->idx = i + 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Mark entry as used, but we don't know the flavor now.  */
Packit 6c4009
  if (r != NULL)
Packit 6c4009
    {
Packit 6c4009
      r->flavor = ef_us;
Packit 6c4009
      ++__new_exitfn_called;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return r;
Packit 6c4009
}