src/oldpool_func.h
author rubidium
Fri, 18 Jul 2008 12:20:31 +0000
changeset 11171 04fc8e49df1b
parent 10429 1b99254f9607
permissions -rw-r--r--
(svn r13729) -Fix: assumption that non-north tiles of a house do not have the 1x1 building bit set was flawed with some NewGRFs. This caused the amount of houses to differ, which causes the town radii to differ, which causes desyncs when towns are expanded.
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     1
/* $Id$ */
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     2
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 10316
diff changeset
     3
/** @file oldpool_func.h Functions related to the old pool. */
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 10316
diff changeset
     4
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     5
#ifndef OLDPOOL_FUNC_H
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 10316
diff changeset
     6
#define OLDPOOL_FUNC_H
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     7
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     8
#include "oldpool.h"
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     9
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    10
/**
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    11
 * Allocate a pool item; possibly allocate a new block in the pool.
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    12
 * @param first the first pool item to start searching
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    13
 * @pre first <= Tpool->GetSize()
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    14
 * @return the allocated pool item (or NULL when the pool is full).
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    15
 */
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    16
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    17
{
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    18
	uint last_minus_one = Tpool->GetSize() - 1;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    19
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    20
	for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    21
		if (!t->IsValid()) {
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    22
			first = t->index;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    23
			Tid index = t->index;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    24
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    25
			memset(t, 0, Tpool->item_size);
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    26
			t->index = index;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    27
			return t;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    28
		}
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    29
	}
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    30
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    31
	/* Check if we can add a block to the pool */
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    32
	if (Tpool->AddBlockToPool()) return AllocateRaw(first);
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    33
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    34
	return NULL;
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    35
}
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    36
10316
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    37
/**
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    38
 * Check whether we can allocate an item in this pool. This to prevent the
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    39
 * need to actually construct the object and then destructing it again,
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    40
 * which could be *very* costly.
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    41
 * @return true if and only if at least ONE item can be allocated.
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    42
 */
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    43
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem()
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    44
{
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    45
	uint last_minus_one = Tpool->GetSize() - 1;
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    46
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    47
	for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    48
		if (!t->IsValid()) return true;
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    49
		Tpool->first_free_index = t->index;
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    50
	}
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    51
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    52
	/* Check if we can add a block to the pool */
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    53
	if (Tpool->AddBlockToPool()) return CanAllocateItem();
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    54
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    55
	return false;
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    56
}
6878402fa6e5 (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9343
diff changeset
    57
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    58
#endif /* OLDPOOL_FUNC_H */