summaryrefslogtreecommitdiff
path: root/testsuite/reftests/reftest-module.c
blob: d47243edd2bc7818271a5296f27a43604fcfc233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* GTK - The GIMP Toolkit
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "reftest-module.h"

struct _ReftestModule {
  int      refcount;
  char    *filename;
  GModule *module;
};


static GHashTable *all_modules = NULL;

static ReftestModule *
reftest_module_find_existing (const char *filename)
{
  if (all_modules == NULL)
    return NULL;

  return g_hash_table_lookup (all_modules, filename ? filename : "");
}

static ReftestModule *
reftest_module_new_take (GModule *module,
                         char    *filename)
{
  ReftestModule *result;

  g_return_val_if_fail (module != NULL, NULL);

  result = g_slice_new0 (ReftestModule);

  result->refcount = 1;
  result->filename = filename;
  result->module = module;

  if (all_modules == NULL)
    all_modules = g_hash_table_new (g_str_hash, g_str_equal);

  g_hash_table_insert (all_modules, filename ? filename : g_strdup (""), result);

  return result;
}

ReftestModule *
reftest_module_new_self (void)
{
  ReftestModule *result;
  GModule *module;

  result = reftest_module_find_existing (NULL);
  if (result)
    return reftest_module_ref (result);

  module = g_module_open (NULL, G_MODULE_BIND_LAZY);
  if (module == NULL)
    return NULL;

  return reftest_module_new_take (module, NULL);
}

ReftestModule *
reftest_module_new (const char *directory,
                    const char *module_name)
{
  ReftestModule *result;
  char *full_path;
  GModule *module;

  g_return_val_if_fail (module_name != NULL, NULL);

  full_path = g_module_build_path (directory, module_name);

  result = reftest_module_find_existing (full_path);
  if (result)
    {
      g_free (full_path);
      return reftest_module_ref (result);
    }

  module = g_module_open (full_path, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
  if (module == NULL)
    {
      g_free (full_path);
      return NULL;
    }

  return reftest_module_new_take (module, full_path);
}

ReftestModule *
reftest_module_ref (ReftestModule *module)
{
  g_return_val_if_fail (module != NULL, NULL);

  module->refcount++;

  return module;
}

void
reftest_module_unref (ReftestModule *module)
{
  g_return_if_fail (module != NULL);

  module->refcount--;
  if (module->refcount > 0)
    return;

  if (!g_module_close (module->module))
    {
      g_assert_not_reached ();
    }

  if (!g_hash_table_remove (all_modules, module->filename ? module->filename : ""))
    {
      g_assert_not_reached ();
    }

  g_free (module->filename);
  g_slice_free (ReftestModule, module);
}

GCallback
reftest_module_lookup (ReftestModule *module,
                       const char    *function_name)
{
  gpointer result;

  g_return_val_if_fail (module != NULL, NULL);
  g_return_val_if_fail (function_name != NULL, NULL);

  if (!g_module_symbol (module->module, function_name, &result))
    return NULL;

  return result;
}