21 #define DEFINE_POOL_METHOD(type) \
22 template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type, bool Tcache, bool Tzero> \
23 type Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
52 assert(index >= this->size);
53 assert(index < Tmax_size);
55 size_t new_size = std::min(Tmax_size,
Align(index + 1, Tgrowth_step));
57 this->data =
ReallocT(this->data, new_size);
58 MemSetT(this->data + this->size, 0, new_size - this->size);
60 this->size = new_size;
69 size_t index = this->first_free;
71 for (; index < this->first_unused; index++) {
72 if (this->data[index] ==
nullptr)
return index;
75 if (index < this->size) {
79 assert(index == this->size);
80 assert(this->first_unused == this->size);
82 if (index < Tmax_size) {
83 this->ResizeFor(index);
87 assert(this->items == Tmax_size);
101 assert(this->data[index] ==
nullptr);
103 this->first_unused = std::max(this->first_unused, index + 1);
107 if (Tcache && this->alloc_cache !=
nullptr) {
108 assert(
sizeof(Titem) == size);
109 item = (Titem *)this->alloc_cache;
110 this->alloc_cache = this->alloc_cache->next;
114 memset((
void *)item, 0,
sizeof(Titem));
117 item = (Titem *)CallocT<byte>(size);
119 item = (Titem *)MallocT<byte>(size);
121 this->data[index] = item;
122 item->index = (Tindex)(uint)index;
134 size_t index = this->FindFirstFree();
137 assert(this->checked != 0);
140 if (index == NO_FREE_ITEM) {
141 error(
"%s: no more free items", this->name);
144 this->first_free = index + 1;
145 return this->AllocateItem(size, index);
159 if (index >= Tmax_size) {
160 SlErrorCorruptFmt(
"%s index " PRINTF_SIZE
" out of range (" PRINTF_SIZE
")", this->name, index, Tmax_size);
163 if (index >= this->size) this->ResizeFor(index);
165 if (this->data[index] !=
nullptr) {
169 return this->AllocateItem(size, index);
180 assert(index < this->size);
181 assert(this->data[index] !=
nullptr);
183 AllocCache *ac = (AllocCache *)this->data[index];
184 ac->next = this->alloc_cache;
185 this->alloc_cache = ac;
187 free(this->data[index]);
189 this->data[index] =
nullptr;
190 this->first_free = std::min(this->first_free, index);
192 if (!this->cleaning) Titem::PostDestructor(index);
198 this->cleaning =
true;
199 for (
size_t i = 0; i < this->first_unused; i++) {
202 assert(this->items == 0);
204 this->first_unused = this->first_free = this->size = 0;
205 this->data =
nullptr;
206 this->cleaning =
false;
209 while (this->alloc_cache !=
nullptr) {
210 AllocCache *ac = this->alloc_cache;
211 this->alloc_cache = ac->next;
217 #undef DEFINE_POOL_METHOD
224 #define INSTANTIATE_POOL_METHODS(name) \
225 template void * name ## Pool::GetNew(size_t size); \
226 template void * name ## Pool::GetNew(size_t size, size_t index); \
227 template void name ## Pool::FreeItem(size_t index); \
228 template void name ## Pool::CleanPool();