2023-06-21 16:59:36 +00:00
|
|
|
per_thread thread_context *ThreadLocal_ThreadContext = 0;
|
|
|
|
|
2023-07-19 15:09:41 +00:00
|
|
|
static thread_context AllocateThreadContext(void)
|
|
|
|
{
|
2023-12-23 07:27:22 +00:00
|
|
|
thread_context Context = {};
|
|
|
|
for(s64 ArenaIndex = 0; ArenaIndex < ArrayCount(Context.Arenas); ArenaIndex += 1)
|
|
|
|
{
|
2024-01-20 11:18:57 +00:00
|
|
|
Context.Arenas[ArenaIndex] = ArenaAlloc(Megabytes(2), true, "Scratch Arena %i", ArenaIndex);
|
2023-12-23 07:27:22 +00:00
|
|
|
}
|
|
|
|
return(Context);
|
2023-07-19 15:09:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:59:36 +00:00
|
|
|
static void SetThreadContext(thread_context *Context)
|
|
|
|
{
|
2023-12-23 07:27:22 +00:00
|
|
|
ThreadLocal_ThreadContext = Context;
|
2023-06-21 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static thread_context *GetThreadContext(void)
|
|
|
|
{
|
2023-12-23 07:27:22 +00:00
|
|
|
return(ThreadLocal_ThreadContext);
|
2023-06-21 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 07:27:22 +00:00
|
|
|
static temp GetScratch(arena **Conflicts, u64 ConflictCount)
|
2023-06-21 16:59:36 +00:00
|
|
|
{
|
2023-12-23 07:27:22 +00:00
|
|
|
temp Scratch = {};
|
|
|
|
thread_context *Context = GetThreadContext();
|
|
|
|
|
|
|
|
for(u64 ArenaIndex = 0;
|
2024-01-20 11:18:57 +00:00
|
|
|
ArenaIndex < ArrayCount(Context->Arenas);
|
|
|
|
++ArenaIndex)
|
2023-12-23 07:27:22 +00:00
|
|
|
{
|
|
|
|
b32 FoundConflict = false;
|
|
|
|
for(u64 ConflictIndex = 0;
|
2024-01-20 11:18:57 +00:00
|
|
|
ConflictIndex < ConflictCount;
|
|
|
|
++ConflictIndex)
|
2023-12-23 07:27:22 +00:00
|
|
|
{
|
|
|
|
arena *Conflict = Conflicts[ConflictIndex];
|
|
|
|
if(Conflict == Context->Arenas[ArenaIndex])
|
|
|
|
{
|
|
|
|
FoundConflict = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!FoundConflict)
|
|
|
|
{
|
|
|
|
Scratch = BeginTemp(Context->Arenas[ArenaIndex]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(Scratch.Arena);
|
|
|
|
|
|
|
|
return(Scratch);
|
2023-06-21 16:59:36 +00:00
|
|
|
}
|