Files
@ r14818:d652bc9ae7bc
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/pool_func.hpp
r14818:d652bc9ae7bc
4.5 KiB
text/x-c++hdr
(svn r19419) -Codechange: Use failed CommandCost object to retrieve message instead of _error_message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | /* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file pool_func.hpp Some methods of Pool are placed here in order to reduce compilation time and binary size. */
#ifndef POOL_FUNC_HPP
#define POOL_FUNC_HPP
#include "alloc_func.hpp"
#include "mem_func.hpp"
#include "pool_type.hpp"
#define DEFINE_POOL_METHOD(type) \
template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache, bool Tzero> \
type Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero>
DEFINE_POOL_METHOD(inline)::Pool(const char *name) :
name(name),
size(0),
first_free(0),
first_unused(0),
items(0),
cleaning(false),
data(NULL),
alloc_cache(NULL)
{ }
DEFINE_POOL_METHOD(inline void)::ResizeFor(size_t index)
{
assert(index >= this->size);
assert(index < Tmax_size);
size_t new_size = min(Tmax_size, Align(index + 1, Tgrowth_step));
this->data = ReallocT(this->data, new_size);
MemSetT(this->data + this->size, 0, new_size - this->size);
this->size = new_size;
}
DEFINE_POOL_METHOD(inline size_t)::FindFirstFree()
{
size_t index = this->first_free;
for (; index < this->first_unused; index++) {
if (this->data[index] == NULL) return index;
}
if (index < this->size) {
return index;
}
assert(index == this->size);
assert(this->first_unused == this->size);
if (index < Tmax_size) {
this->ResizeFor(index);
return index;
}
assert(this->items == Tmax_size);
return NO_FREE_ITEM;
}
DEFINE_POOL_METHOD(inline void *)::AllocateItem(size_t size, size_t index)
{
assert(this->data[index] == NULL);
this->first_unused = max(this->first_unused, index + 1);
this->items++;
Titem *item;
if (Tcache && this->alloc_cache != NULL) {
assert(sizeof(Titem) == size);
item = (Titem *)this->alloc_cache;
this->alloc_cache = this->alloc_cache->next;
if (Tzero) MemSetT(item, 0);
} else if (Tzero) {
item = (Titem *)CallocT<byte>(size);
} else {
item = (Titem *)MallocT<byte>(size);
}
this->data[index] = item;
item->index = (uint)index;
return item;
}
DEFINE_POOL_METHOD(void *)::GetNew(size_t size)
{
size_t index = this->FindFirstFree();
if (index == NO_FREE_ITEM) {
error("%s: no more free items", this->name);
}
this->first_free = index + 1;
return this->AllocateItem(size, index);
}
DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
{
if (index >= Tmax_size) {
usererror("failed loading savegame: %s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size);
}
if (index >= this->size) this->ResizeFor(index);
if (this->data[index] != NULL) {
usererror("failed loading savegame: %s index " PRINTF_SIZE " already in use", this->name, index);
}
return this->AllocateItem(size, index);
}
DEFINE_POOL_METHOD(void)::FreeItem(size_t index)
{
assert(index < this->size);
assert(this->data[index] != NULL);
if (Tcache) {
AllocCache *ac = (AllocCache *)this->data[index];
ac->next = this->alloc_cache;
this->alloc_cache = ac;
} else {
free(this->data[index]);
}
this->data[index] = NULL;
this->first_free = min(this->first_free, index);
this->items--;
if (!this->cleaning) Titem::PostDestructor(index);
}
DEFINE_POOL_METHOD(void)::CleanPool()
{
this->cleaning = true;
for (size_t i = 0; i < this->first_unused; i++) {
delete this->Get(i); // 'delete NULL;' is very valid
}
assert(this->items == 0);
free(this->data);
this->first_unused = this->first_free = this->size = 0;
this->data = NULL;
this->cleaning = false;
if (Tcache) {
while (this->alloc_cache != NULL) {
AllocCache *ac = this->alloc_cache;
this->alloc_cache = ac->next;
free(ac);
}
}
}
#undef DEFINE_POOL_METHOD
/**
* Force instantiation of pool methods so we don't get linker errors.
* Only methods accessed from methods defined in pool.hpp need to be
* forcefully instantiated.
*/
#define INSTANTIATE_POOL_METHODS(name) \
template void * name ## Pool::GetNew(size_t size); \
template void * name ## Pool::GetNew(size_t size, size_t index); \
template void name ## Pool::FreeItem(size_t index); \
template void name ## Pool::CleanPool();
#endif /* POOL_FUNC_HPP */
|