Back to index

glibc  2.9
cxa_atexit.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2001, 2002, 2005, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <assert.h>
00020 #include <stdlib.h>
00021 
00022 #include <bits/libc-lock.h>
00023 #include "exit.h"
00024 #include <atomic.h>
00025 #include <sysdep.h>
00026 
00027 #undef __cxa_atexit
00028 
00029 /* Register a function to be called by exit or when a shared library
00030    is unloaded.  This function is only called from code generated by
00031    the C++ compiler.  */
00032 int
00033 __cxa_atexit (void (*func) (void *), void *arg, void *d)
00034 {
00035   struct exit_function *new = __new_exitfn ();
00036 
00037   if (new == NULL)
00038     return -1;
00039 
00040 #ifdef PTR_MANGLE
00041   PTR_MANGLE (func);
00042 #endif
00043   new->func.cxa.fn = (void (*) (void *, int)) func;
00044   new->func.cxa.arg = arg;
00045   new->func.cxa.dso_handle = d;
00046   atomic_write_barrier ();
00047   new->flavor = ef_cxa;
00048   return 0;
00049 }
00050 INTDEF(__cxa_atexit)
00051 
00052 
00053 /* We change global data, so we need locking.  */
00054 __libc_lock_define_initialized (static, lock)
00055 
00056 
00057 static struct exit_function_list initial;
00058 struct exit_function_list *__exit_funcs = &initial;
00059 uint64_t __new_exitfn_called;
00060 
00061 struct exit_function *
00062 __new_exitfn (void)
00063 {
00064   struct exit_function_list *p = NULL;
00065   struct exit_function_list *l;
00066   struct exit_function *r = NULL;
00067   size_t i = 0;
00068 
00069   __libc_lock_lock (lock);
00070 
00071   for (l = __exit_funcs; l != NULL; p = l, l = l->next)
00072     {
00073       for (i = l->idx; i > 0; --i)
00074        if (l->fns[i - 1].flavor != ef_free)
00075          break;
00076 
00077       if (i > 0)
00078        break;
00079 
00080       /* This block is completely unused.  */
00081       l->idx = 0;
00082     }
00083 
00084   if (l == NULL || i == sizeof (l->fns) / sizeof (l->fns[0]))
00085     {
00086       /* The last entry in a block is used.  Use the first entry in
00087         the previous block if it exists.  Otherwise create a new one.  */
00088       if (p == NULL)
00089        {
00090          assert (l != NULL);
00091          p = (struct exit_function_list *)
00092            calloc (1, sizeof (struct exit_function_list));
00093          if (p != NULL)
00094            {
00095              p->next = __exit_funcs;
00096              __exit_funcs = p;
00097            }
00098        }
00099 
00100       if (p != NULL)
00101        {
00102          r = &p->fns[0];
00103          p->idx = 1;
00104        }
00105     }
00106   else
00107     {
00108       /* There is more room in the block.  */
00109       r = &l->fns[i];
00110       l->idx = i + 1;
00111     }
00112 
00113   /* Mark entry as used, but we don't know the flavor now.  */
00114   if (r != NULL)
00115     {
00116       r->flavor = ef_us;
00117       ++__new_exitfn_called;
00118     }
00119 
00120   __libc_lock_unlock (lock);
00121 
00122   return r;
00123 }