2008-04-06 23:49:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file oldpool_func.h Functions related to the old pool. */
|
|
|
|
|
2008-04-06 23:49:45 +00:00
|
|
|
#ifndef OLDPOOL_FUNC_H
|
2008-05-06 15:11:33 +00:00
|
|
|
#define OLDPOOL_FUNC_H
|
2008-04-06 23:49:45 +00:00
|
|
|
|
|
|
|
#include "oldpool.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a pool item; possibly allocate a new block in the pool.
|
|
|
|
* @param first the first pool item to start searching
|
|
|
|
* @pre first <= Tpool->GetSize()
|
|
|
|
* @return the allocated pool item (or NULL when the pool is full).
|
|
|
|
*/
|
|
|
|
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
|
|
|
|
{
|
|
|
|
uint last_minus_one = Tpool->GetSize() - 1;
|
|
|
|
|
|
|
|
for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
|
|
|
|
if (!t->IsValid()) {
|
|
|
|
first = t->index;
|
|
|
|
Tid index = t->index;
|
|
|
|
|
|
|
|
memset(t, 0, Tpool->item_size);
|
|
|
|
t->index = index;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (Tpool->AddBlockToPool()) return AllocateRaw(first);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-23 22:16:41 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-04-06 23:49:45 +00:00
|
|
|
#endif /* OLDPOOL_FUNC_H */
|