smatz@9343: /* $Id$ */ smatz@9343: rubidium@10429: /** @file oldpool_func.h Functions related to the old pool. */ rubidium@10429: smatz@9343: #ifndef OLDPOOL_FUNC_H rubidium@10429: #define OLDPOOL_FUNC_H smatz@9343: smatz@9343: #include "oldpool.h" smatz@9343: smatz@9343: /** smatz@9343: * Allocate a pool item; possibly allocate a new block in the pool. smatz@9343: * @param first the first pool item to start searching smatz@9343: * @pre first <= Tpool->GetSize() smatz@9343: * @return the allocated pool item (or NULL when the pool is full). smatz@9343: */ smatz@9343: template *Tpool> T *PoolItem::AllocateSafeRaw(uint &first) smatz@9343: { smatz@9343: uint last_minus_one = Tpool->GetSize() - 1; smatz@9343: smatz@9343: for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) { smatz@9343: if (!t->IsValid()) { smatz@9343: first = t->index; smatz@9343: Tid index = t->index; smatz@9343: smatz@9343: memset(t, 0, Tpool->item_size); smatz@9343: t->index = index; smatz@9343: return t; smatz@9343: } smatz@9343: } smatz@9343: smatz@9343: /* Check if we can add a block to the pool */ smatz@9343: if (Tpool->AddBlockToPool()) return AllocateRaw(first); smatz@9343: smatz@9343: return NULL; smatz@9343: } smatz@9343: rubidium@10316: /** rubidium@10316: * Check whether we can allocate an item in this pool. This to prevent the rubidium@10316: * need to actually construct the object and then destructing it again, rubidium@10316: * which could be *very* costly. rubidium@10316: * @return true if and only if at least ONE item can be allocated. rubidium@10316: */ rubidium@10316: template *Tpool> bool PoolItem::CanAllocateItem() rubidium@10316: { rubidium@10316: uint last_minus_one = Tpool->GetSize() - 1; rubidium@10316: rubidium@10316: for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) { rubidium@10316: if (!t->IsValid()) return true; rubidium@10316: Tpool->first_free_index = t->index; rubidium@10316: } rubidium@10316: rubidium@10316: /* Check if we can add a block to the pool */ rubidium@10316: if (Tpool->AddBlockToPool()) return CanAllocateItem(); rubidium@10316: rubidium@10316: return false; rubidium@10316: } rubidium@10316: smatz@9343: #endif /* OLDPOOL_FUNC_H */