Skip to content

Commit

Permalink
Merge !1643: kr_module_load(): clean up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
vcunat committed Dec 20, 2024
2 parents 84fa76b + 094c984 commit ec0d46f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Knot Resolver 6.0.10 (202y-mm-dd)
================================
=================================

Improvements
------------
Expand Down
2 changes: 0 additions & 2 deletions daemon/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,6 @@ int engine_register(const char *name, const char *precedence, const char* ref)
if (!module) {
return kr_error(ENOMEM);
}
module->data = the_engine; /*< some outside modules may still use this value */

int ret = kr_module_load(module, name, LIBDIR "/kres_modules");
if (ret == 0) {
/* We have a C module, loaded and init() was called.
Expand Down
6 changes: 3 additions & 3 deletions lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ int kr_module_load(struct kr_module *module, const char *name, const char *path)
return kr_error(EINVAL);
}

/* Initialize, keep userdata */
void *data = module->data;
/* Initialize */
memset(module, 0, sizeof(struct kr_module));
module->data = data;
module->name = strdup(name);
if (module->name == NULL) {
return kr_error(ENOMEM);
Expand All @@ -123,6 +121,8 @@ int kr_module_load(struct kr_module *module, const char *name, const char *path)
ret = module->init(module);
}
if (ret != 0) {
/* Avoid calling deinit() as init() wasn't called or failed. */
module->deinit = NULL;
kr_module_unload(module);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct kr_prop {
/**
* Load a C module instance into memory. And call its init().
*
* @param module module structure. Will be overwritten except for ->data on success.
* @param module module structure. Will be overwritten.
* @param name module name
* @param path module search path
* @return 0 or an error
Expand Down
3 changes: 3 additions & 0 deletions modules/stats/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ int stats_init(struct kr_module *module)
/* Initialize ring buffer of recently visited upstreams */
array_init(data->upstreams.q);
if (array_reserve(data->upstreams.q, UPSTREAMS_COUNT) != 0) {
trie_free(data->trie);
lru_free(data->queries.frequent);
free(data);
return kr_error(ENOMEM);
}
data->upstreams.q.len = UPSTREAMS_COUNT; /* signify we use the entries */
Expand Down

0 comments on commit ec0d46f

Please sign in to comment.