oldpool_func.h

Go to the documentation of this file.
00001 /* $Id: oldpool_func.h 14935 2009-01-09 15:01:15Z rubidium $ */
00002 
00005 #ifndef OLDPOOL_FUNC_H
00006 #define OLDPOOL_FUNC_H
00007 
00008 #include "oldpool.h"
00009 
00017 template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
00018 {
00019   uint last_minus_one = Tpool->GetSize() - 1;
00020 
00021   for (T *t = Tpool->Get(first); t != NULL; t = ((uint)t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
00022     if (!t->IsValid()) {
00023       first = t->index;
00024       Tid index = t->index;
00025 
00026       memset(t, 0, Tpool->item_size);
00027       t->index = index;
00028       return t;
00029     }
00030   }
00031 
00032   /* Check if we can add a block to the pool */
00033   if (Tpool->AddBlockToPool()) return AllocateRaw(first);
00034 
00035   /* One should *ALWAYS* be sure to have enough space before making vehicles! */
00036   NOT_REACHED();
00037 }
00038 
00046 template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem(uint count)
00047 {
00048   uint last_minus_one = Tpool->GetSize() - 1;
00049   uint orig_count = count;
00050 
00051   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) {
00052     if (!t->IsValid()) count--;
00053   }
00054 
00055   if (count == 0) return true;
00056 
00057   /* Check if we can add a block to the pool */
00058   if (Tpool->AddBlockToPool()) return CanAllocateItem(orig_count);
00059 
00060   return false;
00061 }
00062 
00063 #endif /* OLDPOOL_FUNC_H */

Generated on Sun Nov 15 15:40:13 2009 for OpenTTD by  doxygen 1.5.6