Mercurial > hg > openttd
changeset 4970:7792248217a1 draft
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
author | tron <tron@openttd.org> |
---|---|
date | Sat, 28 Oct 2006 10:54:20 +0000 |
parents | 2f9ccf4f310d |
children | 9a8c63d2ff6e |
files | pool.h |
diffstat | 1 files changed, 44 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/pool.h +++ b/pool.h @@ -16,7 +16,7 @@ * please try to avoid manual calls! */ struct MemoryPool { - const char name[10]; ///< Name of the pool (just for debugging) + const char* const name; ///< Name of the pool (just for debugging) const uint max_blocks; ///< The max amount of blocks this pool can have const uint block_size_bits; ///< The size of each block in bits @@ -56,4 +56,47 @@ return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size); } + +#define POOL_ENUM(name, type, block_size_bits, max_blocks) \ + enum { \ + name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \ + name##_POOL_MAX_BLOCKS = max_blocks \ + }; + + +#define POOL_ACCESSORS(name, type) \ + static inline type* Get##name(uint index) \ + { \ + assert(index < _##name##_pool.total_items); \ + return (type*)( \ + _##name##_pool.blocks[index >> name##_POOL_BLOCK_SIZE_BITS] + \ + (index & ((1 << name##_POOL_BLOCK_SIZE_BITS) - 1)) * sizeof(type) \ + ); \ + } \ +\ + static inline uint Get##name##PoolSize(void) \ + { \ + return _##name##_pool.total_items; \ + } + + +#define DECLARE_POOL(name, type, block_size_bits, max_blocks) \ + POOL_ENUM(name, type, block_size_bits, max_blocks) \ + extern MemoryPool _##name##_pool; \ + POOL_ACCESSORS(name, type) + + +#define DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \ + MemoryPool _##name##_pool = { \ + #name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ + new_block_proc, clean_block_proc, \ + 0, 0, NULL \ + }; + + +#define STATIC_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \ + POOL_ENUM(name, type, block_size_bits, max_blocks) \ + static DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \ + POOL_ACCESSORS(name, type) + #endif /* POOL_H */