overridable scratch; remove the tedious explicit thread init

This commit is contained in:
Allen Webster
2021-08-13 11:19:03 -07:00
parent fcda88e1cd
commit 4ab0adf9cc
12 changed files with 89 additions and 95 deletions
-3
View File
@@ -11,9 +11,6 @@ static MD_Arena *arena = 0;
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
MD_String8 example_code = MD_S8Lit("@struct Foo:\n"
-3
View File
@@ -7,9 +7,6 @@ static MD_Arena *arena = 0;
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
// NOTE(rjf): Parse all the files passed in via command line.
-3
View File
@@ -25,9 +25,6 @@ CleanUp(void)
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
// NOTE(rjf): Parse all the files passed in via command line.
-3
View File
@@ -64,9 +64,6 @@ static void PrintNode(MD_Node* node, FILE* file, int indent_count) {
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
// NOTE(pmh): Parse all the files passed in via command line.
-3
View File
@@ -5,9 +5,6 @@ static MD_Arena *arena = 0;
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
MD_Node *list = MD_MakeList(arena);
@@ -34,9 +34,6 @@ static MD_Arena *arena = 0;
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
//~ NOTE(rjf): Parse command line arguments.
-3
View File
@@ -182,9 +182,6 @@ EvaluateScope(NamespaceNode *ns, MD_Node *code)
int main(int argument_count, char **arguments)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
//- rjf: parse command line
+78 -49
View File
@@ -217,6 +217,7 @@ MD_LINUX_FileIterIncrement(MD_Arena *arena, MD_FileIter *opaque_it, MD_String8 p
#if MD_DEFAULT_ARENA
//- "low level memory" implementation check
#if !defined(MD_IMPL_Reserve)
# error Missing implementation for MD_IMPL_Reserve
#endif
@@ -295,6 +296,78 @@ MD_ArenaDefaultPopTo(MD_ArenaDefault *arena, MD_u64 pos){
#endif
//- "arena" implementation checks
#if !defined(MD_IMPL_ArenaAlloc)
# error Missing implementation for MD_IMPL_ArenaAlloc
#endif
#if !defined(MD_IMPL_ArenaRelease)
# error Missing implementation for MD_IMPL_ArenaRelease
#endif
#if !defined(MD_IMPL_ArenaGetPos)
# error Missing implementation for MD_IMPL_ArenaGetPos
#endif
#if !defined(MD_IMPL_ArenaPush)
# error Missing implementation for MD_IMPL_ArenaPush
#endif
#if !defined(MD_IMPL_ArenaPopTo)
# error Missing implementation for MD_IMPL_ArenaPopTo
#endif
#if !defined(MD_IMPL_ArenaSetAutoAlign)
# error Missing implementation for MD_IMPL_ArenaSetAutoAlign
#endif
#if !defined(MD_IMPL_ArenaHeaderSize)
# error Missing implementation for MD_IMPL_ArenaHeaderSize
#endif
//~/////////////////////////////////////////////////////////////////////////////
///////////////////////////// MD Scratch Pool //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if MD_DEFAULT_SCRATCH
#if !defined(MD_IMPL_ScratchSize)
# define MD_IMPL_ScratchSize (1llu << 30)
#endif
#if !defined(MD_IMPL_ScratchCount)
# define MD_IMPL_ScratchCount 2llu
#endif
#if !defined(MD_IMPL_GetScratch)
# define MD_IMPL_GetScratch MD_GetScratchDefault
#endif
MD_THREAD_LOCAL MD_Arena *md_thread_scratch_pool[MD_IMPL_ScratchCount] = {0, 0};
static MD_Arena*
MD_GetScratchDefault(MD_Arena **conflicts, MD_u64 count){
MD_Arena **scratch_pool = md_thread_scratch_pool;
if (scratch_pool[0] == 0){
MD_Arena **arena_ptr = scratch_pool;
for (MD_u64 i = 0; i < MD_IMPL_ScratchCount; i += 1, arena_ptr += 1){
*arena_ptr = MD_ArenaAlloc(MD_IMPL_ScratchSize);
}
}
MD_Arena *result = 0;
MD_Arena **arena_ptr = scratch_pool;
for (MD_u64 i = 0; i < MD_IMPL_ScratchCount; i += 1, arena_ptr += 1){
MD_Arena *arena = *arena_ptr;
MD_Arena **conflict_ptr = conflicts;
for (MD_u32 j = 0; j < count; j += 1, conflict_ptr += 1){
if (arena == *conflict_ptr){
arena = 0;
break;
}
}
if (arena != 0){
result = arena;
break;
}
}
return(result);
}
#endif
//~/////////////////////////////////////////////////////////////////////////////
//////////////////////// MD Library Implementation /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -347,28 +420,6 @@ MD_MemoryCopy(void *dest, void *src, MD_u64 size)
//~ Arena Functions
#if !defined(MD_IMPL_ArenaAlloc)
# error Missing implementation for MD_IMPL_ArenaAlloc
#endif
#if !defined(MD_IMPL_ArenaRelease)
# error Missing implementation for MD_IMPL_ArenaRelease
#endif
#if !defined(MD_IMPL_ArenaGetPos)
# error Missing implementation for MD_IMPL_ArenaGetPos
#endif
#if !defined(MD_IMPL_ArenaPush)
# error Missing implementation for MD_IMPL_ArenaPush
#endif
#if !defined(MD_IMPL_ArenaPopTo)
# error Missing implementation for MD_IMPL_ArenaPopTo
#endif
#if !defined(MD_IMPL_ArenaSetAutoAlign)
# error Missing implementation for MD_IMPL_ArenaSetAutoAlign
#endif
#if !defined(MD_IMPL_ArenaHeaderSize)
# error Missing implementation for MD_IMPL_ArenaHeaderSize
#endif
MD_FUNCTION_IMPL MD_Arena*
MD_ArenaAlloc(MD_u64 cap){
return(MD_IMPL_ArenaAlloc(cap));
@@ -429,36 +480,14 @@ MD_ArenaEndTemp(MD_ArenaTemp temp){
MD_IMPL_ArenaPopTo(temp.arena, temp.pos);
}
//~ Thread Context Functions
MD_THREAD_LOCAL MD_ThreadContext *md_thread_ctx;
MD_FUNCTION_IMPL void
MD_ThreadInit(MD_ThreadContext *tctx_mem){
md_thread_ctx = tctx_mem;
for (MD_u32 i = 0; i < MD_ArrayCount(md_thread_ctx->scratch_pool); i += 1){
tctx_mem->scratch_pool[i] = MD_ArenaAlloc(MD_SCRATCH_SIZE);
}
}
//~ Arena Scratch Pool
MD_FUNCTION_IMPL MD_ArenaTemp
MD_GetScratch(MD_Arena **conflicts, MD_u32 count){
MD_GetScratch(MD_Arena **conflicts, MD_u64 count){
MD_Arena *arena = MD_IMPL_GetScratch(conflicts, count);
MD_ArenaTemp result = MD_ZERO_STRUCT;
MD_ThreadContext *tctx = md_thread_ctx;
MD_Arena **arena_ptr = tctx->scratch_pool;
for (MD_u32 i = 0; i < MD_ArrayCount(tctx->scratch_pool); i += 1, arena_ptr += 1){
MD_b32 has_conflict = 0;
MD_Arena **conflict_ptr = conflicts;
for (MD_u32 j = 0; j < count; j += 1, conflict_ptr += 1){
if (*arena_ptr == *conflict_ptr){
has_conflict = 1;
break;
}
}
if (!has_conflict){
result = MD_ArenaBeginTemp(*arena_ptr);
break;
}
if (arena != 0){
result = MD_ArenaBeginTemp(arena);
}
return(result);
}
+11 -16
View File
@@ -7,7 +7,7 @@
/* NOTE(allen): Notes on overrides/macro options:
**
** Individual Overridables:
** Overridable :
** "file iteration" ** OPTIONAL
** #define MD_IMPL_FileIterIncrement
** (MD_Arena*, MD_FileIter*, MD_String8, MD_FileInfo* out) -> MD_b32
@@ -28,14 +28,18 @@
** #define MD_IMPL_ArenaSetAutoAlign (MD_IMPL_Arena*, MD_u64) -> void
** #define MD_IMPL_ArenaHeaderSize MD_u64
**
** "constants" ** REQUIRED (defaults to 1 gigabyte)
** #define MD_SCRATCH_SIZE MD_u64
** "scratch" ** REQUIRED (default implementation available)
** #define MD_IMPL_GetScratch (MD_IMPL_Arena**, MD_u64) -> MD_IMPL_Arena*
** "scratch constants" ** OPTIONAL (required for default scratch)
** #define MD_IMPL_ScratchSize MD_u64 / default 1 gigabyte
** #define MD_IMPL_ScratchCount MD_u64 / default 2
**
** Default Implementation Controls
** These controls default to '1' i.e. 'enabled'
** #define MD_DEFAULT_FILE_ITER -> construct "file iteration" from OS headers
** #define MD_DEFAULT_MEMORY -> construct "low level memory" from OS headers
** #define MD_DEFAULT_ARENA -> construct "arena" from "low level memory"
** #define MD_DEFAULT_SCRATCH -> construct "scratch" from "arena"
**
*/
@@ -49,8 +53,8 @@
#if !defined(MD_DEFAULT_ARENA)
# define MD_DEFAULT_ARENA 1
#endif
#if !defined(MD_SCRATCH_SIZE)
# define MD_SCRATCH_SIZE (1 << 30)
#if !defined(MD_DEFAULT_SCRATCH)
# define MD_DEFAULT_SCRATCH 1
#endif
@@ -338,14 +342,6 @@ struct MD_ArenaTemp{
MD_u64 pos;
};
//~ Thread Context
// TODO(allen): overrides for get scratch
typedef struct MD_ThreadContext MD_ThreadContext;
struct MD_ThreadContext{
MD_Arena *scratch_pool[2];
};
//~ Basic Unicode string types.
typedef struct MD_String8 MD_String8;
@@ -766,10 +762,9 @@ MD_FUNCTION void MD_ArenaClear(MD_Arena *arena);
MD_FUNCTION MD_ArenaTemp MD_ArenaBeginTemp(MD_Arena *arena);
MD_FUNCTION void MD_ArenaEndTemp(MD_ArenaTemp temp);
//~ Thread Context Functions
//~ Arena Scratch Pool
MD_FUNCTION void MD_ThreadInit(MD_ThreadContext *tctx_mem);
MD_FUNCTION MD_ArenaTemp MD_GetScratch(MD_Arena **conflicts, MD_u32 count);
MD_FUNCTION MD_ArenaTemp MD_GetScratch(MD_Arena **conflicts, MD_u64 count);
#define MD_ReleaseScratch(scratch) MD_ArenaEndTemp(scratch)
-3
View File
@@ -5,9 +5,6 @@ static MD_Arena *arena = 0;
int main(void)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
printf("%d\n", MD_CPP_VERSION);
-3
View File
@@ -120,9 +120,6 @@ TokenMatch(MD_Token token, MD_String8 string, MD_TokenKind kind)
int main(void)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
Test("Lexer")
-3
View File
@@ -16,9 +16,6 @@ void run_test_on_string(MD_String8 string)
int main(void)
{
MD_ThreadContext tctx;
MD_ThreadInit(&tctx);
arena = MD_ArenaAlloc(1ull << 40);
// TODO(allen): throw more at this.