Changeset - r7974:47c553d80573
[Not reviewed]
master
0 1 0
smatz - 17 years ago 2007-11-26 22:56:30
smatz@openttd.org
(svn r11530) -Codechange: do not update Tpool->first_free_index for PoolItems allocated on stack
1 file changed with 33 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/oldpool.h
Show inline comments
 
@@ -38,24 +38,34 @@ protected:
 
	OldMemoryPoolCleanBlock *clean_block_proc;
 

	
 
	uint current_blocks;        ///< How many blocks we have in our pool
 
	uint total_items;           ///< How many items we now have in this pool
 

	
 
	bool cleaning_pool;         ///< Are we currently cleaning the pool?
 
public:
 
	const uint item_size;       ///< How many bytes one block is
 
	uint first_free_index;      ///< The index of the first free pool item in this pool
 
	byte **blocks;              ///< An array of blocks (one block hold all the items)
 

	
 
	/**
 
	 * Check if the index of pool item being deleted is lower than cached first_free_index
 
	 * @param index index of pool item
 
	 * @note usage of min() will result in better code on some architectures
 
	 */
 
	inline void UpdateFirstFreeIndex(uint index)
 
	{
 
		first_free_index = min(first_free_index, index);
 
	}
 

	
 
	/**
 
	 * Get the size of this pool, i.e. the total number of items you
 
	 * can put into it at the current moment; the pool might still
 
	 * be able to increase the size of the pool.
 
	 * @return the size of the pool
 
	 */
 
	inline uint GetSize() const
 
	{
 
		return this->total_items;
 
	}
 

	
 
	/**
 
	 * Can this pool allocate more blocks, i.e. is the maximum amount
 
@@ -148,90 +158,112 @@ static void PoolCleanBlock(uint start_it
 
 * Generalization for all pool items that are saved in the savegame.
 
 * It specifies all the mechanics to access the pool easily.
 
 */
 
template <typename T, typename Tid, OldMemoryPool<T> *Tpool>
 
struct PoolItem {
 
	/**
 
	 * The pool-wide index of this object.
 
	 */
 
	Tid index;
 

	
 
	/**
 
	 * We like to have the correct class destructed.
 
	 * @warning It is called even for object allocated on stack,
 
	 *          so it is not present in the TPool!
 
	 *          Then, index is undefined, not associated with TPool in any way.
 
	 * @note    The idea is to free up allocated memory etc.
 
	 */
 
	virtual ~PoolItem()
 
	{
 
		if (this->index < Tpool->first_free_index) Tpool->first_free_index = this->index;
 

	
 
	}
 

	
 
	/**
 
	 * Constructor of given class.
 
	 * @warning It is called even for object allocated on stack,
 
	 *          so it may not be present in TPool!
 
	 *          Then, index is undefined, not associated with TPool in any way.
 
	 * @note    The idea is to initialize variables (except index)
 
	 */
 
	PoolItem()
 
	{
 

	
 
	}
 

	
 
	/**
 
	 * An overriden version of new that allocates memory on the pool.
 
	 * @param size the size of the variable (unused)
 
	 * @return the memory that is 'allocated'
 
	 */
 
	void *operator new(size_t size)
 
	{
 
		return AllocateRaw();
 
	}
 

	
 
	/**
 
	 * 'Free' the memory allocated by the overriden new.
 
	 * @param p the memory to 'free'
 
	 * @note we only update Tpool->first_free_index
 
	 */
 
	void operator delete(void *p)
 
	{
 
		Tpool->UpdateFirstFreeIndex(((T*)p)->index);
 
	}
 

	
 
	/**
 
	 * An overriden version of new, so you can directly allocate a new object with
 
	 * the correct index when one is loading the savegame.
 
	 * @param size  the size of the variable (unused)
 
	 * @param index the index of the object
 
	 * @return the memory that is 'allocated'
 
	 */
 
	void *operator new(size_t size, int index)
 
	{
 
		if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());
 

	
 
		return Tpool->Get(index);
 
	}
 

	
 
	/**
 
	 * 'Free' the memory allocated by the overriden new.
 
	 * @param p     the memory to 'free'
 
	 * @param index the original parameter given to create the memory
 
	 * @note we only update Tpool->first_free_index
 
	 */
 
	void operator delete(void *p, int index)
 
	{
 
		Tpool->UpdateFirstFreeIndex(index);
 
	}
 

	
 
	/**
 
	 * An overriden version of new, so you can use the vehicle instance
 
	 * instead of a newly allocated piece of memory.
 
	 * @param size the size of the variable (unused)
 
	 * @param pn   the already existing object to use as 'storage' backend
 
	 * @return the memory that is 'allocated'
 
	 */
 
	void *operator new(size_t size, T *pn)
 
	{
 
		return pn;
 
	}
 

	
 
	/**
 
	 * 'Free' the memory allocated by the overriden new.
 
	 * @param p  the memory to 'free'
 
	 * @param pn the pointer that was given to 'new' on creation.
 
	 * @note we only update Tpool->first_free_index
 
	 */
 
	void operator delete(void *p, T *pn)
 
	{
 
		Tpool->UpdateFirstFreeIndex(pn->index);
 
	}
 

	
 
private:
 
	/**
 
	 * Allocate a pool item; possibly allocate a new block in the pool.
 
	 * @param first the first pool item to start searching
 
	 * @pre first <= Tpool->GetSize()
 
	 * @return the allocated pool item (or NULL when the pool is full).
 
	 */
 
	static inline T *AllocateSafeRaw(uint &first)
 
	{
 
		uint last_minus_one = Tpool->GetSize() - 1;
0 comments (0 inline, 0 general)