tron@2186: /* $Id$ */ tron@2186: glx@9505: /** @file oldpool.cpp */ glx@9505: 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" rubidium@5838: #include "helpers.hpp" truelight@1259: truelight@1259: /** truelight@1259: * Clean a pool in a safe way (does free all blocks) truelight@1259: */ rubidium@9694: void OldMemoryPoolBase::CleanPool() truelight@1259: { truelight@1259: uint i; truelight@1259: rubidium@9694: DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name); truelight@1259: truelight@1259: /* Free all blocks */ rubidium@9694: for (i = 0; i < this->current_blocks; i++) { rubidium@9694: if (this->clean_block_proc != NULL) { rubidium@9694: this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1); peter1138@3585: } rubidium@9694: free(this->blocks[i]); peter1138@3585: } truelight@1259: truelight@1259: /* Free the block itself */ rubidium@9694: free(this->blocks); truelight@1259: truelight@1259: /* Clear up some critical data */ rubidium@9694: this->total_items = 0; rubidium@9694: this->current_blocks = 0; rubidium@9694: this->blocks = NULL; rubidium@9694: 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@9694: bool OldMemoryPoolBase::AddBlockToPool() truelight@1259: { truelight@1259: /* Is the pool at his max? */ rubidium@9694: if (this->max_blocks == this->current_blocks) return false; truelight@1259: rubidium@9694: this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits); truelight@1259: rubidium@9694: 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@9694: this->blocks = ReallocT(this->blocks, this->current_blocks + 1); rubidium@9694: if (this->blocks == NULL) error("Pool: (%s) could not allocate memory for blocks", this->name); truelight@1259: truelight@1259: /* Allocate memory to the new block item */ rubidium@9694: this->blocks[this->current_blocks] = MallocT(this->item_size * (1 << this->block_size_bits)); rubidium@9694: if (this->blocks[this->current_blocks] == NULL) rubidium@9694: error("Pool: (%s) could not allocate memory for blocks", this->name); truelight@1259: truelight@1259: /* Clean the content of the new block */ rubidium@9694: 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@9694: 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@9694: 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@9694: bool OldMemoryPoolBase::AddBlockIfNeeded(uint index) truelight@1259: { rubidium@9694: while (index >= this->total_items) { rubidium@9694: if (!this->AddBlockToPool()) return false; truelight@1259: } truelight@1259: truelight@1259: return true; truelight@1259: }