[PATCH v2] C11 support for <stdlib.h>

Sebastian Huber sebastian.huber@embedded-brains.de
Tue Sep 29 06:33:00 GMT 2015


Import some <stdlib.h> function declarations from latest FreeBSD and
implement them.  aligned_alloc() is implemented in terms of
posix_memalign() which is not defined in Newlib.  I am not sure if we
should call the global reent cleanup in quick_exit() similar to exit().

newlib/ChangeLog
2015-09-29  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	* libc/include/stdlib.h (alloc_aligned): Declare.
	(at_quick_exit): Likewise.
	(quick_exit): Likewise.
	* libc/stdlib/Makefile.am (GENERAL_SOURCES): Add
	alloc_aligned.c and quick_exit.c.
	* libc/stdlib/Makefile.in: Regenerate.
	* libc/stdlib/aligned_alloc.c: New.
	* libc/stdlib/quick_exit.c: Likewise.
---
 newlib/libc/include/stdlib.h       | 11 +++++
 newlib/libc/stdlib/Makefile.am     |  2 +
 newlib/libc/stdlib/Makefile.in     | 84 ++++++++++++++++++++++----------------
 newlib/libc/stdlib/aligned_alloc.c | 38 +++++++++++++++++
 newlib/libc/stdlib/quick_exit.c    | 81 ++++++++++++++++++++++++++++++++++++
 5 files changed, 181 insertions(+), 35 deletions(-)
 create mode 100644 newlib/libc/stdlib/aligned_alloc.c
 create mode 100644 newlib/libc/stdlib/quick_exit.c

diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h
index 38ac296..51a6f0a 100644
--- a/newlib/libc/include/stdlib.h
+++ b/newlib/libc/include/stdlib.h
@@ -280,6 +280,17 @@ 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
+void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
+	    __alloc_size(2);
+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 c86010a..ead5dd1 100644
--- a/newlib/libc/stdlib/Makefile.am
+++ b/newlib/libc/stdlib/Makefile.am
@@ -13,6 +13,7 @@ GENERAL_SOURCES = \
 	_Exit.c		\
 	abort.c  	\
 	abs.c 		\
+	aligned_alloc.c	\
 	assert.c  	\
 	atexit.c	\
 	atof.c 		\
@@ -45,6 +46,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 34da1cb..5f21688 100644
--- a/newlib/libc/stdlib/Makefile.in
+++ b/newlib/libc/stdlib/Makefile.in
@@ -85,30 +85,32 @@ am__objects_2 = lib_a-__adjust.$(OBJEXT) lib_a-__atexit.$(OBJEXT) \
 	lib_a-__call_atexit.$(OBJEXT) lib_a-__exp10.$(OBJEXT) \
 	lib_a-__ten_mu.$(OBJEXT) lib_a-_Exit.$(OBJEXT) \
 	lib_a-abort.$(OBJEXT) lib_a-abs.$(OBJEXT) \
-	lib_a-assert.$(OBJEXT) lib_a-atexit.$(OBJEXT) \
-	lib_a-atof.$(OBJEXT) lib_a-atoff.$(OBJEXT) \
-	lib_a-atoi.$(OBJEXT) lib_a-atol.$(OBJEXT) \
-	lib_a-calloc.$(OBJEXT) lib_a-div.$(OBJEXT) \
-	lib_a-dtoa.$(OBJEXT) lib_a-dtoastub.$(OBJEXT) \
-	lib_a-environ.$(OBJEXT) lib_a-envlock.$(OBJEXT) \
-	lib_a-eprintf.$(OBJEXT) lib_a-exit.$(OBJEXT) \
-	lib_a-gdtoa-gethex.$(OBJEXT) lib_a-gdtoa-hexnan.$(OBJEXT) \
-	lib_a-getenv.$(OBJEXT) lib_a-getenv_r.$(OBJEXT) \
-	lib_a-itoa.$(OBJEXT) lib_a-labs.$(OBJEXT) lib_a-ldiv.$(OBJEXT) \
+	lib_a-aligned_alloc.$(OBJEXT) lib_a-assert.$(OBJEXT) \
+	lib_a-atexit.$(OBJEXT) lib_a-atof.$(OBJEXT) \
+	lib_a-atoff.$(OBJEXT) lib_a-atoi.$(OBJEXT) \
+	lib_a-atol.$(OBJEXT) lib_a-calloc.$(OBJEXT) \
+	lib_a-div.$(OBJEXT) lib_a-dtoa.$(OBJEXT) \
+	lib_a-dtoastub.$(OBJEXT) lib_a-environ.$(OBJEXT) \
+	lib_a-envlock.$(OBJEXT) lib_a-eprintf.$(OBJEXT) \
+	lib_a-exit.$(OBJEXT) lib_a-gdtoa-gethex.$(OBJEXT) \
+	lib_a-gdtoa-hexnan.$(OBJEXT) lib_a-getenv.$(OBJEXT) \
+	lib_a-getenv_r.$(OBJEXT) lib_a-itoa.$(OBJEXT) \
+	lib_a-labs.$(OBJEXT) lib_a-ldiv.$(OBJEXT) \
 	lib_a-ldtoa.$(OBJEXT) lib_a-malloc.$(OBJEXT) \
 	lib_a-mblen.$(OBJEXT) lib_a-mblen_r.$(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) \
