🦄 refactor(module manager): remove utarray

This commit is contained in:
yangwei
2024-09-25 17:44:27 +08:00
parent eccd6e102d
commit 0308205547
3 changed files with 58 additions and 62 deletions

View File

@@ -5,10 +5,9 @@
#include <dlfcn.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
UT_icd module_specs_icd = {sizeof(struct module_specific), NULL, NULL, NULL};
/*******************************************
* module manager internal API *
*******************************************/
@@ -25,17 +24,16 @@ toml_table_t *toml_parse_file_path(const char *toml_conf_path)
return conf;
}
struct module_specific *module_specs_load(toml_table_t* conf, int *mod_num)
int module_specs_load(toml_table_t* conf, struct module_spec_load **mod_spec)
{
if(conf==NULL|| mod_num==NULL) return NULL;
*mod_num = 0;
struct module_specific* mod_spec=NULL;
if(conf==NULL|| mod_spec==NULL) return 0;
int mod_num = 0;
toml_array_t* mod_array = toml_array_in(conf, "module");
if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR;
*mod_num = toml_array_nelem(mod_array);
mod_spec = CALLOC(struct module_specific, *mod_num);
mod_num = toml_array_nelem(mod_array);
*mod_spec = CALLOC(struct module_spec_load, mod_num);
for (int i = 0; i < *mod_num; i++) {
for (int i = 0; i < mod_num; i++) {
toml_table_t* toml_mod = toml_table_at(mod_array, i);
const char *path_raw = toml_raw_in(toml_mod, "path");
@@ -55,58 +53,51 @@ struct module_specific *module_specs_load(toml_table_t* conf, int *mod_num)
goto MODULE_SPEC_LOAD_ERROR;
}
mod_spec[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (!mod_spec[i].on_init_cb) {
mod_spec[i]->on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (!mod_spec[i]->on_init_cb) {
fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror());
}
mod_spec[i].on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
if (!mod_spec[i].on_exit_cb) {
mod_spec[i]->on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
if (!mod_spec[i]->on_exit_cb) {
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
}
FREE(path);
FREE(init_func_name);
FREE(exit_func_name);
}
return mod_spec;
return mod_num;
MODULE_SPEC_LOAD_ERROR:
if(mod_spec)FREE(mod_spec);
*mod_num=0;
return NULL;
if(*mod_spec)FREE(*mod_spec);
return 0;
}
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema)
{
int spec_num = 0;
struct module_specific *specs = module_specs_load(conf, &spec_num);
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
if(spec_num > 0)
{
utarray_new(mod_mgr->schema.module_specs_array,&module_specs_icd);
utarray_reserve(mod_mgr->schema.module_specs_array, spec_num);
}
mod_mgr->schema.max_thread_num=max_thread_num;
mod_mgr->schema.mq_schema=mq_schema;
mod_mgr->schema.load_module_num = module_specs_load(conf, &mod_mgr->schema.module_specs);
// TODO: store module specific in hash
for(int i = 0; i < spec_num; i++)
for(int i = 0; i < mod_mgr->schema.load_module_num; i++)
{
if (specs[i].on_init_cb != NULL)
if (mod_mgr->schema.module_specs[i].on_init_cb != NULL)
{
specs[i].mod=specs[i].on_init_cb(mod_mgr);
if(stellar_module_manager_get_module(mod_mgr, specs[i].mod->name)==NULL)
mod_mgr->schema.module_specs[i].mod=mod_mgr->schema.module_specs[i].on_init_cb(mod_mgr);
if(stellar_module_manager_get_module(mod_mgr, mod_mgr->schema.module_specs[i].mod->name)==NULL)
{
utarray_push_back(mod_mgr->schema.module_specs_array, &specs[i]);
mod_mgr->schema.module_specs[i].is_init_succ=true;
}
else
{
fprintf(stderr, "Module %s already exists\n", specs[i].mod->name);
if(specs[i].on_exit_cb)specs[i].on_exit_cb(mod_mgr, specs[i].mod);
fprintf(stderr, "Module %s already exists\n", mod_mgr->schema.module_specs[i].mod->name);
if(mod_mgr->schema.module_specs[i].on_exit_cb)mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
assert(0);
}
}
}
FREE(specs);
if(mod_mgr->schema.module_specs_array)assert(spec_num==(int)utarray_len(mod_mgr->schema.module_specs_array));
return mod_mgr;
}
@@ -126,18 +117,20 @@ struct stellar_module_manager *stellar_module_manager_new(const char *module_spe
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return;
struct module_specific *p=NULL;
if(mod_mgr->module_spec_toml_path)FREE(mod_mgr->module_spec_toml_path);
if (mod_mgr->schema.module_specs_array)
if (mod_mgr->schema.module_specs)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
for (int i = 0; i < mod_mgr->schema.load_module_num; i++)
{
if (p->on_exit_cb)
p->on_exit_cb(mod_mgr, p->mod);
if (mod_mgr->schema.module_specs[i].on_exit_cb != NULL &&
mod_mgr->schema.module_specs[i].is_init_succ)
{
mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
}
}
utarray_free(mod_mgr->schema.module_specs_array);
FREE(mod_mgr->schema.module_specs);
}
FREE(mod_mgr);
FREE(mod_mgr);
return;
}
@@ -182,19 +175,16 @@ void stellar_module_manager_register_thread(struct stellar_module_manager* mod_m
struct stellar_module *stellar_module_manager_get_module(struct stellar_module_manager *mod_mgr, const char *module_name)
{
if(mod_mgr==NULL || module_name == NULL)return NULL;
struct module_specific *p=NULL;
if (mod_mgr->schema.module_specs_array)
if (mod_mgr->schema.module_specs)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
for(int i=0; i<mod_mgr->schema.load_module_num; i++)
{
if (p->mod)
{
if (strcmp(p->mod->name, module_name) == 0)
{
return p->mod;
}
}
}
if(mod_mgr->schema.module_specs[i].mod == NULL)break;
if(strcmp(mod_mgr->schema.module_specs[i].mod->name, module_name)==0 && mod_mgr->schema.module_specs[i].is_init_succ)
{
return mod_mgr->schema.module_specs[i].mod;
}
}
}
return NULL;
}

