[opengtl-commits] [574] use a single segment, and when not possible anymore, start using a list

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


Revision: 574
Author:   cyrille
Date:     2009-02-28 17:38:26 +0100 (Sat, 28 Feb 2009)

Log Message:
-----------
use a single segment, and when not possible anymore, start using a list

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


Modified: trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp	2009-02-28 15:22:41 UTC (rev 573)
+++ trunk/OpenGTL/OpenGTL/GTLCore/MemoryManager_p.cpp	2009-02-28 16:38:26 UTC (rev 574)
@@ -27,7 +27,7 @@
 
 using namespace GTLCore;
 
-#define DEFAULT_SEGMENT_SIZE 1000
+#define DEFAULT_SEGMENT_SIZE 100000
 
 // #define DEBUG_MEMORY_MANAGER
 
@@ -135,6 +135,8 @@
 };
 
 struct MemoryManager::Private {
+  Private() : uniqueSegment(new Segment(DEFAULT_SEGMENT_SIZE)) {}
+  Segment* uniqueSegment;
   std::list<Segment*> segments;
   static Private* s_instance;
 };
@@ -143,16 +145,27 @@
 
 void* MemoryManager::allocate(int size)
 {
-  MM_DEBUG("Allocate: " << size);
-  foreach(Segment* segment, Private::s_instance->segments)
+  if( Private::s_instance->uniqueSegment )
   {
-    if( segment->canContains(size) )
+    if( Private::s_instance->uniqueSegment->canContains(size) )
     {
-      void* ptr = segment->allocate(size);
-      GTL_ASSERT(segment->contains(ptr));
-      MM_DEBUG("ptr = " << ptr);
-      return ptr;
+      return Private::s_instance->uniqueSegment->allocate(size);
+    } else {
+      Private::s_instance->segments.push_back(Private::s_instance->uniqueSegment);
+      Private::s_instance->uniqueSegment = 0;
     }
+  } else {
+    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;
+      }
+    }
   }
   MM_DEBUG("Create new segment among " << Private::s_instance->segments.size() << " other segments" );
   int newSegSize = size > DEFAULT_SEGMENT_SIZE ? size : DEFAULT_SEGMENT_SIZE;
@@ -166,14 +179,20 @@
 
 void MemoryManager::desallocate(void* ptr)
 {
-  MM_DEBUG("Desallocate ptr = " << ptr);
-  foreach(Segment* segment, Private::s_instance->segments)
+  if( Private::s_instance->uniqueSegment )
   {
-    if( segment->contains(ptr) )
+    GTL_ASSERT(Private::s_instance->uniqueSegment->contains(ptr));
+    Private::s_instance->uniqueSegment->desallocate(ptr);
+  } else {
+    MM_DEBUG("Desallocate ptr = " << ptr);
+    foreach(Segment* segment, Private::s_instance->segments)
     {
-      segment->desallocate(ptr);
-      return;
+      if( segment->contains(ptr) )
+      {
+        segment->desallocate(ptr);
+        return;
+      }
     }
+    GTL_ABORT("Not allocated pointer.");
   }
-  GTL_ABORT("Not allocated pointer.");
 }


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