changeset 10304:575039101d7c draft

(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.
author rubidium <rubidium@openttd.org>
date Thu, 30 Oct 2008 12:32:32 +0000
parents ddb616caa8c3
children ea4558309266
files src/oldpool.h src/oldpool_func.h src/order_cmd.cpp
diffstat 3 files changed, 12 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/oldpool.h
+++ b/src/oldpool.h
@@ -311,7 +311,7 @@
 	}
 
 public:
-	static bool CanAllocateItem();
+	static bool CanAllocateItem(uint count = 1);
 };
 
 
--- a/src/oldpool_func.h
+++ b/src/oldpool_func.h
@@ -38,19 +38,21 @@
  * 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;
 }
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -121,18 +121,6 @@
 			this->dest  == other.dest;
 }
 
-static bool HasOrderPoolFree(uint amount)
-{
-	const Order *order;
-
-	/* There is always room if not all blocks in the pool are reserved */
-	if (_Order_pool.CanAllocateMoreBlocks()) return true;
-
-	FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
-
-	return false;
-}
-
 uint32 Order::Pack() const
 {
 	return this->dest << 16 | this->flags << 8 | this->type;
@@ -493,7 +481,7 @@
 
 	if (sel_ord > v->num_orders) return CMD_ERROR;
 
-	if (!HasOrderPoolFree(1)) return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
+	if (!Order::CanAllocateItem()) return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
 
 	if (v->type == VEH_SHIP && IsHumanCompany(v->owner) && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
 		/* Make sure the new destination is not too far away from the previous */
@@ -1147,8 +1135,9 @@
 
 			/* make sure there are orders available */
 			delta = dst->IsOrderListShared() ? src->num_orders + 1 : src->num_orders - dst->num_orders;
-			if (!HasOrderPoolFree(delta))
+			if (!Order::CanAllocateItem(delta)) {
 				return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
+			}
 
 			if (flags & DC_EXEC) {
 				const Order *order;