oldpool.cpp
Go to the documentation of this file.00001
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
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
00031 free(this->blocks);
00032
00033
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
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
00056 this->blocks = ReallocT(this->blocks, this->current_blocks + 1);
00057
00058
00059 this->blocks[this->current_blocks] = MallocT<byte>(this->item_size * (1 << this->block_size_bits));
00060
00061
00062 memset(this->blocks[this->current_blocks], 0, this->item_size * (1 << this->block_size_bits));
00063
00064
00065 if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits));
00066
00067
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 }