summaryrefslogtreecommitdiff
path: root/src/oldpool_func.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-10-30 12:32:32 +0000
committerrubidium <rubidium@openttd.org>2008-10-30 12:32:32 +0000
commitc1ed1866a4d41e113573a6eaf521912244e4642d (patch)
tree6d78e76d16e51557dfcddeb91bcaa577ffe55801 /src/oldpool_func.h
parent5b625363819f0e77368345f2ef8b4b0abdd2d68e (diff)
downloadopenttd-c1ed1866a4d41e113573a6eaf521912244e4642d.tar.xz
(svn r14547) -Fix: order pool seemed to look full when it was not as it only checked whether it was possible to allocate a new block of pool items instead of checking for free pool items.
Diffstat (limited to 'src/oldpool_func.h')
-rw-r--r--src/oldpool_func.h14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/oldpool_func.h b/src/oldpool_func.h
index 8a26adc22..fc08bf9ee 100644
--- a/src/oldpool_func.h
+++ b/src/oldpool_func.h
@@ -38,19 +38,21 @@ template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid,
* 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.
+ * @param count the number of items to create
+ * @return true if and only if at least count items can be allocated.
*/
-template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem()
+template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem(uint count)
{
uint last_minus_one = Tpool->GetSize() - 1;
- for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = ((uint)t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
- if (!t->IsValid()) return true;
- Tpool->first_free_index = t->index;
+ for (T *t = Tpool->Get(Tpool->first_free_index); count > 0 && t != NULL; t = ((uint)t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
+ if (!t->IsValid()) count--;
}
+ if (count == 0) return true;
+
/* Check if we can add a block to the pool */
- if (Tpool->AddBlockToPool()) return CanAllocateItem();
+ if (Tpool->AddBlockToPool()) return CanAllocateItem(count);
return false;
}