refactor: transition return types to uint8_t and improve error reporting

- Update module load and memory functions to use fixed-width uint8_t
- Implement STK_MOD_REALLOC_FAILURE for granular memory error tracking
- Clean up logging prefixes in stk_poll for consistency
- Update error string helper to support new module error codes
This commit is contained in:
2026-01-31 19:56:36 +01:00
parent 69b4907ff2
commit 4bd1f00b5b
4 changed files with 122 additions and 121 deletions
+1
View File
@@ -21,6 +21,7 @@
#define STK_MOD_INIT_FAILURE 1
#define STK_MOD_LIBRARY_LOAD_ERROR 2
#define STK_MOD_SYMBOL_NOT_FOUND_ERROR 3
#define STK_MOD_REALLOC_FAILURE 4
/* Platform return codes */
#define STK_PLATFORM_OPERATION_SUCCESS 0
+9 -9
View File
@@ -72,7 +72,7 @@ int is_mod_loaded(const char *module_name)
return -1;
}
int stk_module_load(const char *path, int index)
uint8_t stk_module_load(const char *path, int index)
{
void *handle;
stk_init_mod_func init_func;
@@ -113,15 +113,15 @@ int stk_module_load(const char *path, int index)
stk_inits[index] = init_func;
stk_shutdowns[index] = shutdown_func;
return 0;
return STK_MOD_INIT_SUCCESS;
}
int stk_module_load_init(const char *path, int index)
uint8_t stk_module_load_init(const char *path, int index)
{
int result;
result = stk_module_load(path, index);
if (result == 0)
if (result == STK_MOD_INIT_SUCCESS)
++module_count;
return result;
@@ -150,7 +150,7 @@ void stk_module_free_memory(void)
stk_shutdowns = NULL;
}
int stk_module_init_memory(size_t capacity)
uint8_t stk_module_init_memory(size_t capacity)
{
stk_module_ids = malloc(capacity * sizeof(*stk_module_ids));
stk_handles = malloc(capacity * sizeof(void *));
@@ -159,13 +159,13 @@ int stk_module_init_memory(size_t capacity)
if (!stk_module_ids || !stk_handles || !stk_inits || !stk_shutdowns) {
stk_module_free_memory();
return -1;
return STK_INIT_MEMORY_ERROR;
}
return 0;
return STK_INIT_SUCCESS;
}
int stk_module_realloc_memory(size_t new_capacity)
uint8_t stk_module_realloc_memory(size_t new_capacity)
{
char (*new_module_ids)[STK_MOD_ID_BUFFER];
void **new_handles;
@@ -204,7 +204,7 @@ int stk_module_realloc_memory(size_t new_capacity)
stk_inits = old_inits;
stk_shutdowns = old_shutdowns;
return -1;
return STK_MOD_REALLOC_FAILURE;
}
stk_module_ids = new_module_ids;
+11 -12
View File
@@ -286,11 +286,10 @@ void *platform_get_symbol(void *h, const char *s)
#endif
}
char (*platform_directory_init_scan(const char *dir_path,
size_t *out_count))[STK_PATH_MAX]
{
char (*platform_directory_init_scan(const char *dir_path, size_t *out_count))
[STK_PATH_MAX] {
size_t count = 0, i = 0;
char(*list)[STK_PATH_MAX] = NULL;
char (*list)[STK_PATH_MAX] = NULL;
#ifdef _WIN32
WIN32_FIND_DATAA fd;
HANDLE h;
@@ -331,9 +330,9 @@ char (*platform_directory_init_scan(const char *dir_path,
FindClose(h);
goto exit;
create_and_exit:
create_and_exit:
platform_mkdir(dir_path);
exit:
exit:
*out_count = i;
return list;
#else
@@ -346,7 +345,7 @@ exit:
if (!d)
goto create_and_exit;
count_loop:
count_loop:
e = readdir(d);
if (!e)
goto count_done;
@@ -361,7 +360,7 @@ count_loop:
count++;
goto count_loop;
count_done:
count_done:
if (count == 0)
goto close_and_exit;
@@ -370,7 +369,7 @@ count_done:
if (!list)
goto close_and_exit;
fill_loop:
fill_loop:
e = readdir(d);
if (!e || i >= count)
goto close_and_exit;
@@ -385,17 +384,17 @@ fill_loop:
strncpy(list[i++], e->d_name, STK_PATH_MAX - 1);
goto fill_loop;
create_and_exit:
create_and_exit:
platform_mkdir(dir_path);
*out_count = 0;
return NULL;
close_and_exit:
close_and_exit:
closedir(d);
*out_count = i;
return list;
#endif
}
}
#if !defined(__linux__) && !defined(_WIN32)
static void update_watches(platform_watch_context_t *ctx)
+28 -27
View File
@@ -30,20 +30,20 @@ void platform_directory_watch_stop(void *handle);
stk_module_event_t *platform_directory_watch_check(
void *handle, char (**file_list)[STK_PATH_MAX], size_t *out_count,
char (*loaded_module_ids)[STK_MOD_ID_BUFFER], const size_t loaded_count);
int platform_mkdir(const char *path);
int platform_copy_file(const char *from, const char *to);
int platform_remove_dir(const char *path);
uint8_t platform_mkdir(const char *path);
uint8_t platform_copy_file(const char *from, const char *to);
uint8_t platform_remove_dir(const char *path);
char *extract_module_id(const char *path, char *out_id);
void extract_module_id(const char *path, char *out_id);
int is_mod_loaded(const char *module_id);
size_t stk_module_count(void);
int stk_module_load(const char *path, int index);
int stk_module_load_init(const char *path, int index);
uint8_t stk_module_load(const char *path, int index);
uint8_t stk_module_load_init(const char *path, int index);
uint8_t stk_module_init_memory(size_t capacity);
uint8_t stk_module_realloc_memory(size_t new_capacity);
void stk_module_unload(size_t index);
void stk_module_unload_all(void);
int stk_module_init_memory(size_t capacity);
int stk_module_realloc_memory(size_t new_capacity);
static void build_path(char *dest, size_t dest_size, const char *dir,
const char *file)
@@ -63,6 +63,8 @@ static const char *stk_error_string(int error_code)
return "symbol not found";
case STK_MOD_INIT_FAILURE:
return "init failure";
case STK_MOD_REALLOC_FAILURE:
return "memory reallocation failed";
default:
return "unknown error";
}
@@ -86,7 +88,7 @@ int stk_init(void)
free(test_scan);
if (!test_scan && test_count == 0) {
stk_log(stderr,
"[stk] FATAL: Cannot create temp directory: %s",
"FATAL: Cannot create temp directory: %s",
stk_tmp_dir);
return STK_INIT_TMPDIR_ERROR;
}
@@ -95,7 +97,7 @@ int stk_init(void)
files = platform_directory_init_scan(stk_mod_dir, &file_count);
if (file_count > 0 && stk_module_init_memory(file_count) != 0) {
stk_log(stderr, "[stk] FATAL: Memory allocation failed");
stk_log(stderr, "FATAL: Memory allocation failed");
return STK_INIT_MEMORY_ERROR;
}
@@ -108,8 +110,7 @@ int stk_init(void)
if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr,
"[stk] Failed to copy %s to temp directory",
stk_log(stderr, "Failed to copy %s to temp directory",
files[i]);
continue;
}
@@ -117,7 +118,7 @@ int stk_init(void)
load_result = stk_module_load_init(tmp_path, successful_loads);
if (load_result != STK_MOD_INIT_SUCCESS) {
stk_log(stderr, "[stk] Failed to load module %s: %s",
stk_log(stderr, "Failed to load module %s: %s",
files[i], stk_error_string(load_result));
} else {
successful_loads++;
@@ -133,14 +134,13 @@ int stk_init(void)
scanned:
watch_handle = platform_directory_watch_start(stk_mod_dir);
if (!watch_handle) {
stk_log(stderr,
"[stk] FATAL: Cannot start directory watch on %s",
stk_log(stderr, "FATAL: Cannot start directory watch on %s",
stk_mod_dir);
stk_module_unload_all();
return STK_INIT_WATCH_ERROR;
}
stk_log(stdout, "[stk] stk v%s initialized! Loaded %lu mod%s from %s/",
stk_log(stdout, "stk v%s initialized! Loaded %lu mod%s from %s/",
STK_VERSION_STRING, module_count, module_count != 1 ? "s" : "",
stk_mod_dir);
@@ -159,13 +159,12 @@ void stk_shutdown(void)
if (platform_remove_dir(stk_tmp_dir) !=
STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr,
"[stk] Warning: failed to remove temp directory %s",
stk_log(stderr, "Warning: failed to remove temp directory %s",
stk_tmp_dir);
}
stk_initialized = 0;
stk_log(stdout, "[stk] stk shutdown");
stk_log(stdout, "stk shutdown");
}
size_t stk_poll(void)
@@ -235,8 +234,9 @@ size_t stk_poll(void)
handle_grow:
remaining_loads = load_count - unload_count;
new_capacity = module_count + remaining_loads;
if (stk_module_realloc_memory(new_capacity) != 0)
if (stk_module_realloc_memory(new_capacity) != STK_MOD_INIT_SUCCESS) {
goto free_poll;
}
begin_operations:
for (i = 0; i < unload_count; ++i)
@@ -256,14 +256,14 @@ begin_operations:
if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr, "[stk] Failed to copy %s for reload",
stk_log(stderr, "Failed to copy %s for reload",
file_list[file_index]);
continue;
}
load_result = stk_module_load(tmp_path, mod_index);
if (load_result != STK_MOD_INIT_SUCCESS) {
stk_log(stderr, "[stk] Failed to reload module %s: %s",
stk_log(stderr, "Failed to reload module %s: %s",
file_list[file_index],
stk_error_string(load_result));
}
@@ -281,14 +281,14 @@ begin_operations:
if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr, "[stk] Failed to copy %s for loading",
stk_log(stderr, "Failed to copy %s for loading",
file_list[file_index]);
continue;
}
load_result = stk_module_load(tmp_path, target_index);
if (load_result != STK_MOD_INIT_SUCCESS) {
stk_log(stderr, "[stk] Failed to load module %s: %s",
stk_log(stderr, "Failed to load module %s: %s",
file_list[file_index],
stk_error_string(load_result));
}
@@ -313,7 +313,7 @@ append_modules:
if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr, "[stk] Failed to copy %s for loading",
stk_log(stderr, "Failed to copy %s for loading",
file_list[file_index]);
continue;
}
@@ -321,7 +321,7 @@ append_modules:
load_result = stk_module_load(tmp_path, module_count +
successful_appends);
if (load_result != STK_MOD_INIT_SUCCESS) {
stk_log(stderr, "[stk] Failed to load module %s: %s",
stk_log(stderr, "Failed to load module %s: %s",
file_list[file_index],
stk_error_string(load_result));
} else {
@@ -381,7 +381,8 @@ void stk_set_mod_dir(const char *path)
strncpy(stk_tmp_dir, stk_mod_dir, STK_PATH_MAX_OS - 1);
stk_tmp_dir[STK_PATH_MAX_OS - 1] = '\0';
strncat(stk_tmp_dir, "/", STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
strncat(stk_tmp_dir, STK_PATH_SEP_STR,
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
strncat(stk_tmp_dir, stk_tmp_name,
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);