tron@2186: /* $Id$ */ tron@2186: matthijs@5224: #ifndef OLDPOOL_H matthijs@5224: #define OLDPOOL_H truelight@1259: matthijs@5216: typedef struct OldMemoryPool OldMemoryPool; truelight@1259: truelight@1259: /* The function that is called after a new block is added truelight@1259: start_item is the first item of the new made block */ matthijs@5216: typedef void OldMemoryPoolNewBlock(uint start_item); peter1138@3585: /* The function that is called before a block is cleaned up */ matthijs@5216: typedef void OldMemoryPoolCleanBlock(uint start_item, uint end_item); truelight@1259: truelight@1259: /** matthijs@5216: * Stuff for dynamic vehicles. Use the wrappers to access the OldMemoryPool truelight@1259: * please try to avoid manual calls! truelight@1259: */ matthijs@5216: struct OldMemoryPool { tron@4970: const char* const name; ///< Name of the pool (just for debugging) truelight@1259: peter1138@3173: const uint max_blocks; ///< The max amount of blocks this pool can have peter1138@3173: const uint block_size_bits; ///< The size of each block in bits peter1138@3173: const uint item_size; ///< How many bytes one block is truelight@1259: peter1138@3173: /// Pointer to a function that is called after a new block is added matthijs@5216: OldMemoryPoolNewBlock *new_block_proc; peter1138@3585: /// Pointer to a function that is called to clean a block matthijs@5216: OldMemoryPoolCleanBlock *clean_block_proc; truelight@1259: peter1138@3173: uint current_blocks; ///< How many blocks we have in our pool peter1138@3173: uint total_items; ///< How many items we now have in this pool peter1138@3173: peter1138@3173: byte **blocks; ///< An array of blocks (one block hold all the items) truelight@1259: }; truelight@1259: truelight@1259: /** truelight@1259: * Those are the wrappers: truelight@1259: * CleanPool cleans the pool up, but you can use AddBlockToPool directly again truelight@1259: * (no need to call CreatePool!) truelight@1259: * AddBlockToPool adds 1 more block to the pool. Returns false if there is no truelight@1259: * more room truelight@1259: */ matthijs@5216: void CleanPool(OldMemoryPool *array); matthijs@5216: bool AddBlockToPool(OldMemoryPool *array); truelight@1259: truelight@1259: /** truelight@1259: * Adds blocks to the pool if needed (and possible) till index fits inside the pool truelight@1259: * truelight@1259: * @return Returns false if adding failed truelight@1259: */ matthijs@5216: bool AddBlockIfNeeded(OldMemoryPool *array, uint index); truelight@1259: tron@4970: matthijs@5216: #define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ tron@4970: enum { \ tron@4970: name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \ tron@4970: name##_POOL_MAX_BLOCKS = max_blocks \ tron@4970: }; tron@4970: tron@4970: matthijs@5216: #define OLD_POOL_ACCESSORS(name, type) \ tron@4970: static inline type* Get##name(uint index) \ tron@4970: { \ tron@4970: assert(index < _##name##_pool.total_items); \ tron@4970: return (type*)( \ tron@4970: _##name##_pool.blocks[index >> name##_POOL_BLOCK_SIZE_BITS] + \ tron@4970: (index & ((1 << name##_POOL_BLOCK_SIZE_BITS) - 1)) * sizeof(type) \ tron@4970: ); \ tron@4970: } \ tron@4970: \ tron@4970: static inline uint Get##name##PoolSize(void) \ tron@4970: { \ tron@4970: return _##name##_pool.total_items; \ tron@4970: } tron@4970: tron@4970: matthijs@5216: #define DECLARE_OLD_POOL(name, type, block_size_bits, max_blocks) \ matthijs@5216: OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ matthijs@5216: extern OldMemoryPool _##name##_pool; \ matthijs@5216: OLD_POOL_ACCESSORS(name, type) tron@4970: tron@4970: matthijs@5216: #define DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \ matthijs@5216: OldMemoryPool _##name##_pool = { \ tron@4970: #name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ tron@4970: new_block_proc, clean_block_proc, \ tron@4970: 0, 0, NULL \ tron@4970: }; tron@4970: tron@4970: matthijs@5216: #define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \ matthijs@5216: OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ matthijs@5216: static DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \ matthijs@5216: OLD_POOL_ACCESSORS(name, type) tron@4970: matthijs@5224: #endif /* OLDPOOL_H */