1 | /* GTK - The GIMP Toolkit |
2 | * Copyright (C) 2014 Red Hat, Inc. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "config.h" |
19 | |
20 | #include "reftest-module.h" |
21 | |
22 | struct _ReftestModule { |
23 | int refcount; |
24 | char *filename; |
25 | GModule *module; |
26 | }; |
27 | |
28 | |
29 | static GHashTable *all_modules = NULL; |
30 | |
31 | static ReftestModule * |
32 | reftest_module_find_existing (const char *filename) |
33 | { |
34 | if (all_modules == NULL) |
35 | return NULL; |
36 | |
37 | return g_hash_table_lookup (hash_table: all_modules, key: filename ? filename : "" ); |
38 | } |
39 | |
40 | static ReftestModule * |
41 | reftest_module_new_take (GModule *module, |
42 | char *filename) |
43 | { |
44 | ReftestModule *result; |
45 | |
46 | g_return_val_if_fail (module != NULL, NULL); |
47 | |
48 | result = g_slice_new0 (ReftestModule); |
49 | |
50 | result->refcount = 1; |
51 | result->filename = filename; |
52 | result->module = module; |
53 | |
54 | if (all_modules == NULL) |
55 | all_modules = g_hash_table_new (hash_func: g_str_hash, key_equal_func: g_str_equal); |
56 | |
57 | g_hash_table_insert (hash_table: all_modules, key: filename ? filename : g_strdup (str: "" ), value: result); |
58 | |
59 | return result; |
60 | } |
61 | |
62 | ReftestModule * |
63 | reftest_module_new_self (void) |
64 | { |
65 | ReftestModule *result; |
66 | GModule *module; |
67 | |
68 | result = reftest_module_find_existing (NULL); |
69 | if (result) |
70 | return reftest_module_ref (module: result); |
71 | |
72 | module = g_module_open (NULL, flags: G_MODULE_BIND_LAZY); |
73 | if (module == NULL) |
74 | return NULL; |
75 | |
76 | return reftest_module_new_take (module, NULL); |
77 | } |
78 | |
79 | ReftestModule * |
80 | reftest_module_new (const char *directory, |
81 | const char *module_name) |
82 | { |
83 | ReftestModule *result; |
84 | char *full_path; |
85 | GModule *module; |
86 | |
87 | g_return_val_if_fail (module_name != NULL, NULL); |
88 | |
89 | full_path = g_module_build_path (directory, module_name); |
90 | |
91 | result = reftest_module_find_existing (filename: full_path); |
92 | if (result) |
93 | { |
94 | g_free (mem: full_path); |
95 | return reftest_module_ref (module: result); |
96 | } |
97 | |
98 | module = g_module_open (file_name: full_path, flags: G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY); |
99 | if (module == NULL) |
100 | { |
101 | g_free (mem: full_path); |
102 | return NULL; |
103 | } |
104 | |
105 | return reftest_module_new_take (module, filename: full_path); |
106 | } |
107 | |
108 | ReftestModule * |
109 | reftest_module_ref (ReftestModule *module) |
110 | { |
111 | g_return_val_if_fail (module != NULL, NULL); |
112 | |
113 | module->refcount++; |
114 | |
115 | return module; |
116 | } |
117 | |
118 | void |
119 | reftest_module_unref (ReftestModule *module) |
120 | { |
121 | g_return_if_fail (module != NULL); |
122 | |
123 | module->refcount--; |
124 | if (module->refcount > 0) |
125 | return; |
126 | |
127 | if (!g_module_close (module: module->module)) |
128 | { |
129 | g_assert_not_reached (); |
130 | } |
131 | |
132 | if (!g_hash_table_remove (hash_table: all_modules, key: module->filename ? module->filename : "" )) |
133 | { |
134 | g_assert_not_reached (); |
135 | } |
136 | |
137 | g_free (mem: module->filename); |
138 | g_slice_free (ReftestModule, module); |
139 | } |
140 | |
141 | GCallback |
142 | reftest_module_lookup (ReftestModule *module, |
143 | const char *function_name) |
144 | { |
145 | gpointer result; |
146 | |
147 | g_return_val_if_fail (module != NULL, NULL); |
148 | g_return_val_if_fail (function_name != NULL, NULL); |
149 | |
150 | if (!g_module_symbol (module: module->module, symbol_name: function_name, symbol: &result)) |
151 | return NULL; |
152 | |
153 | return result; |
154 | } |
155 | |
156 | |