| 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 (all_modules, 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 (g_str_hash, g_str_equal); |
| 56 |
|
| 57 |
g_hash_table_insert (all_modules, filename ? filename : "", 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 (result); |
| 71 |
|
| 72 |
module = g_module_open (NULL, 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 (full_path); |
| 92 |
if (result) |
| 93 |
{ |
| 94 |
g_free (full_path); |
| 95 |
return reftest_module_ref (result); |
| 96 |
} |
| 97 |
|
| 98 |
module = g_module_open (full_path, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY); |
| 99 |
if (module == NULL) |
| 100 |
{ |
| 101 |
/* libtool hack */ |
| 102 |
char *libtool_dir = g_build_filename (directory, ".libs", NULL); |
| 103 |
|
| 104 |
g_free (full_path); |
| 105 |
full_path = g_module_build_path (libtool_dir, module_name); |
| 106 |
|
| 107 |
result = reftest_module_find_existing (full_path); |
| 108 |
if (result) |
| 109 |
{ |
| 110 |
g_free (full_path); |
| 111 |
return reftest_module_ref (result); |
| 112 |
} |
| 113 |
|
| 114 |
module = g_module_open (full_path, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY); |
| 115 |
if (module == NULL) |
| 116 |
{ |
| 117 |
g_free (full_path); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return reftest_module_new_take (module, full_path); |
| 123 |
} |
| 124 |
|
| 125 |
ReftestModule * |
| 126 |
reftest_module_ref (ReftestModule *module) |
| 127 |
{ |
| 128 |
g_return_val_if_fail (module != NULL, NULL); |
| 129 |
|
| 130 |
module->refcount++; |
| 131 |
|
| 132 |
return module; |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
reftest_module_unref (ReftestModule *module) |
| 137 |
{ |
| 138 |
g_return_if_fail (module != NULL); |
| 139 |
|
| 140 |
module->refcount--; |
| 141 |
if (module->refcount > 0) |
| 142 |
return; |
| 143 |
|
| 144 |
if (!g_module_close (module->module)) |
| 145 |
{ |
| 146 |
g_assert_not_reached (); |
| 147 |
} |
| 148 |
|
| 149 |
if (!g_hash_table_remove (all_modules, module->filename ? module->filename : "")) |
| 150 |
{ |
| 151 |
g_assert_not_reached (); |
| 152 |
} |
| 153 |
|
| 154 |
g_free (module->filename); |
| 155 |
g_slice_free (ReftestModule, module); |
| 156 |
} |
| 157 |
|
| 158 |
GCallback |
| 159 |
reftest_module_lookup (ReftestModule *module, |
| 160 |
const char *function_name) |
| 161 |
{ |
| 162 |
gpointer result; |
| 163 |
|
| 164 |
g_return_val_if_fail (module != NULL, NULL); |
| 165 |
g_return_val_if_fail (function_name != NULL, NULL); |
| 166 |
|
| 167 |
if (!g_module_symbol (module->module, function_name, &result)) |
| 168 |
return NULL; |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|