refactor(module.c, stk.c): replace parallel arrays with stk_mod_t struct
- Consolidate handles, function pointers, metadata, and deps into single stk_mod_t - Remove all separate metadata arrays and index mappings - Simplify init, free, realloc, load, and unload significantly - stk.c externs reduced to stk_modules and module_count
This commit is contained in:
+136
-390
@@ -13,40 +13,33 @@ typedef struct {
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
} stk_dep_t;
|
||||
|
||||
typedef struct {
|
||||
void *handle;
|
||||
stk_init_mod_func init;
|
||||
stk_shutdown_mod_func shutdown;
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char name[STK_MOD_NAME_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
char desc[STK_MOD_DESC_BUFFER];
|
||||
stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
} stk_mod_t;
|
||||
|
||||
void *platform_load_library(const char *path);
|
||||
void platform_unload_library(void *handle);
|
||||
void *platform_get_symbol(void *handle, const char *symbol);
|
||||
|
||||
char (*stk_module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
void **stk_handles = NULL;
|
||||
stk_init_mod_func *stk_inits = NULL;
|
||||
stk_shutdown_mod_func *stk_shutdowns = NULL;
|
||||
|
||||
char (*stk_meta_names)[STK_MOD_NAME_BUFFER] = NULL;
|
||||
size_t *stk_meta_name_indices = NULL;
|
||||
size_t stk_meta_name_count = 0;
|
||||
|
||||
char (*stk_meta_versions)[STK_MOD_VERSION_BUFFER] = NULL;
|
||||
size_t *stk_meta_version_indices = NULL;
|
||||
size_t stk_meta_version_count = 0;
|
||||
|
||||
char (*stk_meta_descs)[STK_MOD_DESC_BUFFER] = NULL;
|
||||
size_t *stk_meta_desc_indices = NULL;
|
||||
size_t stk_meta_desc_count = 0;
|
||||
|
||||
stk_dep_t **stk_deps = NULL;
|
||||
size_t *stk_dep_mod_indices = NULL;
|
||||
size_t *stk_dep_counts = NULL;
|
||||
size_t stk_dep_mod_count = 0;
|
||||
stk_mod_t *stk_modules = NULL;
|
||||
|
||||
extern unsigned char stk_flags;
|
||||
|
||||
static char stk_mod_init_name[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_init";
|
||||
static char stk_mod_shutdown_name[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_shutdown";
|
||||
static char stk_mod_name_fn[STK_MOD_NAME_BUFFER] = "stk_mod_name";
|
||||
static char stk_mod_version_fn[STK_MOD_VERSION_BUFFER] = "stk_mod_version";
|
||||
static char stk_mod_description_fn[STK_MOD_DESC_BUFFER] = "stk_mod_description";
|
||||
static char stk_mod_name_fn[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_name";
|
||||
static char stk_mod_version_fn[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_version";
|
||||
static char stk_mod_description_fn[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_description";
|
||||
static char stk_mod_dependencies_fn[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_dependencies";
|
||||
|
||||
@@ -91,7 +84,7 @@ int is_mod_loaded(const char *module_name)
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < module_count; i++)
|
||||
if (strncmp(stk_module_ids[i], module_name,
|
||||
if (strncmp(stk_modules[i].id, module_name,
|
||||
STK_MOD_ID_BUFFER) == 0)
|
||||
return i;
|
||||
|
||||
@@ -101,131 +94,124 @@ int is_mod_loaded(const char *module_name)
|
||||
unsigned char stk_module_load(const char *path, int index)
|
||||
{
|
||||
void *handle;
|
||||
stk_init_mod_func init_func;
|
||||
stk_shutdown_mod_func shutdown_func;
|
||||
char module_id[STK_MOD_ID_BUFFER];
|
||||
size_t len;
|
||||
union {
|
||||
void *obj;
|
||||
stk_init_mod_func init_func;
|
||||
stk_shutdown_mod_func shutdown_func;
|
||||
const char *(*meta_func)(void);
|
||||
const char *(*(*deps_func)(void))[2];
|
||||
} u;
|
||||
size_t len;
|
||||
const char *meta_str;
|
||||
|
||||
char (*new_meta_names)[STK_MOD_NAME_BUFFER] = NULL;
|
||||
size_t *new_meta_name_indices = NULL;
|
||||
char (*new_meta_versions)[STK_MOD_VERSION_BUFFER] = NULL;
|
||||
size_t *new_meta_version_indices = NULL;
|
||||
char (*new_meta_descs)[STK_MOD_DESC_BUFFER] = NULL;
|
||||
size_t *new_meta_desc_indices = NULL;
|
||||
const char *(*deps)[2];
|
||||
size_t dep_count;
|
||||
stk_dep_t *dep_arr;
|
||||
|
||||
handle = platform_load_library(path);
|
||||
if (!handle)
|
||||
return STK_MOD_LIBRARY_LOAD_ERROR;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_init_name);
|
||||
init_func = u.init_func;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_shutdown_name);
|
||||
shutdown_func = u.shutdown_func;
|
||||
|
||||
if (!init_func || !shutdown_func) {
|
||||
if (!u.obj) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_SYMBOL_NOT_FOUND_ERROR;
|
||||
}
|
||||
stk_modules[index].init = u.init_func;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_shutdown_name);
|
||||
if (!u.obj) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_SYMBOL_NOT_FOUND_ERROR;
|
||||
}
|
||||
stk_modules[index].shutdown = u.shutdown_func;
|
||||
|
||||
extract_module_id(path, module_id);
|
||||
|
||||
if (init_func() != STK_MOD_INIT_SUCCESS) {
|
||||
if (stk_modules[index].init() != STK_MOD_INIT_SUCCESS) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_INIT_FAILURE;
|
||||
}
|
||||
|
||||
stk_modules[index].handle = handle;
|
||||
|
||||
len = strlen(module_id);
|
||||
if (len >= STK_MOD_ID_BUFFER)
|
||||
len = STK_MOD_ID_BUFFER - 1;
|
||||
memcpy(stk_modules[index].id, module_id, len);
|
||||
stk_modules[index].id[len] = '\0';
|
||||
|
||||
memcpy(stk_module_ids[index], module_id, len);
|
||||
stk_module_ids[index][len] = '\0';
|
||||
|
||||
stk_handles[index] = handle;
|
||||
stk_inits[index] = init_func;
|
||||
stk_shutdowns[index] = shutdown_func;
|
||||
|
||||
stk_modules[index].name[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_name_fn);
|
||||
if (!u.obj)
|
||||
goto skip_name;
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (!meta_str)
|
||||
goto skip_name;
|
||||
|
||||
new_meta_names = realloc(stk_meta_names, (stk_meta_name_count + 1) *
|
||||
sizeof(*stk_meta_names));
|
||||
new_meta_name_indices = realloc(
|
||||
stk_meta_name_indices, (stk_meta_name_count + 1) * sizeof(size_t));
|
||||
if (!new_meta_names || !new_meta_name_indices)
|
||||
goto skip_name;
|
||||
|
||||
stk_meta_names = new_meta_names;
|
||||
stk_meta_name_indices = new_meta_name_indices;
|
||||
strncpy(stk_meta_names[stk_meta_name_count], meta_str,
|
||||
if (meta_str) {
|
||||
strncpy(stk_modules[index].name, meta_str,
|
||||
STK_MOD_NAME_BUFFER - 1);
|
||||
stk_meta_names[stk_meta_name_count][STK_MOD_NAME_BUFFER - 1] = '\0';
|
||||
stk_meta_name_indices[stk_meta_name_count] = (size_t)index;
|
||||
stk_meta_name_count++;
|
||||
stk_modules[index].name[STK_MOD_NAME_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
skip_name:
|
||||
stk_modules[index].version[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_version_fn);
|
||||
if (!u.obj)
|
||||
goto skip_version;
|
||||
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (!meta_str)
|
||||
goto skip_version;
|
||||
|
||||
new_meta_versions =
|
||||
realloc(stk_meta_versions,
|
||||
(stk_meta_version_count + 1) * sizeof(*stk_meta_versions));
|
||||
new_meta_version_indices =
|
||||
realloc(stk_meta_version_indices,
|
||||
(stk_meta_version_count + 1) * sizeof(size_t));
|
||||
if (!new_meta_versions || !new_meta_version_indices)
|
||||
goto skip_version;
|
||||
|
||||
stk_meta_versions = new_meta_versions;
|
||||
stk_meta_version_indices = new_meta_version_indices;
|
||||
strncpy(stk_meta_versions[stk_meta_version_count], meta_str,
|
||||
if (meta_str) {
|
||||
strncpy(stk_modules[index].version, meta_str,
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
stk_meta_versions[stk_meta_version_count][STK_MOD_VERSION_BUFFER - 1] =
|
||||
stk_modules[index].version[STK_MOD_VERSION_BUFFER - 1] =
|
||||
'\0';
|
||||
stk_meta_version_indices[stk_meta_version_count] = (size_t)index;
|
||||
stk_meta_version_count++;
|
||||
}
|
||||
}
|
||||
|
||||
skip_version:
|
||||
stk_modules[index].desc[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_description_fn);
|
||||
if (!u.obj)
|
||||
goto skip_description;
|
||||
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (!meta_str)
|
||||
goto skip_description;
|
||||
|
||||
new_meta_descs = realloc(stk_meta_descs, (stk_meta_desc_count + 1) *
|
||||
sizeof(*stk_meta_descs));
|
||||
new_meta_desc_indices = realloc(
|
||||
stk_meta_desc_indices, (stk_meta_desc_count + 1) * sizeof(size_t));
|
||||
if (!new_meta_descs || !new_meta_desc_indices)
|
||||
goto skip_description;
|
||||
stk_meta_descs = new_meta_descs;
|
||||
stk_meta_desc_indices = new_meta_desc_indices;
|
||||
strncpy(stk_meta_descs[stk_meta_desc_count], meta_str,
|
||||
if (meta_str) {
|
||||
strncpy(stk_modules[index].desc, meta_str,
|
||||
STK_MOD_DESC_BUFFER - 1);
|
||||
stk_meta_descs[stk_meta_desc_count][STK_MOD_DESC_BUFFER - 1] = '\0';
|
||||
stk_meta_desc_indices[stk_meta_desc_count] = (size_t)index;
|
||||
stk_meta_desc_count++;
|
||||
stk_modules[index].desc[STK_MOD_DESC_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
skip_description:
|
||||
stk_modules[index].deps = NULL;
|
||||
stk_modules[index].dep_count = 0;
|
||||
u.obj = platform_get_symbol(handle, stk_mod_dependencies_fn);
|
||||
if (!u.obj)
|
||||
goto skip_deps;
|
||||
|
||||
deps = u.deps_func();
|
||||
if (!deps)
|
||||
goto skip_deps;
|
||||
|
||||
dep_count = 0;
|
||||
while (deps[dep_count][0] != NULL)
|
||||
dep_count++;
|
||||
|
||||
if (dep_count == 0)
|
||||
goto skip_deps;
|
||||
|
||||
dep_arr = malloc(dep_count * sizeof(stk_dep_t));
|
||||
if (!dep_arr)
|
||||
goto skip_deps;
|
||||
|
||||
{
|
||||
size_t d;
|
||||
for (d = 0; d < dep_count; d++) {
|
||||
strncpy(dep_arr[d].id, deps[d][0],
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
dep_arr[d].id[STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
strncpy(dep_arr[d].version, deps[d][1],
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
dep_arr[d].version[STK_MOD_VERSION_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
stk_modules[index].deps = dep_arr;
|
||||
stk_modules[index].dep_count = dep_count;
|
||||
|
||||
skip_deps:
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -242,181 +228,50 @@ unsigned char stk_module_load_init(const char *path, int index)
|
||||
|
||||
void stk_module_unload(size_t index)
|
||||
{
|
||||
size_t i;
|
||||
char (*new_meta_names)[STK_MOD_NAME_BUFFER] = NULL;
|
||||
size_t *new_meta_name_indices = NULL;
|
||||
char (*new_meta_versions)[STK_MOD_VERSION_BUFFER] = NULL;
|
||||
size_t *new_meta_version_indices = NULL;
|
||||
char (*new_meta_descs)[STK_MOD_DESC_BUFFER] = NULL;
|
||||
size_t *new_meta_desc_indices = NULL;
|
||||
size_t new_count;
|
||||
stk_modules[index].shutdown();
|
||||
platform_unload_library(stk_modules[index].handle);
|
||||
|
||||
stk_shutdowns[index]();
|
||||
platform_unload_library(stk_handles[index]);
|
||||
stk_handles[index] = NULL;
|
||||
stk_inits[index] = NULL;
|
||||
stk_shutdowns[index] = NULL;
|
||||
stk_module_ids[index][0] = '\0';
|
||||
stk_modules[index].handle = NULL;
|
||||
stk_modules[index].init = NULL;
|
||||
stk_modules[index].shutdown = NULL;
|
||||
stk_modules[index].id[0] = '\0';
|
||||
stk_modules[index].name[0] = '\0';
|
||||
stk_modules[index].version[0] = '\0';
|
||||
stk_modules[index].desc[0] = '\0';
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_name_count; i++)
|
||||
if (stk_meta_name_indices[i] != index)
|
||||
new_count++;
|
||||
|
||||
if (new_count == 0)
|
||||
goto clear_names;
|
||||
|
||||
new_meta_names = malloc(new_count * sizeof(*new_meta_names));
|
||||
new_meta_name_indices = malloc(new_count * sizeof(size_t));
|
||||
if (!new_meta_names || !new_meta_name_indices)
|
||||
goto clear_names;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_name_count; i++) {
|
||||
if (stk_meta_name_indices[i] == index)
|
||||
continue;
|
||||
memcpy(new_meta_names[new_count], stk_meta_names[i],
|
||||
STK_MOD_NAME_BUFFER);
|
||||
new_meta_name_indices[new_count] = stk_meta_name_indices[i];
|
||||
new_count++;
|
||||
if (stk_modules[index].deps) {
|
||||
free(stk_modules[index].deps);
|
||||
stk_modules[index].deps = NULL;
|
||||
}
|
||||
|
||||
clear_names:
|
||||
free(stk_meta_names);
|
||||
free(stk_meta_name_indices);
|
||||
stk_meta_names = new_meta_names;
|
||||
stk_meta_name_indices = new_meta_name_indices;
|
||||
stk_meta_name_count = new_count;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_version_count; i++)
|
||||
if (stk_meta_version_indices[i] != index)
|
||||
new_count++;
|
||||
|
||||
if (new_count == 0)
|
||||
goto clear_versions;
|
||||
|
||||
new_meta_versions = malloc(new_count * sizeof(*new_meta_versions));
|
||||
new_meta_version_indices = malloc(new_count * sizeof(size_t));
|
||||
if (!new_meta_versions || !new_meta_version_indices)
|
||||
goto clear_versions;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_version_count; i++) {
|
||||
if (stk_meta_version_indices[i] == index)
|
||||
continue;
|
||||
memcpy(new_meta_versions[new_count], stk_meta_versions[i],
|
||||
STK_MOD_VERSION_BUFFER);
|
||||
new_meta_version_indices[new_count] =
|
||||
stk_meta_version_indices[i];
|
||||
new_count++;
|
||||
}
|
||||
|
||||
clear_versions:
|
||||
free(stk_meta_versions);
|
||||
free(stk_meta_version_indices);
|
||||
stk_meta_versions = new_meta_versions;
|
||||
stk_meta_version_indices = new_meta_version_indices;
|
||||
stk_meta_version_count = new_count;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_desc_count; i++)
|
||||
if (stk_meta_desc_indices[i] != index)
|
||||
new_count++;
|
||||
|
||||
if (new_count == 0)
|
||||
goto clear_descs;
|
||||
|
||||
new_meta_descs = malloc(new_count * sizeof(*new_meta_descs));
|
||||
new_meta_desc_indices = malloc(new_count * sizeof(size_t));
|
||||
if (!new_meta_descs || !new_meta_desc_indices)
|
||||
goto clear_descs;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < stk_meta_desc_count; i++) {
|
||||
if (stk_meta_desc_indices[i] == index)
|
||||
continue;
|
||||
memcpy(new_meta_descs[new_count], stk_meta_descs[i],
|
||||
STK_MOD_DESC_BUFFER);
|
||||
new_meta_desc_indices[new_count] = stk_meta_desc_indices[i];
|
||||
new_count++;
|
||||
}
|
||||
|
||||
clear_descs:
|
||||
free(stk_meta_descs);
|
||||
free(stk_meta_desc_indices);
|
||||
stk_meta_descs = new_meta_descs;
|
||||
stk_meta_desc_indices = new_meta_desc_indices;
|
||||
stk_meta_desc_count = new_count;
|
||||
stk_modules[index].dep_count = 0;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
free(stk_meta_names);
|
||||
free(stk_meta_name_indices);
|
||||
free(stk_meta_versions);
|
||||
free(stk_meta_version_indices);
|
||||
free(stk_meta_descs);
|
||||
free(stk_meta_desc_indices);
|
||||
|
||||
stk_meta_names = NULL;
|
||||
stk_meta_name_indices = NULL;
|
||||
stk_meta_name_count = 0;
|
||||
stk_meta_versions = NULL;
|
||||
stk_meta_version_indices = NULL;
|
||||
stk_meta_version_count = 0;
|
||||
stk_meta_descs = NULL;
|
||||
stk_meta_desc_indices = NULL;
|
||||
stk_meta_desc_count = 0;
|
||||
if (stk_modules) {
|
||||
size_t i;
|
||||
for (i = 0; i < module_count; i++) {
|
||||
if (stk_modules[i].deps)
|
||||
free(stk_modules[i].deps);
|
||||
}
|
||||
free(stk_modules);
|
||||
stk_modules = NULL;
|
||||
}
|
||||
module_count = 0;
|
||||
}
|
||||
|
||||
unsigned char stk_module_init_memory(size_t capacity)
|
||||
{
|
||||
stk_module_ids = malloc(capacity * sizeof(*stk_module_ids));
|
||||
stk_handles = malloc(capacity * sizeof(void *));
|
||||
stk_inits = malloc(capacity * sizeof(stk_init_mod_func));
|
||||
stk_shutdowns = malloc(capacity * sizeof(stk_shutdown_mod_func));
|
||||
|
||||
if (!stk_module_ids || !stk_handles || !stk_inits || !stk_shutdowns) {
|
||||
stk_module_free_memory();
|
||||
stk_modules = malloc(capacity * sizeof(stk_mod_t));
|
||||
if (!stk_modules)
|
||||
return STK_INIT_MEMORY_ERROR;
|
||||
}
|
||||
|
||||
stk_meta_names = NULL;
|
||||
stk_meta_name_indices = NULL;
|
||||
stk_meta_name_count = 0;
|
||||
stk_meta_versions = NULL;
|
||||
stk_meta_version_indices = NULL;
|
||||
stk_meta_version_count = 0;
|
||||
stk_meta_descs = NULL;
|
||||
stk_meta_desc_indices = NULL;
|
||||
stk_meta_desc_count = 0;
|
||||
|
||||
return STK_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char stk_module_realloc_memory(size_t new_capacity)
|
||||
{
|
||||
char (*new_module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
void **new_handles = NULL;
|
||||
stk_init_mod_func *new_inits = NULL;
|
||||
stk_shutdown_mod_func *new_shutdowns = NULL;
|
||||
char (*new_meta_names)[STK_MOD_NAME_BUFFER] = NULL;
|
||||
size_t *new_meta_name_indices = NULL;
|
||||
char (*new_meta_versions)[STK_MOD_VERSION_BUFFER] = NULL;
|
||||
size_t *new_meta_version_indices = NULL;
|
||||
char (*new_meta_descs)[STK_MOD_DESC_BUFFER] = NULL;
|
||||
size_t *new_meta_desc_indices = NULL;
|
||||
stk_mod_t *new_modules;
|
||||
size_t i, copy_count;
|
||||
|
||||
if (new_capacity == 0) {
|
||||
@@ -424,139 +279,30 @@ unsigned char stk_module_realloc_memory(size_t new_capacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
new_module_ids = malloc(new_capacity * sizeof(*stk_module_ids));
|
||||
new_handles = malloc(new_capacity * sizeof(*new_handles));
|
||||
new_inits = malloc(new_capacity * sizeof(stk_init_mod_func));
|
||||
new_shutdowns = malloc(new_capacity * sizeof(stk_shutdown_mod_func));
|
||||
|
||||
if (!new_module_ids || !new_handles || !new_inits || !new_shutdowns) {
|
||||
if (new_module_ids)
|
||||
free(new_module_ids);
|
||||
if (new_handles)
|
||||
free(new_handles);
|
||||
if (new_inits)
|
||||
free(new_inits);
|
||||
if (new_shutdowns)
|
||||
free(new_shutdowns);
|
||||
new_modules = malloc(new_capacity * sizeof(stk_mod_t));
|
||||
if (!new_modules)
|
||||
return STK_MOD_REALLOC_FAILURE;
|
||||
}
|
||||
|
||||
copy_count =
|
||||
(module_count < new_capacity) ? module_count : new_capacity;
|
||||
|
||||
if (stk_module_ids) {
|
||||
for (i = 0; i < copy_count; i++) {
|
||||
strncpy(new_module_ids[i], stk_module_ids[i],
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
new_module_ids[i][STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (stk_handles)
|
||||
memcpy(new_handles, stk_handles, copy_count * sizeof(void *));
|
||||
|
||||
if (stk_inits)
|
||||
memcpy(new_inits, stk_inits,
|
||||
copy_count * sizeof(stk_init_mod_func));
|
||||
|
||||
if (stk_shutdowns)
|
||||
memcpy(new_shutdowns, stk_shutdowns,
|
||||
copy_count * sizeof(stk_shutdown_mod_func));
|
||||
for (i = 0; i < copy_count; i++)
|
||||
new_modules[i] = stk_modules[i];
|
||||
|
||||
for (i = copy_count; i < new_capacity; i++) {
|
||||
new_module_ids[i][0] = '\0';
|
||||
new_handles[i] = NULL;
|
||||
new_inits[i] = NULL;
|
||||
new_shutdowns[i] = NULL;
|
||||
new_modules[i].handle = NULL;
|
||||
new_modules[i].init = NULL;
|
||||
new_modules[i].shutdown = NULL;
|
||||
new_modules[i].id[0] = '\0';
|
||||
new_modules[i].name[0] = '\0';
|
||||
new_modules[i].version[0] = '\0';
|
||||
new_modules[i].desc[0] = '\0';
|
||||
new_modules[i].deps = NULL;
|
||||
new_modules[i].dep_count = 0;
|
||||
}
|
||||
|
||||
if (stk_meta_name_count == 0)
|
||||
goto skip_meta_names;
|
||||
|
||||
new_meta_names = malloc(stk_meta_name_count * sizeof(*stk_meta_names));
|
||||
new_meta_name_indices = malloc(stk_meta_name_count * sizeof(size_t));
|
||||
if (!new_meta_names || !new_meta_name_indices) {
|
||||
if (new_meta_names)
|
||||
free(new_meta_names);
|
||||
|
||||
if (new_meta_name_indices)
|
||||
free(new_meta_name_indices);
|
||||
|
||||
new_meta_names = NULL;
|
||||
new_meta_name_indices = NULL;
|
||||
goto skip_meta_names;
|
||||
}
|
||||
|
||||
for (i = 0; i < stk_meta_name_count; i++) {
|
||||
strncpy(new_meta_names[i], stk_meta_names[i],
|
||||
STK_MOD_NAME_BUFFER - 1);
|
||||
new_meta_names[i][STK_MOD_NAME_BUFFER - 1] = '\0';
|
||||
new_meta_name_indices[i] = stk_meta_name_indices[i];
|
||||
}
|
||||
|
||||
skip_meta_names:
|
||||
if (stk_meta_version_count == 0)
|
||||
goto skip_meta_versions;
|
||||
|
||||
new_meta_versions =
|
||||
malloc(stk_meta_version_count * sizeof(*stk_meta_versions));
|
||||
new_meta_version_indices =
|
||||
malloc(stk_meta_version_count * sizeof(size_t));
|
||||
if (!new_meta_versions || !new_meta_version_indices) {
|
||||
if (new_meta_versions)
|
||||
free(new_meta_versions);
|
||||
if (new_meta_version_indices)
|
||||
free(new_meta_version_indices);
|
||||
new_meta_versions = NULL;
|
||||
new_meta_version_indices = NULL;
|
||||
goto skip_meta_versions;
|
||||
}
|
||||
|
||||
for (i = 0; i < stk_meta_version_count; i++) {
|
||||
strncpy(new_meta_versions[i], stk_meta_versions[i],
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
new_meta_versions[i][STK_MOD_VERSION_BUFFER - 1] = '\0';
|
||||
new_meta_version_indices[i] = stk_meta_version_indices[i];
|
||||
}
|
||||
|
||||
skip_meta_versions:
|
||||
if (stk_meta_desc_count == 0)
|
||||
goto skip_meta_descs;
|
||||
|
||||
new_meta_descs = malloc(stk_meta_desc_count * sizeof(*stk_meta_descs));
|
||||
new_meta_desc_indices = malloc(stk_meta_desc_count * sizeof(size_t));
|
||||
if (!new_meta_descs || !new_meta_desc_indices) {
|
||||
if (new_meta_descs)
|
||||
free(new_meta_descs);
|
||||
if (new_meta_desc_indices)
|
||||
free(new_meta_desc_indices);
|
||||
new_meta_descs = NULL;
|
||||
new_meta_desc_indices = NULL;
|
||||
goto skip_meta_descs;
|
||||
}
|
||||
|
||||
for (i = 0; i < stk_meta_desc_count; i++) {
|
||||
strncpy(new_meta_descs[i], stk_meta_descs[i],
|
||||
STK_MOD_DESC_BUFFER - 1);
|
||||
new_meta_descs[i][STK_MOD_DESC_BUFFER - 1] = '\0';
|
||||
new_meta_desc_indices[i] = stk_meta_desc_indices[i];
|
||||
}
|
||||
|
||||
skip_meta_descs:
|
||||
|
||||
stk_module_free_memory();
|
||||
|
||||
stk_module_ids = new_module_ids;
|
||||
stk_handles = new_handles;
|
||||
stk_inits = new_inits;
|
||||
stk_shutdowns = new_shutdowns;
|
||||
|
||||
stk_meta_names = new_meta_names;
|
||||
stk_meta_name_indices = new_meta_name_indices;
|
||||
stk_meta_versions = new_meta_versions;
|
||||
stk_meta_version_indices = new_meta_version_indices;
|
||||
stk_meta_descs = new_meta_descs;
|
||||
stk_meta_desc_indices = new_meta_desc_indices;
|
||||
free(stk_modules);
|
||||
stk_modules = new_modules;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,11 +8,24 @@
|
||||
typedef int (*stk_init_mod_func)(void);
|
||||
typedef void (*stk_shutdown_mod_func)(void);
|
||||
|
||||
extern void **stk_handles;
|
||||
extern stk_init_mod_func *stk_inits;
|
||||
extern stk_shutdown_mod_func *stk_shutdowns;
|
||||
extern char (*stk_module_ids)[STK_MOD_ID_BUFFER];
|
||||
typedef struct {
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
} stk_dep_t;
|
||||
|
||||
typedef struct {
|
||||
void *handle;
|
||||
stk_init_mod_func init;
|
||||
stk_shutdown_mod_func shutdown;
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char name[STK_MOD_NAME_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
char desc[STK_MOD_DESC_BUFFER];
|
||||
stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
} stk_mod_t;
|
||||
|
||||
extern stk_mod_t *stk_modules;
|
||||
extern size_t module_count;
|
||||
|
||||
unsigned char stk_flags = STK_FLAG_LOGGING_ENABLED;
|
||||
@@ -185,10 +198,25 @@ size_t stk_poll(void)
|
||||
char mod_id[STK_MOD_ID_BUFFER];
|
||||
int load_result;
|
||||
size_t successful_appends = 0;
|
||||
char (*module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
|
||||
if (module_count > 0) {
|
||||
module_ids = malloc(module_count * sizeof(*module_ids));
|
||||
if (module_ids) {
|
||||
for (i = 0; i < module_count; i++) {
|
||||
strncpy(module_ids[i], stk_modules[i].id,
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
module_ids[i][STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events = platform_directory_watch_check(
|
||||
watch_handle, &file_list, &file_count, module_ids, module_count);
|
||||
|
||||
if (module_ids)
|
||||
free(module_ids);
|
||||
|
||||
events = platform_directory_watch_check(watch_handle, &file_list,
|
||||
&file_count, stk_module_ids,
|
||||
module_count);
|
||||
if (!events)
|
||||
goto finish_poll;
|
||||
|
||||
@@ -358,12 +386,8 @@ trim_arrays:
|
||||
}
|
||||
|
||||
for (read_pos = write_pos + 1; read_pos < module_count; ++read_pos) {
|
||||
if (stk_handles[read_pos] != NULL) {
|
||||
stk_handles[write_pos] = stk_handles[read_pos];
|
||||
stk_inits[write_pos] = stk_inits[read_pos];
|
||||
stk_shutdowns[write_pos] = stk_shutdowns[read_pos];
|
||||
memcpy(stk_module_ids[write_pos],
|
||||
stk_module_ids[read_pos], STK_MOD_ID_BUFFER);
|
||||
if (stk_modules[read_pos].handle != NULL) {
|
||||
stk_modules[write_pos] = stk_modules[read_pos];
|
||||
++write_pos;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user