UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IoAllocators.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7
8template <typename T, uint16 SlabSize = 4096>
10{
11private:
12 struct FSlab;
13
14 struct FElement
15 {
17 FSlab* Slab = nullptr;
18 };
19
20 struct FSlab
21 {
22 uint16 Allocated = 0;
23 uint16 Freed = 0;
24 FElement Elements[SlabSize];
25 };
26
27public:
29 {
30 CurrentSlab = new FSlab();
31 }
32
34 {
35 check(CurrentSlab->Allocated == CurrentSlab->Freed);
36 delete CurrentSlab;
37 }
38
39 template <typename... ArgsType>
40 T* Construct(ArgsType&&... Args)
41 {
42 return new(Alloc()) T(Forward<ArgsType>(Args)...);
43 }
44
45 void Destroy(T* Ptr)
46 {
47 Ptr->~T();
48 Free(Ptr);
49 }
50
51 T* Alloc()
52 {
53 uint16 ElementIndex = CurrentSlab->Allocated++;
54 check(ElementIndex < SlabSize);
55 FElement* Element = CurrentSlab->Elements + ElementIndex;
56 Element->Slab = CurrentSlab;
57 if (CurrentSlab->Allocated == SlabSize)
58 {
59 //TRACE_CPUPROFILER_EVENT_SCOPE(AllocSlab);
60 CurrentSlab = new FSlab();
61 }
62 return Element->Data.GetTypedPtr();
63 }
64
65 void Free(T* Ptr)
66 {
67 FElement* Element = reinterpret_cast<FElement*>(Ptr);
68 FSlab* Slab = Element->Slab;
69 if (++Slab->Freed == SlabSize)
70 {
71 //TRACE_CPUPROFILER_EVENT_SCOPE(FreeSlab);
72 check(Slab->Freed == Slab->Allocated);
73 delete Slab;
74 }
75 }
76
77private:
78 FSlab* CurrentSlab = nullptr;
79};
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition IoAllocators.h:10
T * Alloc()
Definition IoAllocators.h:51
void Destroy(T *Ptr)
Definition IoAllocators.h:45
void Free(T *Ptr)
Definition IoAllocators.h:65
~TSingleThreadedSlabAllocator()
Definition IoAllocators.h:33
TSingleThreadedSlabAllocator()
Definition IoAllocators.h:28
T * Construct(ArgsType &&... Args)
Definition IoAllocators.h:40
Definition TypeCompatibleBytes.h:24