From ed12b7043fa98928f75c289a756fbcef546315f8 Mon Sep 17 00:00:00 2001
From: Ian Lynagh <igloo@earth.li>
Date: Fri, 15 Dec 2006 21:44:30 +0000
Subject: [PATCH] Free more things that we allocate

---
 rts/Capability.c           | 10 ++++++++--
 rts/Capability.h           |  3 +++
 rts/ProfHeap.c             | 19 +++++++++++++++++++
 rts/Profiling.c            |  6 ++++++
 rts/Profiling.h            |  1 +
 rts/RtsSignals.h           |  2 ++
 rts/RtsStartup.c           |  8 ++++++++
 rts/Schedule.c             |  5 +++++
 rts/posix/Signals.c        |  7 +++++++
 rts/win32/ConsoleHandler.c |  5 +++++
 10 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/rts/Capability.c b/rts/Capability.c
index 3d55b421f8..510656f473 100644
--- a/rts/Capability.c
+++ b/rts/Capability.c
@@ -673,8 +673,7 @@ shutdownCapability (Capability *cap, Task *task)
 	    continue;
 	}
 	debugTrace(DEBUG_sched, "capability %d is stopped.", cap->no);
-        stgFree(cap->mut_lists);
-        freeSparkPool(&cap->r.rSparks);
+    freeCapability(cap);
 	RELEASE_LOCK(&cap->lock);
 	break;
     }
@@ -712,4 +711,11 @@ tryGrabCapability (Capability *cap, Task *task)
 
 #endif /* THREADED_RTS */
 
+void
+freeCapability (Capability *cap) {
+    stgFree(cap->mut_lists);
+#if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
+    freeSparkPool(&cap->r.rSparks);
+#endif
+}
 
diff --git a/rts/Capability.h b/rts/Capability.h
index dd17863c60..dedd6351b6 100644
--- a/rts/Capability.h
+++ b/rts/Capability.h
@@ -232,6 +232,9 @@ extern void grabCapability (Capability **pCap);
 
 #endif /* !THREADED_RTS */
 
+// Free a capability on exit
+void freeCapability (Capability *cap);
+
 /* -----------------------------------------------------------------------------
  * INLINE functions... private below here
  * -------------------------------------------------------------------------- */
diff --git a/rts/ProfHeap.c b/rts/ProfHeap.c
index 4aecd0b5b3..e93151d4f4 100644
--- a/rts/ProfHeap.c
+++ b/rts/ProfHeap.c
@@ -316,6 +316,13 @@ initEra(Census *census)
     census->drag_total = 0;
 }
 
+STATIC_INLINE void
+freeEra(Census *census)
+{
+    arenaFree(census->arena);
+    freeHashTable(census->hash, NULL);
+}
+
 /* --------------------------------------------------------------------------
  * Increases era by 1 and initialize census[era].
  * Reallocates gi[] and increases its size if needed.
@@ -355,6 +362,10 @@ void initProfiling1( void )
 {
 }
 
+void freeProfiling1( void )
+{
+}
+
 void initProfiling2( void )
 {
   if (RtsFlags.ProfFlags.doHeapProfile) {
@@ -493,6 +504,14 @@ endHeapProfiling(void)
     }
 #endif
 
+    {
+        nat t;
+        for (t = 0; t <= era; t++) {
+            freeEra( &censuses[t] );
+        }
+    }
+    stgFree(censuses);
+
     seconds = mut_user_time();
     printSample(rtsTrue, seconds);
     printSample(rtsFalse, seconds);
diff --git a/rts/Profiling.c b/rts/Profiling.c
index a8650788e9..4e759b6942 100644
--- a/rts/Profiling.c
+++ b/rts/Profiling.c
@@ -185,6 +185,12 @@ initProfiling1 (void)
    */
 }
 
+void
+freeProfiling1 (void)
+{
+    arenaFree(prof_arena);
+}
+
 void
 initProfiling2 (void)
 {
diff --git a/rts/Profiling.h b/rts/Profiling.h
index d968349a52..edfc1b2c5e 100644
--- a/rts/Profiling.h
+++ b/rts/Profiling.h
@@ -13,6 +13,7 @@
 
 #if defined(PROFILING) || defined(DEBUG)
 void initProfiling1 ( void );
+void freeProfiling1 ( void );
 void initProfiling2 ( void );
 void endProfiling   ( void );
 
diff --git a/rts/RtsSignals.h b/rts/RtsSignals.h
index eafeeaaf55..6d9374a70c 100644
--- a/rts/RtsSignals.h
+++ b/rts/RtsSignals.h
@@ -41,6 +41,8 @@ extern void initUserSignals(void);
  */
 extern void initDefaultHandlers(void);
 
+extern void freeSignalHandlers(void);
+
 /*
  * Function: blockUserSignals()
  *
diff --git a/rts/RtsStartup.c b/rts/RtsStartup.c
index 87df96a13c..7193876970 100644
--- a/rts/RtsStartup.c
+++ b/rts/RtsStartup.c
@@ -378,6 +378,10 @@ hs_exit(void)
     /* start timing the shutdown */
     stat_startExit();
     
+#if defined(RTS_USER_SIGNALS)
+    freeSignalHandlers();
+#endif
+
 #if defined(THREADED_RTS)
     ioManagerDie();
 #endif
@@ -447,6 +451,10 @@ hs_exit(void)
     /* free the stable pointer table */
     exitStablePtrTable();
 
+#if defined(PROFILING) || defined(DEBUG)
+    freeProfiling1();
+#endif
+
 #if defined(DEBUG)
     /* free the thread label table */
     freeThreadLabelTable();
diff --git a/rts/Schedule.c b/rts/Schedule.c
index 8ebedd4279..a11a15e94d 100644
--- a/rts/Schedule.c
+++ b/rts/Schedule.c
@@ -2581,6 +2581,8 @@ exitScheduler( void )
 	boundTaskExiting(task);
 	stopTaskManager();
     }
+#else
+    freeCapability(&MainCapability);
 #endif
 }
 
@@ -2588,6 +2590,9 @@ void
 freeScheduler( void )
 {
     freeTaskManager();
+    if (n_capabilities != 1) {
+        stgFree(capabilities);
+    }
 #if defined(THREADED_RTS)
     closeMutex(&sched_mutex);
 #endif
diff --git a/rts/posix/Signals.c b/rts/posix/Signals.c
index a5044cd6de..2380eacfbd 100644
--- a/rts/posix/Signals.c
+++ b/rts/posix/Signals.c
@@ -545,4 +545,11 @@ initDefaultHandlers()
 #endif
 }
 
+void
+freeSignalHandlers(void) {
+    if (signal_handlers != NULL) {
+        stgFree(signal_handlers);
+    }
+}
+
 #endif /* RTS_USER_SIGNALS */
diff --git a/rts/win32/ConsoleHandler.c b/rts/win32/ConsoleHandler.c
index 5b5cfc338b..a2de74b54a 100644
--- a/rts/win32/ConsoleHandler.c
+++ b/rts/win32/ConsoleHandler.c
@@ -52,6 +52,11 @@ initUserSignals(void)
     return;
 }
 
+void
+freeSignalHandlers(void) {
+    /* Do nothing */
+}
+
 /* Seems to be a bit of an orphan...where used? */
 void
 finiUserSignals(void)
-- 
GitLab