src/oldpool.cpp
author rubidium
Sun, 12 Aug 2007 18:32:47 +0000
branchNewGRF_ports
changeset 6800 6c09e1e86fcb
parent 6743 cabfaa4a0295
child 6872 1c4a4a609f85
permissions -rw-r--r--
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6677
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 5860
diff changeset
     3
/** @file oldpool.cpp */
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 5860
diff changeset
     4
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1833
diff changeset
     6
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1259
diff changeset
     7
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 1891
diff changeset
     8
#include "functions.h"
5216
d581e4db95b6 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 3585
diff changeset
     9
#include "oldpool.h"
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    10
#include "helpers.hpp"
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    11
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    12
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    13
 * Clean a pool in a safe way (does free all blocks)
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    14
 */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    15
void OldMemoryPoolBase::CleanPool()
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    16
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    17
	uint i;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    18
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    19
	DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    20
6800
6c09e1e86fcb (svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents: 6743
diff changeset
    21
	this->cleaning_pool = true;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    22
	/* Free all blocks */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    23
	for (i = 0; i < this->current_blocks; i++) {
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    24
		if (this->clean_block_proc != NULL) {
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    25
			this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1);
3585
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    26
		}
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    27
		free(this->blocks[i]);
3585
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    28
	}
6800
6c09e1e86fcb (svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents: 6743
diff changeset
    29
	this->cleaning_pool = false;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    30
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    31
	/* Free the block itself */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    32
	free(this->blocks);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    33
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    34
	/* Clear up some critical data */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    35
	this->total_items = 0;
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    36
	this->current_blocks = 0;
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    37
	this->blocks = NULL;
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    38
	this->first_free_index = 0;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    39
}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    40
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    41
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    42
 * This function tries to increase the size of array by adding
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    43
 *  1 block too it
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    44
 *
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    45
 * @return Returns false if the pool could not be increased
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    46
 */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    47
bool OldMemoryPoolBase::AddBlockToPool()
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    48
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    49
	/* Is the pool at his max? */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    50
	if (this->max_blocks == this->current_blocks) return false;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    51
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    52
	this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    53
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    54
	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);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    55
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    56
	/* Increase the poolsize */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    57
	this->blocks = ReallocT(this->blocks, this->current_blocks + 1);
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    58
	if (this->blocks == NULL) error("Pool: (%s) could not allocate memory for blocks", this->name);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    59
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    60
	/* Allocate memory to the new block item */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    61
	this->blocks[this->current_blocks] = MallocT<byte>(this->item_size * (1 << this->block_size_bits));
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    62
	if (this->blocks[this->current_blocks] == NULL)
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    63
		error("Pool: (%s) could not allocate memory for blocks", this->name);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    64
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    65
	/* Clean the content of the new block */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    66
	memset(this->blocks[this->current_blocks], 0, this->item_size * (1 << this->block_size_bits));
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    67
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    68
	/* Call a custom function if defined (e.g. to fill indexes) */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    69
	if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits));
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    70
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    71
	/* We have a new block */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    72
	this->current_blocks++;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    73
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    74
	return true;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    75
}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    76
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    77
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    78
 * Adds blocks to the pool if needed (and possible) till index fits inside the pool
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    79
 *
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    80
 * @return Returns false if adding failed
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    81
 */
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    82
bool OldMemoryPoolBase::AddBlockIfNeeded(uint index)
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    83
{
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    84
	while (index >= this->total_items) {
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6677
diff changeset
    85
		if (!this->AddBlockToPool()) return false;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    86
	}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    87
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    88
	return true;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    89
}