CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBHeap.hpp
Go to the documentation of this file.
1 #pragma once
3 namespace CYB {
4  namespace Engine {
5  namespace Memory {
7  class Heap : public API::Heap {
8  private:
10 
13 
15 
16  bool FLocked;
17  private:
24  static void AllocCheck(void);
25 
33  static unsigned long long CalculateInitialCommitSize(const unsigned long long AInitialCommitSize) noexcept;
34 
41  Block& FirstBlock(void) noexcept;
48  const Block& FirstBlock(void) const noexcept;
49 
57  void AddToFreeList(Block& ABlock, Block* const APreviousEntry) noexcept;
65  void RemoveFromFreeList(Block& ABlock, Block* const APreviousEntry) noexcept;
66 
74  void LargeBlockNeedsAtLeast(unsigned int ARequiredNumBytes);
75 
83  void MergeBlockIfPossible(Block*& ABlock, Block* const ALastFreeListEntry) noexcept;
84 
94  Block& AllocImpl(unsigned int ANumBytes, API::LockGuard& ALock);
105  Block& ReallocImpl(Block& ABlock, unsigned int ANumBytes, API::LockGuard& ALock);
113  void FreeImpl(Block& ABlock, API::LockGuard& ALock) noexcept(!API::Platform::IsDebug());
114 
121  void WalkImpl(API::LockGuard& ALock) const;
122  public:
132  Heap(const unsigned long long AInitialCommitSize);
134  Heap(const Heap&) = delete;
136  ~Heap() = default;
137 
144  void Lock(void);
151  void Unlock(void);
158  bool Locked(void) const noexcept;
159 
166  unsigned long long TotalNumberOfAllocations(void) const noexcept;
173  unsigned long long CurrentNumberOfAllocations(void) const noexcept;
180  unsigned long long CurrentAmountOfMemoryCommitted(void) const noexcept;
181 
187  void Walk(void) const;
188 
190  void* Alloc(const int ASize) final override;
192  void* Realloc(void* const APreviousAllocation, const int ANewSize) final override;
194  void Free(void* const APreviousAllocation) noexcept final override;
195  };
196  };
197  };
198 };
void Unlock(void)
Unlock the Heap, allowing reads and writes to the owned memory.
Block & AllocImpl(unsigned int ANumBytes, API::LockGuard &ALock)
Allocates a Block.
Definition: CYBHeap.cpp:79
Platform::System::Mutex FMutex
The lock for the Heap.
Definition: CYBHeap.hpp:14
void Lock(void)
Lock the Heap, preventing reads and writes to the owned memory.
A RAII locking mechanism.
Definition: LockGuard.hpp:7
unsigned long long CurrentAmountOfMemoryCommitted(void) const noexcept
Retrieve the amount of memory currently committed by the heap.
A memory pool manager and allocator.
Definition: CYBHeap.hpp:7
unsigned long long TotalNumberOfAllocations(void) const noexcept
Retrieve the number of allocations over the Heap's lifetime.
unsigned long long CurrentNumberOfAllocations(void) const noexcept
Retrieve the current of allocations in the heap. Implicitly unlocks the Heap for the duration of the ...
bool FLocked
Whether or not the Heap is currently locked.
Definition: CYBHeap.hpp:16
Used to identify the end of a Heap.
void Walk(void) const
Walks the heap blocks and free list and HCFs if an error is detected.
Definition: CYBHeap.cpp:177
Block * FFreeList
The first block in the linked free list.
Definition: CYBHeap.hpp:11
void FreeImpl(Block &ABlock, API::LockGuard &ALock) noexcept(!API::Platform::IsDebug())
Frees a Block.
Definition: CYBHeap.cpp:148
void * Alloc(const int ASize) finaloverride
Allocate memory from the heap for use.
Definition: CYBHeap.cpp:182
void WalkImpl(API::LockGuard &ALock) const
Walks the heap blocks and free list and throws if an error is detected.
Definition: CYBHeap.cpp:160
static void AllocCheck(void)
Used by the testing suite to fail allocations at will.
Definition: CYBEntry.cpp:70
static unsigned long long CalculateInitialCommitSize(const unsigned long long AInitialCommitSize) noexcept
A small max comparison of AInitialCommitSize and sizeof(Block) + 1.
Definition: CYBHeap.cpp:14
void MergeBlockIfPossible(Block *&ABlock, Block *const ALastFreeListEntry) noexcept
Coalesces Blocks to the left of ABlock and updates the free list.
Definition: CYBHeap.cpp:64
Platform::System::VirtualMemory FReservation
The VirtualMemory mapping owned by the heap, also a pointer to the first block.
Definition: CYBHeap.hpp:9
void Free(void *const APreviousAllocation) noexceptfinaloverride
Return memory to the heap. Data in allocated range will be lost.
Definition: CYBHeap.cpp:223
A fast locking, no order guaranteed, mutex.
Definition: CYBMutex.hpp:7
void LargeBlockNeedsAtLeast(unsigned int ARequiredNumBytes)
Ensures that FLargeBlock has at least ARequiredNumBytes of Size by committing more memory if necessar...
Definition: CYBHeap.cpp:45
Block & ReallocImpl(Block &ABlock, unsigned int ANumBytes, API::LockGuard &ALock)
Reallocates a Block.
Definition: CYBHeap.cpp:122
An interface which can allocate memory.
Definition: Heap.hpp:5
void * Realloc(void *const APreviousAllocation, const int ANewSize) finaloverride
Allocate memory from the heap for use while preserving previous data. Passing a valid APreviousAlloca...
Definition: CYBHeap.cpp:197
A unit of memory allocation.
Definition: CYBBlock.hpp:7
LargeBlock * FLargeBlock
The block that extends to the end of the free list.
Definition: CYBHeap.hpp:12
bool Locked(void) const noexcept
Check and see if the Heap is currently locked.
Strong typing for Platform::Identifier.
void RemoveFromFreeList(Block &ABlock, Block *const APreviousEntry) noexcept
Removes a Block from the free list after APreviousEntry while performing all the checks and reassignm...
Definition: CYBHeap.cpp:38
Access and abstraction to the basic OS memory functions.
Block & FirstBlock(void) noexcept
Get a reference to the first block in the reservation.
Definition: CYBHeap.cpp:19
void AddToFreeList(Block &ABlock, Block *const APreviousEntry) noexcept
Adds a Block to the free list after APreviousEntry while performing all the checks and reassignments...
Definition: CYBHeap.cpp:27