tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file oldpool.cpp Implementation of the old pool. */ belugas@6351: truelight@1259: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" matthijs@5216: #include "oldpool.h" rubidium@8130: #include "core/alloc_func.hpp" truelight@1259: truelight@1259: /** truelight@1259: * Clean a pool in a safe way (does free all blocks) truelight@1259: */ rubidium@7375: void OldMemoryPoolBase::CleanPool() truelight@1259: { truelight@1259: uint i; truelight@1259: rubidium@7375: DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name); truelight@1259: rubidium@7413: this->cleaning_pool = true; truelight@1259: /* Free all blocks */ rubidium@7375: for (i = 0; i < this->current_blocks; i++) { rubidium@7375: if (this->clean_block_proc != NULL) { rubidium@7375: this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1); peter1138@3585: } rubidium@7375: free(this->blocks[i]); peter1138@3585: } rubidium@7413: this->cleaning_pool = false; truelight@1259: truelight@1259: /* Free the block itself */ rubidium@7375: free(this->blocks); truelight@1259: truelight@1259: /* Clear up some critical data */ rubidium@7375: this->total_items = 0; rubidium@7375: this->current_blocks = 0; rubidium@7375: this->blocks = NULL; rubidium@7378: this->first_free_index = 0; 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: */ rubidium@7375: bool OldMemoryPoolBase::AddBlockToPool() truelight@1259: { truelight@1259: /* Is the pool at his max? */ rubidium@7375: if (this->max_blocks == this->current_blocks) return false; truelight@1259: rubidium@7375: this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits); truelight@1259: rubidium@7375: DEBUG(misc, 4, "[Pool] (%s) increasing size of pool to %d items (%d bytes)", this->name, this->total_items, this->total_items * this->item_size); truelight@1259: truelight@1259: /* Increase the poolsize */ rubidium@7375: this->blocks = ReallocT(this->blocks, this->current_blocks + 1); truelight@1259: truelight@1259: /* Allocate memory to the new block item */ rubidium@7375: this->blocks[this->current_blocks] = MallocT(this->item_size * (1 << this->block_size_bits)); truelight@1259: truelight@1259: /* Clean the content of the new block */ rubidium@7375: memset(this->blocks[this->current_blocks], 0, this->item_size * (1 << this->block_size_bits)); truelight@1259: truelight@1259: /* Call a custom function if defined (e.g. to fill indexes) */ rubidium@7375: if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits)); truelight@1259: truelight@1259: /* We have a new block */ rubidium@7375: this->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: */ rubidium@7375: bool OldMemoryPoolBase::AddBlockIfNeeded(uint index) truelight@1259: { rubidium@7375: while (index >= this->total_items) { rubidium@7375: if (!this->AddBlockToPool()) return false; truelight@1259: } truelight@1259: truelight@1259: return true; truelight@1259: }