src/oldpool.h
author KUDr
Fri, 12 Jan 2007 15:43:00 +0000
changeset 5620 eab6f02899a0
parent 5587 167d9a91ef02
child 6247 7d81e3a5d803
permissions -rw-r--r--
(svn r8079) -Fix [YAPF]: float division by zero when calculating stats (YAPF cache hit ratio). Caused BSOD on Win9x. (thanks 3iff for report, Darkvater for help)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1259
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1259
diff changeset
     2
5224
df36f84cbb6c (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
     3
#ifndef OLDPOOL_H
df36f84cbb6c (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
8d8515e3da29 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     5
5216
8bd14ee39af2 (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
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 */
5216
8bd14ee39af2 (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
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 */
5216
8bd14ee39af2 (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
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
/**
5216
8bd14ee39af2 (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
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
 */
5216
8bd14ee39af2 (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 {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    19
	const char* 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
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    21
	uint max_blocks;      ///< The max amount of blocks this pool can have
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    22
	uint block_size_bits; ///< The size of each block in bits
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    23
	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
5216
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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
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
 */
5216
8bd14ee39af2 (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);
8bd14ee39af2 (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
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
 */
5216
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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) \
8bd14ee39af2 (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) \
8bd14ee39af2 (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; \
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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) \
8bd14ee39af2 (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
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
5216
8bd14ee39af2 (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) \
8bd14ee39af2 (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) \
8bd14ee39af2 (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) \
8bd14ee39af2 (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
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
5224
df36f84cbb6c (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
    96
#endif /* OLDPOOL_H */