src/oldpool_func.h
branchNewGRF_ports
changeset 10724 68a692eacf22
parent 10184 fcf5fb2548eb
--- a/src/oldpool_func.h	Fri Apr 25 02:15:34 2008 +0000
+++ b/src/oldpool_func.h	Mon May 26 20:45:25 2008 +0000
@@ -1,6 +1,9 @@
 /* $Id$ */
 
+/** @file oldpool_func.h Functions related to the old pool. */
+
 #ifndef OLDPOOL_FUNC_H
+#define OLDPOOL_FUNC_H
 
 #include "oldpool.h"
 
@@ -31,4 +34,25 @@
 	return NULL;
 }
 
+/**
+ * Check whether we can allocate an item in this pool. This to prevent the
+ * need to actually construct the object and then destructing it again,
+ * which could be *very* costly.
+ * @return true if and only if at least ONE item can be allocated.
+ */
+template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem()
+{
+	uint last_minus_one = Tpool->GetSize() - 1;
+
+	for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
+		if (!t->IsValid()) return true;
+		Tpool->first_free_index = t->index;
+	}
+
+	/* Check if we can add a block to the pool */
+	if (Tpool->AddBlockToPool()) return CanAllocateItem();
+
+	return false;
+}
+
 #endif /* OLDPOOL_FUNC_H */