diff --git a/src/module.c b/src/module.c index 2a3e067..d4b757d 100644 --- a/src/module.c +++ b/src/module.c @@ -61,12 +61,8 @@ void stk_module_unload(size_t index) --module_count; } -void stk_module_unload_all(void) +void stk_module_free_memory(void) { - size_t i; - for (i = module_count; i > 0; --i) - stk_module_unload(i - 1); - free(stk_handles); free(stk_inits); free(stk_shutdowns); @@ -75,3 +71,26 @@ void stk_module_unload_all(void) stk_inits = NULL; stk_shutdowns = NULL; } + +int stk_module_init_memory(size_t capacity) +{ + stk_handles = malloc(capacity * sizeof(void *)); + stk_inits = malloc(capacity * sizeof(stk_module_func)); + stk_shutdowns = malloc(capacity * sizeof(stk_module_func)); + + if (!stk_handles || !stk_inits || !stk_shutdowns) { + stk_module_free_memory(); + return -1; + } + + return 0; +} + +void stk_module_unload_all(void) +{ + size_t i; + for (i = module_count; i > 0; --i) + stk_module_unload(i - 1); + + stk_module_free_memory(); +} diff --git a/src/stk.c b/src/stk.c index 3375d62..449f84c 100644 --- a/src/stk.c +++ b/src/stk.c @@ -14,6 +14,7 @@ size_t stk_module_count(void); int stk_module_load(const char *path); void stk_module_unload(size_t index); void stk_module_unload_all(void); +int stk_module_init_memory(size_t capacity); int stk_init(const char *mod_dir) { @@ -29,6 +30,10 @@ int stk_init(const char *mod_dir) } files = platform_directory_init_scan(stk_mod_dir, &file_count); + + if (file_count > 0 && stk_module_init_memory(file_count) != 0) + return -1; + if (!files) goto scanned; @@ -42,7 +47,7 @@ int stk_init(const char *mod_dir) scanned: watch_handle = platform_directory_watch_start(stk_mod_dir); - stk_log(stdout, "[stk] stk initialized v%s! Loaded %zu from %s", + stk_log(stdout, "[stk] stk v%s initialized! Loaded %zu mods from %s", STK_VERSION_STRING, module_count, stk_mod_dir); return 0; }