View File

@@ -7,40 +7,46 @@ extern "C"
#include "stellar/module_manager.h"
#include "uthash/utarray.h"
#include "stellar/mq.h"
#include <limits.h>
#include <stdbool.h>
struct stellar_module
{
char name[NAME_MAX];
void *module_ctx;
};
struct module_spec_load
{
struct stellar_module *mod;
module_on_init_func *on_init_cb;
module_on_exit_func *on_exit_cb;
bool is_init_succ;
}__attribute__((aligned(sizeof(void*))));
struct stellar_module_manager
{
char *module_spec_toml_path;
struct
{
UT_array *module_specs_array;
struct module_spec_load *module_specs;
int load_module_num;
int max_thread_num;
struct mq_schema *mq_schema;
}schema;
}__attribute__((aligned(sizeof(void*))));
struct module_specific
{
struct stellar_module *mod;
module_on_init_func *on_init_cb;
module_on_exit_func *on_exit_cb;
}__attribute__((aligned(sizeof(void*))));
#include "toml/toml.h"
toml_table_t *toml_parse_file_path(const char *toml_conf_path) __attribute__((visibility("hidden")));
struct module_specific *module_specs_load(toml_table_t* conf, int *mod_num) __attribute__((visibility("hidden")));
int module_specs_load(toml_table_t* conf, struct module_spec_load **load_spec) __attribute__((visibility("hidden")));
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema) __attribute__((visibility("hidden")));
#ifdef __cplusplus
}

View File

@@ -32,8 +32,8 @@ TEST(module_manager_internal, module_specs_load) {
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
EXPECT_TRUE(conf!=NULL);
int mod_num=0;
struct module_specific *specs=module_specs_load(conf, &mod_num);
struct module_spec_load *specs=NULL;
int mod_num=module_specs_load(conf, &specs);
EXPECT_EQ(mod_num, 1);
EXPECT_EQ(specs[0].on_init_cb, gtest_mock_init);