Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero > Struct Template Reference

Base class for all pools. More...

#include <pool_type.hpp>


Public Member Functions

 Pool (const char *name)
 Constructor.
void CleanPool ()
 Destroys all items in the pool and resets all member variables.
FORCEINLINE Titem * Get (size_t index)
 Returs Titem with given index.
FORCEINLINE bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-NULL) Titem.
FORCEINLINE bool CanAllocate (size_t n=1)
 Tests whether we can allocate 'n' items.

Data Fields

const char *const name
 Name of this pool.
size_t size
 Current allocated size.
size_t first_free
 No item with index lower than this is free (doesn't say anything about this one!).
size_t first_unused
 This and all higher indexes are free (doesn't say anything about first_unused-1 !).
size_t items
 Number of used indexes (non-NULL).
bool cleaning
 True if cleaning pool (deleting all items).
Titem ** data
 Pointer to array of pointers to Titem.

Static Public Attributes

static const size_t MAX_SIZE = Tmax_size
 Make template parameter accessible from outside.

Private Member Functions

void * AllocateItem (size_t size, size_t index)
 Makes given index valid.
void ResizeFor (size_t index)
 Resizes the pool so 'index' can be addressed.
size_t FindFirstFree ()
 Searches for first free index.
void * GetNew (size_t size)
 Allocates new item.
void * GetNew (size_t size, size_t index)
 Allocates new item with given index.
void FreeItem (size_t index)
 Deallocates memory used by this index and marks item as free.

Private Attributes

AllocCachealloc_cache
 Cache of freed pointers.

Static Private Attributes

static const size_t NO_FREE_ITEM = MAX_UVALUE(size_t)
 Contant to indicate we can't allocate any more items.

Data Structures

struct  AllocCache
 Helper struct to cache 'freed' PoolItems so we do not need to allocate them again. More...
struct  PoolItem
 Base class for all PoolItems. More...


Detailed Description

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
struct Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >

Base class for all pools.

Template Parameters:
Titem Type of the class/struct that is going to be pooled
Tindex Type of the index for this pool
Tgrowth_step Size of growths; if the pool is full increase the size by this amount
Tmax_size Maximum size of the pool
Tcache Whether to perform 'alloc' caching, i.e. don't actually free/malloc just reuse the memory
Tzero Whether to zero the memory
Warning:
when Tcache is enabled *all* instances of this pool's item must be of the same size.

Definition at line 26 of file pool_type.hpp.


Member Function Documentation

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
FORCEINLINE Titem* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::Get ( size_t  index  )  [inline]

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
FORCEINLINE bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::IsValidID ( size_t  index  )  [inline]

Tests whether given index can be used to get valid (non-NULL) Titem.

Parameters:
index index to examine
Returns:
true if PoolItem::Get(index) will return non-NULL pointer

Definition at line 62 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::first_unused, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::Get().

Referenced by SpecializedVehicle< RoadVehicle, VEH_ROAD >::IsValidID().

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
FORCEINLINE bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::CanAllocate ( size_t  n = 1  )  [inline]

Tests whether we can allocate 'n' items.

Parameters:
n number of items we want to allocate
Returns:
true if 'n' items can be allocated

Definition at line 72 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::items.

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
void* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::AllocateItem ( size_t  size,
size_t  index 
) [private]

Makes given index valid.

Parameters:
size size of item
index index of item
Precondition:
index < this->size

this->Get(index) == NULL

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
void Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::ResizeFor ( size_t  index  )  [private]

Resizes the pool so 'index' can be addressed.

Parameters:
index index we will allocate later
Precondition:
index >= this->size

index < Tmax_size

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::FindFirstFree (  )  [private]

Searches for first free index.

Returns:
first free index, NO_FREE_ITEM on failure

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
void* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::GetNew ( size_t  size  )  [private]

Allocates new item.

Parameters:
size size of item
Returns:
pointer to allocated item
Note:
error() on failure! (no free item)

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
void* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::GetNew ( size_t  size,
size_t  index 
) [private]

Allocates new item with given index.

Parameters:
size size of item
index index of item
Returns:
pointer to allocated item
Note:
usererror() on failure! (index out of range or already used)

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
void Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::FreeItem ( size_t  index  )  [private]

Deallocates memory used by this index and marks item as free.

Parameters:
index item to deallocate
Precondition:
unit is allocated (non-NULL)
Note:
'delete NULL' doesn't cause call of this function, so it is safe


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

Generated on Wed Dec 23 23:28:20 2009 for OpenTTD by  doxygen 1.5.6