Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
LinearAllocator.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_PS_LINEARALLOCATOR
19#define INCLUDED_PS_LINEARALLOCATOR
20
21#include "lib/bits.h"
23
24#include <bit>
25#include <cstddef>
26#include <fmt/format.h>
27#include <memory>
28#include <version>
29
30namespace PS::Memory
31{
32
33/**
34 * Linear allocator (also known as an arena allocator or bump allocator) is
35 * a simple and fast system that allocates memory sequentially from a single
36 * growing memory buffer. Each allocation only moves a free pointer forward.
37 * Releasing all allocations happens at the Release call. The allocated memory
38 * is not initialized.
39 *
40 * It's not thread-safe.
41 *
42 * It's intendent to use for allocations with a short lifespan (f.e. function
43 * scoped).
44 *
45 * There is std::pmr::monotonic_buffer_resource but it doesn't provide
46 * information about the current capacity and doesn't allow to limit the
47 * capacity. Also avoiding virtual calls for frequent allocations.
48 */
50{
51public:
52 LinearAllocator(const std::size_t initialSize, const std::size_t maxCapacity)
53 : m_Capacity{initialSize}, m_MaxCapacity{maxCapacity},
54#if defined(__cpp_lib_smart_ptr_for_overwrite)
55 m_Buffer{std::make_unique_for_overwrite<std::byte[]>(initialSize)}
56#else
57 m_Buffer{std::make_unique<std::byte[]>(initialSize)}
58#endif
59 {
60 ENSURE(initialSize > 0);
61 }
62
64 {
65 Release();
66 }
67
68 void* allocate(std::size_t n, std::size_t alignment)
69 {
70 m_Size = ROUND_UP(m_Size, alignment);
71 if (m_Size + n > m_Capacity)
72 {
74 m_Size = 0;
75 m_Capacity = std::min(std::max(std::bit_ceil(n), m_Capacity * 2), m_MaxCapacity);
76 if (n > m_Capacity)
77 {
78 throw CapacityExceededException{fmt::format(
79 "Tried to allocate from LinearAllocator with a size of {} but the capacity is only {}",
80 n, m_Capacity)};
81 }
82#if defined(__cpp_lib_smart_ptr_for_overwrite)
83 m_Buffer = std::make_unique_for_overwrite<std::byte[]>(m_Capacity);
84#else
85 m_Buffer = std::make_unique<std::byte[]>(m_Capacity);
86#endif
87 }
88 void* ptr{m_Buffer.get() + m_Size};
89 m_Size += n;
91 return ptr;
92 }
93
94 void deallocate([[maybe_unused]] void* ptr, [[maybe_unused]] std::size_t n)
95 {
96 // Do nothing. All allocations will be removed in Release.
99 }
100
101 std::size_t GetSize() const { return m_Size; }
102
103 std::size_t GetCapacity() const { return m_Capacity; }
104
105 void Release()
106 {
108 m_Size = 0u;
110 }
111
112private:
113 std::size_t m_Size{0u}, m_Capacity, m_MaxCapacity;
114 std::unique_ptr<std::byte[]> m_Buffer;
115 // We keep track of the allocation count to catch incorrect usages.
116 std::uint32_t m_AllocationCount{0u};
117 // initialSize * 2^16 bytes should be enough for all allocations.
119};
120
121/**
122 * @see LinearAllocator
123 *
124 * Scoped linear allocators ensures that the base allocator is empty.
125 *
126 * TODO: currently it gives a very simple tree structure of one root node -
127 * base allocator and many leaves. We need to implement a tree-like call
128 * structure. It allows to reuse memory between different "tree" branches.
129 * While a "node" is locked nobody can't allocate from that allocator.
130 */
132{
133public:
135 : m_Allocator{allocator}
136 {
137 ENSURE(m_Allocator.GetSize() == 0);
138 }
139
141 {
144 }
145
146 void* allocate(std::size_t n, std::size_t alignment)
147 {
149 return m_Allocator.allocate(n, alignment);
150 }
151
152 void deallocate(void* ptr, std::size_t n)
153 {
154 m_Allocator.deallocate(ptr, n);
157 }
158
159private:
161 std::uint32_t m_AllocationCount{0u};
162};
163
164} // namespace PS::Memory
165
166#endif // INCLUDED_PS_LINEARALLOCATOR
#define ROUND_UP(n, multiple)
Definition: bits.h:143
Linear allocator (also known as an arena allocator or bump allocator) is a simple and fast system tha...
Definition: LinearAllocator.h:50
std::unique_ptr< std::byte[]> m_Buffer
Definition: LinearAllocator.h:114
std::size_t m_Size
Definition: LinearAllocator.h:113
PS::StaticVector< std::unique_ptr< std::byte[]>, 16 > m_BuffersToFree
Definition: LinearAllocator.h:118
std::size_t GetSize() const
Definition: LinearAllocator.h:101
std::uint32_t m_AllocationCount
Definition: LinearAllocator.h:116
LinearAllocator(const std::size_t initialSize, const std::size_t maxCapacity)
Definition: LinearAllocator.h:52
~LinearAllocator()
Definition: LinearAllocator.h:63
std::size_t GetCapacity() const
Definition: LinearAllocator.h:103
std::size_t m_Capacity
Definition: LinearAllocator.h:113
void deallocate(void *ptr, std::size_t n)
Definition: LinearAllocator.h:94
std::size_t m_MaxCapacity
Definition: LinearAllocator.h:113
void * allocate(std::size_t n, std::size_t alignment)
Definition: LinearAllocator.h:68
void Release()
Definition: LinearAllocator.h:105
Definition: LinearAllocator.h:132
std::uint32_t m_AllocationCount
Definition: LinearAllocator.h:161
void deallocate(void *ptr, std::size_t n)
Definition: LinearAllocator.h:152
~ScopedLinearAllocator()
Definition: LinearAllocator.h:140
void * allocate(std::size_t n, std::size_t alignment)
Definition: LinearAllocator.h:146
ScopedLinearAllocator(LinearAllocator &allocator)
Definition: LinearAllocator.h:134
LinearAllocator & m_Allocator
Definition: LinearAllocator.h:160
A conntainer close to std::vector but the elements are stored in place: There is a fixed capacity and...
Definition: StaticVector.h:89
void clear() noexcept
Definition: StaticVector.h:395
reference emplace_back(Args &&... args)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:477
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Definition: AllocatorAdapter.h:26
Definition: ShaderDefines.cpp:43
Definition: StaticVector.h:35