diff options
author | tron <tron@openttd.org> | 2006-10-28 10:54:20 +0000 |
---|---|---|
committer | tron <tron@openttd.org> | 2006-10-28 10:54:20 +0000 |
commit | 41e34a1c1657fd39692c876235dab877a6c62d18 (patch) | |
tree | 8be1f90c017d4f48f02b5ee991e09c41e345f0ff | |
parent | 5cdeab55f067f6d554d47c7510bc5afeb643cc24 (diff) | |
download | openttd-41e34a1c1657fd39692c876235dab877a6c62d18.tar.xz |
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
-rw-r--r-- | pool.h | 45 |
1 files changed, 44 insertions, 1 deletions
@@ -16,7 +16,7 @@ typedef void MemoryPoolCleanBlock(uint start_item, uint end_item); * 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 @@ static inline byte *GetItemFromPool(const MemoryPool *pool, uint index) 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 */ |