1 // Copyright (C) 2010-2011 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_
27 template<class X> class ObjectPool
31 std::vector<X*> _chunkList;
32 std::vector<bool> _freeList;
39 for (int i = _nextFree; i < _maxAvail; i++)
40 if (_freeList[i] == true)
48 void checkDelete(int chunkId)
50 int i0 = _chunkSize * chunkId;
51 int i1 = _chunkSize * (chunkId + 1);
52 for (int i = i0; i < i1; i++)
53 if (_freeList[i] == false)
55 std::cerr << "a chunk to delete" << std::endl;
56 // compactage des vecteurs un peu lourd, pas necessaire
57 //X* chunk = _chunkList[chunkId];
73 for (int i = 0; i < _chunkList.size(); i++)
74 delete[] _chunkList[i];
80 _nextFree = getNextFree();
81 if (_nextFree == _maxAvail)
83 X* newChunk = new X[_chunkSize];
84 _chunkList.push_back(newChunk);
85 _freeList.insert(_freeList.end(), _chunkSize, true);
86 _maxAvail += _chunkSize;
87 _freeList[_nextFree] = false;
88 obj = newChunk; // &newChunk[0];
92 int chunkId = _nextFree / _chunkSize;
93 int rank = _nextFree - chunkId * _chunkSize;
94 _freeList[_nextFree] = false;
95 obj = _chunkList[chunkId] + rank; // &_chunkList[chunkId][rank];
103 long adrobj = (long) (obj);
104 for (int i = 0; i < _chunkList.size(); i++)
106 X* chunk = _chunkList[i];
107 long adrmin = (long) (chunk);
110 long adrmax = (long) (chunk + _chunkSize);
111 if (adrobj >= adrmax)
113 int rank = (adrobj - adrmin) / sizeof(X);
114 int toFree = i * _chunkSize + rank;
115 _freeList[toFree] = true;
116 if (toFree < _nextFree)
119 //checkDelete(i); compactage non fait
124 // void destroy(int toFree)
126 // // no control 0<= toFree < _freeList.size()
127 // _freeList[toFree] = true;
128 // if (toFree < _nextFree)
129 // _nextFree = toFree;