author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1259 | 8d8515e3da29 |
child 2186 | db48cf29b983 |
permissions | -rw-r--r-- |
1259
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
1 |
#ifndef POOL_H |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
2 |
#define POOL_H |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
3 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
4 |
typedef struct MemoryPool MemoryPool; |
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 |
/* 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
|
7 |
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
|
8 |
typedef void MemoryPoolNewBlock(uint start_item); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
9 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
10 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
11 |
* 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
|
12 |
* please try to avoid manual calls! |
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 |
struct MemoryPool { |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
15 |
const char name[10]; //! Name of the pool (just for debugging) |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
16 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
17 |
const uint max_blocks; //! The max amount of blocks this pool can have |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
18 |
const uint block_size_bits; //! The size of each block in bits |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
19 |
const uint item_size; //! How many bytes one block is |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
20 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
21 |
MemoryPoolNewBlock *new_block_proc; |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
22 |
//!< Pointer to a 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
|
23 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
24 |
uint current_blocks; //! How many blocks we have in our pool |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
25 |
uint total_items; //! How many items we now have in this pool |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
26 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
27 |
byte **blocks; //! An array of blocks (one block hold all the items) |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
28 |
}; |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
29 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
30 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
31 |
* Those are the wrappers: |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
32 |
* 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
|
33 |
* (no need to call CreatePool!) |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
34 |
* 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
|
35 |
* more room |
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 |
void CleanPool(MemoryPool *array); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
38 |
bool AddBlockToPool(MemoryPool *array); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
39 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
40 |
/** |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
41 |
* 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
|
42 |
* |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
43 |
* @return Returns false if adding failed |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
44 |
*/ |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
45 |
bool AddBlockIfNeeded(MemoryPool *array, uint index); |
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 |
static inline byte *GetItemFromPool(MemoryPool *pool, uint index) |
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 |
assert(index < pool->total_items); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
50 |
return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size); |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
51 |
} |
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
52 |
|
8d8515e3da29
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
53 |
#endif /* POOL_H */ |