oldpool.c
author glx
Mon, 24 Sep 2007 03:08:47 +0000
branch0.5
changeset 5545 f42dc59a45f5
parent 5216 d581e4db95b6
child 5568 75f13d7bfaed
permissions -rw-r--r--
(svn r11153) [0.5] -Fix [FS#1251]: incorrect usage of {G} tag in slovak translation
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
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1833
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1259
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 1891
diff changeset
     6
#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
     7
#include "oldpool.h"
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     8
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     9
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    10
 * 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
    11
 */
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
    12
void CleanPool(OldMemoryPool *pool)
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    13
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    14
	uint i;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    15
1833
4dc6393a96ca (svn r2338) - CodeChange: only call the variable hooks when you set their value not when you query them
Darkvater
parents: 1299
diff changeset
    16
	DEBUG(misc, 4)("[Pool] (%s) Cleaning pool..", pool->name);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    17
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    18
	/* Free all blocks */
3585
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    19
	for (i = 0; i < pool->current_blocks; i++) {
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    20
		if (pool->clean_block_proc != NULL) {
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    21
			pool->clean_block_proc(i * (1 << pool->block_size_bits), (i + 1) * (1 << pool->block_size_bits) - 1);
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 2186
diff changeset
    22
		}
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    23
		free(pool->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
    24
	}
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    25
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    26
	/* Free the block itself */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    27
	free(pool->blocks);
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    28
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    29
	/* Clear up some critical data */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    30
	pool->total_items = 0;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    31
	pool->current_blocks = 0;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    32
	pool->blocks = NULL;
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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    35
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    36
 * 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
    37
 *  1 block too it
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
 * @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
    40
 */
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
    41
bool AddBlockToPool(OldMemoryPool *pool)
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    42
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    43
	/* Is the pool at his max? */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    44
	if (pool->max_blocks == pool->current_blocks)
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    45
		return false;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    46
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    47
	pool->total_items = (pool->current_blocks + 1) * (1 << pool->block_size_bits);
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
	DEBUG(misc, 4)("[Pool] (%s) Increasing size of pool to %d items (%d bytes)", pool->name, pool->total_items, pool->total_items * pool->item_size);
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    50
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    51
	/* Increase the poolsize */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    52
	pool->blocks = realloc(pool->blocks, sizeof(pool->blocks[0]) * (pool->current_blocks + 1));
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    53
	if (pool->blocks == NULL)
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    54
		error("Pool: (%s) could not allocate memory for blocks", pool->name);
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
	/* Allocate memory to the new block item */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    57
	pool->blocks[pool->current_blocks] = malloc(pool->item_size * (1 << pool->block_size_bits));
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    58
	if (pool->blocks[pool->current_blocks] == NULL)
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    59
		error("Pool: (%s) could not allocate memory for blocks", pool->name);
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 */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    62
	memset(pool->blocks[pool->current_blocks], 0, pool->item_size * (1 << pool->block_size_bits));
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) */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    65
	if (pool->new_block_proc != NULL)
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    66
		pool->new_block_proc(pool->current_blocks * (1 << pool->block_size_bits));
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
	/* We have a new block */
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    69
	pool->current_blocks++;
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
	return true;
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
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    75
 * 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
    76
 *
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    77
 * @return Returns false if adding failed
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    78
 */
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
    79
bool AddBlockIfNeeded(OldMemoryPool *pool, uint index)
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    80
{
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    81
	while (index >= pool->total_items) {
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    82
		if (!AddBlockToPool(pool))
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    83
			return false;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    84
	}
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    85
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    86
	return true;
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    87
}