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