Back to index

salome-smesh  6.5.0
Public Member Functions | Private Member Functions | Private Attributes
ObjectPool< X > Class Template Reference

#include <ObjectPool.hxx>

Inheritance diagram for ObjectPool< X >:
Inheritance graph
[legend]
Collaboration diagram for ObjectPool< X >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ObjectPool (int nblk)
virtual ~ObjectPool ()
X * getNew ()
void destroy (X *obj)

Private Member Functions

int getNextFree ()
void checkDelete (int chunkId)

Private Attributes

std::vector< X * > _chunkList
std::vector< bool > _freeList
int _nextFree
int _maxAvail
int _chunkSize

Detailed Description

template<class X>
class ObjectPool< X >

Definition at line 27 of file ObjectPool.hxx.


Constructor & Destructor Documentation

template<class X>
ObjectPool< X >::ObjectPool ( int  nblk) [inline]

Definition at line 62 of file ObjectPool.hxx.

  {
    _chunkSize = nblk;
    _nextFree = 0;
    _maxAvail = 0;
    _chunkList.clear();
    _freeList.clear();
  }
template<class X>
virtual ObjectPool< X >::~ObjectPool ( ) [inline, virtual]

Definition at line 71 of file ObjectPool.hxx.

  {
    for (int i = 0; i < _chunkList.size(); i++)
      delete[] _chunkList[i];
  }

Member Function Documentation

template<class X>
void ObjectPool< X >::checkDelete ( int  chunkId) [inline, private]

Definition at line 48 of file ObjectPool.hxx.

  {
    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;
  }
template<class X>
void ObjectPool< X >::destroy ( X *  obj) [inline]

Definition at line 101 of file ObjectPool.hxx.

  {
    long adrobj = (long) (obj);
    for (int i = 0; i < _chunkList.size(); i++)
      {
        X* chunk = _chunkList[i];
        long adrmin = (long) (chunk);
        if (adrobj < adrmin)
          continue;
        long adrmax = (long) (chunk + _chunkSize);
        if (adrobj >= adrmax)
          continue;
        int rank = (adrobj - adrmin) / sizeof(X);
        int toFree = i * _chunkSize + rank;
        _freeList[toFree] = true;
        if (toFree < _nextFree)
          _nextFree = toFree;
        //obj->clean();
        //checkDelete(i); compactage non fait
        break;
      }
  }
template<class X>
X* ObjectPool< X >::getNew ( ) [inline]

Definition at line 77 of file ObjectPool.hxx.

  {
    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;
        obj = newChunk; // &newChunk[0];
      }
    else
      {
        int chunkId = _nextFree / _chunkSize;
        int rank = _nextFree - chunkId * _chunkSize;
        _freeList[_nextFree] = false;
        obj = _chunkList[chunkId] + rank; // &_chunkList[chunkId][rank];
      }
    //obj->init();
    return obj;
  }
template<class X>
int ObjectPool< X >::getNextFree ( ) [inline, private]

Definition at line 37 of file ObjectPool.hxx.

  {
    for (int i = _nextFree; i < _maxAvail; i++)
      if (_freeList[i] == true)
        {
          return i;
          break;
        }
    return _maxAvail;
  }

Here is the caller graph for this function:


Member Data Documentation

template<class X>
std::vector<X*> ObjectPool< X >::_chunkList [private]

Definition at line 31 of file ObjectPool.hxx.

template<class X>
int ObjectPool< X >::_chunkSize [private]

Definition at line 35 of file ObjectPool.hxx.

template<class X>
std::vector<bool> ObjectPool< X >::_freeList [private]

Definition at line 32 of file ObjectPool.hxx.

template<class X>
int ObjectPool< X >::_maxAvail [private]

Definition at line 34 of file ObjectPool.hxx.

template<class X>
int ObjectPool< X >::_nextFree [private]

Definition at line 33 of file ObjectPool.hxx.


The documentation for this class was generated from the following file: