tron@2186: /* $Id$ */ tron@2186: truelight@1259: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" truelight@1259: #include "pool.h" truelight@1259: truelight@1259: /** truelight@1259: * Clean a pool in a safe way (does free all blocks) truelight@1259: */ truelight@1259: void CleanPool(MemoryPool *pool) truelight@1259: { truelight@1259: uint i; truelight@1259: Darkvater@1833: DEBUG(misc, 4)("[Pool] (%s) Cleaning pool..", pool->name); truelight@1259: truelight@1259: /* Free all blocks */ truelight@1259: for (i = 0; i < pool->current_blocks; i++) truelight@1259: free(pool->blocks[i]); truelight@1259: truelight@1259: /* Free the block itself */ truelight@1259: free(pool->blocks); truelight@1259: truelight@1259: /* Clear up some critical data */ truelight@1259: pool->total_items = 0; truelight@1259: pool->current_blocks = 0; truelight@1259: pool->blocks = NULL; truelight@1259: } truelight@1259: truelight@1259: /** truelight@1259: * This function tries to increase the size of array by adding truelight@1259: * 1 block too it truelight@1259: * truelight@1259: * @return Returns false if the pool could not be increased truelight@1259: */ truelight@1259: bool AddBlockToPool(MemoryPool *pool) truelight@1259: { truelight@1259: /* Is the pool at his max? */ truelight@1259: if (pool->max_blocks == pool->current_blocks) truelight@1259: return false; truelight@1259: truelight@1259: pool->total_items = (pool->current_blocks + 1) * (1 << pool->block_size_bits); truelight@1259: truelight@1259: DEBUG(misc, 4)("[Pool] (%s) Increasing size of pool to %d items (%d bytes)", pool->name, pool->total_items, pool->total_items * pool->item_size); truelight@1259: truelight@1259: /* Increase the poolsize */ truelight@1259: pool->blocks = realloc(pool->blocks, sizeof(pool->blocks[0]) * (pool->current_blocks + 1)); truelight@1259: if (pool->blocks == NULL) truelight@1259: error("Pool: (%s) could not allocate memory for blocks", pool->name); truelight@1259: truelight@1259: /* Allocate memory to the new block item */ truelight@1259: pool->blocks[pool->current_blocks] = malloc(pool->item_size * (1 << pool->block_size_bits)); truelight@1259: if (pool->blocks[pool->current_blocks] == NULL) truelight@1259: error("Pool: (%s) could not allocate memory for blocks", pool->name); truelight@1259: truelight@1259: /* Clean the content of the new block */ truelight@1259: memset(pool->blocks[pool->current_blocks], 0, pool->item_size * (1 << pool->block_size_bits)); truelight@1259: truelight@1259: /* Call a custom function if defined (e.g. to fill indexes) */ truelight@1259: if (pool->new_block_proc != NULL) truelight@1259: pool->new_block_proc(pool->current_blocks * (1 << pool->block_size_bits)); truelight@1259: truelight@1259: /* We have a new block */ truelight@1259: pool->current_blocks++; truelight@1259: truelight@1259: return true; truelight@1259: } 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 *pool, uint index) truelight@1259: { truelight@1259: while (index >= pool->total_items) { truelight@1259: if (!AddBlockToPool(pool)) truelight@1259: return false; truelight@1259: } truelight@1259: truelight@1259: return true; truelight@1259: }