src/oldpool.cpp
author convert-repo
Mon, 07 Apr 2008 16:21:55 +0000
changeset 10076 dfd70e42c4ae
parent 8627 448ebf3a8291
child 10429 1b99254f9607
permissions -rw-r--r--
update tags
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"
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
     8
#include "oldpool.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8536
diff changeset
     9
#include "core/alloc_func.hpp"
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    10
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
 * 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
    13
 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    14
void OldMemoryPoolBase::CleanPool()
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    15
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    16
	uint i;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    17
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    18
	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
    19
7909
8df54a2839a1 (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: 7874
diff changeset
    20
	this->cleaning_pool = true;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    21
	/* Free all blocks */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    22
	for (i = 0; i < this->current_blocks; i++) {
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    23
		if (this->clean_block_proc != NULL) {
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    24
			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
    25
		}
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    26
		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
    27
	}
7909
8df54a2839a1 (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: 7874
diff changeset
    28
	this->cleaning_pool = false;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    29
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    30
	/* Free the block itself */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    31
	free(this->blocks);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    32
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    33
	/* Clear up some critical data */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    34
	this->total_items = 0;
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    35
	this->current_blocks = 0;
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    36
	this->blocks = NULL;
7874
c9fae1fb5d6c (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: 7871
diff changeset
    37
	this->first_free_index = 0;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    38
}
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
 * 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
    42
 *  1 block too it
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    43
 *
6dc9a1521c00 (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    45
 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    46
bool OldMemoryPoolBase::AddBlockToPool()
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    47
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    48
	/* Is the pool at his max? */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    49
	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
    50
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    51
	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
    52
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    54
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    55
	/* Increase the poolsize */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    56
	this->blocks = ReallocT(this->blocks, this->current_blocks + 1);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    57
6dc9a1521c00 (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 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    59
	this->blocks[this->current_blocks] = MallocT<byte>(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
    60
6dc9a1521c00 (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 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    62
	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
    63
6dc9a1521c00 (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) */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    65
	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
    66
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    67
	/* We have a new block */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    68
	this->current_blocks++;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    69
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    70
	return true;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    71
}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    72
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
 * 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
    75
 *
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    76
 * @return Returns false if adding failed
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    77
 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    78
bool OldMemoryPoolBase::AddBlockIfNeeded(uint index)
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    79
{
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    80
	while (index >= this->total_items) {
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    81
		if (!this->AddBlockToPool()) return false;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    82
	}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    83
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    84
	return true;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    85
}