author | truebrain |
Fri, 18 Jul 2008 01:00:03 +0000 | |
branch | noai |
changeset 11166 | 17960948c3af |
parent 10455 | 22c441f5adf9 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9723
diff
changeset
|
3 |
/** @file oldpool.cpp Implementation of the old pool. */ |
9505 | 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" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9701
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 |
*/ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
22 |
for (i = 0; i < this->current_blocks; i++) { |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
23 |
if (this->clean_block_proc != NULL) { |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
} |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
} |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
34 |
this->total_items = 0; |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
35 |
this->current_blocks = 0; |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
36 |
this->blocks = NULL; |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
*/ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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? */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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) */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 */ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
*/ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
{ |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
diff
changeset
|
80 |
while (index >= this->total_items) { |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9505
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 |
} |