src/oldpool.cpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:25:44 +0200
branchterom-mini
changeset 10442 7089fa402bfd
parent 9111 48ce04029fe4
permissions -rw-r--r--
mini-branch
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8131
diff changeset
     3
/** @file oldpool.cpp Implementation of the old pool. */
6351
8d0b6cce8d6d (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 5609
diff changeset
     4
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1833
diff changeset
     6
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1259
diff changeset
     7
#include "debug.h"
5216
8bd14ee39af2 (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
     8
#include "oldpool.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8040
diff changeset
     9
#include "core/alloc_func.hpp"
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    10
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    11
/**
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    12
 * Clean a pool in a safe way (does free all blocks)
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    13
 */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    14
void OldMemoryPoolBase::CleanPool()
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    15
{
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    16
	uint i;
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    17
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    18
	DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name);
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    19
7413
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7378
diff changeset
    20
	this->cleaning_pool = true;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    21
	/* Free all blocks */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    22
	for (i = 0; i < this->current_blocks; i++) {
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    23
		if (this->clean_block_proc != NULL) {
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    24
			this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1);
3585
43461f26b729 (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    25
		}
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    26
		free(this->blocks[i]);
3585
43461f26b729 (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    27
	}
7413
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7378
diff changeset
    28
	this->cleaning_pool = false;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    29
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    30
	/* Free the block itself */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    31
	free(this->blocks);
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    32
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    33
	/* Clear up some critical data */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    34
	this->total_items = 0;
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    35
	this->current_blocks = 0;
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    36
	this->blocks = NULL;
7378
7e7a9122b7b1 (svn r10747) -Codechange: add a variable that points to some index in the pool that is not beyond the first free pool item. It does not necessarily point to the first free item, but it reduces allocation time as it does not have to start at the first item in the pool to find the first free item.
rubidium
parents: 7375
diff changeset
    37
	this->first_free_index = 0;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    38
}
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    39
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    40
/**
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    41
 * This function tries to increase the size of array by adding
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    42
 *  1 block too it
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    43
 *
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    44
 * @return Returns false if the pool could not be increased
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    45
 */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    46
bool OldMemoryPoolBase::AddBlockToPool()
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    47
{
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    48
	/* Is the pool at his max? */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    49
	if (this->max_blocks == this->current_blocks) return false;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    50
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    51
	this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits);
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    52
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    53
	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
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    54
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    55
	/* Increase the poolsize */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    56
	this->blocks = ReallocT(this->blocks, this->current_blocks + 1);
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    57
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    58
	/* Allocate memory to the new block item */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    59
	this->blocks[this->current_blocks] = MallocT<byte>(this->item_size * (1 << this->block_size_bits));
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    60
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    61
	/* Clean the content of the new block */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    62
	memset(this->blocks[this->current_blocks], 0, this->item_size * (1 << this->block_size_bits));
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    63
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    64
	/* Call a custom function if defined (e.g. to fill indexes) */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    65
	if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits));
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    66
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    67
	/* We have a new block */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    68
	this->current_blocks++;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    69
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    70
	return true;
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    71
}
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    72
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    73
/**
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    74
 * Adds blocks to the pool if needed (and possible) till index fits inside the pool
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    75
 *
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    76
 * @return Returns false if adding failed
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    77
 */
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    78
bool OldMemoryPoolBase::AddBlockIfNeeded(uint index)
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    79
{
7375
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    80
	while (index >= this->total_items) {
961ab798c4b6 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6351
diff changeset
    81
		if (!this->AddBlockToPool()) return false;
1259
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    82
	}
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    83
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    84
	return true;
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    85
}