src/oldpool.h
author rubidium
Thu, 24 Apr 2008 11:48:09 +0000
branch0.6
changeset 10323 57d8fd25473e
parent 8609 8c0c3e9dd6a0
permissions -rw-r--r--
(svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
- Fix: Make the town rating tests use less memory and much quicker (r12859)
- Fix: Usage of AutoPtr made (trying to) build stuff very expensive (r12857, r12855)
- Fix: Use YAPF for fairly old savegames from before YAPF was introduced (r12845)
- Fix: The industry tick trigger should only be triggered once every 256 ticks, not every tick... Also bail out of the triggers a little earlier if you know they are not going to happen anyway (r12844)
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
6677
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 6574
diff changeset
     3
/** @file oldpool.h */
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
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
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8470
diff changeset
     8
#include "core/math_func.hpp"
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8470
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
 */
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    20
struct OldMemoryPoolBase {
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    21
	void CleanPool();
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    22
	bool AddBlockToPool();
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    23
	bool AddBlockIfNeeded(uint index);
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    24
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    25
protected:
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    26
	OldMemoryPoolBase(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    27
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
7881
9f20c98e2af7 (svn r10754) -Fix: MorphOS does not like sizeof in the templated pool item class, so use the item size that is set in the pool.
rubidium
parents: 7879
diff changeset
    28
		name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    29
		new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
    30
		total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
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
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    34
	const uint max_blocks;      ///< The max amount of blocks this pool can have
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
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
7879
5aebaae4c34b (svn r10752) -Fix: apparantly const on function pointers is ignored.
rubidium
parents: 7877
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
7879
5aebaae4c34b (svn r10752) -Fix: apparantly const on function pointers is ignored.
rubidium
parents: 7877
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
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
    45
	bool cleaning_pool;         ///< Are we currently cleaning the pool?
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    46
public:
7881
9f20c98e2af7 (svn r10754) -Fix: MorphOS does not like sizeof in the templated pool item class, so use the item size that is set in the pool.
rubidium
parents: 7879
diff changeset
    47
	const uint item_size;       ///< How many bytes one block is
7874
c9fae1fb5d6c (svn r10747) -Codechange: add a variable that points to some index in the pool that is not beyond the first free pool item. It does not necessarily point to the first free item, but it reduces allocation time as it does not have to start at the first item in the pool to find the first free item.
rubidium
parents: 7873
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)
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    50
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    51
	/**
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    52
	 * Check if the index of pool item being deleted is lower than cached first_free_index
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    53
	 * @param index index of pool item
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    54
	 * @note usage of min() will result in better code on some architectures
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    55
	 */
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    56
	inline void UpdateFirstFreeIndex(uint index)
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    57
	{
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    58
		first_free_index = min(first_free_index, index);
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    59
	}
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    60
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
    61
	/**
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    62
	 * Get the size of this pool, i.e. the total number of items you
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    63
	 * can put into it at the current moment; the pool might still
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    64
	 * be able to increase the size of the pool.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    65
	 * @return the size of the pool
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    66
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    67
	inline uint GetSize() const
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    68
	{
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    69
		return this->total_items;
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    70
	}
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    71
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    72
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    73
	 * Can this pool allocate more blocks, i.e. is the maximum amount
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    74
	 * of allocated blocks not yet reached?
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    75
	 * @return the if and only if the amount of allocable blocks is
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    76
	 *         less than the amount of allocated blocks.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    77
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    78
	inline bool CanAllocateMoreBlocks() const
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    79
	{
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    80
		return this->current_blocks < this->max_blocks;
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    81
	}
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    82
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    83
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    84
	 * Get the maximum number of allocable blocks.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    85
	 * @return the numebr of blocks
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    86
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    87
	inline uint GetBlockCount() const
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    88
	{
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    89
		return this->current_blocks;
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
    90
	}
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    91
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    92
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    93
	 * Get the name of this pool.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    94
	 * @return the name
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    95
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    96
	inline const char *GetName() const
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    97
	{
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    98
		return this->name;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
    99
	}
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   100
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   101
	/**
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   102
	 * Is the pool in the cleaning phase?
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   103
	 * @return true if it is
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   104
	 */
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   105
	inline bool CleaningPool() const
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   106
	{
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   107
		return this->cleaning_pool;
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   108
	}
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   109
};
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   110
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   111
template <typename T>
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   112
struct OldMemoryPool : public OldMemoryPoolBase {
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   113
	OldMemoryPool(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   114
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   115
		OldMemoryPoolBase(name, max_blocks, block_size_bits, item_size, new_block_proc, clean_block_proc) {}
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   116
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   117
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   118
	 * Get the pool entry at the given index.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   119
	 * @param index the index into the pool
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   120
	 * @pre index < this->GetSize()
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   121
	 * @return the pool entry.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   122
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   123
	inline T *Get(uint index) const
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   124
	{
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   125
		assert(index < this->GetSize());
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   126
		return (T*)(this->blocks[index >> this->block_size_bits] +
7881
9f20c98e2af7 (svn r10754) -Fix: MorphOS does not like sizeof in the templated pool item class, so use the item size that is set in the pool.
rubidium
parents: 7879
diff changeset
   127
				(index & ((1 << this->block_size_bits) - 1)) * this->item_size);
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
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
/**
7872
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   132
 * Generic function to initialize a new block in a pool.
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   133
 * @param start_item the first item that needs to be initialized
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   134
 */
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   135
template <typename T, OldMemoryPool<T> *Tpool>
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   136
static void PoolNewBlock(uint start_item)
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   137
{
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   138
	for (T *t = Tpool->Get(start_item); t != NULL; t = (t->index + 1U < Tpool->GetSize()) ? Tpool->Get(t->index + 1U) : NULL) {
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   139
		t = new (t) T();
7872
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   140
		t->index = start_item++;
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   141
	}
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   142
}
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   143
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   144
/**
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   145
 * Generic function to free a new block in a pool.
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   146
 * @param start_item the first item that needs to be cleaned
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   147
 * @param end_item   the last item that needs to be cleaned
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   148
 */
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   149
template <typename T, OldMemoryPool<T> *Tpool>
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   150
static void PoolCleanBlock(uint start_item, uint end_item)
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   151
{
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   152
	for (uint i = start_item; i <= end_item; i++) {
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   153
		T *t = Tpool->Get(i);
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   154
		delete t;
7872
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   155
	}
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   156
}
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   157
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   158
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   159
/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   160
 * Generalization for all pool items that are saved in the savegame.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   161
 * It specifies all the mechanics to access the pool easily.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   162
 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   163
template <typename T, typename Tid, OldMemoryPool<T> *Tpool>
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   164
struct PoolItem {
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   165
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   166
	 * The pool-wide index of this object.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   167
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   168
	Tid index;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   169
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   170
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   171
	 * We like to have the correct class destructed.
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   172
	 * @warning It is called even for object allocated on stack,
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   173
	 *          so it is not present in the TPool!
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   174
	 *          Then, index is undefined, not associated with TPool in any way.
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   175
	 * @note    The idea is to free up allocated memory etc.
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   176
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   177
	virtual ~PoolItem()
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   178
	{
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   179
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   180
	}
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   181
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   182
	/**
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   183
	 * Constructor of given class.
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   184
	 * @warning It is called even for object allocated on stack,
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   185
	 *          so it may not be present in TPool!
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   186
	 *          Then, index is undefined, not associated with TPool in any way.
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   187
	 * @note    The idea is to initialize variables (except index)
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   188
	 */
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   189
	PoolItem()
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   190
	{
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   191
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   192
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   193
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   194
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   195
	 * An overriden version of new that allocates memory on the pool.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   196
	 * @param size the size of the variable (unused)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   197
	 * @return the memory that is 'allocated'
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   198
	 */
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7874
diff changeset
   199
	void *operator new(size_t size)
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   200
	{
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   201
		return AllocateRaw();
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   202
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   203
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   204
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   205
	 * 'Free' the memory allocated by the overriden new.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   206
	 * @param p the memory to 'free'
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   207
	 * @note we only update Tpool->first_free_index
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   208
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   209
	void operator delete(void *p)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   210
	{
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   211
		Tpool->UpdateFirstFreeIndex(((T*)p)->index);
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   212
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   213
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   214
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   215
	 * An overriden version of new, so you can directly allocate a new object with
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   216
	 * the correct index when one is loading the savegame.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   217
	 * @param size  the size of the variable (unused)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   218
	 * @param index the index of the object
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   219
	 * @return the memory that is 'allocated'
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   220
	 */
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7874
diff changeset
   221
	void *operator new(size_t size, int index)
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   222
	{
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   223
		if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   224
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   225
		return Tpool->Get(index);
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   226
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   227
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   228
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   229
	 * 'Free' the memory allocated by the overriden new.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   230
	 * @param p     the memory to 'free'
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   231
	 * @param index the original parameter given to create the memory
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   232
	 * @note we only update Tpool->first_free_index
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   233
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   234
	void operator delete(void *p, int index)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   235
	{
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   236
		Tpool->UpdateFirstFreeIndex(index);
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   237
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   238
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   239
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   240
	 * An overriden version of new, so you can use the vehicle instance
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   241
	 * instead of a newly allocated piece of memory.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   242
	 * @param size the size of the variable (unused)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   243
	 * @param pn   the already existing object to use as 'storage' backend
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   244
	 * @return the memory that is 'allocated'
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   245
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   246
	void *operator new(size_t size, T *pn)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   247
	{
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   248
		return pn;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   249
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   250
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   251
	/**
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   252
	 * 'Free' the memory allocated by the overriden new.
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   253
	 * @param p  the memory to 'free'
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   254
	 * @param pn the pointer that was given to 'new' on creation.
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   255
	 * @note we only update Tpool->first_free_index
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   256
	 */
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   257
	void operator delete(void *p, T *pn)
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   258
	{
8470
791d75060ebd (svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
smatz
parents: 7992
diff changeset
   259
		Tpool->UpdateFirstFreeIndex(pn->index);
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   260
	}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   261
7922
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   262
private:
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   263
	/**
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   264
	 * Allocate a pool item; possibly allocate a new block in the pool.
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   265
	 * @param first the first pool item to start searching
7922
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   266
	 * @pre first <= Tpool->GetSize()
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   267
	 * @return the allocated pool item (or NULL when the pool is full).
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   268
	 */
7922
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   269
	static inline T *AllocateSafeRaw(uint &first)
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   270
	{
7898
fdc041ac5190 (svn r10774) -Fix (r10768): obiwan pleased us with a visit.
rubidium
parents: 7897
diff changeset
   271
		uint last_minus_one = Tpool->GetSize() - 1;
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   272
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   273
		for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   274
			if (!t->IsValid()) {
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   275
				first = t->index;
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   276
				Tid index = t->index;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   277
7881
9f20c98e2af7 (svn r10754) -Fix: MorphOS does not like sizeof in the templated pool item class, so use the item size that is set in the pool.
rubidium
parents: 7879
diff changeset
   278
				memset(t, 0, Tpool->item_size);
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   279
				t->index = index;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   280
				return t;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   281
			}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   282
		}
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   283
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   284
		/* Check if we can add a block to the pool */
7893
09e1a1fe297d (svn r10768) -Codechange: allow to specify from which index to search for a free pool item.
rubidium
parents: 7881
diff changeset
   285
		if (Tpool->AddBlockToPool()) return AllocateRaw(first);
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   286
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   287
		return NULL;
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   288
	}
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   289
7922
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   290
protected:
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   291
	/**
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   292
	 * Allocate a pool item; possibly allocate a new block in the pool.
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   293
	 * @return the allocated pool item (or NULL when the pool is full).
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   294
	 */
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   295
	static inline T *AllocateRaw()
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   296
	{
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   297
		return AllocateSafeRaw(Tpool->first_free_index);
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   298
	}
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   299
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   300
	/**
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   301
	 * Allocate a pool item; possibly allocate a new block in the pool.
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   302
	 * @param first the first pool item to start searching
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   303
	 * @return the allocated pool item (or NULL when the pool is full).
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   304
	 */
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   305
	static inline T *AllocateRaw(uint &first)
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   306
	{
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   307
		if (first >= Tpool->GetSize() && !Tpool->AddBlockToPool()) return NULL;
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   308
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   309
		return AllocateSafeRaw(first);
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   310
	}
9a67cb1ca279 (svn r10827) -Fix [FS#1112]: out of bounds access in corner case of list allocations of vehicles.
rubidium
parents: 7909
diff changeset
   311
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   312
	/**
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   313
	 * Are we cleaning this pool?
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   314
	 * @return true if we are
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   315
	 */
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   316
	static inline bool CleaningPool()
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   317
	{
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   318
		return Tpool->CleaningPool();
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7898
diff changeset
   319
	}
10323
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   320
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   321
public:
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   322
	/**
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   323
	 * Check whether we can allocate an item in this pool. This to prevent the
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   324
	 * need to actually construct the object and then destructing it again,
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   325
	 * which could be *very* costly.
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   326
	 * @return true if and only if at least ONE item can be allocated.
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   327
	 */
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   328
	static inline bool CanAllocateItem()
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   329
	{
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   330
		uint last_minus_one = Tpool->GetSize() - 1;
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   331
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   332
		for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   333
			if (!t->IsValid()) return true;
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   334
			Tpool->first_free_index = t->index;
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   335
		}
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   336
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   337
		/* Check if we can add a block to the pool */
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   338
		if (Tpool->AddBlockToPool()) return CanAllocateItem();
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   339
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   340
		return false;
57d8fd25473e (svn r12864) [0.6] -Backport from trunk r12859, r12857, r12855, r12845, r12844:
rubidium
parents: 8609
diff changeset
   341
	}
7873
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   342
};
b3bccdcf85ef (svn r10746) -Codechange: add a generic superclass for almost all pool items so we do not have to duplicate code for each of the pool item classes and use it for the station and roadstop classes.
rubidium
parents: 7872
diff changeset
   343
7872
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   344
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
   345
#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
   346
	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
   347
		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
   348
		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
   349
	};
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
   350
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
   351
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
   352
#define OLD_POOL_ACCESSORS(name, type) \
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   353
	static inline type* Get##name(uint index) { return _##name##_pool.Get(index);  } \
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   354
	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
   355
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
   356
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
   357
#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
   358
	OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   359
	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
   360
	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
   361
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
   362
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
   363
#define DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   364
	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
   365
		#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
7871
23f2383ad490 (svn r10744) -Codechange: make the pool a little more OO, so it can be easier in other places.
rubidium
parents: 6677
diff changeset
   366
		new_block_proc, clean_block_proc);
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
   367
7872
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   368
#define DEFINE_OLD_POOL_GENERIC(name, type) \
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   369
	OldMemoryPool<type> _##name##_pool( \
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   370
		#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   371
		PoolNewBlock<type, &_##name##_pool>, PoolCleanBlock<type, &_##name##_pool>);
a0646ade9d4a (svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).
rubidium
parents: 7871
diff changeset
   372
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
   373
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
   374
#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
   375
	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
   376
	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
   377
	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
   378
5224
ddab137de945 (svn r7341) - Codechange: Also rename the POOL_H define to OLDPOOL_H (forgotten in r7331).
matthijs
parents: 5216
diff changeset
   379
#endif /* OLDPOOL_H */