src/oldpool.h
author rubidium
Sat, 12 Apr 2008 21:38:49 +0000
branchnoai
changeset 10142 56ee7da4ad56
parent 9723 eee46cb39750
child 10355 ee4b5f7a5bf2
permissions -rw-r--r--
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
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
9505
9711235f5693 (svn r9396) [NoAI] -Sync with trunk r9362:9393
glx
parents: 6574
diff changeset
     3
/** @file oldpool.h */
9711235f5693 (svn r9396) [NoAI] -Sync with trunk r9362:9393
glx
parents: 6574
diff changeset
     4
5224
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
     5
#ifndef OLDPOOL_H
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
     6
#define OLDPOOL_H
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
     7
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
     8
#include "core/math_func.hpp"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
     9
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    10
/* 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
    11
     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
    12
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
    13
/* 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
    14
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
    15
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    16
/**
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
    17
 * 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
    18
 *  please try to avoid manual calls!
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    19
 */
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    20
struct OldMemoryPoolBase {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    21
	void CleanPool();
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    22
	bool AddBlockToPool();
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    23
	bool AddBlockIfNeeded(uint index);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    24
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    25
protected:
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    26
	OldMemoryPoolBase(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    27
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    28
		name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    29
		new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
    30
		total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    31
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    32
	const char* 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
    33
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    34
	const uint max_blocks;      ///< The max amount of blocks this pool can have
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    35
	const uint block_size_bits; ///< The size of each block in bits
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    36
3173
1b75b9a6ff71 (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2436
diff changeset
    37
	/// 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
    38
	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
    39
	/// 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
    40
	OldMemoryPoolCleanBlock *clean_block_proc;
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
    41
3173
1b75b9a6ff71 (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2436
diff changeset
    42
	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
    43
	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
    44
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
    45
	bool cleaning_pool;         ///< Are we currently cleaning the pool?
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    46
public:
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    47
	const uint item_size;       ///< How many bytes one block is
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    48
	uint first_free_index;      ///< The index of the first free pool item in this pool
3173
1b75b9a6ff71 (svn r3805) - [FS#62] Fix doxygen comments to refer to the correct parameter. (sulai)
peter1138
parents: 2436
diff changeset
    49
	byte **blocks;              ///< An array of blocks (one block hold all the items)
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    50
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    51
	/**
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    52
	 * Check if the index of pool item being deleted is lower than cached first_free_index
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    53
	 * @param index index of pool item
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    54
	 * @note usage of min() will result in better code on some architectures
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    55
	 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    56
	inline void UpdateFirstFreeIndex(uint index)
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    57
	{
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    58
		first_free_index = min(first_free_index, index);
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    59
	}
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    60
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    61
	/**
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    62
	 * Get the size of this pool, i.e. the total number of items you
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    63
	 * can put into it at the current moment; the pool might still
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    64
	 * be able to increase the size of the pool.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    65
	 * @return the size of the pool
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    66
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    67
	inline uint GetSize() const
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    68
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    69
		return this->total_items;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    70
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    71
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    72
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    73
	 * Can this pool allocate more blocks, i.e. is the maximum amount
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    74
	 * of allocated blocks not yet reached?
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    75
	 * @return the if and only if the amount of allocable blocks is
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    76
	 *         less than the amount of allocated blocks.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    77
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    78
	inline bool CanAllocateMoreBlocks() const
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    79
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    80
		return this->current_blocks < this->max_blocks;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    81
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    82
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    83
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    84
	 * Get the maximum number of allocable blocks.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    85
	 * @return the numebr of blocks
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    86
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    87
	inline uint GetBlockCount() const
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    88
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    89
		return this->current_blocks;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    90
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    91
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    92
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    93
	 * Get the name of this pool.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    94
	 * @return the name
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    95
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    96
	inline const char *GetName() const
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    97
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    98
		return this->name;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    99
	}
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   100
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   101
	/**
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   102
	 * Is the pool in the cleaning phase?
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   103
	 * @return true if it is
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   104
	 */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   105
	inline bool CleaningPool() const
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   106
	{
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   107
		return this->cleaning_pool;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   108
	}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   109
};
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   110
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   111
template <typename T>
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   112
struct OldMemoryPool : public OldMemoryPoolBase {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   113
	OldMemoryPool(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   114
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   115
		OldMemoryPoolBase(name, max_blocks, block_size_bits, item_size, new_block_proc, clean_block_proc) {}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   116
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   117
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   118
	 * Get the pool entry at the given index.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   119
	 * @param index the index into the pool
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   120
	 * @pre index < this->GetSize()
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   121
	 * @return the pool entry.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   122
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   123
	inline T *Get(uint index) const
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   124
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   125
		assert(index < this->GetSize());
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   126
		return (T*)(this->blocks[index >> this->block_size_bits] +
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   127
				(index & ((1 << this->block_size_bits) - 1)) * this->item_size);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   128
	}
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   129
};
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   130
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   131
/**
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   132
 * Generic function to initialize a new block in a pool.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   133
 * @param start_item the first item that needs to be initialized
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   134
 */
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   135
template <typename T, OldMemoryPool<T> *Tpool>
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   136
static void PoolNewBlock(uint start_item)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   137
{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   138
	for (T *t = Tpool->Get(start_item); t != NULL; t = (t->index + 1U < Tpool->GetSize()) ? Tpool->Get(t->index + 1U) : NULL) {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   139
		t = new (t) T();
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   140
		t->index = start_item++;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   141
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   142
}
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   143
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   144
/**
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   145
 * Generic function to free a new block in a pool.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   146
 * @param start_item the first item that needs to be cleaned
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   147
 * @param end_item   the last item that needs to be cleaned
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   148
 */
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   149
template <typename T, OldMemoryPool<T> *Tpool>
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   150
static void PoolCleanBlock(uint start_item, uint end_item)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   151
{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   152
	for (uint i = start_item; i <= end_item; i++) {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   153
		T *t = Tpool->Get(i);
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   154
		delete t;
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   155
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   156
}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   157
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   158
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   159
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   160
 * Generalization for all pool items that are saved in the savegame.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   161
 * It specifies all the mechanics to access the pool easily.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   162
 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   163
template <typename T, typename Tid, OldMemoryPool<T> *Tpool>
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   164
struct PoolItem {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   165
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   166
	 * The pool-wide index of this object.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   167
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   168
	Tid index;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   169
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   170
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   171
	 * We like to have the correct class destructed.
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   172
	 * @warning It is called even for object allocated on stack,
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   173
	 *          so it is not present in the TPool!
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   174
	 *          Then, index is undefined, not associated with TPool in any way.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   175
	 * @note    The idea is to free up allocated memory etc.
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   176
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   177
	virtual ~PoolItem()
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   178
	{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   179
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   180
	}
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   181
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   182
	/**
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   183
	 * Constructor of given class.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   184
	 * @warning It is called even for object allocated on stack,
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   185
	 *          so it may not be present in TPool!
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   186
	 *          Then, index is undefined, not associated with TPool in any way.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   187
	 * @note    The idea is to initialize variables (except index)
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   188
	 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   189
	PoolItem()
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   190
	{
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   191
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   192
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   193
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   194
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   195
	 * An overriden version of new that allocates memory on the pool.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   196
	 * @param size the size of the variable (unused)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   197
	 * @return the memory that is 'allocated'
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   198
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   199
	void *operator new(size_t size)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   200
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   201
		return AllocateRaw();
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   202
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   203
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   204
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   205
	 * 'Free' the memory allocated by the overriden new.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   206
	 * @param p the memory to 'free'
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   207
	 * @note we only update Tpool->first_free_index
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   208
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   209
	void operator delete(void *p)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   210
	{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   211
		Tpool->UpdateFirstFreeIndex(((T*)p)->index);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   212
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   213
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   214
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   215
	 * An overriden version of new, so you can directly allocate a new object with
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   216
	 * the correct index when one is loading the savegame.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   217
	 * @param size  the size of the variable (unused)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   218
	 * @param index the index of the object
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   219
	 * @return the memory that is 'allocated'
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   220
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   221
	void *operator new(size_t size, int index)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   222
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   223
		if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   224
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   225
		return Tpool->Get(index);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   226
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   227
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   228
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   229
	 * 'Free' the memory allocated by the overriden new.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   230
	 * @param p     the memory to 'free'
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   231
	 * @param index the original parameter given to create the memory
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   232
	 * @note we only update Tpool->first_free_index
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   233
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   234
	void operator delete(void *p, int index)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   235
	{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   236
		Tpool->UpdateFirstFreeIndex(index);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   237
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   238
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   239
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   240
	 * An overriden version of new, so you can use the vehicle instance
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   241
	 * instead of a newly allocated piece of memory.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   242
	 * @param size the size of the variable (unused)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   243
	 * @param pn   the already existing object to use as 'storage' backend
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   244
	 * @return the memory that is 'allocated'
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   245
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   246
	void *operator new(size_t size, T *pn)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   247
	{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   248
		return pn;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   249
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   250
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   251
	/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   252
	 * 'Free' the memory allocated by the overriden new.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   253
	 * @param p  the memory to 'free'
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   254
	 * @param pn the pointer that was given to 'new' on creation.
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   255
	 * @note we only update Tpool->first_free_index
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   256
	 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   257
	void operator delete(void *p, T *pn)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   258
	{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
   259
		Tpool->UpdateFirstFreeIndex(pn->index);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   260
	}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   261
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   262
private:
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents: 9723
diff changeset
   263
	static T *AllocateSafeRaw(uint &first);
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   264
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   265
protected:
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   266
	/**
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   267
	 * Allocate a pool item; possibly allocate a new block in the pool.
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   268
	 * @return the allocated pool item (or NULL when the pool is full).
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   269
	 */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   270
	static inline T *AllocateRaw()
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   271
	{
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   272
		return AllocateSafeRaw(Tpool->first_free_index);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   273
	}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   274
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   275
	/**
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   276
	 * Allocate a pool item; possibly allocate a new block in the pool.
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   277
	 * @param first the first pool item to start searching
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   278
	 * @return the allocated pool item (or NULL when the pool is full).
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   279
	 */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   280
	static inline T *AllocateRaw(uint &first)
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   281
	{
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   282
		if (first >= Tpool->GetSize() && !Tpool->AddBlockToPool()) return NULL;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   283
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   284
		return AllocateSafeRaw(first);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   285
	}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   286
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   287
	/**
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   288
	 * Are we cleaning this pool?
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   289
	 * @return true if we are
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   290
	 */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   291
	static inline bool CleaningPool()
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   292
	{
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   293
		return Tpool->CleaningPool();
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   294
	}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   295
};
1259
6dc9a1521c00 (svn r1763) -Add: pool.c / pool.h: generalized routines for dynamic arrays (MemoryPools)
truelight
parents:
diff changeset
   296
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
   297
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
   298
#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
   299
	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
   300
		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
   301
		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
   302
	};
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
   303
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
   304
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
   305
#define OLD_POOL_ACCESSORS(name, type) \
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   306
	static inline type* Get##name(uint index) { return _##name##_pool.Get(index);  } \
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   307
	static inline uint Get##name##PoolSize()  { return _##name##_pool.GetSize(); }
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
   308
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
   309
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
   310
#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
   311
	OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   312
	extern OldMemoryPool<type> _##name##_pool; \
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
   313
	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
   314
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
   315
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
   316
#define DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   317
	OldMemoryPool<type> _##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
   318
		#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   319
		new_block_proc, clean_block_proc);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   320
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   321
#define DEFINE_OLD_POOL_GENERIC(name, type) \
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   322
	OldMemoryPool<type> _##name##_pool( \
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
   323
		#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents: 9723
diff changeset
   324
		PoolNewBlock<type, &_##name##_pool>, PoolCleanBlock<type, &_##name##_pool>); \
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents: 9723
diff changeset
   325
		template type *PoolItem<type, type##ID, &_##name##_pool>::AllocateSafeRaw(uint &first);
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
   326
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
   327
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
   328
#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
   329
	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
   330
	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
   331
	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
   332
5224
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
   333
#endif /* OLDPOOL_H */