diff --git a/bin/run_examples.sh b/bin/run_examples.sh index 5532d48..22f2f8a 100755 --- a/bin/run_examples.sh +++ b/bin/run_examples.sh @@ -9,21 +9,14 @@ build_path=$root_path/build examps=$root_path/examples # Setup a big list of files for a few of the examples -big_list= $examps/intro/hello_world.mdesk -bit_list=$big_list $examps/intro/labels.mdesk -bit_list=$big_list $examps/intro/sets.mdesk -bit_list=$big_list $examps/type_metadata/types.mdesk -bit_list=$big_list $examps/type_metadata/bad_types.mdesk -bit_list=$big_list $examps/expr/expr_intro.mdesk -bit_list=$big_list $examps/expr/expr_c_like.mdesk - -echo ~~~ Running Memory Management Example ~~~ -$build_path/memory_management.exe $big_list -echo - -echo ~~~ Running Multi-Threaded Parse Example ~~~ -$build_path/multi_threaded.exe $big_list -echo +big_list=() +big_list+=("$examps/intro/hello_world.mdesk") +big_list+=("$examps/intro/labels.mdesk") +big_list+=("$examps/intro/sets.mdesk") +big_list+=("$examps/type_metadata/types.mdesk") +big_list+=("$examps/type_metadata/bad_types.mdesk") +big_list+=("$examps/expr/expr_intro.mdesk") +big_list+=("$examps/expr/expr_c_like.mdesk") echo ~~~ Running Type Metadata Generator Example ~~~ cd $examps/type_metadata/generated @@ -42,5 +35,13 @@ echo ~~~ Running C Like Expression ~~~ $build_path/expr_c_like.exe $examps/expr/expr_c_like.mdesk echo +echo ~~~ Running Multi-Threaded Parse Example ~~~ +$build_path/multi_threaded.exe ${big_list[@]} +echo + +echo ~~~ Running Memory Management Example ~~~ +$build_path/memory_management.exe ${big_list[@]} +echo + ###### Restore Path ########################################################### cd $og_path diff --git a/examples/integration/memory_management.c b/examples/integration/memory_management.c index 916be1c..e097438 100644 --- a/examples/integration/memory_management.c +++ b/examples/integration/memory_management.c @@ -9,10 +9,95 @@ #include "md.h" #include "md.c" +//~ pretend config file /////////////////////////////////////////////////////// + +typedef struct ConfigFile{ + MD_Arena *arena; + MD_String8 file_name; + MD_String8 contents; + MD_Node *root; + MD_MessageList errors; +} ConfigFile; + +ConfigFile* +new_config_file_from_file_name(char *file_name_cstr) +{ + MD_Arena *arena = MD_ArenaAlloc(); + ConfigFile *result = MD_PushArrayZero(arena, ConfigFile, 1); + + MD_String8 file_name = MD_S8Copy(arena, MD_S8CString(file_name_cstr)); + MD_String8 contents = MD_LoadEntireFile(arena, file_name); + MD_ParseResult parse = MD_ParseWholeString(arena, file_name, contents); + + result->arena = arena; + result->file_name = file_name; + result->contents = contents; + result->root = parse.node; + result->errors = parse.errors; + return(result); +} + +void +release_config_file(ConfigFile *file) +{ + if (file != 0) + { + MD_ArenaRelease(file->arena); + } +} + +//~ just to simulate new config files coming from somewhere /////////////////// + +int in_files_count = 0; +char** in_file_names = 0; +int in_file_iter = 0; + +ConfigFile* +new_config_file(void) +{ + ConfigFile *result = new_config_file_from_file_name(in_file_names[in_file_iter]); + in_file_iter = (in_file_iter + 1)%in_files_count; + return(result); +} + //~ main ////////////////////////////////////////////////////////////////////// int main(int argc, char **argv) { - // TODO(allen) + // make sure we have something to parse + if (argc <= 1) + { + fprintf(stderr, "pass at least one input file"); + exit(1); + } + + // setup the the source of files + in_files_count = argc - 1; + in_file_names = argv + 1; + + + // pretend there are unpredictable lifetimes tied to real-time events + { + // first we get three config files + ConfigFile *files[3]; + files[0] = new_config_file(); + files[1] = new_config_file(); + files[2] = new_config_file(); + + // then we chaotically replace the slots for a while + for (MD_u32 i = 10000; i < 20000; i += 1) + { + MD_u32 x = (i >> (i&3)) ^ (i << (16 + (i&3))); + MD_u32 slot_index = x%3; + + release_config_file(files[slot_index]); + files[slot_index] = new_config_file(); + } + + // then we're done with all the config files + release_config_file(files[0]); + release_config_file(files[1]); + release_config_file(files[2]); + } } diff --git a/examples/integration/multi_threaded.c b/examples/integration/multi_threaded.c index e78471e..5962383 100644 --- a/examples/integration/multi_threaded.c +++ b/examples/integration/multi_threaded.c @@ -75,21 +75,6 @@ parse_worker_win32(LPVOID parameter) int main(int argc, char **argv) { -#if 1 - char *argv_dummy[8] = { - 0, - "W:\\metadesk\\examples\\intro\\hello_world.mdesk", - "W:\\metadesk\\examples\\intro\\labels.mdesk", - "W:\\metadesk\\examples\\intro\\sets.mdesk", - "W:\\metadesk\\examples\\type_metadata\\types.mdesk", - "W:\\metadesk\\examples\\type_metadata\\bad_types.mdesk", - "W:\\metadesk\\examples\\expr\\expr_intro.mdesk", - "W:\\metadesk\\examples\\expr\\expr_c_like.mdesk", - }; - argc = 8; - argv = argv_dummy; -#endif - // make sure we have something to parse if (argc <= 1) {