src/oldpool_func.h
changeset 8847 426dd2d582e7
child 9038 38f0f10f8cca
equal deleted inserted replaced
8846:1b90782b2c4e 8847:426dd2d582e7
       
     1 /* $Id$ */
       
     2 
       
     3 #ifndef OLDPOOL_FUNC_H
       
     4 
       
     5 #include "oldpool.h"
       
     6 
       
     7 /**
       
     8  * Allocate a pool item; possibly allocate a new block in the pool.
       
     9  * @param first the first pool item to start searching
       
    10  * @pre first <= Tpool->GetSize()
       
    11  * @return the allocated pool item (or NULL when the pool is full).
       
    12  */
       
    13 template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
       
    14 {
       
    15 	uint last_minus_one = Tpool->GetSize() - 1;
       
    16 
       
    17 	for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
       
    18 		if (!t->IsValid()) {
       
    19 			first = t->index;
       
    20 			Tid index = t->index;
       
    21 
       
    22 			memset(t, 0, Tpool->item_size);
       
    23 			t->index = index;
       
    24 			return t;
       
    25 		}
       
    26 	}
       
    27 
       
    28 	/* Check if we can add a block to the pool */
       
    29 	if (Tpool->AddBlockToPool()) return AllocateRaw(first);
       
    30 
       
    31 	return NULL;
       
    32 }
       
    33 
       
    34 #endif /* OLDPOOL_FUNC_H */