CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBLargeBlock.cpp
Go to the documentation of this file.
1 #include "CYB.hpp"
3 #ifdef TARGET_OS_WINDOWS
4 #pragma warning(disable : 4355) //this used in initalizer list
5 #endif
6 
7 static_assert(sizeof(CYB::Engine::Memory::LargeBlock) - sizeof(CYB::Engine::Memory::Block) ==
8 #ifdef DEBUG
9  24
10 #else
11  8
12 #endif
13  , "LargeBlock size has changed, check algorithms");
14 
15 CYB::Engine::Memory::LargeBlock::LargeBlock(const unsigned long long ASpaceAvailable, Block* const ALeftBlock) noexcept :
16  Block(sizeof(Block), ALeftBlock == nullptr ? *this : *ALeftBlock, true),
17 #ifdef DEBUG
18  FMagicHeader(MAGIC_HEADER),
19 #endif
20  FRemainingSize(ASpaceAvailable)
21 #ifdef DEBUG
22  ,FMagicFooter(MAGIC_FOOTER)
23 #endif
24 {}
25 
26 CYB::Engine::Memory::Block& CYB::Engine::Memory::LargeBlock::AllocateBlock(LargeBlock*& ALargeBlock, const unsigned int ANewBlockSize) noexcept {
27  const auto SizeWithBlock(ANewBlockSize + sizeof(Block));
28  const auto OriginalSize(ALargeBlock->FRemainingSize);
29  API::Assert::LessThan<unsigned long long>(SizeWithBlock, OriginalSize);
30  Block& NewBlock(*API::Interop::Allocator::InPlaceAllocation<Block>(ALargeBlock, static_cast<unsigned int>(SizeWithBlock), ALargeBlock->LeftBlock() == nullptr ? *ALargeBlock : *ALargeBlock->LeftBlock(), false));
31  ALargeBlock = API::Interop::Allocator::InPlaceAllocation<LargeBlock>(reinterpret_cast<byte*>(&NewBlock) + SizeWithBlock, OriginalSize - SizeWithBlock, &NewBlock);
32  return NewBlock;
33 }
34 
36 #ifdef DEBUG
37  API::Assert::Equal<unsigned long long>(FMagicHeader, MAGIC_HEADER);
38  API::Assert::Equal<unsigned long long>(FMagicFooter, MAGIC_FOOTER);
39 #endif
41 }
42 
44  API::Assert::NotEqual<Block*>(LeftBlock(), nullptr);
45  return *API::Interop::Allocator::InPlaceAllocation<LargeBlock>(LeftBlock(), FRemainingSize + LeftBlock()->Size() + sizeof(Block), LeftBlock()->LeftBlock()); //Even though we are eating a LargeBlock header, we are are also creating a LargeBlock Header - A block header
46 }
47 
48 unsigned long long CYB::Engine::Memory::LargeBlock::Size(void) const noexcept {
49  return FRemainingSize;
50 }
51 
52 void CYB::Engine::Memory::LargeBlock::SetSize(const unsigned long long ANewSize) noexcept {
53  FRemainingSize = ANewSize;
54 }
LargeBlock(const unsigned long long ASpaceAvailable, Block *const ALeftBlock) noexcept
Initialization constructor. Should be used with operator new in a memory location at least ASpaceAvai...
unsigned char byte
It's a byte, 8 bits, etc...
Definition: Types.hpp:4
#define DEBUG
Should be defined or not by user before all inclusions of CyberEngine.hpp. Enables engine debugging l...
Used to identify the end of a Heap.
unsigned long long Size(void) const noexcept
Get the size of the Block. This version must be used when referring to large blocks ...
static Block & AllocateBlock(LargeBlock *&ALargeBlock, const unsigned int ANewBlockSize) noexcept
Allocate a portion of the LargeBlock's owned data to create a regular block.
void SetSize(const unsigned long long ANewSize) noexcept
Set the Block's size. This version must be used when referring to large blocks
void Validate(void) const
Checks the validity of a Block HCFs if invalid. Has no effect outside of DEBUG mode. This version must be used when referring to large blocks
Precompiled header for inter-engine operations.
void Validate(void) const
Checks the validity of a Block HCFs if invalid. Has no effect outside of DEBUG mode.
Definition: CYBBlock.cpp:91
A unit of memory allocation.
Definition: CYBBlock.hpp:7
LargeBlock & EatLeftBlock(void) noexcept
Merge size and header into the size of the Block to the left. Does not modify free lists...