diff --git a/samples/c_code_generation.c b/samples/c_code_generation.c index 454d9c3..25264ce 100644 --- a/samples/c_code_generation.c +++ b/samples/c_code_generation.c @@ -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" diff --git a/samples/node_errors/node_errors.c b/samples/node_errors/node_errors.c index c0153c2..742733e 100644 --- a/samples/node_errors/node_errors.c +++ b/samples/node_errors/node_errors.c @@ -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. diff --git a/samples/old_style_custom_layer.c b/samples/old_style_custom_layer.c index 94bf681..1e710ae 100644 --- a/samples/old_style_custom_layer.c +++ b/samples/old_style_custom_layer.c @@ -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. diff --git a/samples/output_parse/output_parse.c b/samples/output_parse/output_parse.c index 587a266..4e6959e 100644 --- a/samples/output_parse/output_parse.c +++ b/samples/output_parse/output_parse.c @@ -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. diff --git a/samples/parse_check.c b/samples/parse_check.c index 6dcd20f..b342992 100644 --- a/samples/parse_check.c +++ b/samples/parse_check.c @@ -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); diff --git a/samples/static_site_generator/static_site_generator.c b/samples/static_site_generator/static_site_generator.c index 8893c4e..9de4d2f 100644 --- a/samples/static_site_generator/static_site_generator.c +++ b/samples/static_site_generator/static_site_generator.c @@ -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. diff --git a/samples/toy_language/toy_language.c b/samples/toy_language/toy_language.c index 7e8b691..76c610c 100644 --- a/samples/toy_language/toy_language.c +++ b/samples/toy_language/toy_language.c @@ -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 diff --git a/source/md.c b/source/md.c index 7acb786..fc91a8d 100644 --- a/source/md.c +++ b/source/md.c @@ -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); } diff --git a/source/md.h b/source/md.h index 8d9c925..758b040 100644 --- a/source/md.h +++ b/source/md.h @@ -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) diff --git a/tests/cpp_build_test.cpp b/tests/cpp_build_test.cpp index 1fa785b..78c3583 100644 --- a/tests/cpp_build_test.cpp +++ b/tests/cpp_build_test.cpp @@ -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); diff --git a/tests/sanity_tests.c b/tests/sanity_tests.c index 188fc4a..40b07f1 100644 --- a/tests/sanity_tests.c +++ b/tests/sanity_tests.c @@ -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") diff --git a/tests/unicode_test.c b/tests/unicode_test.c index 4886b09..04a7ec9 100644 --- a/tests/unicode_test.c +++ b/tests/unicode_test.c @@ -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.