From 3d5dad7beed3bbf0612faba852cb65693f4efc30 Mon Sep 17 00:00:00 2001
From: Nic Barker <contact+github@nicbarker.com>
Date: Wed, 29 Jan 2025 13:13:13 +1300
Subject: [PATCH] Make sure memory passed to clay is cache line aligned

---
 clay.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clay.h b/clay.h
index d5dac01..f8f3e79 100644
--- a/clay.h
+++ b/clay.h
@@ -856,13 +856,15 @@ struct Clay_Context {
 
 Clay_Context* Clay__Context_Allocate_Arena(Clay_Arena *arena) {
     size_t totalSizeBytes = sizeof(Clay_Context);
-    uintptr_t nextAllocOffset = arena->nextAllocation + (64 - (arena->nextAllocation % 64));
+    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)
     {
         return NULL;
     }
     arena->nextAllocation = nextAllocOffset + totalSizeBytes;
-    return (Clay_Context*)((uintptr_t)arena->memory + nextAllocOffset);
+    return (Clay_Context*)(memoryAddress + nextAllocOffset);
 }
 
 Clay_String Clay__WriteStringToCharBuffer(Clay__charArray *buffer, Clay_String string) {