plugin: cache module_info symbol as well

This commit is contained in:
Daniel Eklöf 2019-01-12 19:44:24 +01:00
parent 962252467f
commit 45eb2b85f0

View file

@ -12,6 +12,7 @@
struct plugin { struct plugin {
char *name; char *name;
void *lib; void *lib;
const void *sym;
}; };
static tll(struct plugin) libs = tll_init(); static tll(struct plugin) libs = tll_init();
@ -39,20 +40,17 @@ plugin_load_module(const char *name)
char path[128]; char path[128];
snprintf(path, sizeof(path), "./modules/lib%s.so", name); snprintf(path, sizeof(path), "./modules/lib%s.so", name);
void *lib = NULL;
/* Have we already loaded it? */ /* Have we already loaded it? */
tll_foreach(libs, plug) { tll_foreach(libs, plug) {
if (strcmp(plug->item.name, name) == 0) { if (strcmp(plug->item.name, name) == 0) {
lib = plug->item.lib; LOG_DBG("%s already loaded: %p", name, plug->item.lib);
LOG_DBG("%s already loaded: %p", name, lib); assert(plug->item.sym != NULL);
break; return plug->item.sym;
} }
} }
if (lib == NULL) {
/* Not loaded - do it now */ /* Not loaded - do it now */
lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW);
LOG_DBG("%s: dlopened to %p", name, lib); LOG_DBG("%s: dlopened to %p", name, lib);
if (lib == NULL) { if (lib == NULL) {
@ -61,15 +59,14 @@ plugin_load_module(const char *name)
} }
tll_push_back(libs, ((struct plugin){strdup(name), lib})); tll_push_back(libs, ((struct plugin){strdup(name), lib}));
} struct plugin *plug = &tll_back(libs);
/* TODO: use same name in all modules */ /* TODO: use same name in all modules */
char sym[128]; char sym[128];
snprintf(sym, sizeof(sym), "module_%s", name); snprintf(sym, sizeof(sym), "module_%s", name);
/* TODO: cache symbol */
dlerror(); /* Clear previous error */ dlerror(); /* Clear previous error */
const struct module_info *info = dlsym(lib, sym); plug->sym = dlsym(lib, sym);
const char *dlsym_error = dlerror(); const char *dlsym_error = dlerror();
if (dlsym_error != NULL) { if (dlsym_error != NULL) {
@ -77,6 +74,6 @@ plugin_load_module(const char *name)
return NULL; return NULL;
} }
assert(info != NULL); assert(plug->sym != NULL);
return info; return plug->sym;
} }