Debian Codegraph @

gi/pygi-cache-hashtable.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
5 * Copyright (C) 2014 Simon Feltman <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "pygi-argument.h"
22 #include "pygi-util.h"
23 #include "pygi-cache-private.h"
24
25 typedef struct _PyGIHashCache {
26 PyGIArgCache arg_cache;
27 PyGIArgCache *key_cache;
28 PyGIArgCache *value_cache;
29 } PyGIHashCache;
30
31
32 static void
33 _hash_cache_free_func (PyGIHashCache *cache)
34 {
35 if (cache != NULL) {
36 pygi_arg_cache_free (cache->key_cache);
37 pygi_arg_cache_free (cache->value_cache);
38 g_slice_free (PyGIHashCache, cache);
39 }
40 }
41
42 static gboolean
43 _pygi_marshal_from_py_ghash (PyGIInvokeState *state,
44 PyGICallableCache *callable_cache,
45 PyGIArgCache *arg_cache, PyObject *py_arg,
46 GIArgument *arg, gpointer *cleanup_data)
47 {
48 PyGIMarshalFromPyFunc key_from_py_marshaller;
49 PyGIMarshalFromPyFunc value_from_py_marshaller;
50
51 int i;
52 Py_ssize_t length;
53 PyObject *py_keys, *py_values;
54
55 GHashFunc hash_func = NULL;
56 GEqualFunc equal_func = NULL;
57 GDestroyNotify key_destroy_func = NULL;
58 GDestroyNotify value_destroy_func = NULL;
59
60 GHashTable *hash_ = NULL;
61 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
62
63 if (Py_IsNone (py_arg)) {
64 arg->v_pointer = NULL;
65 return TRUE;
66 }
67
68 py_keys = PyMapping_Keys (py_arg);
69 if (py_keys == NULL) {
70 PyErr_Format (PyExc_TypeError, "Must be mapping, not %s",
71 Py_TYPE (py_arg)->tp_name);
72 return FALSE;
73 }
74
75 length = PyMapping_Length (py_arg);
76 if (length < 0) {
77 Py_DECREF (py_keys);
78 return FALSE;
79 }
80
81 py_values = PyMapping_Values (py_arg);
82 if (py_values == NULL) {
83 Py_DECREF (py_keys);
84 return FALSE;
85 }
86
87 key_from_py_marshaller = hash_cache->key_cache->from_py_marshaller;
88 value_from_py_marshaller = hash_cache->value_cache->from_py_marshaller;
89
90 if (hash_cache->key_cache->type_tag == GI_TYPE_TAG_UTF8
91 || hash_cache->key_cache->type_tag == GI_TYPE_TAG_FILENAME) {
92 hash_func = g_str_hash;
93 equal_func = g_str_equal;
94 /* Transfer rule has been updated in hash_table_item_cache_new(). */
95 key_destroy_func = g_free;
96 }
97 if (hash_cache->value_cache->type_tag == GI_TYPE_TAG_UTF8
98 || hash_cache->value_cache->type_tag == GI_TYPE_TAG_FILENAME) {
99 value_destroy_func = g_free;
100 }
101
102 hash_ = g_hash_table_new_full (hash_func, equal_func, key_destroy_func,
103 value_destroy_func);
104 if (hash_ == NULL) {
105 PyErr_NoMemory ();
106 Py_DECREF (py_keys);
107 Py_DECREF (py_values);
108 return FALSE;
109 }
110
111 for (i = 0; i < length; i++) {
112 GIArgument key, value;
113 gpointer key_cleanup_data = NULL;
114 gpointer value_cleanup_data = NULL;
115 PyObject *py_key = PyList_GET_ITEM (py_keys, i);
116 PyObject *py_value = PyList_GET_ITEM (py_values, i);
117 if (py_key == NULL || py_value == NULL) goto err;
118
119 // LEAK: key_cleanup_data and value_cleanup_data are never cleaned
120 if (!key_from_py_marshaller (state, callable_cache,
121 hash_cache->key_cache, py_key, &key,
122 &key_cleanup_data))
123 goto err;
124
125 if (!value_from_py_marshaller (state, callable_cache,
126 hash_cache->value_cache, py_value,
127 &value, &value_cleanup_data))
128 goto err;
129
130 g_hash_table_insert (
131 hash_,
132 _pygi_arg_to_hash_pointer (key, hash_cache->key_cache->type_info),
133 _pygi_arg_to_hash_pointer (value,
134 hash_cache->value_cache->type_info));
135 continue;
136 err:
137 /* FIXME: cleanup hash keys and values */
138 Py_DECREF (py_keys);
139 Py_DECREF (py_values);
140 g_hash_table_unref (hash_);
141 _PyGI_ERROR_PREFIX ("Item %i: ", i);
142 return FALSE;
143 }
144
145 arg->v_pointer = hash_;
146 Py_DECREF (py_keys);
147 Py_DECREF (py_values);
148
149 if (arg_cache->transfer == GI_TRANSFER_NOTHING) {
150 /* Free everything in cleanup. */
151 *cleanup_data = arg->v_pointer;
152 } else if (arg_cache->transfer == GI_TRANSFER_CONTAINER) {
153 /* Make a shallow copy so we can free the elements later in cleanup
154 * because it is possible invoke will free the list before our cleanup. */
155 *cleanup_data = g_hash_table_ref (arg->v_pointer);
156 } else { /* GI_TRANSFER_EVERYTHING */
157 /* No cleanup, everything is given to the callee.
158 * Note that the keys and values will leak for transfer everything because
159 * we do not use g_hash_table_new_full and set key/value_destroy_func. */
160 *cleanup_data = NULL;
161 }
162
163 return TRUE;
164 }
165
166 static void
167 _pygi_marshal_cleanup_from_py_ghash (PyGIInvokeState *state,
168 PyGIArgCache *arg_cache, PyObject *py_arg,
169 gpointer data, gboolean was_processed)
170 {
171 if (data == NULL) return;
172
173 if (was_processed) {
174 GHashTable *hash_;
175 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
176
177 hash_ = (GHashTable *)data;
178
179 /* clean up keys and values first */
180 if (hash_cache->key_cache->from_py_cleanup != NULL
181 || hash_cache->value_cache->from_py_cleanup != NULL) {
182 GHashTableIter hiter;
183 gpointer key;
184 gpointer value;
185
186 PyGIMarshalFromPyCleanupFunc key_cleanup_func =
187 hash_cache->key_cache->from_py_cleanup;
188 PyGIMarshalFromPyCleanupFunc value_cleanup_func =
189 hash_cache->value_cache->from_py_cleanup;
190
191 g_hash_table_iter_init (&hiter, hash_);
192 while (g_hash_table_iter_next (&hiter, &key, &value)) {
193 if (key != NULL && key_cleanup_func != NULL)
194 key_cleanup_func (state, hash_cache->key_cache, NULL, key,
195 TRUE);
196 if (value != NULL && value_cleanup_func != NULL)
197 value_cleanup_func (state, hash_cache->value_cache, NULL,
198 value, TRUE);
199 }
200 }
201
202 g_hash_table_unref (hash_);
203 }
204 }
205
206 static PyObject *
207 _pygi_marshal_to_py_ghash (PyGIInvokeState *state,
208 PyGICallableCache *callable_cache,
209 PyGIArgCache *arg_cache, GIArgument *arg,
210 gpointer *cleanup_data)
211 {
212 GHashTable *hash_;
213 GHashTableIter hash_table_iter;
214
215 PyGIMarshalToPyFunc key_to_py_marshaller;
216 PyGIMarshalToPyFunc value_to_py_marshaller;
217
218 PyGIArgCache *key_arg_cache;
219 PyGIArgCache *value_arg_cache;
220 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
221
222 GIArgument key_arg;
223 GIArgument value_arg;
224
225 PyObject *py_obj = NULL;
226
227 hash_ = arg->v_pointer;
228
229 if (hash_ == NULL) {
230 py_obj = Py_None;
231 Py_INCREF (py_obj);
232 return py_obj;
233 }
234
235 py_obj = PyDict_New ();
236 if (py_obj == NULL) return NULL;
237
238 key_arg_cache = hash_cache->key_cache;
239 key_to_py_marshaller = key_arg_cache->to_py_marshaller;
240
241 value_arg_cache = hash_cache->value_cache;
242 value_to_py_marshaller = value_arg_cache->to_py_marshaller;
243
244 g_hash_table_iter_init (&hash_table_iter, hash_);
245 while (g_hash_table_iter_next (&hash_table_iter, &key_arg.v_pointer,
246 &value_arg.v_pointer)) {
247 gpointer key_cleanup_data = NULL;
248 gpointer value_cleanup_data = NULL;
249 PyObject *py_key;
250 PyObject *py_value;
251 int retval;
252
253
254 _pygi_hash_pointer_to_arg_in_place (&key_arg,
255 hash_cache->key_cache->type_info);
256 py_key = key_to_py_marshaller (state, callable_cache, key_arg_cache,
257 &key_arg, &key_cleanup_data);
258
259 if (py_key == NULL) {
260 Py_CLEAR (py_obj);
261 return NULL;
262 }
263
264 _pygi_hash_pointer_to_arg_in_place (
265 &value_arg, hash_cache->value_cache->type_info);
266 py_value = value_to_py_marshaller (state, callable_cache,
267 value_arg_cache, &value_arg,
268 &value_cleanup_data);
269
270 if (py_value == NULL) {
271 Py_CLEAR (py_obj);
272 Py_DECREF (py_key);
273 return NULL;
274 }
275
276 retval = PyDict_SetItem (py_obj, py_key, py_value);
277
278 Py_DECREF (py_key);
279 Py_DECREF (py_value);
280
281 if (retval < 0) {
282 Py_CLEAR (py_obj);
283 return NULL;
284 }
285 }
286
287 return py_obj;
288 }
289
290 static void
291 _pygi_marshal_cleanup_to_py_ghash (PyGIInvokeState *state,
292 PyGIArgCache *arg_cache,
293 gpointer cleanup_data, gpointer data,
294 gboolean was_processed)
295 {
296 if (data == NULL) return;
297
298 /* assume hashtable has boxed key and value */
299 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING
300 || arg_cache->transfer == GI_TRANSFER_CONTAINER)
301 g_hash_table_unref ((GHashTable *)data);
302 }
303
304 static void
305 _arg_cache_from_py_ghash_setup (PyGIArgCache *arg_cache)
306 {
307 arg_cache->from_py_marshaller = _pygi_marshal_from_py_ghash;
308 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_ghash;
309 }
310
311 static void
312 _arg_cache_to_py_ghash_setup (PyGIArgCache *arg_cache)
313 {
314 arg_cache->to_py_marshaller = _pygi_marshal_to_py_ghash;
315 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_ghash;
316 }
317
318 static PyGIArgCache *
319 hash_table_item_cache_new (GITypeInfo *type_info, int index,
320 GITransfer transfer, PyGIDirection direction,
321 PyGICallableCache *callable_cache)
322 {
323 PyGIArgCache *cache;
324 GITypeInfo *item_type_info =
325 gi_type_info_get_param_type (type_info, index);
326 GITypeTag item_type_tag = gi_type_info_get_tag (item_type_info);
327 GITransfer item_transfer;
328
329 if (item_type_tag == GI_TYPE_TAG_UTF8
330 || item_type_tag == GI_TYPE_TAG_FILENAME)
331 /* We assign a destroy notifier to the hash table,
332 * so we do not need to free strings ourselves. */
333 item_transfer = GI_TRANSFER_EVERYTHING;
334 else if (transfer == GI_TRANSFER_CONTAINER)
335 item_transfer = GI_TRANSFER_NOTHING;
336 else
337 item_transfer = transfer;
338
339 cache = pygi_arg_cache_new (item_type_info, NULL, item_transfer, direction,
340 callable_cache, 0, 0);
341
342 gi_base_info_unref ((GIBaseInfo *)item_type_info);
343
344 return cache;
345 }
346
347 PyGIArgCache *
348 pygi_arg_hash_table_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
349 GITransfer transfer,
350 PyGIDirection direction,
351 PyGICallableCache *callable_cache)
352 {
353 PyGIHashCache *hc = NULL;
354
355 hc = g_slice_new0 (PyGIHashCache);
356 if (hc == NULL) return NULL;
357
358 pygi_arg_base_setup ((PyGIArgCache *)hc, type_info, arg_info, transfer,
359 direction);
360
361 ((PyGIArgCache *)hc)->destroy_notify =
362 (GDestroyNotify)_hash_cache_free_func;
363
364 hc->key_cache = hash_table_item_cache_new (
365 type_info, /*index=*/0, transfer, direction, callable_cache);
366
367 if (hc->key_cache == NULL) {
368 pygi_arg_cache_free ((PyGIArgCache *)hc);
369 return NULL;
370 }
371
372 hc->value_cache = hash_table_item_cache_new (
373 type_info, /*index=*/1, transfer, direction, callable_cache);
374
375 if (hc->value_cache == NULL) {
376 pygi_arg_cache_free ((PyGIArgCache *)hc);
377 return NULL;
378 }
379
380 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
381 _arg_cache_from_py_ghash_setup ((PyGIArgCache *)hc);
382 }
383
384 if (direction & PYGI_DIRECTION_TO_PYTHON) {
385 _arg_cache_to_py_ghash_setup ((PyGIArgCache *)hc);
386 }
387
388 return (PyGIArgCache *)hc;
389 }