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" matthijs@5216: #include "oldpool.h" truelight@1259: truelight@1259: /** truelight@1259: * Clean a pool in a safe way (does free all blocks) truelight@1259: */ matthijs@5216: void CleanPool(OldMemoryPool *pool) truelight@1259: { truelight@1259: uint i; truelight@1259: Darkvater@5568: DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", pool->name); truelight@1259: truelight@1259: /* Free all blocks */ peter1138@3585: for (i = 0; i < pool->current_blocks; i++) { peter1138@3585: if (pool->clean_block_proc != NULL) { peter1138@3585: pool->clean_block_proc(i * (1 << pool->block_size_bits), (i + 1) * (1 << pool->block_size_bits) - 1); peter1138@3585: } truelight@1259: free(pool->blocks[i]); peter1138@3585: } 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: */ matthijs@5216: bool AddBlockToPool(OldMemoryPool *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: Darkvater@5568: 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)); Darkvater@5568: if (pool->blocks == NULL) 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: */ matthijs@5216: bool AddBlockIfNeeded(OldMemoryPool *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: }