Module discovery working for real this time

- Tested on Linux
- Allocate module memory before loading anything (seg fault fixed)
This commit is contained in:
2025-11-02 19:41:56 +01:00
parent e878819b02
commit 35cc5afa9d
2 changed files with 30 additions and 6 deletions
+24 -5
View File
@@ -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();
}
+6 -1
View File
@@ -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;
}