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_INIT_FAILURE 1
#define STK_MOD_LIBRARY_LOAD_ERROR 2 #define STK_MOD_LIBRARY_LOAD_ERROR 2
#define STK_MOD_SYMBOL_NOT_FOUND_ERROR 3 #define STK_MOD_SYMBOL_NOT_FOUND_ERROR 3
#define STK_MOD_REALLOC_FAILURE 4
/* Platform return codes */ /* Platform return codes */
#define STK_PLATFORM_OPERATION_SUCCESS 0 #define STK_PLATFORM_OPERATION_SUCCESS 0
+9 -9
View File
@@ -72,7 +72,7 @@ int is_mod_loaded(const char *module_name)
return -1; return -1;
} }
int stk_module_load(const char *path, int index) uint8_t stk_module_load(const char *path, int index)
{ {
void *handle; void *handle;
stk_init_mod_func init_func; 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_inits[index] = init_func;
stk_shutdowns[index] = shutdown_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; int result;
result = stk_module_load(path, index); result = stk_module_load(path, index);
if (result == 0) if (result == STK_MOD_INIT_SUCCESS)
++module_count; ++module_count;
return result; return result;
@@ -150,7 +150,7 @@ void stk_module_free_memory(void)
stk_shutdowns = NULL; 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_module_ids = malloc(capacity * sizeof(*stk_module_ids));
stk_handles = malloc(capacity * sizeof(void *)); 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) { if (!stk_module_ids || !stk_handles || !stk_inits || !stk_shutdowns) {
stk_module_free_memory(); 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]; char (*new_module_ids)[STK_MOD_ID_BUFFER];
void **new_handles; void **new_handles;
@@ -204,7 +204,7 @@ int stk_module_realloc_memory(size_t new_capacity)
stk_inits = old_inits; stk_inits = old_inits;
stk_shutdowns = old_shutdowns; stk_shutdowns = old_shutdowns;
return -1; return STK_MOD_REALLOC_FAILURE;
} }
stk_module_ids = new_module_ids; stk_module_ids = new_module_ids;
+84 -85
View File
@@ -286,116 +286,115 @@ void *platform_get_symbol(void *h, const char *s)
#endif #endif
} }
char (*platform_directory_init_scan(const char *dir_path, char (*platform_directory_init_scan(const char *dir_path, size_t *out_count))
size_t *out_count))[STK_PATH_MAX] [STK_PATH_MAX] {
{ size_t count = 0, i = 0;
size_t count = 0, i = 0; char (*list)[STK_PATH_MAX] = NULL;
char(*list)[STK_PATH_MAX] = NULL;
#ifdef _WIN32 #ifdef _WIN32
WIN32_FIND_DATAA fd; WIN32_FIND_DATAA fd;
HANDLE h; HANDLE h;
char s[STK_PATH_MAX_OS]; char s[STK_PATH_MAX_OS];
sprintf(s, "%s\\*", dir_path); sprintf(s, "%s\\*", dir_path);
h = FindFirstFileA(s, &fd); h = FindFirstFileA(s, &fd);
if (h == INVALID_HANDLE_VALUE) if (h == INVALID_HANDLE_VALUE)
goto create_and_exit; goto create_and_exit;
do { do {
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue; continue;
if (is_valid_module_file(fd.cFileName)) if (is_valid_module_file(fd.cFileName))
count++; count++;
} while (FindNextFileA(h, &fd)); } while (FindNextFileA(h, &fd));
FindClose(h); FindClose(h);
if (count == 0) if (count == 0)
goto exit; goto exit;
list = malloc(count * sizeof(*list)); list = malloc(count * sizeof(*list));
if (!list) if (!list)
goto exit; goto exit;
h = FindFirstFileA(s, &fd); h = FindFirstFileA(s, &fd);
if (h == INVALID_HANDLE_VALUE) if (h == INVALID_HANDLE_VALUE)
goto exit; goto exit;
do { do {
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue; continue;
if (is_valid_module_file(fd.cFileName) && i < count) if (is_valid_module_file(fd.cFileName) && i < count)
strncpy(list[i++], fd.cFileName, STK_PATH_MAX - 1); strncpy(list[i++], fd.cFileName, STK_PATH_MAX - 1);
} while (FindNextFileA(h, &fd)); } while (FindNextFileA(h, &fd));
FindClose(h); FindClose(h);
goto exit; goto exit;
create_and_exit: create_and_exit:
platform_mkdir(dir_path); platform_mkdir(dir_path);
exit: exit:
*out_count = i; *out_count = i;
return list; return list;
#else #else
DIR *d; DIR *d;
struct dirent *e; struct dirent *e;
struct stat st; struct stat st;
char f[STK_PATH_MAX_OS]; char f[STK_PATH_MAX_OS];
d = opendir(dir_path); d = opendir(dir_path);
if (!d) if (!d)
goto create_and_exit; goto create_and_exit;
count_loop: count_loop:
e = readdir(d); e = readdir(d);
if (!e) if (!e)
goto count_done; goto count_done;
sprintf(f, "%s/%s", dir_path, e->d_name); sprintf(f, "%s/%s", dir_path, e->d_name);
if (!is_valid_module_file(e->d_name)) if (!is_valid_module_file(e->d_name))
goto count_loop; goto count_loop;
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
goto count_loop; goto count_loop;
count++; count++;
goto count_loop; goto count_loop;
count_done: count_done:
if (count == 0) if (count == 0)
goto close_and_exit; goto close_and_exit;
rewinddir(d); rewinddir(d);
list = malloc(count * sizeof(*list)); list = malloc(count * sizeof(*list));
if (!list) if (!list)
goto close_and_exit; goto close_and_exit;
fill_loop: fill_loop:
e = readdir(d); e = readdir(d);
if (!e || i >= count) if (!e || i >= count)
goto close_and_exit; goto close_and_exit;
sprintf(f, "%s/%s", dir_path, e->d_name); sprintf(f, "%s/%s", dir_path, e->d_name);
if (!is_valid_module_file(e->d_name)) if (!is_valid_module_file(e->d_name))
goto fill_loop; goto fill_loop;
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode)) if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
goto fill_loop; goto fill_loop;
strncpy(list[i++], e->d_name, STK_PATH_MAX - 1); strncpy(list[i++], e->d_name, STK_PATH_MAX - 1);
goto fill_loop; goto fill_loop;
create_and_exit: create_and_exit:
platform_mkdir(dir_path); platform_mkdir(dir_path);
*out_count = 0; *out_count = 0;
return NULL; return NULL;
close_and_exit: close_and_exit:
closedir(d); closedir(d);
*out_count = i; *out_count = i;
return list; return list;
#endif #endif
} }
#if !defined(__linux__) && !defined(_WIN32) #if !defined(__linux__) && !defined(_WIN32)
static void update_watches(platform_watch_context_t *ctx) 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( stk_module_event_t *platform_directory_watch_check(
void *handle, char (**file_list)[STK_PATH_MAX], size_t *out_count, 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); char (*loaded_module_ids)[STK_MOD_ID_BUFFER], const size_t loaded_count);
int platform_mkdir(const char *path); uint8_t platform_mkdir(const char *path);
int platform_copy_file(const char *from, const char *to); uint8_t platform_copy_file(const char *from, const char *to);
int platform_remove_dir(const char *path); 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); int is_mod_loaded(const char *module_id);
size_t stk_module_count(void); size_t stk_module_count(void);
int stk_module_load(const char *path, int index); uint8_t stk_module_load(const char *path, int index);
int stk_module_load_init(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(size_t index);
void stk_module_unload_all(void); 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, static void build_path(char *dest, size_t dest_size, const char *dir,
const char *file) const char *file)
@@ -63,6 +63,8 @@ static const char *stk_error_string(int error_code)
return "symbol not found"; return "symbol not found";
case STK_MOD_INIT_FAILURE: case STK_MOD_INIT_FAILURE:
return "init failure"; return "init failure";
case STK_MOD_REALLOC_FAILURE:
return "memory reallocation failed";
default: default:
return "unknown error"; return "unknown error";
} }
@@ -86,7 +88,7 @@ int stk_init(void)
free(test_scan); free(test_scan);
if (!test_scan && test_count == 0) { if (!test_scan && test_count == 0) {
stk_log(stderr, stk_log(stderr,
"[stk] FATAL: Cannot create temp directory: %s", "FATAL: Cannot create temp directory: %s",
stk_tmp_dir); stk_tmp_dir);
return STK_INIT_TMPDIR_ERROR; return STK_INIT_TMPDIR_ERROR;
} }
@@ -95,7 +97,7 @@ int stk_init(void)
files = platform_directory_init_scan(stk_mod_dir, &file_count); files = platform_directory_init_scan(stk_mod_dir, &file_count);
if (file_count > 0 && stk_module_init_memory(file_count) != 0) { 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; return STK_INIT_MEMORY_ERROR;
} }
@@ -108,8 +110,7 @@ int stk_init(void)
if (platform_copy_file(full_path, tmp_path) != if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) { STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr, stk_log(stderr, "Failed to copy %s to temp directory",
"[stk] Failed to copy %s to temp directory",
files[i]); files[i]);
continue; continue;
} }
@@ -117,7 +118,7 @@ int stk_init(void)
load_result = stk_module_load_init(tmp_path, successful_loads); load_result = stk_module_load_init(tmp_path, successful_loads);
if (load_result != STK_MOD_INIT_SUCCESS) { 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)); files[i], stk_error_string(load_result));
} else { } else {
successful_loads++; successful_loads++;
@@ -133,14 +134,13 @@ int stk_init(void)
scanned: scanned:
watch_handle = platform_directory_watch_start(stk_mod_dir); watch_handle = platform_directory_watch_start(stk_mod_dir);
if (!watch_handle) { if (!watch_handle) {
stk_log(stderr, stk_log(stderr, "FATAL: Cannot start directory watch on %s",
"[stk] FATAL: Cannot start directory watch on %s",
stk_mod_dir); stk_mod_dir);
stk_module_unload_all(); stk_module_unload_all();
return STK_INIT_WATCH_ERROR; 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_VERSION_STRING, module_count, module_count != 1 ? "s" : "",
stk_mod_dir); stk_mod_dir);
@@ -159,13 +159,12 @@ void stk_shutdown(void)
if (platform_remove_dir(stk_tmp_dir) != if (platform_remove_dir(stk_tmp_dir) !=
STK_PLATFORM_OPERATION_SUCCESS) { STK_PLATFORM_OPERATION_SUCCESS) {
stk_log(stderr, stk_log(stderr, "Warning: failed to remove temp directory %s",
"[stk] Warning: failed to remove temp directory %s",
stk_tmp_dir); stk_tmp_dir);
} }
stk_initialized = 0; stk_initialized = 0;
stk_log(stdout, "[stk] stk shutdown"); stk_log(stdout, "stk shutdown");
} }
size_t stk_poll(void) size_t stk_poll(void)
@@ -235,8 +234,9 @@ size_t stk_poll(void)
handle_grow: handle_grow:
remaining_loads = load_count - unload_count; remaining_loads = load_count - unload_count;
new_capacity = module_count + remaining_loads; 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; goto free_poll;
}
begin_operations: begin_operations:
for (i = 0; i < unload_count; ++i) for (i = 0; i < unload_count; ++i)
@@ -256,14 +256,14 @@ begin_operations:
if (platform_copy_file(full_path, tmp_path) != if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) { 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]); file_list[file_index]);
continue; continue;
} }
load_result = stk_module_load(tmp_path, mod_index); load_result = stk_module_load(tmp_path, mod_index);
if (load_result != STK_MOD_INIT_SUCCESS) { 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], file_list[file_index],
stk_error_string(load_result)); stk_error_string(load_result));
} }
@@ -281,14 +281,14 @@ begin_operations:
if (platform_copy_file(full_path, tmp_path) != if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) { 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]); file_list[file_index]);
continue; continue;
} }
load_result = stk_module_load(tmp_path, target_index); load_result = stk_module_load(tmp_path, target_index);
if (load_result != STK_MOD_INIT_SUCCESS) { 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], file_list[file_index],
stk_error_string(load_result)); stk_error_string(load_result));
} }
@@ -313,7 +313,7 @@ append_modules:
if (platform_copy_file(full_path, tmp_path) != if (platform_copy_file(full_path, tmp_path) !=
STK_PLATFORM_OPERATION_SUCCESS) { 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]); file_list[file_index]);
continue; continue;
} }
@@ -321,7 +321,7 @@ append_modules:
load_result = stk_module_load(tmp_path, module_count + load_result = stk_module_load(tmp_path, module_count +
successful_appends); successful_appends);
if (load_result != STK_MOD_INIT_SUCCESS) { 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], file_list[file_index],
stk_error_string(load_result)); stk_error_string(load_result));
} else { } 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); strncpy(stk_tmp_dir, stk_mod_dir, STK_PATH_MAX_OS - 1);
stk_tmp_dir[STK_PATH_MAX_OS - 1] = '\0'; 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, strncat(stk_tmp_dir, stk_tmp_name,
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1); STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);