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
 
@@ -44,12 +44,22 @@ protected:
 
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
 
@@ -154,16 +164,32 @@ 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'
 
@@ -173,15 +199,17 @@ struct PoolItem {
 
		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)
 
@@ -196,15 +224,17 @@ struct PoolItem {
 
	}
 

	
 
	/**
 
	 * '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)
 
@@ -217,15 +247,17 @@ struct PoolItem {
 
	}
 

	
 
	/**
 
	 * '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
0 comments (0 inline, 0 general)