2021-03-23 19:44:27 +05:00
|
|
|
// Copyright (C) 2010-2021 CEA/DEN, EDF R&D, OPEN CASCADE
|
2012-08-09 16:03:55 +06:00
|
|
|
//
|
|
|
|
// 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
|
2014-02-20 18:25:37 +06:00
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
2012-08-09 16:03:55 +06:00
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
//
|
|
|
|
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _OBJECTPOOL_HXX_
|
|
|
|
#define _OBJECTPOOL_HXX_
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
|
2017-06-19 20:42:49 +05:00
|
|
|
#include "SMDS_Iterator.hxx"
|
|
|
|
|
2013-02-12 20:37:44 +06:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// assure deallocation of memory of a vector
|
2017-12-22 16:14:24 +05:00
|
|
|
template<class Y> void clearVector(Y & v )
|
2013-02-12 20:37:44 +06:00
|
|
|
{
|
2017-12-22 16:14:24 +05:00
|
|
|
Y emptyVec; v.swap( emptyVec );
|
2013-02-12 20:37:44 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:42:49 +05:00
|
|
|
template<class X> class ObjectPoolIterator;
|
|
|
|
|
2012-08-09 16:03:55 +06:00
|
|
|
template<class X> class ObjectPool
|
|
|
|
{
|
|
|
|
|
|
|
|
private:
|
2017-06-19 20:42:49 +05:00
|
|
|
std::vector<X*> _chunkList;
|
2012-08-09 16:03:55 +06:00
|
|
|
std::vector<bool> _freeList;
|
2017-06-19 20:42:49 +05:00
|
|
|
int _nextFree; // either the 1st hole or last added
|
|
|
|
int _maxAvail; // nb allocated elements
|
|
|
|
int _chunkSize;
|
|
|
|
int _maxOccupied; // max used ID
|
|
|
|
int _nbHoles;
|
|
|
|
int _lastDelChunk;
|
|
|
|
|
|
|
|
friend class ObjectPoolIterator<X>;
|
2012-08-09 16:03:55 +06:00
|
|
|
|
|
|
|
int getNextFree()
|
|
|
|
{
|
2013-07-22 14:46:57 +06:00
|
|
|
// 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);
|
|
|
|
|
2012-08-09 16:03:55 +06:00
|
|
|
for (int i = _nextFree; i < _maxAvail; i++)
|
|
|
|
if (_freeList[i] == true)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _maxAvail;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkDelete(int chunkId)
|
|
|
|
{
|
|
|
|
int i0 = _chunkSize * chunkId;
|
|
|
|
int i1 = _chunkSize * (chunkId + 1);
|
|
|
|
for (int i = i0; i < i1; i++)
|
|
|
|
if (_freeList[i] == false)
|
|
|
|
return;
|
|
|
|
std::cerr << "a chunk to delete" << std::endl;
|
|
|
|
// compactage des vecteurs un peu lourd, pas necessaire
|
|
|
|
//X* chunk = _chunkList[chunkId];
|
|
|
|
//delete [] chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-06-19 20:42:49 +05:00
|
|
|
ObjectPool(int nblk = 1024)
|
2012-08-09 16:03:55 +06:00
|
|
|
{
|
2017-06-19 20:42:49 +05:00
|
|
|
_chunkSize = nblk;
|
|
|
|
_nextFree = 0;
|
|
|
|
_maxAvail = 0;
|
|
|
|
_maxOccupied = -1;
|
|
|
|
_nbHoles = 0;
|
|
|
|
_lastDelChunk = 0;
|
2012-08-09 16:03:55 +06:00
|
|
|
_chunkList.clear();
|
|
|
|
_freeList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ObjectPool()
|
|
|
|
{
|
2013-02-12 20:37:44 +06:00
|
|
|
for (size_t i = 0; i < _chunkList.size(); i++)
|
2012-08-09 16:03:55 +06:00
|
|
|
delete[] _chunkList[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
X* getNew()
|
|
|
|
{
|
|
|
|
X *obj = 0;
|
|
|
|
_nextFree = getNextFree();
|
|
|
|
if (_nextFree == _maxAvail)
|
|
|
|
{
|
|
|
|
X* newChunk = new X[_chunkSize];
|
|
|
|
_chunkList.push_back(newChunk);
|
|
|
|
_freeList.insert(_freeList.end(), _chunkSize, true);
|
|
|
|
_maxAvail += _chunkSize;
|
|
|
|
_freeList[_nextFree] = false;
|
2017-06-19 20:42:49 +05:00
|
|
|
obj = newChunk;
|
2012-08-09 16:03:55 +06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int chunkId = _nextFree / _chunkSize;
|
|
|
|
int rank = _nextFree - chunkId * _chunkSize;
|
|
|
|
_freeList[_nextFree] = false;
|
2017-06-19 20:42:49 +05:00
|
|
|
obj = _chunkList[chunkId] + rank;
|
2012-08-09 16:03:55 +06:00
|
|
|
}
|
2017-06-19 20:42:49 +05:00
|
|
|
if (_nextFree <= _maxOccupied)
|
2013-07-22 14:46:57 +06:00
|
|
|
{
|
|
|
|
_nbHoles-=1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_maxOccupied = _nextFree;
|
|
|
|
}
|
2012-08-09 16:03:55 +06:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy(X* obj)
|
|
|
|
{
|
2016-06-29 22:53:22 +05:00
|
|
|
size_t i = 0;
|
|
|
|
if ( obj >= _chunkList[ _lastDelChunk ] &&
|
|
|
|
obj < _chunkList[ _lastDelChunk ] + _chunkSize )
|
|
|
|
i = _lastDelChunk;
|
|
|
|
else
|
|
|
|
for ( ; i < _chunkList.size(); i++ )
|
2012-08-09 16:03:55 +06:00
|
|
|
{
|
2016-06-29 22:53:22 +05:00
|
|
|
if ( obj >= _chunkList[ i ] &&
|
|
|
|
obj < _chunkList[ i ] + _chunkSize )
|
|
|
|
break;
|
2012-08-09 16:03:55 +06:00
|
|
|
}
|
2016-06-29 22:53:22 +05:00
|
|
|
X* chunk = _chunkList[i];
|
|
|
|
long adrobj = (long) (obj);
|
|
|
|
long adrmin = (long) (chunk);
|
|
|
|
int rank = (adrobj - adrmin) / sizeof(X);
|
|
|
|
int toFree = i * _chunkSize + rank;
|
|
|
|
_freeList[toFree] = true;
|
|
|
|
if (toFree < _nextFree)
|
|
|
|
_nextFree = toFree;
|
|
|
|
if (toFree < _maxOccupied)
|
2017-06-19 20:42:49 +05:00
|
|
|
++_nbHoles;
|
|
|
|
else
|
|
|
|
--_maxOccupied;
|
2016-06-29 22:53:22 +05:00
|
|
|
_lastDelChunk = i;
|
2012-08-09 16:03:55 +06:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:37:44 +06:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_nextFree = 0;
|
|
|
|
_maxAvail = 0;
|
2013-07-22 14:46:57 +06:00
|
|
|
_maxOccupied = 0;
|
|
|
|
_nbHoles = 0;
|
2016-06-29 22:53:22 +05:00
|
|
|
_lastDelChunk = 0;
|
2013-02-12 20:37:44 +06:00
|
|
|
for (size_t i = 0; i < _chunkList.size(); i++)
|
|
|
|
delete[] _chunkList[i];
|
|
|
|
clearVector( _chunkList );
|
|
|
|
clearVector( _freeList );
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:42:49 +05:00
|
|
|
// nb allocated elements
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return _freeList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nb used elements
|
|
|
|
size_t nbElements() const
|
|
|
|
{
|
|
|
|
return _maxOccupied + 1 - _nbHoles;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return an element w/o any check
|
|
|
|
const X* operator[]( size_t i ) const // i < size()
|
|
|
|
{
|
|
|
|
int chunkId = i / _chunkSize;
|
|
|
|
int rank = i - chunkId * _chunkSize;
|
|
|
|
return _chunkList[ chunkId ] + rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return only being used element
|
|
|
|
const X* at( size_t i ) const // i < size()
|
|
|
|
{
|
|
|
|
if ( i >= size() || _freeList[ i ] )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int chunkId = i / _chunkSize;
|
|
|
|
int rank = i - chunkId * _chunkSize;
|
|
|
|
return _chunkList[ chunkId ] + rank;
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:03:55 +06:00
|
|
|
// void destroy(int toFree)
|
|
|
|
// {
|
|
|
|
// // no control 0<= toFree < _freeList.size()
|
|
|
|
// _freeList[toFree] = true;
|
|
|
|
// if (toFree < _nextFree)
|
|
|
|
// _nextFree = toFree;
|
|
|
|
// }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-06-19 20:42:49 +05:00
|
|
|
template<class X> class ObjectPoolIterator : public SMDS_Iterator<const X*>
|
|
|
|
{
|
|
|
|
const ObjectPool<X>& _pool;
|
|
|
|
int _i, _nbFound;
|
|
|
|
public:
|
|
|
|
|
|
|
|
ObjectPoolIterator( const ObjectPool<X>& pool ) : _pool( pool ), _i( 0 ), _nbFound( 0 )
|
|
|
|
{
|
|
|
|
if ( more() && _pool._freeList[ _i ] == true )
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
--_nbFound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool more()
|
|
|
|
{
|
|
|
|
return ( _i <= _pool._maxOccupied && _nbFound < (int)_pool.nbElements() );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const X* next()
|
|
|
|
{
|
|
|
|
const X* x = 0;
|
|
|
|
if ( more() )
|
|
|
|
{
|
|
|
|
x = _pool[ _i ];
|
|
|
|
|
|
|
|
++_nbFound;
|
|
|
|
|
|
|
|
for ( ++_i; _i <= _pool._maxOccupied; ++_i )
|
|
|
|
if ( _pool._freeList[ _i ] == false )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-09 16:03:55 +06:00
|
|
|
#endif
|