Plasma Engine  2.0
Loading...
Searching...
No Matches
LargeBlockAllocator.h
1#pragma once
2
3#include <Foundation/Containers/DynamicArray.h>
4#include <Foundation/Memory/MemoryTracker.h>
5#include <Foundation/Memory/PageAllocator.h>
6#include <Foundation/System/SystemInformation.h>
7#include <Foundation/Threading/Lock.h>
8#include <Foundation/Threading/Mutex.h>
9#include <Foundation/Threading/ThreadUtils.h>
10
12template <typename T, plUInt32 SizeInBytes>
14{
15 PL_DECLARE_POD_TYPE();
16
17 enum
18 {
19 SIZE_IN_BYTES = SizeInBytes,
20 CAPACITY = SIZE_IN_BYTES / sizeof(T)
21 };
22
23 plDataBlock(T* pData, plUInt32 uiCount);
24
25 T* ReserveBack();
26 T* PopBack();
27
28 bool IsEmpty() const;
29 bool IsFull() const;
30
31 T& operator[](plUInt32 uiIndex) const;
32
33 T* m_pData;
34 plUInt32 m_uiCount;
35};
36
38template <plUInt32 BlockSizeInByte>
40{
41public:
42 plLargeBlockAllocator(plStringView sName, plAllocator* pParent, plAllocatorTrackingMode mode = plAllocatorTrackingMode::Default);
44
45 template <typename T>
46 plDataBlock<T, BlockSizeInByte> AllocateBlock();
47
48 template <typename T>
49 void DeallocateBlock(plDataBlock<T, BlockSizeInByte>& ref_block);
50
51
52 plStringView GetName() const;
53
54 plAllocatorId GetId() const;
55
56 const plAllocator::Stats& GetStats() const;
57
58private:
59 void* Allocate(size_t uiAlign);
60 void Deallocate(void* ptr);
61
62 plAllocatorId m_Id;
63 plAllocatorTrackingMode m_TrackingMode;
64
65 plMutex m_Mutex;
66 plThreadID m_ThreadID;
67
68 struct SuperBlock
69 {
70 PL_DECLARE_POD_TYPE();
71
72 enum
73 {
74 NUM_BLOCKS = 16,
75 SIZE_IN_BYTES = BlockSizeInByte * NUM_BLOCKS
76 };
77
78 void* m_pBasePtr;
79
80 plUInt32 m_uiUsedBlocks;
81 };
82
83 plDynamicArray<SuperBlock> m_SuperBlocks;
84 plDynamicArray<plUInt32> m_FreeBlocks;
85};
86
87#include <Foundation/Memory/Implementation/LargeBlockAllocator_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Definition DynamicArray.h:81
A block allocator which can only allocates blocks of memory at once.
Definition LargeBlockAllocator.h:40
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Definition Allocator.h:26
This struct represents a block of type T, typically 4kb.
Definition LargeBlockAllocator.h:14