summaryrefslogtreecommitdiff
path: root/src/oldpool.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-08-05 21:20:55 +0000
committerrubidium <rubidium@openttd.org>2007-08-05 21:20:55 +0000
commit83e1fdcb0167c36729889511f7b1165038044c88 (patch)
treebfe043c51f23052fc39f35769a2ede1e66998148 /src/oldpool.h
parentab5fa3add2f5f2feeb8f48127f6ee5ab69d42f65 (diff)
downloadopenttd-83e1fdcb0167c36729889511f7b1165038044c88.tar.xz
(svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
Diffstat (limited to 'src/oldpool.h')
-rw-r--r--src/oldpool.h39
1 files changed, 23 insertions, 16 deletions
diff --git a/src/oldpool.h b/src/oldpool.h
index 38a373474..caeabccab 100644
--- a/src/oldpool.h
+++ b/src/oldpool.h
@@ -25,7 +25,7 @@ protected:
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 @@ protected:
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 @@ public:
{
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 @@ static void PoolNewBlock(uint start_item)
/**
* 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 @@ static void PoolCleanBlock(uint start_item, uint end_item)
{
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 @@ struct PoolItem {
}
/**
- * 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'
@@ -241,7 +239,7 @@ 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()
+ static inline T *AllocateRaw()
{
return AllocateRaw(Tpool->first_free_index);
}
@@ -251,7 +249,7 @@ protected:
* @param first the first pool item to start searching
* @return the allocated pool item (or NULL when the pool is full).
*/
- static T *AllocateRaw(uint &first)
+ static inline T *AllocateRaw(uint &first)
{
uint last_minus_one = Tpool->GetSize() - 1;
@@ -271,6 +269,15 @@ protected:
return NULL;
}
+
+ /**
+ * Are we cleaning this pool?
+ * @return true if we are
+ */
+ static inline bool CleaningPool()
+ {
+ return Tpool->CleaningPool();
+ }
};