From b9e27178c0aee15ff77351335b2643eca2423502 Mon Sep 17 00:00:00 2001 From: Nic Barker Date: Tue, 17 Jun 2025 10:32:30 +1000 Subject: [PATCH] [Core] Align base arena memory to 64 byte cache line --- clay.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/clay.h b/clay.h index 226fa31..3d53a9f 100644 --- a/clay.h +++ b/clay.h @@ -1284,15 +1284,12 @@ struct Clay_Context { Clay_Context* Clay__Context_Allocate_Arena(Clay_Arena *arena) { size_t totalSizeBytes = sizeof(Clay_Context); - uintptr_t memoryAddress = (uintptr_t)arena->memory; - // Make sure the memory address passed in for clay to use is cache line aligned - uintptr_t nextAllocOffset = (memoryAddress % 64); - if (nextAllocOffset + totalSizeBytes > arena->capacity) + if (totalSizeBytes > arena->capacity) { return NULL; } - arena->nextAllocation = nextAllocOffset + totalSizeBytes; - return (Clay_Context*)(memoryAddress + nextAllocOffset); + arena->nextAllocation += totalSizeBytes; + return (Clay_Context*)(arena->memory); } Clay_String Clay__WriteStringToCharBuffer(Clay__charArray *buffer, Clay_String string) { @@ -3990,6 +3987,10 @@ void Clay_SetPointerState(Clay_Vector2 position, bool isPointerDown) { CLAY_WASM_EXPORT("Clay_Initialize") Clay_Context* Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler) { + // Cacheline align memory passed in + uintptr_t baseOffset = 64 - ((uintptr_t)arena.memory % 64); + baseOffset = baseOffset == 64 ? 0 : baseOffset; + arena.memory += baseOffset; Clay_Context *context = Clay__Context_Allocate_Arena(&arena); if (context == NULL) return NULL; // DEFAULTS -- 2.39.5