F4MP/tiltedcode/Code/immersive_launcher/memory/RipAllocator.cpp
Jous99 37b16f1547 code upload
codigo original de f4mp y tilted para referencias
2026-01-06 18:45:00 +01:00

43 lines
1.3 KiB
C++

// Deliberately in no namespace...
#include <cstdint>
#include <cstring>
extern uint8_t highrip[];
static uint8_t* globalPointer = &highrip[0];
void* RipAllocateN(size_t blockLength)
{
// Typical alignment size for function calls on x64 systems
const size_t alignment = 16;
// Calculate the next aligned address for the start of the block
uintptr_t currentPtr = reinterpret_cast<uintptr_t>(globalPointer);
uintptr_t alignedPtr = (currentPtr + (alignment - 1)) & ~(alignment - 1);
// Pad the unused space with 0xCC
size_t paddingSize = alignedPtr - currentPtr;
memset(reinterpret_cast<void*>(currentPtr), 0xCC, paddingSize);
// Align the block length as well
size_t alignedBlockLength = (blockLength + (alignment - 1)) & ~(alignment - 1);
// Advance the global pointer by the aligned block length plus the padding
globalPointer += alignedBlockLength + paddingSize;
// Pad the end of the block if there is any space remaining due to alignment
if (alignedBlockLength > blockLength)
{
size_t endPaddingSize = alignedBlockLength - blockLength;
memset(reinterpret_cast<void*>(alignedPtr + blockLength), 0xCC, endPaddingSize);
}
// Return the aligned pointer
return reinterpret_cast<void*>(alignedPtr);
}
// TBD
void RipFree(void* apBlock)
{
}