author | miham |
Wed, 17 Aug 2005 12:30:07 +0000 | |
changeset 2349 | df02d0ffb588 |
parent 2186 | 461a2aff3486 |
child 2436 | 177cb6a8339f |
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 |
#ifndef POOL_H |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
4 |
#define POOL_H |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
5 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
6 |
typedef struct MemoryPool MemoryPool; |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
7 |
|
6dc9a1521c00
(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 |
6dc9a1521c00
(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 */ |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
10 |
typedef void MemoryPoolNewBlock(uint start_item); |
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 |
/** |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
13 |
* Stuff for dynamic vehicles. Use the wrappers to access the MemoryPool |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
14 |
* please try to avoid manual calls! |
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 |
struct MemoryPool { |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
17 |
const char name[10]; //! Name of the pool (just for debugging) |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
18 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
19 |
const uint max_blocks; //! The max amount of blocks this pool can have |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
20 |
const uint block_size_bits; //! The size of each block in bits |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
21 |
const uint item_size; //! How many bytes one block is |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
22 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
23 |
MemoryPoolNewBlock *new_block_proc; |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
24 |
//!< Pointer to a function that is called after a new block is added |
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 |
uint current_blocks; //! How many blocks we have in our pool |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
27 |
uint total_items; //! How many items we now have in this pool |
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 |
byte **blocks; //! An array of blocks (one block hold all the items) |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
30 |
}; |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
31 |
|
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 |
* Those are the wrappers: |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
34 |
* CleanPool cleans the pool up, but you can use AddBlockToPool directly again |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
35 |
* (no need to call CreatePool!) |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
36 |
* AddBlockToPool adds 1 more block to the pool. Returns false if there is no |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
37 |
* more room |
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 |
void CleanPool(MemoryPool *array); |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
40 |
bool AddBlockToPool(MemoryPool *array); |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
41 |
|
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 |
* 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
|
44 |
* |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
45 |
* @return Returns false if adding failed |
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 |
bool AddBlockIfNeeded(MemoryPool *array, uint index); |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
48 |
|
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
49 |
static inline byte *GetItemFromPool(MemoryPool *pool, uint index) |
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 |
assert(index < pool->total_items); |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
52 |
return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size); |
6dc9a1521c00
(svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff
changeset
|
53 |
} |
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 |
#endif /* POOL_H */ |