author | tron |
Sat, 28 Oct 2006 12:10:11 +0000 | |
changeset 4986 | 76eebd65bc6a |
parent 4970 | eb2c0fde4b02 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
3 |
#ifndef POOL_H |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
4 |
#define POOL_H |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
5 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
6 |
typedef struct MemoryPool MemoryPool; |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
7 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
8 |
/* The function that is called after a new block is added |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
9 |
start_item is the first item of the new made block */ |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
10 |
typedef void MemoryPoolNewBlock(uint start_item); |
3585
43461f26b729
(svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents:
3173
diff
changeset
|
11 |
/* The function that is called before a block is cleaned up */ |
43461f26b729
(svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents:
3173
diff
changeset
|
12 |
typedef void MemoryPoolCleanBlock(uint start_item, uint end_item); |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
13 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
14 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
15 |
* Stuff for dynamic vehicles. Use the wrappers to access the MemoryPool |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
16 |
* please try to avoid manual calls! |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
17 |
*/ |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
18 |
struct MemoryPool { |
4970
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
19 |
const char* const name; ///< Name of the pool (just for debugging) |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
20 |
|
3173
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
21 |
const uint max_blocks; ///< The max amount of blocks this pool can have |
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
22 |
const uint block_size_bits; ///< The size of each block in bits |
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
23 |
const uint item_size; ///< How many bytes one block is |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
24 |
|
3173
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
25 |
/// Pointer to a function that is called after a new block is added |
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
26 |
MemoryPoolNewBlock *new_block_proc; |
3585
43461f26b729
(svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents:
3173
diff
changeset
|
27 |
/// Pointer to a function that is called to clean a block |
43461f26b729
(svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents:
3173
diff
changeset
|
28 |
MemoryPoolCleanBlock *clean_block_proc; |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
29 |
|
3173
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
30 |
uint current_blocks; ///< How many blocks we have in our pool |
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
31 |
uint total_items; ///< How many items we now have in this pool |
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
32 |
|
f56ca618721b
(svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents:
2436
diff
changeset
|
33 |
byte **blocks; ///< An array of blocks (one block hold all the items) |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
34 |
}; |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
35 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
36 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
37 |
* Those are the wrappers: |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
38 |
* CleanPool cleans the pool up, but you can use AddBlockToPool directly again |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
39 |
* (no need to call CreatePool!) |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
40 |
* AddBlockToPool adds 1 more block to the pool. Returns false if there is no |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
41 |
* more room |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
42 |
*/ |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
43 |
void CleanPool(MemoryPool *array); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
44 |
bool AddBlockToPool(MemoryPool *array); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
45 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
46 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
47 |
* Adds blocks to the pool if needed (and possible) till index fits inside the pool |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
48 |
* |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
49 |
* @return Returns false if adding failed |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
50 |
*/ |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
51 |
bool AddBlockIfNeeded(MemoryPool *array, uint index); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
52 |
|
4970
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
53 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
54 |
#define POOL_ENUM(name, type, block_size_bits, max_blocks) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
55 |
enum { \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
56 |
name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
57 |
name##_POOL_MAX_BLOCKS = max_blocks \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
58 |
}; |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
59 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
60 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
61 |
#define POOL_ACCESSORS(name, type) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
62 |
static inline type* Get##name(uint index) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
63 |
{ \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
64 |
assert(index < _##name##_pool.total_items); \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
65 |
return (type*)( \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
66 |
_##name##_pool.blocks[index >> name##_POOL_BLOCK_SIZE_BITS] + \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
67 |
(index & ((1 << name##_POOL_BLOCK_SIZE_BITS) - 1)) * sizeof(type) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
68 |
); \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
69 |
} \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
70 |
\ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
71 |
static inline uint Get##name##PoolSize(void) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
72 |
{ \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
73 |
return _##name##_pool.total_items; \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
74 |
} |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
75 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
76 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
77 |
#define DECLARE_POOL(name, type, block_size_bits, max_blocks) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
78 |
POOL_ENUM(name, type, block_size_bits, max_blocks) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
79 |
extern MemoryPool _##name##_pool; \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
80 |
POOL_ACCESSORS(name, type) |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
81 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
82 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
83 |
#define DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
84 |
MemoryPool _##name##_pool = { \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
85 |
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
86 |
new_block_proc, clean_block_proc, \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
87 |
0, 0, NULL \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
88 |
}; |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
89 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
90 |
|
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
91 |
#define STATIC_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
92 |
POOL_ENUM(name, type, block_size_bits, max_blocks) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
93 |
static DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \ |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
94 |
POOL_ACCESSORS(name, type) |
eb2c0fde4b02
(svn r6973) Add macros to easily create a pool with less code duplication and more opportunities for constant expression evaluation
tron
parents:
3585
diff
changeset
|
95 |
|
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
96 |
#endif /* POOL_H */ |