Salome HOME
INT PAL 0052683: Parameter "Color group" in the "Create Group" dialog box is empty
[modules/smesh.git] / src / SMDS / ObjectPool.hxx
index 1ee3710556b2804591e9eb086455142911367455..f27161122c983743511211b7e7a6b744c7500d11 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2010-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #define _OBJECTPOOL_HXX_
 
 #include <vector>
-#include <stack>
+//#include <stack>
 #include <iostream>
 
+namespace
+{
+  // assure deallocation of memory of a vector
+  template<class Y> void clearVector(std::vector<Y>& v )
+  {
+    std::vector<Y> emptyVec; v.swap( emptyVec );
+  }
+}
+
 template<class X> class ObjectPool
 {
 
@@ -33,9 +42,16 @@ private:
   int _nextFree;
   int _maxAvail;
   int _chunkSize;
+  int _maxOccupied;
+  int _nbHoles;
 
   int getNextFree()
   {
+    // Don't iterate on the _freeList if all the "holes"
+    // are filled. Go straight to the last occupied ID + 1
+    if ( _nbHoles == 0 )
+      return std::min(_maxOccupied + 1, _maxAvail);
+    
     for (int i = _nextFree; i < _maxAvail; i++)
       if (_freeList[i] == true)
         {
@@ -64,13 +80,15 @@ public:
     _chunkSize = nblk;
     _nextFree = 0;
     _maxAvail = 0;
+    _maxOccupied = 0;
+    _nbHoles = 0;
     _chunkList.clear();
     _freeList.clear();
   }
 
   virtual ~ObjectPool()
   {
-    for (int i = 0; i < _chunkList.size(); i++)
+    for (size_t i = 0; i < _chunkList.size(); i++)
       delete[] _chunkList[i];
   }
 
@@ -94,6 +112,14 @@ public:
         _freeList[_nextFree] = false;
         obj = _chunkList[chunkId] + rank; // &_chunkList[chunkId][rank];
       }
+    if (_nextFree < _maxOccupied)
+      {
+        _nbHoles-=1;
+      }
+    else
+      {
+        _maxOccupied = _nextFree;
+      }
     //obj->init();
     return obj;
   }
@@ -101,7 +127,7 @@ public:
   void destroy(X* obj)
   {
     long adrobj = (long) (obj);
-    for (int i = 0; i < _chunkList.size(); i++)
+    for (size_t i = 0; i < _chunkList.size(); i++)
       {
         X* chunk = _chunkList[i];
         long adrmin = (long) (chunk);
@@ -115,12 +141,26 @@ public:
         _freeList[toFree] = true;
         if (toFree < _nextFree)
           _nextFree = toFree;
+        if (toFree < _maxOccupied)
+          _nbHoles += 1;
         //obj->clean();
         //checkDelete(i); compactage non fait
         break;
       }
   }
 
+  void clear()
+  {
+    _nextFree = 0;
+    _maxAvail = 0;
+    _maxOccupied = 0;
+    _nbHoles = 0;
+    for (size_t i = 0; i < _chunkList.size(); i++)
+      delete[] _chunkList[i];
+    clearVector( _chunkList );
+    clearVector( _freeList );
+  }
+
   //  void destroy(int toFree)
   //  {
   //    // no control 0<= toFree < _freeList.size()