oldpool.cpp

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

Generated on Wed Oct 1 17:03:22 2008 for openttd by  doxygen 1.5.6