1 // Copyright (C) 2010-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef _OBJECTPOOL_HXX_
21 #define _OBJECTPOOL_HXX_
29 // assure deallocation of memory of a vector
30 template<class Y> void clearVector(std::vector<Y>& v )
32 std::vector<Y> emptyVec; v.swap( emptyVec );
36 template<class X> class ObjectPool
40 std::vector<X*> _chunkList;
41 std::vector<bool> _freeList;
50 // Don't iterate on the _freeList if all the "holes"
51 // are filled. Go straight to the last occupied ID + 1
53 return std::min(_maxOccupied + 1, _maxAvail);
55 for (int i = _nextFree; i < _maxAvail; i++)
56 if (_freeList[i] == true)
64 void checkDelete(int chunkId)
66 int i0 = _chunkSize * chunkId;
67 int i1 = _chunkSize * (chunkId + 1);
68 for (int i = i0; i < i1; i++)
69 if (_freeList[i] == false)
71 std::cerr << "a chunk to delete" << std::endl;
72 // compactage des vecteurs un peu lourd, pas necessaire
73 //X* chunk = _chunkList[chunkId];
91 for (size_t i = 0; i < _chunkList.size(); i++)
92 delete[] _chunkList[i];
98 _nextFree = getNextFree();
99 if (_nextFree == _maxAvail)
101 X* newChunk = new X[_chunkSize];
102 _chunkList.push_back(newChunk);
103 _freeList.insert(_freeList.end(), _chunkSize, true);
104 _maxAvail += _chunkSize;
105 _freeList[_nextFree] = false;
106 obj = newChunk; // &newChunk[0];
110 int chunkId = _nextFree / _chunkSize;
111 int rank = _nextFree - chunkId * _chunkSize;
112 _freeList[_nextFree] = false;
113 obj = _chunkList[chunkId] + rank; // &_chunkList[chunkId][rank];
115 if (_nextFree < _maxOccupied)
121 _maxOccupied = _nextFree;
129 long adrobj = (long) (obj);
130 for (size_t i = 0; i < _chunkList.size(); i++)
132 X* chunk = _chunkList[i];
133 long adrmin = (long) (chunk);
136 long adrmax = (long) (chunk + _chunkSize);
137 if (adrobj >= adrmax)
139 int rank = (adrobj - adrmin) / sizeof(X);
140 int toFree = i * _chunkSize + rank;
141 _freeList[toFree] = true;
142 if (toFree < _nextFree)
144 if (toFree < _maxOccupied)
147 //checkDelete(i); compactage non fait
158 for (size_t i = 0; i < _chunkList.size(); i++)
159 delete[] _chunkList[i];
160 clearVector( _chunkList );
161 clearVector( _freeList );
164 // void destroy(int toFree)
166 // // no control 0<= toFree < _freeList.size()
167 // _freeList[toFree] = true;
168 // if (toFree < _nextFree)
169 // _nextFree = toFree;