about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
Diffstat (limited to 'malloc')
-rw-r--r--malloc/Makefile3
-rw-r--r--malloc/mtrace.c27
-rw-r--r--malloc/set-freeres.c33
3 files changed, 63 insertions, 0 deletions
diff --git a/malloc/Makefile b/malloc/Makefile
index addabf12f8..bad756aa64 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -41,6 +41,9 @@ non-lib.a := libmcheck.a
 # These should be removed by `make clean'.
 extra-objs = mcheck-init.o libmcheck.a
 
+# Include the cleanup handler.
+aux := set-freeres
+
 # The AWK script to analyze the output of the mtrace functions.
 ifneq ($(PERL),no)
 install-bin = mtrace
diff --git a/malloc/mtrace.c b/malloc/mtrace.c
index aeefd5600e..54c9dfffbd 100644
--- a/malloc/mtrace.c
+++ b/malloc/mtrace.c
@@ -193,6 +193,23 @@ tr_reallochook (ptr, size, caller)
   return hdr;
 }
 
+
+#ifdef _LIBC
+extern void __libc_freeres (void);
+
+/* This function gets called to make sure all memory the library
+   allocates get freed and so does not irritate the user when studying
+   the mtrace output.  */
+static void
+release_libc_mem (void)
+{
+  /* Only call the free function if we still are running in mtrace mode.  */
+  if (mallstream != NULL)
+    __libc_freeres ();
+}
+#endif
+
+
 /* We enable tracing if either the environment variable MALLOC_TRACE
    is set, or if the variable mallwatch has been patched to an address
    that the debugging user wants us to stop on.  When patching mallwatch,
@@ -201,6 +218,9 @@ tr_reallochook (ptr, size, caller)
 void
 mtrace ()
 {
+#ifdef _LIBC
+  static int added_atexit_handler = 0;
+#endif
   char *mallfile;
 
   /* Don't panic if we're called more than once.  */
@@ -229,6 +249,13 @@ mtrace ()
 	  __malloc_hook = tr_mallochook;
 	  tr_old_realloc_hook = __realloc_hook;
 	  __realloc_hook = tr_reallochook;
+#ifdef _LIBC
+	  if (!added_atexit_handler)
+	    {
+	      added_atexit_handler = 1;
+	      atexit (release_libc_mem);
+	    }
+#endif
 	}
     }
 }
diff --git a/malloc/set-freeres.c b/malloc/set-freeres.c
new file mode 100644
index 0000000000..7efd06e33c
--- /dev/null
+++ b/malloc/set-freeres.c
@@ -0,0 +1,33 @@
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <stdlib.h>
+#include <set-hooks.h>
+
+DEFINE_HOOK (__libc_subfreeres, (void));
+
+void
+__libc_freeres (void)
+{
+  /* This function might be called from different places.  So better
+     protect for multiple executions since these are fatal.  */
+  static int already_called = 0;
+
+  if (!already_called)
+    RUN_HOOK (__libc_subfreeres, ());
+}