mirror of
https://github.com/nicbarker/clay.git
synced 2025-12-23 17:41:06 +00:00
fix: improve cache line alignment logic
This commit is contained in:
parent
2749e56e7e
commit
62e3860202
12
clay.h
12
clay.h
|
|
@ -53,6 +53,7 @@
|
||||||
|
|
||||||
#define CLAY__MAX(x, y) (((x) > (y)) ? (x) : (y))
|
#define CLAY__MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||||
#define CLAY__MIN(x, y) (((x) < (y)) ? (x) : (y))
|
#define CLAY__MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||||
|
#define CLAY__OFFSET_FOR_64_BYTE_ALIGNMENT(x) ((64 - (x & 63)) & 63)
|
||||||
|
|
||||||
#define CLAY_TEXT_CONFIG(...) Clay__StoreTextElementConfig(CLAY__CONFIG_WRAPPER(Clay_TextElementConfig, __VA_ARGS__))
|
#define CLAY_TEXT_CONFIG(...) Clay__StoreTextElementConfig(CLAY__CONFIG_WRAPPER(Clay_TextElementConfig, __VA_ARGS__))
|
||||||
|
|
||||||
|
|
@ -3849,7 +3850,7 @@ Clay_Color Clay__debugViewHighlightColor = { 168, 66, 28, 100 };
|
||||||
Clay__WarningArray Clay__WarningArray_Allocate_Arena(int32_t capacity, Clay_Arena *arena) {
|
Clay__WarningArray Clay__WarningArray_Allocate_Arena(int32_t capacity, Clay_Arena *arena) {
|
||||||
size_t totalSizeBytes = capacity * sizeof(Clay_String);
|
size_t totalSizeBytes = capacity * sizeof(Clay_String);
|
||||||
Clay__WarningArray array = {.capacity = capacity, .length = 0};
|
Clay__WarningArray array = {.capacity = capacity, .length = 0};
|
||||||
uintptr_t nextAllocOffset = arena->nextAllocation + (64 - (arena->nextAllocation % 64));
|
uintptr_t nextAllocOffset = arena->nextAllocation + CLAY__OFFSET_FOR_64_BYTE_ALIGNMENT(arena->nextAllocation);
|
||||||
if (nextAllocOffset + totalSizeBytes <= arena->capacity) {
|
if (nextAllocOffset + totalSizeBytes <= arena->capacity) {
|
||||||
array.internalArray = (Clay__Warning*)((uintptr_t)arena->memory + (uintptr_t)nextAllocOffset);
|
array.internalArray = (Clay__Warning*)((uintptr_t)arena->memory + (uintptr_t)nextAllocOffset);
|
||||||
arena->nextAllocation = nextAllocOffset + totalSizeBytes;
|
arena->nextAllocation = nextAllocOffset + totalSizeBytes;
|
||||||
|
|
@ -3875,7 +3876,7 @@ Clay__Warning *Clay__WarningArray_Add(Clay__WarningArray *array, Clay__Warning i
|
||||||
void* Clay__Array_Allocate_Arena(int32_t capacity, uint32_t itemSize, Clay_Arena *arena)
|
void* Clay__Array_Allocate_Arena(int32_t capacity, uint32_t itemSize, Clay_Arena *arena)
|
||||||
{
|
{
|
||||||
size_t totalSizeBytes = capacity * itemSize;
|
size_t totalSizeBytes = capacity * itemSize;
|
||||||
uintptr_t nextAllocOffset = arena->nextAllocation + ((64 - (arena->nextAllocation % 64)) & 63);
|
uintptr_t nextAllocOffset = arena->nextAllocation + CLAY__OFFSET_FOR_64_BYTE_ALIGNMENT(arena->nextAllocation);
|
||||||
if (nextAllocOffset + totalSizeBytes <= arena->capacity) {
|
if (nextAllocOffset + totalSizeBytes <= arena->capacity) {
|
||||||
arena->nextAllocation = nextAllocOffset + totalSizeBytes;
|
arena->nextAllocation = nextAllocOffset + totalSizeBytes;
|
||||||
return (void*)((uintptr_t)arena->memory + (uintptr_t)nextAllocOffset);
|
return (void*)((uintptr_t)arena->memory + (uintptr_t)nextAllocOffset);
|
||||||
|
|
@ -4040,9 +4041,12 @@ void Clay_SetPointerState(Clay_Vector2 position, bool isPointerDown) {
|
||||||
CLAY_WASM_EXPORT("Clay_Initialize")
|
CLAY_WASM_EXPORT("Clay_Initialize")
|
||||||
Clay_Context* Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler) {
|
Clay_Context* Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler) {
|
||||||
// Cacheline align memory passed in
|
// Cacheline align memory passed in
|
||||||
uintptr_t baseOffset = 64 - ((uintptr_t)arena.memory % 64);
|
uintptr_t baseOffset = CLAY__OFFSET_FOR_64_BYTE_ALIGNMENT((uintptr_t)arena.memory);
|
||||||
baseOffset = baseOffset == 64 ? 0 : baseOffset;
|
|
||||||
arena.memory += baseOffset;
|
arena.memory += baseOffset;
|
||||||
|
// Ensure capacity won't underflow when adjusted for alignment
|
||||||
|
if (arena.capacity < baseOffset) return NULL;
|
||||||
|
// Adjust capacity to account for alignment offset
|
||||||
|
arena.capacity -= baseOffset;
|
||||||
Clay_Context *context = Clay__Context_Allocate_Arena(&arena);
|
Clay_Context *context = Clay__Context_Allocate_Arena(&arena);
|
||||||
if (context == NULL) return NULL;
|
if (context == NULL) return NULL;
|
||||||
// DEFAULTS
|
// DEFAULTS
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue