[Tarantool-patches] [PATCH v12 6/8] module_cache: provide helpers to load and unload modules

Cyrill Gorcunov gorcunov at gmail.com
Mon Jan 18 23:35:54 MSK 2021


We need to be able to load and unload modules without
functions bound to them, just plain dlopen inside. This
is a part of API for future patches.

Part-of #4642

Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 src/box/module_cache.c | 47 +++++++++++++++++++++++++++++++-----------
 src/box/module_cache.h | 19 +++++++++++++++++
 2 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/src/box/module_cache.c b/src/box/module_cache.c
index ae874caab..cb580f342 100644
--- a/src/box/module_cache.c
+++ b/src/box/module_cache.c
@@ -198,7 +198,7 @@ module_find(const char *package, const char *package_end,
  * for cases of a function reload.
  */
 static struct module *
-module_load(const char *package, const char *package_end)
+module_dlopen(const char *package, const char *package_end)
 {
 	char path[PATH_MAX];
 	if (module_find(package, package_end, path, sizeof(path)) != 0)
@@ -338,16 +338,10 @@ module_sym_load(struct module_sym *mod_sym)
 	struct func_name_desc d;
 	parse_func_name(mod_sym->name, &d);
 
-	struct module *module = module_cache_find(d.package, d.package_end);
-	if (module == NULL) {
-		module = module_load(d.package, d.package_end);
-		if (module == NULL)
-			return -1;
-		if (module_cache_add(module) != 0) {
-			module_delete(module);
-			return -1;
-		}
-	}
+	size_t len = d.package_end - d.package;
+	struct module *module = module_load(d.package, len);
+	if (module == NULL)
+		return -1;
 
 	mod_sym->addr = module_sym(module, d.sym);
 	if (mod_sym->addr == NULL)
@@ -437,6 +431,34 @@ module_cache_update(const char *name, const char *name_end,
 	return 0;
 }
 
+/**
+ * Load a new module.
+ */
+struct module *
+module_load(const char *path, size_t path_len)
+{
+	struct module *module = module_cache_find(path, &path[path_len]);
+	if (module == NULL) {
+		module = module_dlopen(path, &path[path_len]);
+		if (module == NULL)
+			return NULL;
+		if (module_cache_add(module) != 0) {
+			module_delete(module);
+			return NULL;
+		}
+	}
+	return module;
+}
+
+/**
+ * Unload a module.
+ */
+void
+module_unload(struct module *module)
+{
+	module_gc(module);
+}
+
 int
 module_reload(const char *package, const char *package_end,
 	      struct module **module)
@@ -448,7 +470,8 @@ module_reload(const char *package, const char *package_end,
 		return 0;
 	}
 
-	struct module *new = module_load(package, package_end);
+	size_t len = package_end - package;
+	struct module *new = module_dlopen(package, &package[len]);
 	if (new == NULL)
 		return -1;
 
diff --git a/src/box/module_cache.h b/src/box/module_cache.h
index feb1f2266..87108ae81 100644
--- a/src/box/module_cache.h
+++ b/src/box/module_cache.h
@@ -107,6 +107,25 @@ int
 module_sym_call(struct module_sym *mod_sym, struct port *args,
 		struct port *ret);
 
+/**
+ * Load a module.
+ *
+ * @param path path to a module file without extension.
+ * @param path_len length of @path.
+ *
+ * @return pointer to a module or NULL on error.
+ */
+struct module *
+module_load(const char *path, size_t path_len);
+
+/**
+ * Unload a module.
+ *
+ * @param module pointer to a module.
+ */
+void
+module_unload(struct module *module);
+
 /**
  * Reload a module and all associated symbols.
  *
-- 
2.29.2



More information about the Tarantool-patches mailing list