tron@2186: /* $Id$ */ tron@2186: truelight@1259: #ifndef POOL_H truelight@1259: #define POOL_H truelight@1259: truelight@1259: typedef struct MemoryPool MemoryPool; 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 */ truelight@1259: typedef void MemoryPoolNewBlock(uint start_item); truelight@1259: truelight@1259: /** truelight@1259: * Stuff for dynamic vehicles. Use the wrappers to access the MemoryPool truelight@1259: * please try to avoid manual calls! truelight@1259: */ truelight@1259: struct MemoryPool { truelight@1259: const char name[10]; //! Name of the pool (just for debugging) truelight@1259: truelight@1259: const uint max_blocks; //! The max amount of blocks this pool can have truelight@1259: const uint block_size_bits; //! The size of each block in bits truelight@1259: const uint item_size; //! How many bytes one block is truelight@1259: truelight@1259: MemoryPoolNewBlock *new_block_proc; truelight@1259: //!< Pointer to a function that is called after a new block is added truelight@1259: truelight@1259: uint current_blocks; //! How many blocks we have in our pool truelight@1259: uint total_items; //! How many items we now have in this pool truelight@1259: truelight@1259: 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: */ truelight@1259: void CleanPool(MemoryPool *array); truelight@1259: bool AddBlockToPool(MemoryPool *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: */ truelight@1259: bool AddBlockIfNeeded(MemoryPool *array, uint index); truelight@1259: Darkvater@2436: static inline byte *GetItemFromPool(const MemoryPool *pool, uint index) truelight@1259: { truelight@1259: assert(index < pool->total_items); truelight@1259: return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size); truelight@1259: } truelight@1259: truelight@1259: #endif /* POOL_H */