~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/oldpool_func.h

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: oldpool_func.h 14935 2009-01-09 15:01:15Z rubidium $ */
 
2
 
 
3
/** @file oldpool_func.h Functions related to the old pool. */
 
4
 
 
5
#ifndef OLDPOOL_FUNC_H
 
6
#define OLDPOOL_FUNC_H
 
7
 
 
8
#include "oldpool.h"
 
9
 
 
10
/**
 
11
 * Allocate a pool item; possibly allocate a new block in the pool.
 
12
 * @param first the first pool item to start searching
 
13
 * @pre first <= Tpool->GetSize()
 
14
 * @pre CanAllocateItem()
 
15
 * @return the allocated pool item
 
16
 */
 
17
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
 
18
{
 
19
        uint last_minus_one = Tpool->GetSize() - 1;
 
20
 
 
21
        for (T *t = Tpool->Get(first); t != NULL; t = ((uint)t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
 
22
                if (!t->IsValid()) {
 
23
                        first = t->index;
 
24
                        Tid index = t->index;
 
25
 
 
26
                        memset(t, 0, Tpool->item_size);
 
27
                        t->index = index;
 
28
                        return t;
 
29
                }
 
30
        }
 
31
 
 
32
        /* Check if we can add a block to the pool */
 
33
        if (Tpool->AddBlockToPool()) return AllocateRaw(first);
 
34
 
 
35
        /* One should *ALWAYS* be sure to have enough space before making vehicles! */
 
36
        NOT_REACHED();
 
37
}
 
38
 
 
39
/**
 
40
 * Check whether we can allocate an item in this pool. This to prevent the
 
41
 * need to actually construct the object and then destructing it again,
 
42
 * which could be *very* costly.
 
43
 * @param count the number of items to create
 
44
 * @return true if and only if at least count items can be allocated.
 
45
 */
 
46
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem(uint count)
 
47
{
 
48
        uint last_minus_one = Tpool->GetSize() - 1;
 
49
        uint orig_count = count;
 
50
 
 
51
        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) {
 
52
                if (!t->IsValid()) count--;
 
53
        }
 
54
 
 
55
        if (count == 0) return true;
 
56
 
 
57
        /* Check if we can add a block to the pool */
 
58
        if (Tpool->AddBlockToPool()) return CanAllocateItem(orig_count);
 
59
 
 
60
        return false;
 
61
}
 
62
 
 
63
#endif /* OLDPOOL_FUNC_H */