This commit is contained in:
Till Wübbers
2025-04-27 12:00:22 +02:00
parent d75e5627f9
commit 4cdd80977c
8 changed files with 148 additions and 34 deletions

View File

@@ -37,16 +37,30 @@ namespace Game
GameInst = &instance;
}
void* AllocateScratch(size_t byteCount, size_t align)
uint8_t* AllocateScratch(size_t byteCount, size_t align)
{
size_t offset = GetInstance().UsedScratchAmount;
uint8_t* base = static_cast<uint8_t*>(GetShared().Game.TransientStorage);
uint8_t* base = GetShared().Game.TransientArena.Ptr;
uint8_t* current = base + offset;
size_t offsetAligned = ((offset + align - 1) / align) * align;
uint8_t* ptrAligned = base + offsetAligned;
size_t newOffset = offsetAligned + byteCount;
if (newOffset > GetShared().Game.TransientStorageSize) return nullptr;
if (newOffset > GetShared().Game.TransientArena.Size) return nullptr;
GetInstance().UsedScratchAmount = newOffset;
return reinterpret_cast<void*>(ptrAligned);
GetShared().Game.TransientArena.LastAllocSize = byteCount;
return ptrAligned;
}
bool ResizeLastScratchAlloc(size_t newByteCount)
{
auto& arena = GetShared().Game.TransientArena;
if (newByteCount > arena.LastAllocSize)
{
LOG_ERROR("Can't resize to more than previous size!");
return false;
}
arena.Ptr -= arena.LastAllocSize;
arena.Ptr += newByteCount;
return true;
}
} // namespace Game