diff options
author | rubidium <rubidium@openttd.org> | 2008-04-23 22:16:41 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-04-23 22:16:41 +0000 |
commit | dce1b331965c660a80996230503149b28221f34c (patch) | |
tree | 3c648d4bd6e453e588d664c0eabe3c948a556f7c /src/oldpool_func.h | |
parent | 10ab24a5f149955f3af71ba152609a4be676b795 (diff) | |
download | openttd-dce1b331965c660a80996230503149b28221f34c.tar.xz |
(svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
Diffstat (limited to 'src/oldpool_func.h')
-rw-r--r-- | src/oldpool_func.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/oldpool_func.h b/src/oldpool_func.h index e4d1c47d5..2c9801354 100644 --- a/src/oldpool_func.h +++ b/src/oldpool_func.h @@ -31,4 +31,25 @@ template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, return NULL; } +/** + * 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; +} + #endif /* OLDPOOL_FUNC_H */ |