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] Use malloc and free in atexit only when available. (Take 2)


Hi,

Attached is a revised patch to use malloc and free in the support
functions for atexit only when available.

The previous version of this patch was posted at:

  http://sourceware.org/ml/newlib/2009/msg00011.html

In this iteration, I am leaving _ATEXIT_DYNAMIC_ALLOC mostly as is.

There is one place where I forgot to guard a call to malloc with
_ATEXIT_DYNAMIC_ALLOC in __atexit.c, so I am fixing that.

This patch doesn't do the "if (!_ATEXIT_DYNAMIC_ALLOC ...)" trick
anymore.  This way, I can ensure that --disable-atexit-dynamic-alloc
really disables dynamic allocation even if the compiler does not
optimize away dead code containing calls to malloc and/or free.

Tested on fido-none-elf.  OK to apply?

Kazu Hirata

2009-04-22  Paul Brook  <paul@codesourcery.com>
	    Kazu Hirata  <kazu@codesourcery.com>

	* libc/stdlib/__atexit.c (__register_exitproc): Use weak reference
	to malloc.  Allocate dynamically only if it is present.
	* libc/stdlib/__call_atexit.c (__call_exitprocs): Use weak
	reference to free.  Call free only if it is present.

Index: newlib/libc/stdlib/__atexit.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/__atexit.c,v
retrieving revision 1.4
diff -u -d -p -r1.4 __atexit.c
--- newlib/libc/stdlib/__atexit.c	21 Mar 2006 00:57:34 -0000	1.4
+++ newlib/libc/stdlib/__atexit.c	22 Apr 2009 04:40:00 -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.
@@ -38,6 +40,11 @@ _DEFUN (__register_exitproc,
 #ifndef _ATEXIT_DYNAMIC_ALLOC
       return -1;
 #else
+      /* Don't dynamically allocate the atexit array if malloc is not
+	 available.  */
+      if (!malloc)
+	return -1;
+
       p = (struct _atexit *) malloc (sizeof *p);
       if (p == NULL)
 	{
@@ -62,7 +69,11 @@ _DEFUN (__register_exitproc,
       args = p->_on_exit_args_ptr;
       if (args == NULL)
 	{
-	  args = malloc (sizeof * p->_on_exit_args_ptr);
+#ifdef _ATEXIT_DYNAMIC_ALLOC
+	  if (malloc)
+	    args = malloc (sizeof * p->_on_exit_args_ptr);
+#endif
+
 	  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 -d -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	22 Apr 2009 04:40:00 -0000
@@ -7,6 +7,9 @@
 #include <reent.h>
 #include "atexit.h"
 
+/* Make this a weak reference to avoid pulling in free.  */
+void free(void *) _ATTRIBUTE((__weak__));
+
 /*
  * Call registered exit handlers.  If D is null then all handlers are called,
  * otherwise only the handlers from that DSO are called.
@@ -76,6 +79,11 @@ _DEFUN (__call_exitprocs, (code, d),
 #ifndef _ATEXIT_DYNAMIC_ALLOC
       break;
 #else
+      /* Don't dynamically free the atexit array if free is not
+	 available.  */
+      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)


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