diff options
author | Fam Zheng <famz@redhat.com> | 2016-09-05 10:50:44 +0800 |
---|---|---|
committer | Max Reitz <mreitz@redhat.com> | 2016-10-07 14:14:06 +0200 |
commit | dffa41b48651c4002af02e80b7459e56a77152c7 (patch) | |
tree | 909495655b4efc4182f9814d634d89ca8188996e | |
parent | 159975f38b2c88cd7b1fef511ba86dd7266a9f4e (diff) | |
download | qemu-dffa41b48651c4002af02e80b7459e56a77152c7.zip qemu-dffa41b48651c4002af02e80b7459e56a77152c7.tar.gz qemu-dffa41b48651c4002af02e80b7459e56a77152c7.tar.bz2 |
module: Don't load the same module if requested multiple times
Use a hash table to keep record of all loaded modules, and return early
if the requested module is already loaded.
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-id: 1473043845-13197-3-git-send-email-famz@redhat.com
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
-rw-r--r-- | util/module.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/util/module.c b/util/module.c index a5f7fbd..c909737 100644 --- a/util/module.c +++ b/util/module.c @@ -163,14 +163,28 @@ void module_load_one(const char *prefix, const char *lib_name) char *fname = NULL; char *exec_dir; char *dirs[3]; + char *module_name; int i = 0; int ret; + static GHashTable *loaded_modules; if (!g_module_supported()) { fprintf(stderr, "Module is not supported by system.\n"); return; } + if (!loaded_modules) { + loaded_modules = g_hash_table_new(g_str_hash, g_str_equal); + } + + module_name = g_strdup_printf("%s%s", prefix, lib_name); + + if (g_hash_table_lookup(loaded_modules, module_name)) { + g_free(module_name); + return; + } + g_hash_table_insert(loaded_modules, module_name, module_name); + exec_dir = qemu_get_exec_dir(); dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR); dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : ""); @@ -180,8 +194,8 @@ void module_load_one(const char *prefix, const char *lib_name) exec_dir = NULL; for (i = 0; i < ARRAY_SIZE(dirs); i++) { - fname = g_strdup_printf("%s/%s%s%s", - dirs[i], prefix, lib_name, HOST_DSOSUF); + fname = g_strdup_printf("%s/%s%s", + dirs[i], module_name, HOST_DSOSUF); ret = module_load_file(fname); g_free(fname); fname = NULL; |