UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BlockAllocator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if (defined(__AUTORTFM) && __AUTORTFM)
6
7#include "Utils.h"
8#include "ExternAPI.h"
9
10#include <cstddef>
11
12namespace AutoRTFM
13{
14
15// A single-threaded, bump memory allocator that sub-allocates out of blocks of
16// inline memory and once that's exhausted, from heap allocated memory.
17// All allocated memory is freed when the TBlockAllocator is destructed.
18// There is no way to free individual allocations.
19//
20// Template parameters:
21// InlineBlockDataSize - The size of the data section of the inline block.
22// DataAlignment - The maximum supported alignment of data from this allocator.
23// GrowthPercentage - The percentage to grow the block size each reallocation.
24template<size_t InlineBlockDataSize = 256, size_t DataAlignment = 16, size_t GrowthPercentage = 200>
26{
27public:
28 // Constructor
29 TBlockAllocator() = default;
30
31 // Destructor - frees all the memory allocated by the allocator.
33 {
34 FreeAll();
35 }
36
37 // Frees all allocations made by the allocator.
38 void FreeAll()
39 {
40 while (Tail != &InlineBlock)
41 {
42 FBlockHeader* Prev = Tail->Prev;
43 AutoRTFM::Free(Tail);
44 Tail = Prev;
45 }
48 }
49
50 // Allocates memory from the block allocator.
51 // Alignment must be a power of two and no larger than DataAlignment.
52 inline void* Allocate(size_t Size, size_t Alignment)
53 {
54 AUTORTFM_ASSERT(Alignment <= DataAlignment);
55
56 if (void* Allocation = Tail->TryAllocate(Size, Alignment))
57 {
58 return Allocation;
59 }
60
61 size_t NewBlockSize = std::max(NextBlockSize, Size);
63 FBlockHeader* NewBlock = FBlockHeader::New(Tail, NewBlockSize);
64 Tail = NewBlock;
65
66 void* Allocation = NewBlock->TryAllocate(Size, Alignment);
67 AUTORTFM_ASSERT(Allocation);
68 return Allocation;
69 }
70
71 // Constructs and returns a new T into the memory returned by calling
72 // Allocate(sizeof(T), alignof(T)).
73 template<typename T, typename... ArgTypes> T* New(ArgTypes&&... Args)
74 {
75 return new (Allocate(sizeof(T), alignof(T))) T(Forward<ArgTypes>(Args)...);
76 }
77
78private:
80 TBlockAllocator(const TBlockAllocator&) = delete;
81 TBlockAllocator& operator=(const TBlockAllocator&) = delete;
82 TBlockAllocator& operator=(TBlockAllocator&&) = delete;
83
84 static constexpr size_t BlockAlignment = std::max<size_t>(DataAlignment, 8);
85
86 struct alignas(BlockAlignment) FBlockHeader
87 {
88 // The previous FBlock in the singly linked-list
89 FBlockHeader* const Prev = nullptr;
90 // The size of the block's data
91 const size_t BlockDataSize = 0;
92 // The number of unallocated bytes remaining in the block
93 size_t Remaining = BlockDataSize;
94 // <data>
95
96 // Allocates, constructs and returns a new block from the heap-allocated memory.
97 static FBlockHeader* New(FBlockHeader* Prev, size_t BlockDataSize)
98 {
99 void* Memory = AutoRTFM::Allocate(sizeof(FBlockHeader) + BlockDataSize, BlockAlignment);
100 return new (Memory) FBlockHeader{Prev, BlockDataSize};
101 }
102
103 // Attempts to sub-allocate with out of this block. Returns a pointer to
104 // the sub-allocated memory on success, or nullptr on failure.
105 inline void* TryAllocate(size_t Size, size_t Alignment)
106 {
107 size_t RemainingAligned = AlignDown(Remaining, Alignment);
109 {
110 return nullptr;
111 }
112 void* Ptr = reinterpret_cast<std::byte*>(this) + sizeof(FBlockHeader) + BlockDataSize - RemainingAligned;
113 Remaining = RemainingAligned - Size;
114 return Ptr;
115 }
116 };
117
118 static_assert(sizeof(FBlockHeader) % DataAlignment == 0);
119
121 FBlockHeader* Tail = &InlineBlock;
122 FBlockHeader InlineBlock{/* Prev */ nullptr, InlineBlockDataSize};
123 alignas(BlockAlignment) std::byte Data[InlineBlockDataSize];
124};
125
126}
127
128#endif // (defined(__AUTORTFM) && __AUTORTFM)
constexpr T AlignDown(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:34
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
T * New(FMemStackBase &Mem, int32 Count=1, int32 Align=DEFAULT_ALIGNMENT)
Definition MemStack.h:259
uint32 Size
Definition VulkanMemory.cpp:4034
Definition IoDispatcher.cpp:201
Definition API.cpp:57