Add module ID tracking for hot-reloading

- Add stk_module_ids array to track module IDs by filename.
- extract_module_id() extracts module ID from it's filename.
- Enables tracking of file changes of already loaded modules.
This commit is contained in:
2025-11-04 07:39:20 +01:00
parent da4dcee490
commit 3b993458d1
+26
View File
@@ -1,5 +1,6 @@
#include "stk.h"
#include <stdlib.h>
#include <string.h>
void *platform_load_library(const char *path);
void platform_unload_library(void *handle);
@@ -7,6 +8,7 @@ void *platform_get_symbol(void *handle, const char *symbol);
typedef void (*stk_module_func)(void);
static char **stk_module_ids = NULL;
static void **stk_handles = NULL;
static stk_module_func *stk_inits = NULL;
static stk_module_func *stk_shutdowns = NULL;
@@ -15,6 +17,25 @@ size_t module_count = 0;
size_t stk_module_count(void) { return module_count; }
static char *extract_module_id(const char *path)
{
char *id, *dot;
const char *basename = strrchr(path, '/');
if (!basename)
basename = path;
else
basename++;
id = malloc(strlen(basename) + 1);
strcpy(id, basename);
dot = strrchr(id, '.');
if (dot)
*dot = '\0';
return id;
}
int stk_module_load(const char *path)
{
void *handle;
@@ -35,6 +56,7 @@ int stk_module_load(const char *path)
return -2;
}
stk_module_ids[module_count] = extract_module_id(path);
stk_handles[module_count] = handle;
stk_inits[module_count] = init_func;
stk_shutdowns[module_count] = shutdown_func;
@@ -53,6 +75,7 @@ void stk_module_unload(size_t index)
platform_unload_library(stk_handles[index]);
for (i = index; i < module_count - 1; ++i) {
stk_module_ids[i] = stk_module_ids[i + 1];
stk_handles[i] = stk_handles[i + 1];
stk_inits[i] = stk_inits[i + 1];
stk_shutdowns[i] = stk_shutdowns[i + 1];
@@ -63,10 +86,12 @@ void stk_module_unload(size_t index)
void stk_module_free_memory(void)
{
free(stk_module_ids);
free(stk_handles);
free(stk_inits);
free(stk_shutdowns);
stk_module_ids = NULL;
stk_handles = NULL;
stk_inits = NULL;
stk_shutdowns = NULL;
@@ -74,6 +99,7 @@ void stk_module_free_memory(void)
int stk_module_init_memory(size_t capacity)
{
stk_module_ids = malloc(capacity * sizeof(char *));
stk_handles = malloc(capacity * sizeof(void *));
stk_inits = malloc(capacity * sizeof(stk_module_func));
stk_shutdowns = malloc(capacity * sizeof(stk_module_func));