2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-02-01 18:30:11 +00:00
|
|
|
#ifndef POOL_H
|
|
|
|
#define POOL_H
|
|
|
|
|
|
|
|
typedef struct MemoryPool MemoryPool;
|
|
|
|
|
|
|
|
/* The function that is called after a new block is added
|
|
|
|
start_item is the first item of the new made block */
|
|
|
|
typedef void MemoryPoolNewBlock(uint start_item);
|
2006-04-18 18:48:50 +00:00
|
|
|
/* The function that is called before a block is cleaned up */
|
|
|
|
typedef void MemoryPoolCleanBlock(uint start_item, uint end_item);
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stuff for dynamic vehicles. Use the wrappers to access the MemoryPool
|
|
|
|
* please try to avoid manual calls!
|
|
|
|
*/
|
|
|
|
struct MemoryPool {
|
2006-03-09 20:37:51 +00:00
|
|
|
const char name[10]; ///< Name of the pool (just for debugging)
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
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
|
|
|
|
const uint item_size; ///< How many bytes one block is
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
/// Pointer to a function that is called after a new block is added
|
2005-02-01 18:30:11 +00:00
|
|
|
MemoryPoolNewBlock *new_block_proc;
|
2006-04-18 18:48:50 +00:00
|
|
|
/// Pointer to a function that is called to clean a block
|
|
|
|
MemoryPoolCleanBlock *clean_block_proc;
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
uint current_blocks; ///< How many blocks we have in our pool
|
|
|
|
uint total_items; ///< How many items we now have in this pool
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
byte **blocks; ///< An array of blocks (one block hold all the items)
|
2005-02-01 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Those are the wrappers:
|
|
|
|
* CleanPool cleans the pool up, but you can use AddBlockToPool directly again
|
|
|
|
* (no need to call CreatePool!)
|
|
|
|
* AddBlockToPool adds 1 more block to the pool. Returns false if there is no
|
|
|
|
* more room
|
|
|
|
*/
|
|
|
|
void CleanPool(MemoryPool *array);
|
|
|
|
bool AddBlockToPool(MemoryPool *array);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds blocks to the pool if needed (and possible) till index fits inside the pool
|
|
|
|
*
|
|
|
|
* @return Returns false if adding failed
|
|
|
|
*/
|
|
|
|
bool AddBlockIfNeeded(MemoryPool *array, uint index);
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static inline byte *GetItemFromPool(const MemoryPool *pool, uint index)
|
2005-02-01 18:30:11 +00:00
|
|
|
{
|
|
|
|
assert(index < pool->total_items);
|
|
|
|
return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* POOL_H */
|