LCOV - code coverage report
Current view: top level - source/ps/memory - LinearAllocator.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 48 48 100.0 %
Date: 2026-12-03 01:19:39 Functions: 7 7 100.0 %

          Line data    Source code
       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"
      22             : #include "ps/containers/StaticVector.h"
      23             : 
      24             : #include <bit>
      25             : #include <cstddef>
      26             : #include <fmt/format.h>
      27             : #include <memory>
      28             : #include <version>
      29             : 
      30             : namespace 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             :  */
      49             : class LinearAllocator
      50             : {
      51             : public:
      52          18 :     LinearAllocator(const std::size_t initialSize, const std::size_t maxCapacity)
      53          18 :         : m_Capacity{initialSize}, m_MaxCapacity{maxCapacity},
      54             : #if defined(__cpp_lib_smart_ptr_for_overwrite)
      55          18 :         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          18 :         ENSURE(initialSize > 0);
      61             :     }
      62             : 
      63          18 :     ~LinearAllocator()
      64             :     {
      65          18 :         Release();
      66          18 :     }
      67             : 
      68          18 :     void* allocate(std::size_t n, std::size_t alignment)
      69             :     {
      70          18 :         m_Size = ROUND_UP(m_Size, alignment);
      71          18 :         if (m_Size + n > m_Capacity)
      72             :         {
      73           7 :             m_BuffersToFree.emplace_back(std::move(m_Buffer));
      74           7 :             m_Size = 0;
      75          15 :             m_Capacity = std::min(std::max(std::bit_ceil(n), m_Capacity * 2), m_MaxCapacity);
      76           7 :             if (n > m_Capacity)
      77             :             {
      78           2 :                 throw CapacityExceededException{fmt::format(
      79             :                     "Tried to allocate from LinearAllocator with a size of {} but the capacity is only {}",
      80           4 :                     n, m_Capacity)};
      81             :             }
      82             : #if defined(__cpp_lib_smart_ptr_for_overwrite)
      83           5 :             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          16 :         void* ptr{m_Buffer.get() + m_Size};
      89          16 :         m_Size += n;
      90          16 :         ++m_AllocationCount;
      91          16 :         return ptr;
      92             :     }
      93             : 
      94          16 :     void deallocate([[maybe_unused]] void* ptr, [[maybe_unused]] std::size_t n)
      95             :     {
      96             :         // Do nothing. All allocations will be removed in Release.
      97          16 :         ENSURE(m_AllocationCount > 0);
      98          16 :         --m_AllocationCount;
      99          16 :     }
     100             : 
     101          22 :     std::size_t GetSize() const { return m_Size; }
     102             : 
     103           5 :     std::size_t GetCapacity() const { return m_Capacity; }
     104             : 
     105          25 :     void Release()
     106             :     {
     107          25 :         ENSURE(m_AllocationCount == 0);
     108          25 :         m_Size = 0u;
     109          25 :         m_BuffersToFree.clear();
     110          25 :     }
     111             : 
     112             : private:
     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.
     118             :     PS::StaticVector<std::unique_ptr<std::byte[]>, 16> m_BuffersToFree;
     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             :  */
     131             : class ScopedLinearAllocator
     132             : {
     133             : public:
     134           6 :     ScopedLinearAllocator(LinearAllocator& allocator)
     135           6 :         : m_Allocator{allocator}
     136             :     {
     137           6 :         ENSURE(m_Allocator.GetSize() == 0);
     138           6 :     }
     139             : 
     140           6 :     ~ScopedLinearAllocator()
     141             :     {
     142           6 :         ENSURE(m_AllocationCount == 0);
     143           6 :         m_Allocator.Release();
     144           6 :     }
     145             : 
     146           4 :     void* allocate(std::size_t n, std::size_t alignment)
     147             :     {
     148           4 :         ++m_AllocationCount;
     149           4 :         return m_Allocator.allocate(n, alignment);
     150             :     }
     151             : 
     152           4 :     void deallocate(void* ptr, std::size_t n)
     153             :     {
     154           4 :         m_Allocator.deallocate(ptr, n);
     155           4 :         ENSURE(m_AllocationCount > 0);
     156           4 :         --m_AllocationCount;
     157           4 :     }
     158             : 
     159             : private:
     160             :     LinearAllocator& m_Allocator;
     161             :     std::uint32_t m_AllocationCount{0u};
     162             : };
     163             : 
     164             : } // namespace PS::Memory
     165             : 
     166             : #endif // INCLUDED_PS_LINEARALLOCATOR

Generated by: LCOV version 1.16