From 4bd1f00b5b94a7e6b076cc2af6c37a58145ae40a Mon Sep 17 00:00:00 2001 From: anth64 Date: Sat, 31 Jan 2026 19:56:36 +0100 Subject: [PATCH] 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 --- include/stk.h | 1 + src/module.c | 18 +++--- src/platform.c | 169 ++++++++++++++++++++++++------------------------- src/stk.c | 55 ++++++++-------- 4 files changed, 122 insertions(+), 121 deletions(-) diff --git a/include/stk.h b/include/stk.h index 1103158..3c367d9 100644 --- a/include/stk.h +++ b/include/stk.h @@ -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 diff --git a/src/module.c b/src/module.c index 1d6f964..f4f3d32 100644 --- a/src/module.c +++ b/src/module.c @@ -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; diff --git a/src/platform.c b/src/platform.c index af1250a..1914ed9 100644 --- a/src/platform.c +++ b/src/platform.c @@ -286,116 +286,115 @@ 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] -{ - size_t count = 0, i = 0; - char(*list)[STK_PATH_MAX] = NULL; +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; #ifdef _WIN32 - WIN32_FIND_DATAA fd; - HANDLE h; - char s[STK_PATH_MAX_OS]; + WIN32_FIND_DATAA fd; + HANDLE h; + char s[STK_PATH_MAX_OS]; - sprintf(s, "%s\\*", dir_path); - h = FindFirstFileA(s, &fd); - if (h == INVALID_HANDLE_VALUE) - goto create_and_exit; + sprintf(s, "%s\\*", dir_path); + h = FindFirstFileA(s, &fd); + if (h == INVALID_HANDLE_VALUE) + goto create_and_exit; - do { - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - continue; - if (is_valid_module_file(fd.cFileName)) - count++; - } while (FindNextFileA(h, &fd)); + do { + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + continue; + if (is_valid_module_file(fd.cFileName)) + count++; + } while (FindNextFileA(h, &fd)); - FindClose(h); + FindClose(h); - if (count == 0) - goto exit; + if (count == 0) + goto exit; - list = malloc(count * sizeof(*list)); - if (!list) - goto exit; + list = malloc(count * sizeof(*list)); + if (!list) + goto exit; - h = FindFirstFileA(s, &fd); - if (h == INVALID_HANDLE_VALUE) - goto exit; + h = FindFirstFileA(s, &fd); + if (h == INVALID_HANDLE_VALUE) + goto exit; - do { - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - continue; - if (is_valid_module_file(fd.cFileName) && i < count) - strncpy(list[i++], fd.cFileName, STK_PATH_MAX - 1); - } while (FindNextFileA(h, &fd)); + do { + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + continue; + if (is_valid_module_file(fd.cFileName) && i < count) + strncpy(list[i++], fd.cFileName, STK_PATH_MAX - 1); + } while (FindNextFileA(h, &fd)); - FindClose(h); - goto exit; + FindClose(h); + goto exit; -create_and_exit: - platform_mkdir(dir_path); -exit: - *out_count = i; - return list; + create_and_exit: + platform_mkdir(dir_path); + exit: + *out_count = i; + return list; #else - DIR *d; - struct dirent *e; - struct stat st; - char f[STK_PATH_MAX_OS]; + DIR *d; + struct dirent *e; + struct stat st; + char f[STK_PATH_MAX_OS]; - d = opendir(dir_path); - if (!d) - goto create_and_exit; + d = opendir(dir_path); + if (!d) + goto create_and_exit; -count_loop: - e = readdir(d); - if (!e) - goto count_done; + count_loop: + e = readdir(d); + if (!e) + goto count_done; - sprintf(f, "%s/%s", dir_path, e->d_name); - if (!is_valid_module_file(e->d_name)) - goto count_loop; + sprintf(f, "%s/%s", dir_path, e->d_name); + if (!is_valid_module_file(e->d_name)) + goto count_loop; - if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) - goto count_loop; + if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) + goto count_loop; - count++; - goto count_loop; + count++; + goto count_loop; -count_done: - if (count == 0) - goto close_and_exit; + count_done: + if (count == 0) + goto close_and_exit; - rewinddir(d); - list = malloc(count * sizeof(*list)); - if (!list) - goto close_and_exit; + rewinddir(d); + list = malloc(count * sizeof(*list)); + if (!list) + goto close_and_exit; -fill_loop: - e = readdir(d); - if (!e || i >= count) - goto close_and_exit; + fill_loop: + e = readdir(d); + if (!e || i >= count) + goto close_and_exit; - sprintf(f, "%s/%s", dir_path, e->d_name); - if (!is_valid_module_file(e->d_name)) - goto fill_loop; + sprintf(f, "%s/%s", dir_path, e->d_name); + if (!is_valid_module_file(e->d_name)) + goto fill_loop; - if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) - goto fill_loop; + if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) + goto fill_loop; - strncpy(list[i++], e->d_name, STK_PATH_MAX - 1); - goto fill_loop; + strncpy(list[i++], e->d_name, STK_PATH_MAX - 1); + goto fill_loop; -create_and_exit: - platform_mkdir(dir_path); - *out_count = 0; - return NULL; + create_and_exit: + platform_mkdir(dir_path); + *out_count = 0; + return NULL; -close_and_exit: - closedir(d); - *out_count = i; - return list; + 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) diff --git a/src/stk.c b/src/stk.c index 50bb3f1..6f98588 100644 --- a/src/stk.c +++ b/src/stk.c @@ -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);