src/oldpool.h
branchnoai
changeset 9701 d1ac22c62f64
parent 9694 e72987579514
child 9723 eee46cb39750
--- a/src/oldpool.h	Sun Aug 19 14:04:13 2007 +0000
+++ b/src/oldpool.h	Sun Sep 02 11:17:33 2007 +0000
@@ -25,7 +25,7 @@
 				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
 		name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
 		new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
-		total_items(0), item_size(item_size), first_free_index(0), blocks(NULL) {}
+		total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
 
 	const char* name;     ///< Name of the pool (just for debugging)
 
@@ -40,6 +40,7 @@
 	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
@@ -84,6 +85,15 @@
 	{
 		return this->name;
 	}
+
+	/**
+	 * Is the pool in the cleaning phase?
+	 * @return true if it is
+	 */
+	inline bool CleaningPool() const
+	{
+		return this->cleaning_pool;
+	}
 };
 
 template <typename T>
@@ -121,7 +131,6 @@
 
 /**
  * Generic function to free a new block in a pool.
- * This function uses QuickFree that is intended to only free memory that would be lost if the pool is freed.
  * @param start_item the first item that needs to be cleaned
  * @param end_item   the last item that needs to be cleaned
  */
@@ -130,9 +139,7 @@
 {
 	for (uint i = start_item; i <= end_item; i++) {
 		T *t = Tpool->Get(i);
-		if (t->IsValid()) {
-			t->QuickFree();
-		}
+		delete t;
 	}
 }
 
@@ -157,15 +164,6 @@
 	}
 
 	/**
-	 * Called on each object when the pool is being destroyed, so one
-	 * can free allocated memory without the need for freeing for
-	 * example orders.
-	 */
-	virtual void QuickFree()
-	{
-	}
-
-	/**
 	 * 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'
@@ -227,31 +225,14 @@
 	{
 	}
 
-	/**
-	 * Is this a valid object or not?
-	 * @return true if and only if it is valid
-	 */
-	virtual bool IsValid() const
-	{
-		return false;
-	}
-
-protected:
-	/**
-	 * Allocate a pool item; possibly allocate a new block in the pool.
-	 * @return the allocated pool item (or NULL when the pool is full).
-	 */
-	static T *AllocateRaw()
-	{
-		return AllocateRaw(Tpool->first_free_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 T *AllocateRaw(uint &first)
+	static inline T *AllocateSafeRaw(uint &first)
 	{
 		uint last_minus_one = Tpool->GetSize() - 1;
 
@@ -271,6 +252,37 @@
 
 		return NULL;
 	}
+
+protected:
+	/**
+	 * Allocate a pool item; possibly allocate a new block in the pool.
+	 * @return the allocated pool item (or NULL when the pool is full).
+	 */
+	static inline T *AllocateRaw()
+	{
+		return AllocateSafeRaw(Tpool->first_free_index);
+	}
+
+	/**
+	 * Allocate a pool item; possibly allocate a new block in the pool.
+	 * @param first the first pool item to start searching
+	 * @return the allocated pool item (or NULL when the pool is full).
+	 */
+	static inline T *AllocateRaw(uint &first)
+	{
+		if (first >= Tpool->GetSize() && !Tpool->AddBlockToPool()) return NULL;
+
+		return AllocateSafeRaw(first);
+	}
+
+	/**
+	 * Are we cleaning this pool?
+	 * @return true if we are
+	 */
+	static inline bool CleaningPool()
+	{
+		return Tpool->CleaningPool();
+	}
 };