@@ -148,16 +150,16 @@ lib_a_OBJECTS = $(am_lib_a_OBJECTS)
 LTLIBRARIES = $(noinst_LTLIBRARIES)
 @HAVE_LONG_DOUBLE_TRUE@am__objects_8 = strtold.lo wcstold.lo
 am__objects_9 = __adjust.lo __atexit.lo __call_atexit.lo __exp10.lo \
-	__ten_mu.lo _Exit.lo abort.lo abs.lo assert.lo atexit.lo \
-	atof.lo atoff.lo atoi.lo atol.lo calloc.lo div.lo dtoa.lo \
-	dtoastub.lo environ.lo envlock.lo eprintf.lo exit.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 \
+	__ten_mu.lo _Exit.lo abort.lo abs.lo aligned_alloc.lo \
+	assert.lo atexit.lo atof.lo atoff.lo atoi.lo atol.lo calloc.lo \
+	div.lo dtoa.lo dtoastub.lo environ.lo envlock.lo eprintf.lo \
+	exit.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 \
@@ -352,15 +354,15 @@ top_srcdir = @top_srcdir@
 AUTOMAKE_OPTIONS = cygnus
 INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS)
 GENERAL_SOURCES = __adjust.c __atexit.c __call_atexit.c __exp10.c \
-	__ten_mu.c _Exit.c abort.c abs.c assert.c atexit.c atof.c \
-	atoff.c atoi.c atol.c calloc.c div.c dtoa.c dtoastub.c \
-	environ.c envlock.c eprintf.c exit.c gdtoa-gethex.c \
+	__ten_mu.c _Exit.c abort.c abs.c aligned_alloc.c assert.c \
+	atexit.c atof.c atoff.c atoi.c atol.c calloc.c div.c dtoa.c \
+	dtoastub.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
@@ -647,6 +649,12 @@ lib_a-abs.o: abs.c
 lib_a-abs.obj: abs.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-abs.obj `if test -f 'abs.c'; then $(CYGPATH_W) 'abs.c'; else $(CYGPATH_W) '$(srcdir)/abs.c'; fi`
 
+lib_a-aligned_alloc.o: aligned_alloc.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-aligned_alloc.o `test -f 'aligned_alloc.c' || echo '$(srcdir)/'`aligned_alloc.c
+
+lib_a-aligned_alloc.obj: aligned_alloc.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-aligned_alloc.obj `if test -f 'aligned_alloc.c'; then $(CYGPATH_W) 'aligned_alloc.c'; else $(CYGPATH_W) '$(srcdir)/aligned_alloc.c'; fi`
+
 lib_a-assert.o: assert.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-assert.o `test -f 'assert.c' || echo '$(srcdir)/'`assert.c
 
@@ -839,6 +847,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/aligned_alloc.c b/newlib/libc/stdlib/aligned_alloc.c
new file mode 100644
index 0000000..88413ce
--- /dev/null
+++ b/newlib/libc/stdlib/aligned_alloc.c
@@ -0,0 +1,38 @@
+/*-
+ * 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.
+ */
+
+#include <stdlib.h>
+
+void *
+aligned_alloc(size_t alignment, size_t size)
+{
+	void *p;
+	int error;
+
+	error = posix_memalign(&p, alignment, size);
+
+	return (error == 0 ? p : NULL);
+}
diff --git a/newlib/libc/stdlib/quick_exit.c b/newlib/libc/stdlib/quick_exit.c
new file mode 100644
index 0000000..1bcd43f
--- /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 <stdlib.h>
+#include <sys/lock.h>
+
+/**
+ * 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);
+}
-- 
1.8.4.5



More information about the Newlib mailing list