fix memory arena alloc
This commit is contained in:
@@ -37,21 +37,20 @@ namespace Game
|
||||
GameInst = &instance;
|
||||
}
|
||||
|
||||
uint8_t* AllocateScratch(size_t byteCount, size_t align)
|
||||
uint8_t* AllocateScratch(uint64_t byteCount, uint32_t align)
|
||||
{
|
||||
size_t offset = GetInstance().UsedScratchAmount;
|
||||
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.TransientArena.Size) return nullptr;
|
||||
GetInstance().UsedScratchAmount = newOffset;
|
||||
GetShared().Game.TransientArena.LastAllocSize = byteCount;
|
||||
assert(align <= 64); // The alignment of the arena limits the alignment that can be specified here!
|
||||
auto& arena = GetShared().Game.TransientArena;
|
||||
uint64_t offsetAligned = ((arena.Used + align - 1) / align) * align;
|
||||
uint8_t* ptrAligned = arena.Base + offsetAligned;
|
||||
uint64_t newOffset = offsetAligned + byteCount;
|
||||
if (newOffset > arena.MaxSize) return nullptr;
|
||||
arena.Used = newOffset;
|
||||
arena.LastAllocSize = byteCount;
|
||||
return ptrAligned;
|
||||
}
|
||||
|
||||
bool ResizeLastScratchAlloc(size_t newByteCount)
|
||||
bool ResizeLastScratchAlloc(uint64_t newByteCount)
|
||||
{
|
||||
auto& arena = GetShared().Game.TransientArena;
|
||||
if (newByteCount > arena.LastAllocSize)
|
||||
@@ -59,8 +58,17 @@ namespace Game
|
||||
LOG_ERROR("Can't resize to more than previous size!");
|
||||
return false;
|
||||
}
|
||||
arena.Ptr -= arena.LastAllocSize;
|
||||
arena.Ptr += newByteCount;
|
||||
LOG("Resizing last allocation from %llu to %llu", arena.LastAllocSize, newByteCount);
|
||||
arena.Used -= arena.LastAllocSize;
|
||||
arena.Used += newByteCount;
|
||||
LOG("New total: %llu", arena.Used);
|
||||
return true;
|
||||
}
|
||||
|
||||
void ResetScratch()
|
||||
{
|
||||
auto& arena = GetShared().Game.TransientArena;
|
||||
arena.Used = 0;
|
||||
arena.LastAllocSize = 0;
|
||||
}
|
||||
} // namespace Game
|
||||
|
||||
Reference in New Issue
Block a user