11 struct module* module_load(struct core *c, const char *name, const char *argument) {
12 struct module *m = NULL;
17 m = malloc(sizeof(struct module));
20 if (!(m->dl = lt_dlopenext(name)))
23 if (!(m->init = lt_dlsym(m->dl, "module_init")))
26 if (!(m->done = lt_dlsym(m->dl, "module_done")))
29 m->name = strdup(name);
30 m->argument = argument ? strdup(argument) : NULL;
35 if (m->init(c, m) < 0)
39 c->modules = idxset_new(NULL, NULL);
42 r = idxset_put(c->modules, m, &m->index);
43 assert(r >= 0 && m->index != IDXSET_INVALID);
45 fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
63 static void module_free(struct module *m) {
64 assert(m && m->done && m->core);
69 fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
77 void module_unload(struct core *c, struct module *m) {
81 if (!(m = idxset_remove_by_data(c->modules, m, NULL)))
87 void module_unload_by_index(struct core *c, uint32_t index) {
89 assert(c && index != IDXSET_INVALID);
92 if (!(m = idxset_remove_by_index(c->modules, index)))
98 void free_callback(void *p, void *userdata) {
104 void module_unload_all(struct core *c) {
110 idxset_free(c->modules, free_callback, NULL);
114 char *module_list_to_string(struct core *c) {
117 uint32_t index = IDXSET_INVALID;
123 strbuf_printf(s, "%u module(s) loaded.\n", idxset_ncontents(c->modules));
125 for (m = idxset_first(c->modules, &index); m; m = idxset_next(c->modules, &index))
126 strbuf_printf(s, " index: %u, name: <%s>, argument: <%s>\n", m->index, m->name, m->argument);
128 return strbuf_tostring_free(s);