[opengtl-commits] [573] fix the MemoryManager to correctly free memory

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 573
Author:   cyrille
Date:     2009-02-28 16:22:41 +0100 (Sat, 28 Feb 2009)

Log Message:
-----------
fix the MemoryManager to correctly free memory

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp


Modified: trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2009-02-28 09:49:42 UTC (rev 572)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2009-02-28 15:22:41 UTC (rev 573)
@@ -43,7 +43,7 @@
 
 // Define _USE_GTL_MM_ if you want to enable the use of GTLCore::MemoryManager
 // instead of plain call to malloc.
-// #define _USE_GTL_MM_
+#define _USE_GTL_MM_
 
 #define UNIFORMIZE_TYPES( _v1_, _v2_) \
   GTL_ASSERT( _v1_.value() ); \

Modified: trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp	2009-02-28 09:49:42 UTC (rev 572)
+++ trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp	2009-02-28 15:22:41 UTC (rev 573)
@@ -29,6 +29,19 @@
 
 #define DEFAULT_SEGMENT_SIZE 1000
 
+// #define DEBUG_MEMORY_MANAGER
+
+#ifdef DEBUG_MEMORY_MANAGER
+
+#define MM_DEBUG(msg) GTL_DEBUG(msg)
+
+#else
+
+#define MM_DEBUG(msg)
+
+#endif
+
+
 struct MemoryManager::Header {
   enum Status {
     USED, ///< Indicate the memory is still used
@@ -72,6 +85,7 @@
     nextFreeH->previous = previous;
     nextFreeH->next = 0;
     nextFreeH->data = nextFree + sizeof(Header);
+    MM_DEBUG( nextFreeH << " with a previous of " << previous << " data " << (void*)nextFreeH->data << " and size of " << size );
     if(previous)
     {
       previous->next = nextFreeH;
@@ -87,24 +101,31 @@
     Header* currentHeader = reinterpret_cast<Header*>(ptr8-sizeof(Header));
     GTL_ASSERT(currentHeader->status == Header::USED);
     currentHeader->status = Header::FREED;
-    while(currentHeader->next == 0)
+    if(currentHeader->next == 0)
     {
-      if(currentHeader->previous == 0)
+      while(true)
       {
-        nextFree = reinterpret_cast<uint8_t*>(currentHeader);
-        previous = 0;
-        return;
+        if(currentHeader->previous == 0)
+        {
+          MM_DEBUG("No previous, free = " << uint(end - nextFree) );
+          nextFree = reinterpret_cast<uint8_t*>(currentHeader);
+          previous = 0;
+          return;
+        }
+        Header* previousCurrentHeader = currentHeader->previous;
+        if( previousCurrentHeader->status == Header::FREED )
+        {
+          MM_DEBUG("Has free previous = " << previousCurrentHeader << " " << (void*)previousCurrentHeader->data );
+          currentHeader = previousCurrentHeader;
+          MM_DEBUG( currentHeader->next );
+        } else {
+          MM_DEBUG("Previous isn't free = " << uint(end - nextFree) );
+          previous = previousCurrentHeader;
+          previous->next = 0;
+          nextFree = reinterpret_cast<uint8_t*>(currentHeader);
+          return;
+        }
       }
-      Header* previousCurrentHeader = currentHeader->previous;
-      if( previousCurrentHeader->status == Header::FREED )
-      {
-        currentHeader = previousCurrentHeader;
-      } else {
-        previous = previousCurrentHeader;
-        previous->next = 0;
-        nextFree = reinterpret_cast<uint8_t*>(currentHeader);
-        return;
-      }
     }
   }
   uint8_t* nextFree;
@@ -122,26 +143,30 @@
 
 void* MemoryManager::allocate(int size)
 {
+  MM_DEBUG("Allocate: " << size);
   foreach(Segment* segment, Private::s_instance->segments)
   {
     if( segment->canContains(size) )
     {
       void* ptr = segment->allocate(size);
       GTL_ASSERT(segment->contains(ptr));
+      MM_DEBUG("ptr = " << ptr);
       return ptr;
     }
   }
-//   GTL_DEBUG("Create new segment");
+  MM_DEBUG("Create new segment among " << Private::s_instance->segments.size() << " other segments" );
   int newSegSize = size > DEFAULT_SEGMENT_SIZE ? size : DEFAULT_SEGMENT_SIZE;
   Segment* segment = new Segment(newSegSize);
   Private::s_instance->segments.push_back(segment);
   void* ptr = segment->allocate(size);
   GTL_ASSERT(segment->contains(ptr));
+  MM_DEBUG("ptr = " << ptr);
   return ptr;
 }
 
 void MemoryManager::desallocate(void* ptr)
 {
+  MM_DEBUG("Desallocate ptr = " << ptr);
   foreach(Segment* segment, Private::s_instance->segments)
   {
     if( segment->contains(ptr) )


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/