on_exit.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright (C) 1999-2022 Free Software Foundation, Inc.
  2. NOTE: This source is derived from an old version taken from the GNU C
  3. Library (glibc).
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include <stdlib.h>
  21. #include "exit.h"
  22. #define atomic_write_barrier() __asm__ ("eieio" ::: "memory")
  23. /* Register a function to be called by exit. */
  24. int
  25. on_exit (void (*func) (int status, void *arg), void *arg)
  26. {
  27. struct exit_function *new = __new_exitfn (&__exit_funcs);
  28. if (new == NULL)
  29. return -1;
  30. #ifdef PTR_MANGLE
  31. PTR_MANGLE (func);
  32. #endif
  33. new->func.on.fn = func;
  34. new->func.on.arg = arg;
  35. atomic_write_barrier ();
  36. new->flavor = ef_on;
  37. return 0;
  38. }