src/oldpool_func.h
author glx
Mon, 26 May 2008 17:40:33 +0000
branchnoai
changeset 10718 7e9d9e40e16f
parent 10455 22c441f5adf9
permissions -rw-r--r--
(svn r13268) [NoAI] -Fix: kill AIs on game abortion
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     1
/* $Id$ */
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     2
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10355
diff changeset
     3
/** @file oldpool_func.h Functions related to the old pool. */
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10355
diff changeset
     4
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     5
#ifndef OLDPOOL_FUNC_H
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10355
diff changeset
     6
#define OLDPOOL_FUNC_H
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     7
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     8
#include "oldpool.h"
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
     9
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    10
/**
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    11
 * Allocate a pool item; possibly allocate a new block in the pool.
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    12
 * @param first the first pool item to start searching
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    13
 * @pre first <= Tpool->GetSize()
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    14
 * @return the allocated pool item (or NULL when the pool is full).
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    15
 */
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    16
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    17
{
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    18
	uint last_minus_one = Tpool->GetSize() - 1;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    19
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    20
	for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    21
		if (!t->IsValid()) {
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    22
			first = t->index;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    23
			Tid index = t->index;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    24
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    25
			memset(t, 0, Tpool->item_size);
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    26
			t->index = index;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    27
			return t;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    28
		}
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    29
	}
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    30
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    31
	/* Check if we can add a block to the pool */
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    32
	if (Tpool->AddBlockToPool()) return AllocateRaw(first);
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    33
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    34
	return NULL;
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    35
}
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    36
10355
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    37
/**
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    38
 * Check whether we can allocate an item in this pool. This to prevent the
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    39
 * need to actually construct the object and then destructing it again,
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    40
 * which could be *very* costly.
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    41
 * @return true if and only if at least ONE item can be allocated.
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    42
 */
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    43
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem()
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    44
{
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    45
	uint last_minus_one = Tpool->GetSize() - 1;
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    46
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    47
	for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    48
		if (!t->IsValid()) return true;
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    49
		Tpool->first_free_index = t->index;
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    50
	}
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    51
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    52
	/* Check if we can add a block to the pool */
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    53
	if (Tpool->AddBlockToPool()) return CanAllocateItem();
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    54
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    55
	return false;
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    56
}
ee4b5f7a5bf2 (svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents: 10142
diff changeset
    57
10142
56ee7da4ad56 (svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
diff changeset
    58
#endif /* OLDPOOL_FUNC_H */