ObjPool Class Template Reference

Inheritance diagram for ObjPool:

NonCopyable List of all members.

Detailed Description

template<size_t A>
class omni::pool::ObjPool< A >

The pool of fixed-size memory blocks.

The class manages the fixed-size memory blocks. These memory blocks are organized as a single-linked list (list of unused blocks or pool). The get() method returns the first memory block from the pool. The put() method puts the memory block back into the pool. If there are no unused memory blocks (i.e. the pool is empty), then you should call grow() method. The grow() method allocates memory chunk (several adjacent memory blocks) and puts these blocks into the list of unused blocks.

For chunk allocation/deallocation global new / delete operators are used.

The template parameter A is an alignment of memory blocks. It should be integer power of two: 1, 2, 4, 8, 16, ...

The class does not contain the memory block size. So you should provide the correct block size each time the grow() method is called.

As an example of using ObjPool class see implementation of FastObjT class.

See also:
Fast memory management.

Public Types

Public Member Functions

Static Public Member Functions


Member Typedef Documentation

typedef size_t size_type

Size type.

typedef void* pointer

Pointer type.


Member Enumeration Documentation

anonymous enum

Constants.

Enumerator:
ALIGNMENT  Alignment of memory blocks.


Constructor & Destructor Documentation

ObjPool (  )  [inline]

Construction.

The constructor initializes an empty pool. The empty pool has no unused memory blocks. So, before getting the memory block you should call grow() method.

See also:
empty()

grow()

~ObjPool (  )  [inline]

Destruction.

The destructor releases all (used and unused) memory blocks.

Warning:
Make sure that all memory blocks are returned to the pool.
In debug version there is a memory leak checking.


Member Function Documentation

void grow ( size_type  obj_size,
size_type  chunk_size = details::DEFAULT_CHUNK_SIZE 
) [inline]

Grow the pool.

One memory chunk is several adjacent memory blocks. This method allocates one memory chunk and puts these blocks into the list of unused blocks.

Allocated memory chunk contains at least one memory block.

The ObjPool class does not contain memory block size, so, you should provide the correct memory block size each time grow() method is called. You should specify the same memory block size obj_size each time grow() is called. Otherwise your program will have undefined behavior.

Parameters:
[in] obj_size The memory block size in bytes.
[in] chunk_size Approximate memory chunk size in bytes.
See also:
empty()

bool empty (  )  const [inline]

Is the pool empty?

This method checks the list of unused blocks. If the pool is empty, then you should call grow() method before using get().

For example:

  ObjPool<4> m_pool;
  size_t m_block_size;

  // ...

  void* my_alloc()
  {
    if (m_pool.empty())
      m_pool.grow(m_block_size);

    return m_pool.get();
  }

Returns:
true if the pool is empty, otherwise false.

pointer get (  )  [inline]

Get the memory block from the pool.

This method returns the first unused memory block. To ensure that the pool has unused memory block call empty() and grow() if needed.

Returns:
Pointer to the memory block.
See also:
empty()

grow()

void put ( pointer  obj  )  [inline]

Put the memory block back into the pool.

This method puts the memory block obj back into the pool. The memory block is inserted into the beginning of unused blocks list. So, next call of the get() method will return memory block obj again.

Parameters:
[in] obj Pointer to the memory block.

static size_type align ( size_type  obj_size  )  [inline, static]

Align block size.

This method aligns the memory block size obj_size.

Parameters:
[in] obj_size The memory block size.
Returns:
The aligned memory block size.

static pointer align ( pointer  ptr  )  [inline, static]

Align block pointer.

This method aligns the memory block pointer ptr.

Parameters:
[in] ptr The memory block pointer.
Returns:
The aligned memory block pointer.


Generated on Wed Jun 6 17:27:47 2007 for OMNI by  doxygen 1.5.2