Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
pool.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * pool allocator (fixed-size blocks, freelist).
25 */
26
27#ifndef INCLUDED_ALLOCATORS_POOL
28#define INCLUDED_ALLOCATORS_POOL
29
32#include "lib/bits.h" // ROUND_UP
33#include "lib/code_annotation.h"
34#include "lib/debug.h"
35#include "lib/status.h"
36
37#include <cstddef>
38#include <cstdint>
39
40namespace Allocators {
41
42/**
43 * allocator design parameters:
44 * - O(1) allocation and deallocation;
45 * - fixed-size objects;
46 * - support for deallocating all objects;
47 * - consecutive allocations are back-to-back;
48 * - objects are aligned to the pointer size.
49 **/
50template<typename T, class Storage = Storage_Fixed<> >
51class Pool
52{
54public:
55 // (must round up because freelist stores pointers inside objects)
56 static const size_t objectSize = ROUND_UP(sizeof(T), sizeof(intptr_t));
57
58 Pool(size_t maxObjects)
59 : storage(maxObjects*objectSize)
60 {
62 }
63
65 {
66 return (storage.MaxCapacity() - end) / objectSize;
67 }
68
70 {
72 if(p)
73 {
74 ASSERT(Contains((uintptr_t)p));
75 return (T*)p;
76 }
77
79 }
80
81 void Deallocate(T* p)
82 {
83 ASSERT(Contains((uintptr_t)p));
85 }
86
88 {
90 end = 0;
91 }
92
93 // @return whether the address lies within the previously allocated range.
94 bool Contains(uintptr_t address) const
95 {
96 return (address - storage.Address()) < end;
97 }
98
99private:
101 size_t end;
102 void* freelist;
103};
104
105void TestPool();
106
107} // namespace Allocators
108
109
111
112/**
113 * allocator design parameters:
114 * - O(1) alloc and free;
115 * - either fixed- or variable-sized blocks;
116 * - doesn't preallocate the entire pool;
117 * - returns sequential addresses.
118 *
119 * opaque! do not read/write any fields!
120 **/
121struct Pool
122{
124
125 /**
126 * size of elements. = 0 if pool set up for variable-sized
127 * elements, otherwise rounded up to pool alignment.
128 **/
129 size_t el_size;
130
131 /**
132 * pointer to freelist (opaque); see freelist_*.
133 * never used (remains 0) if elements are of variable size.
134 **/
135 void* freelist;
136};
137
138/**
139 * pass as pool_create's <el_size> param to indicate variable-sized allocs
140 * are required (see below).
141 **/
142const size_t POOL_VARIABLE_ALLOCS = ~(size_t)0u;
143
144/**
145 * Ready Pool for use.
146 *
147 * @param p Pool*
148 * @param max_size Max size [bytes] of the Pool; this much
149 * (rounded up to next page multiple) virtual address space is reserved.
150 * no virtual memory is actually committed until calls to pool_alloc.
151 * @param el_size Number of bytes that will be returned by each
152 * pool_alloc (whose size parameter is then ignored). Can be 0 to
153 * allow variable-sized allocations, but pool_free is then unusable.
154 * @return Status
155 **/
156Status pool_create(Pool* p, size_t max_size, size_t el_size);
157
158/**
159 * free all memory (address space + physical) that constitutes the
160 * given Pool.
161 *
162 * future alloc and free calls on this pool will fail.
163 * continued use of the allocated memory (*) is
164 * impossible because it is marked not-present via MMU.
165 * (* no matter if in freelist or unused or "allocated" to user)
166 *
167 * @param p Pool*
168 * @return Status.
169 **/
171
172/**
173 * indicate whether a pointer was allocated from the given pool.
174 *
175 * this is useful for callers that use several types of allocators.
176 *
177 * @param p Pool*
178 * @param el
179 * @return bool.
180 **/
181bool pool_contains(const Pool* p, void* el);
182
183/**
184 * Dole out memory from the pool.
185 * exhausts the freelist before returning new entries to improve locality.
186 *
187 * @param p Pool*
188 * @param size bytes to allocate; ignored if pool_create's el_size was not 0.
189 * @return allocated memory, or 0 if the Pool would have to be expanded and
190 * there isn't enough memory to do so.
191 **/
192void* pool_alloc(Pool* p, size_t size);
193
194/**
195 * Make a fixed-size element available for reuse in the given Pool.
196 *
197 * this is not allowed if the Pool was created for variable-size elements.
198 * rationale: avoids having to pass el_size here and compare with size when
199 * allocating; also prevents fragmentation and leaking memory.
200 *
201 * @param p Pool*
202 * @param el Element returned by pool_alloc.
203 **/
204void pool_free(Pool* p, void* el);
205
206/**
207 * "free" all user allocations that ensued from the given Pool.
208 *
209 * this resets it as if freshly pool_create-d, but doesn't release the
210 * underlying reserved virtual memory.
211 *
212 * @param p Pool*
213 **/
214void pool_free_all(Pool* p);
215
216/**
217 * Return the number of bytes committed in the pool's backing array.
218 *
219 * This is roughly the number of bytes allocated in this pool plus the
220 * unused freelist entries.
221 *
222 * @param p Pool*
223 * @return number of bytes
224 **/
225size_t pool_committed(Pool* p);
226
227#endif // #ifndef INCLUDED_ALLOCATORS_POOL
#define ROUND_UP(n, multiple)
Definition: bits.h:143
allocator design parameters:
Definition: pool.h:52
Pool(size_t maxObjects)
Definition: pool.h:58
size_t end
Definition: pool.h:101
void Deallocate(T *p)
Definition: pool.h:81
void * freelist
Definition: pool.h:102
bool Contains(uintptr_t address) const
Definition: pool.h:94
void DeallocateAll()
Definition: pool.h:87
size_t RemainingObjects()
Definition: pool.h:64
T * Allocate()
Definition: pool.h:69
static const size_t objectSize
Definition: pool.h:56
Storage storage
Definition: pool.h:100
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:250
void * mem_freelist_Sentinel()
Definition: freelist.cpp:26
static void * mem_freelist_Detach(void *&freelist)
Definition: freelist.h:57
static void mem_freelist_AddToFront(void *&freelist, void *el)
Definition: freelist.h:46
Definition: allocator_policies.h:42
static uintptr_t StorageAppend(Storage &storage, size_t &end, size_t size)
Definition: allocator_policies.h:326
void TestPool()
Definition: pool.cpp:76
void pool_free(Pool *p, void *el)
Make a fixed-size element available for reuse in the given Pool.
Definition: pool.cpp:142
bool pool_contains(const Pool *p, void *el)
indicate whether a pointer was allocated from the given pool.
Definition: pool.cpp:105
void * pool_alloc(Pool *p, size_t size)
Dole out memory from the pool.
Definition: pool.cpp:117
Status pool_create(Pool *p, size_t max_size, size_t el_size)
Ready Pool for use.
Definition: pool.cpp:83
void pool_free_all(Pool *p)
"free" all user allocations that ensued from the given Pool.
Definition: pool.cpp:160
Status pool_destroy(Pool *p)
free all memory (address space + physical) that constitutes the given Pool.
Definition: pool.cpp:95
size_t pool_committed(Pool *p)
Return the number of bytes committed in the pool's backing array.
Definition: pool.cpp:170
const size_t POOL_VARIABLE_ALLOCS
pass as pool_create's <el_size> param to indicate variable-sized allocs are required (see below).
Definition: pool.h:142
#define T(string_literal)
Definition: secure_crt.cpp:81
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: allocator_policies.h:94
size_t MaxCapacity() const
uintptr_t Address() const
provides a memory range that can be expanded but doesn't waste physical memory or relocate itself.
Definition: dynarray.h:42
allocator design parameters:
Definition: pool.h:122
size_t el_size
size of elements.
Definition: pool.h:129
void * freelist
pointer to freelist (opaque); see freelist_*.
Definition: pool.h:135
DynArray da
Definition: pool.h:123