Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Multi instance support #174

Merged
merged 12 commits into from
Jan 9, 2025

Conversation

monodop
Copy link
Contributor

@monodop monodop commented Jan 8, 2025

  • Moves global variables into Clay_Context
  • Adds Clay_GetCurrentClayContext() and Clay_SetCurrentClayContext() to determine which context to use. This is stored in thread_local
  • Clay_Initialize automatically sets (and returns) the current context, so that you don't need to worry about it if you don't care about it
  • All public and internal functions use GetCurrentClayContext() internally, to avoid polluting function signatures with new argument
  • maxElementCount & max cache moved to arena, because I was having some issues getting the MinMemorySize function to work properly early on if it was in the context, but this can probably be fixed easily.
  • maxElementCount & max cache can now only be set for initial values, they are made concrete within Clay_Initialize and cannot be changed for the arena/context after this point

This appears to be working on my local branch, but maybe could use some more testing

TODO:

  • update generators
  • tests
  • add define statement in case thread_local doesn't exist?
  • click/hover callbacks probably need some more thought, could these ever be called with the wrong context?
  • I accidentally modified some signatures that shouldn't be modified. they should be reverted to the original signatures
  • update docs
  • move struct initializers to clay_initialize
  • Odin bindings updates

Comment on lines -587 to -668
Clay__WarningArray Clay__WarningArray_Allocate_Arena(int32_t capacity, Clay_Arena *arena) {
size_t totalSizeBytes = capacity * sizeof(Clay_String);
Clay__WarningArray array = {.capacity = capacity, .length = 0};
uintptr_t nextAllocAddress = arena->nextAllocation + (uintptr_t)arena->memory;
uintptr_t arenaOffsetAligned = nextAllocAddress + (CLAY__ALIGNMENT(Clay_String) - (nextAllocAddress % CLAY__ALIGNMENT(Clay_String)));
arenaOffsetAligned -= (uintptr_t)arena->memory;
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
array.internalArray = (Clay__Warning*)((uintptr_t)arena->memory + (uintptr_t)arenaOffsetAligned);
arena->nextAllocation = arenaOffsetAligned + totalSizeBytes;
}
else {
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED,
.errorText = CLAY_STRING("Clay attempted to allocate memory in its arena, but ran out of capacity. Try increasing the capacity of the arena passed to Clay_Initialize()"),
.userData = Clay__errorHandler.userData });
}
return array;
}

Clay__WarningArray Clay_warnings = CLAY__DEFAULT_STRUCT;

Clay__Warning *Clay__WarningArray_Add(Clay__WarningArray *array, Clay__Warning item)
{
if (array->length < array->capacity) {
array->internalArray[array->length++] = item;
return &array->internalArray[array->length - 1];
}
return &CLAY__WARNING_DEFAULT;
}

void* Clay__Array_Allocate_Arena(int32_t capacity, uint32_t itemSize, uint32_t alignment, Clay_Arena *arena)
{
size_t totalSizeBytes = capacity * itemSize;
uintptr_t nextAllocAddress = arena->nextAllocation + (uintptr_t)arena->memory;
uintptr_t arenaOffsetAligned = nextAllocAddress + (alignment - (nextAllocAddress % alignment));
arenaOffsetAligned -= (uintptr_t)arena->memory;
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
arena->nextAllocation = arenaOffsetAligned + totalSizeBytes;
return (void*)((uintptr_t)arena->memory + (uintptr_t)arenaOffsetAligned);
}
else {
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED,
.errorText = CLAY_STRING("Clay attempted to allocate memory in its arena, but ran out of capacity. Try increasing the capacity of the arena passed to Clay_Initialize()"),
.userData = Clay__errorHandler.userData });
}
return CLAY__NULL;
}

bool Clay__Array_RangeCheck(int32_t index, int32_t length)
{
if (index < length && index >= 0) {
return true;
}
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_INTERNAL_ERROR,
.errorText = CLAY_STRING("Clay attempted to make an out of bounds array access. This is an internal error and is likely a bug."),
.userData = Clay__errorHandler.userData });
return false;
}

bool Clay__Array_AddCapacityCheck(int32_t length, int32_t capacity)
{
if (length < capacity) {
return true;
}
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_INTERNAL_ERROR,
.errorText = CLAY_STRING("Clay attempted to make an out of bounds array access. This is an internal error and is likely a bug."),
.userData = Clay__errorHandler.userData });
return false;
}

bool CLAY__BOOL_DEFAULT = false;
Copy link
Contributor Author

@monodop monodop Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These needed to be moved below Clay_Context, otherwise the struct's fields are not available.

clay.h Show resolved Hide resolved
clay.h Show resolved Hide resolved
@nicbarker
Copy link
Owner

👀

@nicbarker nicbarker changed the title [wip] Multi instance support [Core] Multi instance support Jan 8, 2025
@nicbarker nicbarker merged commit 944d290 into nicbarker:main Jan 9, 2025
3 checks passed
St0wy pushed a commit to St0wy/clay that referenced this pull request Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants