oldpool.cpp

Go to the documentation of this file.
00001 /* $Id: oldpool.cpp 15569 2009-02-24 21:32:23Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include "debug.h"
00007 #include "oldpool.h"
00008 #include "core/alloc_func.hpp"
00009 
00013 void OldMemoryPoolBase::CleanPool()
00014 {
00015   uint i;
00016 
00017   DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name);
00018 
00019   this->cleaning_pool = true;
00020   /* Free all blocks */
00021   for (i = 0; i < this->current_blocks; i++) {
00022     if (this->clean_block_proc != NULL) {
00023       this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1);
00024     }
00025     free(this->blocks[i]);
00026   }
00027   this->cleaning_pool = false;
00028 
00029   /* Free the block itself */
00030   free(this->blocks);
00031 
00032   /* Clear up some critical data */
00033   this->total_items = 0;
00034   this->current_blocks = 0;
00035   this->blocks = NULL;
00036   this->first_free_index = 0;
00037 }
00038 
00045 bool OldMemoryPoolBase::AddBlockToPool()
00046 {
00047   /* Is the pool at his max? */
00048   if (this->max_blocks == this->current_blocks) return false;
00049 
00050   this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits);
00051 
00052   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);
00053 
00054   /* Increase the poolsize */
00055   this->blocks = ReallocT(this->blocks, this->current_blocks + 1);
00056 
00057   /* Allocate memory to the new block item */
00058   this->blocks[this->current_blocks] = CallocT<byte>(this->item_size * (1 << this->block_size_bits));
00059 
00060   /* Call a custom function if defined (e.g. to fill indexes) */
00061   if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits));
00062 
00063   /* We have a new block */
00064   this->current_blocks++;
00065 
00066   return true;
00067 }
00068 
00074 bool OldMemoryPoolBase::AddBlockIfNeeded(uint index)
00075 {
00076   while (index >= this->total_items) {
00077     if (!this->AddBlockToPool()) return false;
00078   }
00079 
00080   return true;
00081 }

Generated on Thu Oct 1 11:03:15 2009 for OpenTTD by  doxygen 1.5.6