oldpool.h
author celestar
Fri, 29 Dec 2006 10:13:35 +0000
branchcustombridgeheads
changeset 5592 fd60d4ecc921
parent 5224 ddab137de945
permissions -rw-r--r--
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1259
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1259
diff changeset
     2
5224
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
     3
#ifndef OLDPOOL_H
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
     4
#define OLDPOOL_H
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     5
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: 4986
diff changeset
     6
typedef struct OldMemoryPool OldMemoryPool;
1259
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 */
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: 4986
diff changeset
    10
typedef void OldMemoryPoolNewBlock(uint start_item);
3585
18a23b37e99a (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 */
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: 4986
diff changeset
    12
typedef void OldMemoryPoolCleanBlock(uint start_item, uint end_item);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    13
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    14
/**
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: 4986
diff changeset
    15
 * Stuff for dynamic vehicles. Use the wrappers to access the OldMemoryPool
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    16
 *  please try to avoid manual calls!
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    17
 */
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: 4986
diff changeset
    18
struct OldMemoryPool {
4970
d5b2b619f48c (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    20
3173
1b75b9a6ff71 (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
1b75b9a6ff71 (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
1b75b9a6ff71 (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    24
3173
1b75b9a6ff71 (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
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: 4986
diff changeset
    26
	OldMemoryPoolNewBlock *new_block_proc;
3585
18a23b37e99a (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
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: 4986
diff changeset
    28
	OldMemoryPoolCleanBlock *clean_block_proc;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    29
3173
1b75b9a6ff71 (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
1b75b9a6ff71 (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
1b75b9a6ff71 (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2436
diff changeset
    32
1b75b9a6ff71 (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    34
};
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    35
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    36
/**
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    37
 * Those are the wrappers:
6dc9a1521c00 (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    39
 *     (no need to call CreatePool!)
6dc9a1521c00 (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
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    41
 *     more room
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    42
 */
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: 4986
diff changeset
    43
void CleanPool(OldMemoryPool *array);
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: 4986
diff changeset
    44
bool AddBlockToPool(OldMemoryPool *array);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    45
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
 * 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
    48
 *
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    49
 * @return Returns false if adding failed
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    50
 */
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: 4986
diff changeset
    51
bool AddBlockIfNeeded(OldMemoryPool *array, uint index);
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    52
4970
d5b2b619f48c (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
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: 4986
diff changeset
    54
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
4970
d5b2b619f48c (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 { \
d5b2b619f48c (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, \
d5b2b619f48c (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 \
d5b2b619f48c (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
	};
d5b2b619f48c (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
d5b2b619f48c (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
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: 4986
diff changeset
    61
#define OLD_POOL_ACCESSORS(name, type) \
4970
d5b2b619f48c (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) \
d5b2b619f48c (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
	{ \
d5b2b619f48c (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); \
d5b2b619f48c (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*)( \
d5b2b619f48c (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] + \
d5b2b619f48c (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) \
d5b2b619f48c (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
		); \
d5b2b619f48c (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
	} \
d5b2b619f48c (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
\
d5b2b619f48c (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) \
d5b2b619f48c (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
	{ \
d5b2b619f48c (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; \
d5b2b619f48c (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
	}
d5b2b619f48c (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
d5b2b619f48c (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
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: 4986
diff changeset
    77
#define DECLARE_OLD_POOL(name, type, block_size_bits, max_blocks) \
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: 4986
diff changeset
    78
	OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
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: 4986
diff changeset
    79
	extern OldMemoryPool _##name##_pool; \
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: 4986
diff changeset
    80
	OLD_POOL_ACCESSORS(name, type)
4970
d5b2b619f48c (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
d5b2b619f48c (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
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: 4986
diff changeset
    83
#define DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
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: 4986
diff changeset
    84
	OldMemoryPool _##name##_pool = { \
4970
d5b2b619f48c (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), \
d5b2b619f48c (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, \
d5b2b619f48c (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 \
d5b2b619f48c (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
	};
d5b2b619f48c (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
d5b2b619f48c (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
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: 4986
diff changeset
    91
#define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \
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: 4986
diff changeset
    92
	OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
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: 4986
diff changeset
    93
	static DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
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: 4986
diff changeset
    94
	OLD_POOL_ACCESSORS(name, type)
4970
d5b2b619f48c (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
5224
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
    96
#endif /* OLDPOOL_H */