From cfa0985d64957ddf8ef40873ce250625e4f0880f Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 14 Oct 2015 07:39:35 +0200 Subject: [PATCH] C11 quick_exit() support for Import some function declarations from latest FreeBSD and implement them. I am not sure if we should call the global reent cleanup in quick_exit() similar to exit(). newlib/ChangeLog 2015-10-14 Sebastian Huber * libc/include/stdlib.h (at_quick_exit): Declare. (quick_exit): Likewise. * libc/stdlib/Makefile.am (GENERAL_SOURCES): Add quick_exit.c. * libc/stdlib/Makefile.in: Regenerate. * libc/stdlib/quick_exit.c: New. --- newlib/ChangeLog | 9 ++++ newlib/libc/include/stdlib.h | 9 ++++ newlib/libc/stdlib/Makefile.am | 1 + newlib/libc/stdlib/Makefile.in | 41 ++++++++++------- newlib/libc/stdlib/quick_exit.c | 81 +++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 newlib/libc/stdlib/quick_exit.c diff --git a/newlib/ChangeLog b/newlib/ChangeLog index eb5e3c798..9e78fea33 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,12 @@ +2015-10-14 Sebastian Huber + + * libc/include/stdlib.h (at_quick_exit): Declare. + (quick_exit): Likewise. + * libc/stdlib/Makefile.am (GENERAL_SOURCES): Add + quick_exit.c. + * libc/stdlib/Makefile.in: Regenerate. + * libc/stdlib/quick_exit.c: New. + 2015-10-13 Sebastian Huber * libc/include/sys/cdefs.h: Synchronize with latest FreeBSD diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h index 38ac29654..859c84494 100644 --- a/newlib/libc/include/stdlib.h +++ b/newlib/libc/include/stdlib.h @@ -280,6 +280,15 @@ extern long double strtold (const char *__restrict, char **__restrict); #endif #endif /* _HAVE_LONG_DOUBLE */ +/* + * If we're in a mode greater than C99, expose C11 functions. + */ +#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L +int at_quick_exit(void (*)(void)); +_Noreturn void + quick_exit(int); +#endif /* __ISO_C_VISIBLE >= 2011 */ + _END_STD_C #endif /* _STDLIB_H_ */ diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index c86010add..5c0e14290 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -45,6 +45,7 @@ GENERAL_SOURCES = \ mlock.c \ mprec.c \ mstats.c \ + quick_exit.c \ rand.c \ rand_r.c \ realloc.c \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index 34da1cbaa..f1f9e3b8c 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -100,15 +100,16 @@ am__objects_2 = lib_a-__adjust.$(OBJEXT) lib_a-__atexit.$(OBJEXT) \ lib_a-mbstowcs.$(OBJEXT) lib_a-mbstowcs_r.$(OBJEXT) \ lib_a-mbtowc.$(OBJEXT) lib_a-mbtowc_r.$(OBJEXT) \ lib_a-mlock.$(OBJEXT) lib_a-mprec.$(OBJEXT) \ - lib_a-mstats.$(OBJEXT) lib_a-rand.$(OBJEXT) \ - lib_a-rand_r.$(OBJEXT) lib_a-realloc.$(OBJEXT) \ - lib_a-reallocf.$(OBJEXT) lib_a-sb_charsets.$(OBJEXT) \ - lib_a-strtod.$(OBJEXT) lib_a-strtol.$(OBJEXT) \ - lib_a-strtoul.$(OBJEXT) lib_a-utoa.$(OBJEXT) \ - lib_a-wcstod.$(OBJEXT) lib_a-wcstol.$(OBJEXT) \ - lib_a-wcstoul.$(OBJEXT) lib_a-wcstombs.$(OBJEXT) \ - lib_a-wcstombs_r.$(OBJEXT) lib_a-wctomb.$(OBJEXT) \ - lib_a-wctomb_r.$(OBJEXT) $(am__objects_1) + lib_a-mstats.$(OBJEXT) lib_a-quick_exit.$(OBJEXT) \ + lib_a-rand.$(OBJEXT) lib_a-rand_r.$(OBJEXT) \ + lib_a-realloc.$(OBJEXT) lib_a-reallocf.$(OBJEXT) \ + lib_a-sb_charsets.$(OBJEXT) lib_a-strtod.$(OBJEXT) \ + lib_a-strtol.$(OBJEXT) lib_a-strtoul.$(OBJEXT) \ + lib_a-utoa.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \ + lib_a-wcstol.$(OBJEXT) lib_a-wcstoul.$(OBJEXT) \ + lib_a-wcstombs.$(OBJEXT) lib_a-wcstombs_r.$(OBJEXT) \ + lib_a-wctomb.$(OBJEXT) lib_a-wctomb_r.$(OBJEXT) \ + $(am__objects_1) am__objects_3 = lib_a-cxa_atexit.$(OBJEXT) \ lib_a-cxa_finalize.$(OBJEXT) lib_a-drand48.$(OBJEXT) \ lib_a-ecvtbuf.$(OBJEXT) lib_a-efgcvt.$(OBJEXT) \ @@ -154,10 +155,10 @@ am__objects_9 = __adjust.lo __atexit.lo __call_atexit.lo __exp10.lo \ gdtoa-gethex.lo gdtoa-hexnan.lo getenv.lo getenv_r.lo itoa.lo \ labs.lo ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo \ mbstowcs.lo mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo \ - mprec.lo mstats.lo rand.lo rand_r.lo realloc.lo reallocf.lo \ - sb_charsets.lo strtod.lo strtol.lo strtoul.lo utoa.lo \ - wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo \ - wctomb.lo wctomb_r.lo $(am__objects_8) + mprec.lo mstats.lo quick_exit.lo rand.lo rand_r.lo realloc.lo \ + reallocf.lo sb_charsets.lo strtod.lo strtol.lo strtoul.lo \ + utoa.lo wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo \ + wcstombs_r.lo wctomb.lo wctomb_r.lo $(am__objects_8) am__objects_10 = cxa_atexit.lo cxa_finalize.lo drand48.lo ecvtbuf.lo \ efgcvt.lo erand48.lo jrand48.lo lcong48.lo lrand48.lo \ mrand48.lo msize.lo mtrim.lo nrand48.lo rand48.lo seed48.lo \ @@ -357,10 +358,10 @@ GENERAL_SOURCES = __adjust.c __atexit.c __call_atexit.c __exp10.c \ environ.c envlock.c eprintf.c exit.c gdtoa-gethex.c \ gdtoa-hexnan.c getenv.c getenv_r.c itoa.c labs.c ldiv.c \ ldtoa.c malloc.c mblen.c mblen_r.c mbstowcs.c mbstowcs_r.c \ - mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c rand.c rand_r.c \ - realloc.c reallocf.c sb_charsets.c strtod.c strtol.c strtoul.c \ - utoa.c wcstod.c wcstol.c wcstoul.c wcstombs.c wcstombs_r.c \ - wctomb.c wctomb_r.c $(am__append_1) + mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c quick_exit.c \ + rand.c rand_r.c realloc.c reallocf.c sb_charsets.c strtod.c \ + strtol.c strtoul.c utoa.c wcstod.c wcstol.c wcstoul.c \ + wcstombs.c wcstombs_r.c wctomb.c wctomb_r.c $(am__append_1) @NEWLIB_NANO_MALLOC_FALSE@MALIGNR = malignr @NEWLIB_NANO_MALLOC_TRUE@MALIGNR = nano-malignr @NEWLIB_NANO_MALLOC_FALSE@MALLOPTR = malloptr @@ -839,6 +840,12 @@ lib_a-mstats.o: mstats.c lib_a-mstats.obj: mstats.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-mstats.obj `if test -f 'mstats.c'; then $(CYGPATH_W) 'mstats.c'; else $(CYGPATH_W) '$(srcdir)/mstats.c'; fi` +lib_a-quick_exit.o: quick_exit.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-quick_exit.o `test -f 'quick_exit.c' || echo '$(srcdir)/'`quick_exit.c + +lib_a-quick_exit.obj: quick_exit.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-quick_exit.obj `if test -f 'quick_exit.c'; then $(CYGPATH_W) 'quick_exit.c'; else $(CYGPATH_W) '$(srcdir)/quick_exit.c'; fi` + lib_a-rand.o: rand.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-rand.o `test -f 'rand.c' || echo '$(srcdir)/'`rand.c diff --git a/newlib/libc/stdlib/quick_exit.c b/newlib/libc/stdlib/quick_exit.c new file mode 100644 index 000000000..fc435711e --- /dev/null +++ b/newlib/libc/stdlib/quick_exit.c @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2011 David Chisnall + * Copyright (c) 2015 embedded brains GmbH + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include + +/** + * Linked list of quick exit handlers. This is simpler than the atexit() + * version, because it is not required to support C++ destructors or + * DSO-specific cleanups. + */ +struct quick_exit_handler { + struct quick_exit_handler *next; + void (*cleanup)(void); +}; + +/** + * Lock protecting the handlers list. + */ +__LOCK_INIT(static, atexit_mutex); +/** + * Stack of cleanup handlers. These will be invoked in reverse order when + */ +static struct quick_exit_handler *handlers; + +int +at_quick_exit(void (*func)(void)) +{ + struct quick_exit_handler *h; + + h = malloc(sizeof(*h)); + + if (NULL == h) + return (1); + h->cleanup = func; + __lock_acquire(atexit_mutex); + h->next = handlers; + handlers = h; + __lock_release(atexit_mutex); + return (0); +} + +void +quick_exit(int status) +{ + struct quick_exit_handler *h; + + /* + * XXX: The C++ spec requires us to call std::terminate if there is an + * exception here. + */ + for (h = handlers; NULL != h; h = h->next) + h->cleanup(); + _exit(status); +} -- 2.43.5