Mercurial > hg > openttd
annotate src/oldpool.cpp @ 8108:4faab45e2603 draft
(svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
author | rubidium <rubidium@openttd.org> |
---|---|
date | Wed, 19 Dec 2007 23:26:02 +0000 (2007-12-19) |
parents | 515808dcc91c |
children | 0586823afe39 |
rev | line source |
---|---|
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
1 /* $Id$ */ |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
2 |
6351
1dcf39ea9b9b
(svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas <belugas@openttd.org>
parents:
5609
diff
changeset
|
3 /** @file oldpool.cpp */ |
1dcf39ea9b9b
(svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas <belugas@openttd.org>
parents:
5609
diff
changeset
|
4 |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
5 #include "stdafx.h" |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
6 #include "openttd.h" |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
7 #include "debug.h" |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
8 #include "functions.h" |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
9 #include "oldpool.h" |
5587
c44c070c5032
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium <rubidium@openttd.org>
parents:
5584
diff
changeset
|
10 #include "helpers.hpp" |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
11 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
12 /** |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
13 * Clean a pool in a safe way (does free all blocks) |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
14 */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
15 void OldMemoryPoolBase::CleanPool() |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
16 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
17 uint i; |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
18 |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
19 DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
20 |
7413
571c87eed06c
(svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium <rubidium@openttd.org>
parents:
7378
diff
changeset
|
21 this->cleaning_pool = true; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
22 /* Free all blocks */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
23 for (i = 0; i < this->current_blocks; i++) { |
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
24 if (this->clean_block_proc != NULL) { |
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
25 this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
26 } |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
27 free(this->blocks[i]); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
28 } |
7413
571c87eed06c
(svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium <rubidium@openttd.org>
parents:
7378
diff
changeset
|
29 this->cleaning_pool = false; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
30 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
31 /* Free the block itself */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
32 free(this->blocks); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
33 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
34 /* Clear up some critical data */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
35 this->total_items = 0; |
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
36 this->current_blocks = 0; |
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
37 this->blocks = NULL; |
7378
b65f9fb4175f
(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 <rubidium@openttd.org>
parents:
7375
diff
changeset
|
38 this->first_free_index = 0; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
39 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
40 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
41 /** |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
42 * This function tries to increase the size of array by adding |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
43 * 1 block too it |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
44 * |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
45 * @return Returns false if the pool could not be increased |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
46 */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
47 bool OldMemoryPoolBase::AddBlockToPool() |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
48 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
49 /* Is the pool at his max? */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
50 if (this->max_blocks == this->current_blocks) return false; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
51 |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
52 this->total_items = (this->current_blocks + 1) * (1 << this->block_size_bits); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
53 |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
54 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); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
55 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
56 /* Increase the poolsize */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
57 this->blocks = ReallocT(this->blocks, this->current_blocks + 1); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
58 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
59 /* Allocate memory to the new block item */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
60 this->blocks[this->current_blocks] = MallocT<byte>(this->item_size * (1 << this->block_size_bits)); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
61 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
62 /* Clean the content of the new block */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
63 memset(this->blocks[this->current_blocks], 0, this->item_size * (1 << this->block_size_bits)); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
64 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
65 /* Call a custom function if defined (e.g. to fill indexes) */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
66 if (this->new_block_proc != NULL) this->new_block_proc(this->current_blocks * (1 << this->block_size_bits)); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
67 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
68 /* We have a new block */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
69 this->current_blocks++; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
70 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
71 return true; |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
72 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
73 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
74 /** |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
75 * Adds blocks to the pool if needed (and possible) till index fits inside the pool |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
76 * |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
77 * @return Returns false if adding failed |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
78 */ |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
79 bool OldMemoryPoolBase::AddBlockIfNeeded(uint index) |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
80 { |
7375
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
81 while (index >= this->total_items) { |
d9b33b58878c
(svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium <rubidium@openttd.org>
parents:
6351
diff
changeset
|
82 if (!this->AddBlockToPool()) return false; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
83 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
84 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
85 return true; |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
86 } |