author | KUDr |
Sun, 31 Dec 2006 02:53:23 +0000 | |
branch | custombridgeheads |
changeset 5611 | 11da6bafbfb9 |
parent 5568 | 75f13d7bfaed |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
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 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5216
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 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5216
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); |
1259
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)); |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5216
diff
changeset
|
53 |
if (pool->blocks == NULL) error("Pool: (%s) could not allocate memory for blocks", pool->name); |
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 |
/* 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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
60 |
/* 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
|
61 |
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
|
62 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
63 |
/* 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
|
64 |
if (pool->new_block_proc != NULL) |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
65 |
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
|
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 */ |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
68 |
pool->current_blocks++; |
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 |
*/ |
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
|
78 |
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
|
79 |
{ |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
80 |
while (index >= pool->total_items) { |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
81 |
if (!AddBlockToPool(pool)) |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
82 |
return false; |
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 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
85 |
return true; |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
86 |
} |