Index

C++ Code Documentation

base::Pool

File: BASE/base/Pool.H

Class Pool - maintains a pool of fixed-sized elements. Using Pool is often more efficient than calling new/delete for each element, especially if many elements are allocated and deallocated. Memory grows but does not shrink until Pool's destructor is called. The Pool is not aware of the type of the element, so never calls an element's constructor or destructor. Pool does not support base::vector allocation (like new[]); this functionality can be implemented by the client Example:
class A {
public:
A() {}
~A() {}
static void *operator new( size_t )
{ return _pool.allocate(); }
static void operator delete( void *p )
{ _pool.deallocate(p); }
// ...
protected:
// ...
int a1, a2, a3, a4, a5;
private:
static Pool _pool;
// ...
// disabled - not implemented
static void *operator new[]( size_t ) {abort(); return (void *)0;}
static void operator delete[]( void * ) {abort();}
};

Pool A::_pool(sizeof(A), 1024);


public:
  • Pool( size_t element_size, size_t elements_in_block );
    Constructor. elements_in_block is the number of elements A in each block. A block is a piece of memory allocated by malloc().
  • virtual ~Pool();
    Destructor. Calls A's destructor on all allocated space.
  • void *allocate();
    returns pointer to space allocating A. Calls A's default (no-argument) destructor.
  • void deallocate(void *a);
    Deallocates the space pointed to by a . Does not call A's destructor.
  • size_t memory_in_use() const;
    Returns the total amount of memory to date allocated with new(). Not all of this memory need have been allocated to the client with Pool::allocate().
  • size_t elements_in_use() const;
    Returns the number of elements allocated (in use) This does not include the potential elements for which space has been allocated but which have not been doled out with allocate().
  • size_t elements_in_block() const;
    size of block.
  • size_t element_size() const;
    size of element, adjusted up to a multiple of alignment size.
  • size_t blocks_in_use() const;
    number of blocks currently in use.
protected:
  • size_t _element_size;
    size of element.
  • size_t _elements_in_block;
    size of block.
  • size_t _elements_in_use;
    number of elements currently in use.
  • std::slist<char *> _block_list;
    pointer to first block.
  • void *_free_list_top;
    pointer to next free element.
  • size_t _block_index;
private:
  • Pool( Pool const & );
  • Pool &operator=( Pool const & );


base::Pool GANG