From aa78685c99dc1cd2fe28a05ba55eb719e12f4029 Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 1 Aug 2007 23:49:06 +0000 Subject: (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too). --- src/oldpool.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/oldpool.h') diff --git a/src/oldpool.h b/src/oldpool.h index 3813e8f1c..80e121cb6 100644 --- a/src/oldpool.h +++ b/src/oldpool.h @@ -92,6 +92,40 @@ static inline bool AddBlockToPool(OldMemoryPoolBase *array) { return array->AddB static inline bool AddBlockIfNeeded(OldMemoryPoolBase *array, uint index) { return array->AddBlockIfNeeded(index); } +/** + * Generic function to initialize a new block in a pool. + * @param start_item the first item that needs to be initialized + */ +template *Tpool> +static void PoolNewBlock(uint start_item) +{ + /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. + * TODO - This is just a temporary stage, this will be removed. */ + for (T *t = Tpool->Get(start_item); t != NULL; t = (t->index + 1U < Tpool->GetSize()) ? Tpool->Get(t->index + 1U) : NULL) { + t->index = start_item++; + t->PreInit(); + } +} + +/** + * Generic function to free a new block in a pool. + * This function uses QuickFree that is intended to only free memory that would be lost if the pool is freed. + * @param start_item the first item that needs to be cleaned + * @param end_item the last item that needs to be cleaned + */ +template *Tpool> +static void PoolCleanBlock(uint start_item, uint end_item) +{ + for (uint i = start_item; i <= end_item; i++) { + T *t = Tpool->Get(i); + if (t->IsValid()) { + t->QuickFree(); + } + } +} + + + #define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ enum { \ name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \ @@ -115,6 +149,11 @@ static inline bool AddBlockIfNeeded(OldMemoryPoolBase *array, uint index) { retu #name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ new_block_proc, clean_block_proc); +#define DEFINE_OLD_POOL_GENERIC(name, type) \ + OldMemoryPool _##name##_pool( \ + #name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ + PoolNewBlock, PoolCleanBlock); + #define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \ OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ -- cgit v1.2.3-54-g00ecf