This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Remove --disable-newlib-atexit-alloc.


Hi,

Attached is a patch to remove --disable-newlib-atexit-alloc and make
malloc a weak symbol in the support functions for atexit.

A while ago, Mark Mitchell added --disable-newlib-atexit-dynamic-alloc
to prevent atexit from pulling malloc.  This was a bit inflexible
because once newlib was built with --disable-newlib-atexit-alloc, the
user couldn't enable malloc without rebuilding newlib.

This patch makes malloc a weak symbol in the support functions of
atexit.  This way, if malloc is available for some other reason, we
use that.  Otherwise, we don't to conserve space.

OK to apply?

Kazu Hirata

2008-11-05  Paul Brook  <paul@codesourcery.com>

	* acconfig.h (_ATEXIT_DYNAMIC_ALLOC): Remove.
	* configure.in: Remove --disable-newlib-atexit-dynamic-alloc.
	* libc/stdlib/__atexit.c (__register_exitproc): Use weak reference
	to malloc.  Only allocate dynamically if it is present.  Avoid
	calling malloc if not present.
	* libc/stdlib/__call_atexit.c (__call_exitprocs): Use weak
	reference to free.
	* configure: Regenerate.
	* newlib.hin: Regenerate.

Index: newlib/acconfig.h
===================================================================
RCS file: /cvs/src/src/newlib/acconfig.h,v
retrieving revision 1.4
diff -u -p -r1.4 acconfig.h
--- newlib/acconfig.h	11 May 2007 20:09:00 -0000	1.4
+++ newlib/acconfig.h	5 Nov 2008 21:17:33 -0000
@@ -42,10 +42,6 @@
  * sections.  */
 #undef  HAVE_INITFINI_ARRAY
 
-/* True if atexit() may dynamically allocate space for cleanup
-   functions.  */
-#undef  _ATEXIT_DYNAMIC_ALLOC
-
 /* Define if the compiler supports aliasing an array to an address.  */
 #undef  _HAVE_ARRAY_ALIASING
 @BOTTOM@
Index: newlib/configure.in
===================================================================
RCS file: /cvs/src/src/newlib/configure.in,v
retrieving revision 1.37
diff -u -p -r1.37 configure.in
--- newlib/configure.in	24 May 2007 17:33:30 -0000	1.37
+++ newlib/configure.in	5 Nov 2008 21:17:33 -0000
@@ -95,17 +95,6 @@ AC_ARG_ENABLE(newlib-iconv-external-ccs,
    esac
  fi], [newlib_iconv_external_ccs=${newlib_iconv_external_ccs}])dnl
 
-dnl Support --disable-newlib-atexit-dynamic-alloc
-AC_ARG_ENABLE(newlib-atexit-dynamic-alloc,
-[  --disable-newlib-atexit-alloc    disable dynamic allocation of atexit entries],
-[if test "${newlib_atexit_dynamic_alloc+set}" != set; then
-  case "${enableval}" in
-    yes) newlib_atexit_dynamic_alloc=yes ;;
-    no)  newlib_atexit_dynamic_alloc=no  ;;
-    *)   AC_MSG_ERROR(bad value ${enableval} for newlib-atexit-dynamic-alloc option) ;;
-  esac
- fi], [newlib_atexit_dynamic_alloc=${newlib_atexit_dynamic_alloc}])dnl
-
 dnl Support --enable-newlib-reent-small
 AC_ARG_ENABLE(newlib-reent-small,
 [  --enable-newlib-reent-small   enable small reentrant struct support],
@@ -289,10 +278,6 @@ if test "x${newlib_iconv_external_ccs}" 
   AC_DEFINE_UNQUOTED(_ICONV_ENABLE_EXTERNAL_CCS,1)
 fi
 
-if test "${newlib_atexit_dynamic_alloc}" = "yes"; then
-AC_DEFINE_UNQUOTED(_ATEXIT_DYNAMIC_ALLOC)
-fi
-
 dnl
 dnl Parse --enable-newlib-iconv-encodings option argument
 dnl
Index: newlib/libc/stdlib/__atexit.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/__atexit.c,v
retrieving revision 1.4
diff -u -p -r1.4 __atexit.c
--- newlib/libc/stdlib/__atexit.c	21 Mar 2006 00:57:34 -0000	1.4
+++ newlib/libc/stdlib/__atexit.c	5 Nov 2008 21:17:33 -0000
@@ -8,6 +8,8 @@
 #include <sys/lock.h>
 #include "atexit.h"
 
+/* Make this a weak reference to avoid pulling in malloc.  */
+void * malloc(size_t) __attribute__((weak));
 
 /*
  * Register a function to be performed at exit or on shared library unload.
@@ -35,9 +37,8 @@ _DEFUN (__register_exitproc,
     _GLOBAL_REENT->_atexit = p = &_GLOBAL_REENT->_atexit0;
   if (p->_ind >= _ATEXIT_SIZE)
     {
-#ifndef _ATEXIT_DYNAMIC_ALLOC
-      return -1;
-#else
+      if (!malloc)
+	return -1;
       p = (struct _atexit *) malloc (sizeof *p);
       if (p == NULL)
 	{
@@ -53,7 +54,6 @@ _DEFUN (__register_exitproc,
       p->_on_exit_args._fntypes = 0;
       p->_on_exit_args._is_cxa = 0;
 #endif
-#endif
     }
 
   if (type != __et_atexit)
@@ -62,7 +62,9 @@ _DEFUN (__register_exitproc,
       args = p->_on_exit_args_ptr;
       if (args == NULL)
 	{
-	  args = malloc (sizeof * p->_on_exit_args_ptr);
+	  if (malloc)
+	      args = malloc (sizeof * p->_on_exit_args_ptr);
+
 	  if (args == NULL)
 	    {
 #ifndef __SINGLE_THREAD__
Index: newlib/libc/stdlib/__call_atexit.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/__call_atexit.c,v
retrieving revision 1.5
diff -u -p -r1.5 __call_atexit.c
--- newlib/libc/stdlib/__call_atexit.c	5 Apr 2007 16:47:38 -0000	1.5
+++ newlib/libc/stdlib/__call_atexit.c	5 Nov 2008 21:17:33 -0000
@@ -12,6 +12,9 @@
  * otherwise only the handlers from that DSO are called.
  */
 
+/* Make this a weak reference to avoid pulling in malloc.  */
+void free(void *) __attribute__((weak));
+
 void 
 _DEFUN (__call_exitprocs, (code, d),
 	int code _AND _PTR d)
@@ -73,9 +76,9 @@ _DEFUN (__call_exitprocs, (code, d),
 	    goto restart;
 	}
 
-#ifndef _ATEXIT_DYNAMIC_ALLOC
-      break;
-#else
+      if (!free)
+	break;
+
       /* Move to the next block.  Free empty blocks except the last one,
 	 which is part of _GLOBAL_REENT.  */
       if (p->_ind == 0 && p->_next)
@@ -94,6 +97,5 @@ _DEFUN (__call_exitprocs, (code, d),
 	  lastp = &p->_next;
 	  p = p->_next;
 	}
-#endif
     }
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]