diff --git a/runtime/include/module_database.h b/runtime/include/module_database.h index 7589eca5..0c09ec55 100644 --- a/runtime/include/module_database.h +++ b/runtime/include/module_database.h @@ -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); diff --git a/runtime/include/tenant_functions.h b/runtime/include/tenant_functions.h index 862f779d..cdf446cd 100644 --- a/runtime/include/tenant_functions.h +++ b/runtime/include/tenant_functions.h @@ -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); - config->routes[i].path = NULL; + 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); diff --git a/runtime/src/module_database.c b/runtime/src/module_database.c index a804d037..643ae69b 100644 --- a/runtime/src/module_database.c +++ b/runtime/src/module_database.c @@ -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; }