1
/* $Id: oldpool_func.h 14935 2009-01-09 15:01:15Z rubidium $ */
3
/** @file oldpool_func.h Functions related to the old pool. */
11
* Allocate a pool item; possibly allocate a new block in the pool.
12
* @param first the first pool item to start searching
13
* @pre first <= Tpool->GetSize()
14
* @pre CanAllocateItem()
15
* @return the allocated pool item
17
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
19
uint last_minus_one = Tpool->GetSize() - 1;
21
for (T *t = Tpool->Get(first); t != NULL; t = ((uint)t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
26
memset(t, 0, Tpool->item_size);
32
/* Check if we can add a block to the pool */
33
if (Tpool->AddBlockToPool()) return AllocateRaw(first);
35
/* One should *ALWAYS* be sure to have enough space before making vehicles! */
40
* Check whether we can allocate an item in this pool. This to prevent the
41
* need to actually construct the object and then destructing it again,
42
* which could be *very* costly.
43
* @param count the number of items to create
44
* @return true if and only if at least count items can be allocated.
46
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem(uint count)
48
uint last_minus_one = Tpool->GetSize() - 1;
49
uint orig_count = count;
51
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) {
52
if (!t->IsValid()) count--;
55
if (count == 0) return true;
57
/* Check if we can add a block to the pool */
58
if (Tpool->AddBlockToPool()) return CanAllocateItem(orig_count);
63
#endif /* OLDPOOL_FUNC_H */