refactor: make module_add more explicit

master
Sean McBride 3 years ago
parent e20de0d18a
commit 73a1a7eb43

@ -11,3 +11,4 @@ struct module_database {
struct module *module_database_find_by_path(struct module_database *db, char *path);
void module_database_init(struct module_database *db);
int module_database_add(struct module_database *db, struct module *module);

@ -39,9 +39,18 @@ tenant_alloc(struct tenant_config *config)
module_database_init(&tenant->module_db);
for (int i = 0; i < config->routes_len; i++) {
/* Resolve module, Ownership of path moves here */
struct module *module = module_database_find_by_path(&tenant->module_db, config->routes[i].path);
if (module == NULL) {
/* Ownership of path moves here */
module = module_alloc(config->routes[i].path);
if (module != NULL) {
module_database_add(&tenant->module_db, module);
config->routes[i].path = NULL;
}
} else {
free(config->routes[i].path);
config->routes[i].path = NULL;
}
assert(module != NULL);

@ -18,7 +18,7 @@ module_database_init(struct module_database *db)
* @param module module to add
* @return 0 on success. -ENOSPC when full
*/
static int
int
module_database_add(struct module_database *db, struct module *module)
{
assert(db->count <= MODULE_DATABASE_CAPACITY);
@ -51,11 +51,5 @@ module_database_find_by_path(struct module_database *db, char *path)
if (strcmp(db->modules[i]->path, path) == 0) return db->modules[i];
}
struct module *module = module_alloc(path);
if (module != NULL) {
module_database_add(db, module);
return module;
}
return NULL;
}

Loading…
Cancel
Save