src/oldpool_func.h
author rubidium
Fri, 25 Apr 2008 15:22:32 +0000
changeset 9055 4dc6a0c0ef47
parent 9038 38f0f10f8cca
child 9111 48ce04029fe4
permissions -rw-r--r--
(svn r12897) -Codechange: some coding style in station_cmd.cpp.
8847
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     1
/* $Id$ */
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     2
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     3
#ifndef OLDPOOL_FUNC_H
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     4
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     5
#include "oldpool.h"
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     6
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     7
/**
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     8
 * Allocate a pool item; possibly allocate a new block in the pool.
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
     9
 * @param first the first pool item to start searching
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    10
 * @pre first <= Tpool->GetSize()
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    11
 * @return the allocated pool item (or NULL when the pool is full).
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    12
 */
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    13
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> T *PoolItem<T, Tid, Tpool>::AllocateSafeRaw(uint &first)
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    14
{
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    15
	uint last_minus_one = Tpool->GetSize() - 1;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    16
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    17
	for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    18
		if (!t->IsValid()) {
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    19
			first = t->index;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    20
			Tid index = t->index;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    21
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    22
			memset(t, 0, Tpool->item_size);
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    23
			t->index = index;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    24
			return t;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    25
		}
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    26
	}
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    27
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    28
	/* Check if we can add a block to the pool */
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    29
	if (Tpool->AddBlockToPool()) return AllocateRaw(first);
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    30
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    31
	return NULL;
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    32
}
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    33
9038
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    34
/**
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    35
 * Check whether we can allocate an item in this pool. This to prevent the
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    36
 * need to actually construct the object and then destructing it again,
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    37
 * which could be *very* costly.
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    38
 * @return true if and only if at least ONE item can be allocated.
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    39
 */
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    40
template<typename T, typename Tid, OldMemoryPool<T> *Tpool> bool PoolItem<T, Tid, Tpool>::CanAllocateItem()
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    41
{
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    42
	uint last_minus_one = Tpool->GetSize() - 1;
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    43
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    44
	for (T *t = Tpool->Get(Tpool->first_free_index); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    45
		if (!t->IsValid()) return true;
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    46
		Tpool->first_free_index = t->index;
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    47
	}
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    48
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    49
	/* Check if we can add a block to the pool */
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    50
	if (Tpool->AddBlockToPool()) return CanAllocateItem();
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    51
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    52
	return false;
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    53
}
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 8847
diff changeset
    54
8847
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents:
diff changeset
    55
#endif /* OLDPOOL_FUNC_H */