Files
@ r28800:381a234fb097
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/pool_func.hpp
r28800:381a234fb097
7.0 KiB
text/x-c++hdr
Update: Translations from eints
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | /*
* 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 "bitmath_func.hpp"
#include "mem_func.hpp"
#include "pool_type.hpp"
#include "../error_func.h"
#include "../saveload/saveload_error.hpp" // SlErrorCorruptFmt
/**
* Helper for defining the method's signature.
* @param type The return type of the method.
*/
#define DEFINE_POOL_METHOD(type) \
template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type, bool Tcache, bool Tzero> \
type Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
/**
* Create a clean pool.
* @param name The name for the pool.
*/
DEFINE_POOL_METHOD(inline)::Pool(const char *name) :
PoolBase(Tpool_type),
name(name),
size(0),
first_free(0),
first_unused(0),
items(0),
#ifdef WITH_ASSERT
checked(0),
#endif /* WITH_ASSERT */
cleaning(false),
data(nullptr),
alloc_cache(nullptr)
{ }
/**
* Resizes the pool so 'index' can be addressed
* @param index index we will allocate later
* @pre index >= this->size
* @pre index < Tmax_size
*/
DEFINE_POOL_METHOD(inline void)::ResizeFor(size_t index)
{
assert(index >= this->size);
assert(index < Tmax_size);
size_t new_size = std::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->used_bitmap.resize(Align(new_size, BITMAP_SIZE) / BITMAP_SIZE);
if (this->size % BITMAP_SIZE != 0) {
/* Already-allocated bits above old size are now unused. */
this->used_bitmap[this->size / BITMAP_SIZE] &= ~((~static_cast<BitmapStorage>(0)) << (this->size % BITMAP_SIZE));
}
if (new_size % BITMAP_SIZE != 0) {
/* Bits above new size are considered used. */
this->used_bitmap[new_size / BITMAP_SIZE] |= (~static_cast<BitmapStorage>(0)) << (new_size % BITMAP_SIZE);
}
this->size = new_size;
}
/**
* Searches for first free index
* @return first free index, NO_FREE_ITEM on failure
*/
DEFINE_POOL_METHOD(inline size_t)::FindFirstFree()
{
for (auto it = std::next(std::begin(this->used_bitmap), this->first_free / BITMAP_SIZE); it != std::end(this->used_bitmap); ++it) {
BitmapStorage available = ~(*it);
if (available == 0) continue;
return std::distance(std::begin(this->used_bitmap), it) * BITMAP_SIZE + FindFirstBit(available);
}
assert(this->first_unused == this->size);
if (this->first_unused < Tmax_size) {
this->ResizeFor(this->first_unused);
return this->first_unused;
}
assert(this->first_unused == Tmax_size);
return NO_FREE_ITEM;
}
/**
* Makes given index valid
* @param size size of item
* @param index index of item
* @pre index < this->size
* @pre this->Get(index) == nullptr
*/
DEFINE_POOL_METHOD(inline void *)::AllocateItem(size_t size, size_t index)
{
assert(this->data[index] == nullptr);
this->first_unused = std::max(this->first_unused, index + 1);
this->items++;
Titem *item;
if (Tcache && this->alloc_cache != nullptr) {
assert(sizeof(Titem) == size);
item = (Titem *)this->alloc_cache;
this->alloc_cache = this->alloc_cache->next;
if (Tzero) {
/* Explicitly casting to (void *) prevents a clang warning -
* we are actually memsetting a (not-yet-constructed) object */
memset((void *)item, 0, sizeof(Titem));
}
} else if (Tzero) {
item = (Titem *)CallocT<byte>(size);
} else {
item = (Titem *)MallocT<byte>(size);
}
this->data[index] = item;
SetBit(this->used_bitmap[index / BITMAP_SIZE], index % BITMAP_SIZE);
item->index = (Tindex)(uint)index;
return item;
}
/**
* Allocates new item
* @param size size of item
* @return pointer to allocated item
* @note FatalError() on failure! (no free item)
*/
DEFINE_POOL_METHOD(void *)::GetNew(size_t size)
{
size_t index = this->FindFirstFree();
#ifdef WITH_ASSERT
assert(this->checked != 0);
this->checked--;
#endif /* WITH_ASSERT */
if (index == NO_FREE_ITEM) {
FatalError("{}: no more free items", this->name);
}
this->first_free = index + 1;
return this->AllocateItem(size, index);
}
/**
* Allocates new item with given index
* @param size size of item
* @param index index of item
* @return pointer to allocated item
* @note SlErrorCorruptFmt() on failure! (index out of range or already used)
*/
DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
{
if (index >= Tmax_size) {
SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, Tmax_size);
}
if (index >= this->size) this->ResizeFor(index);
if (this->data[index] != nullptr) {
SlErrorCorruptFmt("{} index {} already in use", this->name, index);
}
return this->AllocateItem(size, index);
}
/**
* Deallocates memory used by this index and marks item as free
* @param index item to deallocate
* @pre unit is allocated (non-nullptr)
* @note 'delete nullptr' doesn't cause call of this function, so it is safe
*/
DEFINE_POOL_METHOD(void)::FreeItem(size_t index)
{
assert(index < this->size);
assert(this->data[index] != nullptr);
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] = nullptr;
this->first_free = std::min(this->first_free, index);
this->items--;
if (!this->cleaning) {
ClrBit(this->used_bitmap[index / BITMAP_SIZE], index % BITMAP_SIZE);
Titem::PostDestructor(index);
}
}
/** Destroys all items in the pool and resets all member variables. */
DEFINE_POOL_METHOD(void)::CleanPool()
{
this->cleaning = true;
for (size_t i = 0; i < this->first_unused; i++) {
delete this->Get(i); // 'delete nullptr;' is very valid
}
assert(this->items == 0);
free(this->data);
this->used_bitmap.clear();
this->used_bitmap.shrink_to_fit();
this->first_unused = this->first_free = this->size = 0;
this->data = nullptr;
this->cleaning = false;
if (Tcache) {
while (this->alloc_cache != nullptr) {
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 */
|