/src/glib/gobject/gclosure.c
Line | Count | Source |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 2000-2001 Red Hat, Inc. |
3 | | * Copyright (C) 2005 Imendio AB |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
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 |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | /* |
22 | | * MT safe with regards to reference counting. |
23 | | */ |
24 | | |
25 | | #include "config.h" |
26 | | |
27 | | #include "../glib/gvalgrind.h" |
28 | | #include <string.h> |
29 | | |
30 | | #include <ffi.h> |
31 | | |
32 | | #include "gclosure.h" |
33 | | #include "gboxed.h" |
34 | | #include "gobject.h" |
35 | | #include "genums.h" |
36 | | #include "gvalue.h" |
37 | | #include "gvaluetypes.h" |
38 | | #include "gtype-private.h" |
39 | | |
40 | | |
41 | | /** |
42 | | * GClosure: |
43 | | * @in_marshal: Indicates whether the closure is currently being invoked with |
44 | | * g_closure_invoke() |
45 | | * @is_invalid: Indicates whether the closure has been invalidated by |
46 | | * g_closure_invalidate() |
47 | | * |
48 | | * A `GClosure` represents a callback supplied by the programmer. |
49 | | * |
50 | | * It will generally comprise a function of some kind and a marshaller |
51 | | * used to call it. It is the responsibility of the marshaller to |
52 | | * convert the arguments for the invocation from #GValues into |
53 | | * a suitable form, perform the callback on the converted arguments, |
54 | | * and transform the return value back into a #GValue. |
55 | | * |
56 | | * In the case of C programs, a closure usually just holds a pointer |
57 | | * to a function and maybe a data argument, and the marshaller |
58 | | * converts between #GValue and native C types. The GObject |
59 | | * library provides the #GCClosure type for this purpose. Bindings for |
60 | | * other languages need marshallers which convert between #GValues |
61 | | * and suitable representations in the runtime of the language in |
62 | | * order to use functions written in that language as callbacks. Use |
63 | | * g_closure_set_marshal() to set the marshaller on such a custom |
64 | | * closure implementation. |
65 | | * |
66 | | * Within GObject, closures play an important role in the |
67 | | * implementation of signals. When a signal is registered, the |
68 | | * @c_marshaller argument to g_signal_new() specifies the default C |
69 | | * marshaller for any closure which is connected to this |
70 | | * signal. GObject provides a number of C marshallers for this |
71 | | * purpose, see the g_cclosure_marshal_*() functions. Additional C |
72 | | * marshallers can be generated with the [glib-genmarshal][glib-genmarshal] |
73 | | * utility. Closures can be explicitly connected to signals with |
74 | | * g_signal_connect_closure(), but it usually more convenient to let |
75 | | * GObject create a closure automatically by using one of the |
76 | | * g_signal_connect_*() functions which take a callback function/user |
77 | | * data pair. |
78 | | * |
79 | | * Using closures has a number of important advantages over a simple |
80 | | * callback function/data pointer combination: |
81 | | * |
82 | | * - Closures allow the callee to get the types of the callback parameters, |
83 | | * which means that language bindings don't have to write individual glue |
84 | | * for each callback type. |
85 | | * |
86 | | * - The reference counting of #GClosure makes it easy to handle reentrancy |
87 | | * right; if a callback is removed while it is being invoked, the closure |
88 | | * and its parameters won't be freed until the invocation finishes. |
89 | | * |
90 | | * - g_closure_invalidate() and invalidation notifiers allow callbacks to be |
91 | | * automatically removed when the objects they point to go away. |
92 | | */ |
93 | | |
94 | | #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1) |
95 | | #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1) |
96 | | #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1) |
97 | | #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1) |
98 | 0 | #define CLOSURE_N_MFUNCS(cl) (((cl)->n_guards << 1L)) |
99 | | /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */ |
100 | 0 | #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \ |
101 | 0 | (cl)->n_fnotifiers + \ |
102 | 0 | (cl)->n_inotifiers) |
103 | | |
104 | | typedef union { |
105 | | GClosure closure; |
106 | | gint vint; |
107 | | } ClosureInt; |
108 | | |
109 | 128k | #define ATOMIC_CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \ |
110 | 128k | G_STMT_START { \ |
111 | 128k | ClosureInt *cunion = (ClosureInt*) _closure; \ |
112 | 128k | gint new_int, old_int, success; \ |
113 | 128k | old_int = g_atomic_int_get (&cunion->vint); \ |
114 | 128k | do \ |
115 | 128k | { \ |
116 | 128k | ClosureInt tmp; \ |
117 | 128k | tmp.vint = old_int; \ |
118 | 128k | _SET_OLD tmp.closure._field; \ |
119 | 128k | tmp.closure._field _OP _value; \ |
120 | 128k | _SET_NEW tmp.closure._field; \ |
121 | 128k | new_int = tmp.vint; \ |
122 | 128k | success = g_atomic_int_compare_and_exchange_full (&cunion->vint, old_int, new_int,\ |
123 | 128k | &old_int); \ |
124 | 128k | } \ |
125 | 128k | while (!success && _must_set); \ |
126 | 128k | } G_STMT_END |
127 | | |
128 | 28.4k | #define ATOMIC_SWAP(_closure, _field, _value, _oldv) ATOMIC_CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) ) |
129 | 56.9k | #define ATOMIC_SET(_closure, _field, _value) ATOMIC_CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) ) |
130 | 0 | #define ATOMIC_INC(_closure, _field) ATOMIC_CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) ) |
131 | 14.2k | #define ATOMIC_INC_ASSIGN(_closure, _field, _newv) ATOMIC_CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = ) |
132 | 0 | #define ATOMIC_DEC(_closure, _field) ATOMIC_CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) ) |
133 | 28.4k | #define ATOMIC_DEC_ASSIGN(_closure, _field, _newv) ATOMIC_CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = ) |
134 | | |
135 | | enum { |
136 | | FNOTIFY, |
137 | | INOTIFY, |
138 | | PRE_NOTIFY, |
139 | | POST_NOTIFY |
140 | | }; |
141 | | |
142 | | |
143 | | /* --- functions --- */ |
144 | | /** |
145 | | * g_closure_new_simple: |
146 | | * @sizeof_closure: the size of the structure to allocate, must be at least |
147 | | * `sizeof (GClosure)` |
148 | | * @data: data to store in the @data field of the newly allocated #GClosure |
149 | | * |
150 | | * Allocates a struct of the given size and initializes the initial |
151 | | * part as a #GClosure. |
152 | | * |
153 | | * This function is mainly useful when implementing new types of closures: |
154 | | * |
155 | | * |[<!-- language="C" --> |
156 | | * typedef struct _MyClosure MyClosure; |
157 | | * struct _MyClosure |
158 | | * { |
159 | | * GClosure closure; |
160 | | * // extra data goes here |
161 | | * }; |
162 | | * |
163 | | * static void |
164 | | * my_closure_finalize (gpointer notify_data, |
165 | | * GClosure *closure) |
166 | | * { |
167 | | * MyClosure *my_closure = (MyClosure *)closure; |
168 | | * |
169 | | * // free extra data here |
170 | | * } |
171 | | * |
172 | | * MyClosure *my_closure_new (gpointer data) |
173 | | * { |
174 | | * GClosure *closure; |
175 | | * MyClosure *my_closure; |
176 | | * |
177 | | * closure = g_closure_new_simple (sizeof (MyClosure), data); |
178 | | * my_closure = (MyClosure *) closure; |
179 | | * |
180 | | * // initialize extra data here |
181 | | * |
182 | | * g_closure_add_finalize_notifier (closure, notify_data, |
183 | | * my_closure_finalize); |
184 | | * return my_closure; |
185 | | * } |
186 | | * ]| |
187 | | * |
188 | | * Returns: (transfer floating): a floating reference to a new #GClosure |
189 | | */ |
190 | | GClosure* |
191 | | g_closure_new_simple (guint sizeof_closure, |
192 | | gpointer data) |
193 | 14.2k | { |
194 | 14.2k | GClosure *closure; |
195 | 14.2k | gint private_size; |
196 | 14.2k | gchar *allocated; |
197 | | |
198 | 14.2k | g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL); |
199 | | |
200 | 14.2k | private_size = sizeof (GRealClosure) - sizeof (GClosure); |
201 | | |
202 | 14.2k | #ifdef ENABLE_VALGRIND |
203 | | /* See comments in gtype.c about what's going on here... */ |
204 | 14.2k | if (RUNNING_ON_VALGRIND) |
205 | 0 | { |
206 | 0 | private_size += sizeof (gpointer); |
207 | |
|
208 | 0 | allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer)); |
209 | |
|
210 | 0 | *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer); |
211 | |
|
212 | 0 | VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, sizeof_closure + sizeof (gpointer), 0, TRUE); |
213 | 0 | VALGRIND_MALLOCLIKE_BLOCK (allocated + sizeof (gpointer), private_size - sizeof (gpointer), 0, TRUE); |
214 | 0 | } |
215 | 14.2k | else |
216 | 14.2k | #endif |
217 | 14.2k | allocated = g_malloc0 (private_size + sizeof_closure); |
218 | | |
219 | 14.2k | closure = (GClosure *) (allocated + private_size); |
220 | | |
221 | 14.2k | ATOMIC_SET (closure, ref_count, 1); |
222 | 14.2k | ATOMIC_SET (closure, floating, TRUE); |
223 | 14.2k | closure->data = data; |
224 | | |
225 | 14.2k | return closure; |
226 | 14.2k | } |
227 | | |
228 | | static inline void |
229 | | closure_invoke_notifiers (GClosure *closure, |
230 | | guint notify_type) |
231 | 28.4k | { |
232 | | /* notifier layout: |
233 | | * n_guards n_guards n_fnotif. n_inotifiers |
234 | | * ->[[pre_guards][post_guards][fnotifiers][inotifiers]] |
235 | | * |
236 | | * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards; |
237 | | * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers |
238 | | * |
239 | | * constrains/catches: |
240 | | * - closure->notifiers may be reloacted during callback |
241 | | * - closure->n_fnotifiers and closure->n_inotifiers may change during callback |
242 | | * - i.e. callbacks can be removed/added during invocation |
243 | | * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=) |
244 | | * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify) |
245 | | * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY |
246 | | * + none of the callbacks can cause recursion |
247 | | * + closure->n_inotifiers is const 0 during FNOTIFY |
248 | | */ |
249 | 28.4k | switch (notify_type) |
250 | 28.4k | { |
251 | 0 | GClosureNotifyData *ndata; |
252 | 0 | guint i, offs; |
253 | 14.2k | case FNOTIFY: |
254 | 14.2k | while (closure->n_fnotifiers) |
255 | 0 | { |
256 | 0 | guint n; |
257 | 0 | ATOMIC_DEC_ASSIGN (closure, n_fnotifiers, &n); |
258 | |
|
259 | 0 | ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n; |
260 | 0 | closure->marshal = (GClosureMarshal) ndata->notify; |
261 | 0 | closure->data = ndata->data; |
262 | 0 | ndata->notify (ndata->data, closure); |
263 | 0 | } |
264 | 14.2k | closure->marshal = NULL; |
265 | 14.2k | closure->data = NULL; |
266 | 14.2k | break; |
267 | 14.2k | case INOTIFY: |
268 | 14.2k | ATOMIC_SET (closure, in_inotify, TRUE); |
269 | 14.2k | while (closure->n_inotifiers) |
270 | 0 | { |
271 | 0 | guint n; |
272 | 0 | ATOMIC_DEC_ASSIGN (closure, n_inotifiers, &n); |
273 | |
|
274 | 0 | ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n; |
275 | 0 | closure->marshal = (GClosureMarshal) ndata->notify; |
276 | 0 | closure->data = ndata->data; |
277 | 0 | ndata->notify (ndata->data, closure); |
278 | 0 | } |
279 | 14.2k | closure->marshal = NULL; |
280 | 14.2k | closure->data = NULL; |
281 | 14.2k | ATOMIC_SET (closure, in_inotify, FALSE); |
282 | 14.2k | break; |
283 | 0 | case PRE_NOTIFY: |
284 | 0 | i = closure->n_guards; |
285 | 0 | offs = 0; |
286 | 0 | while (i--) |
287 | 0 | { |
288 | 0 | ndata = closure->notifiers + offs + i; |
289 | 0 | ndata->notify (ndata->data, closure); |
290 | 0 | } |
291 | 0 | break; |
292 | 0 | case POST_NOTIFY: |
293 | 0 | i = closure->n_guards; |
294 | 0 | offs = i; |
295 | 0 | while (i--) |
296 | 0 | { |
297 | 0 | ndata = closure->notifiers + offs + i; |
298 | 0 | ndata->notify (ndata->data, closure); |
299 | 0 | } |
300 | 0 | break; |
301 | 28.4k | } |
302 | 28.4k | } |
303 | | |
304 | | static void |
305 | | g_closure_set_meta_va_marshal (GClosure *closure, |
306 | | GVaClosureMarshal va_meta_marshal) |
307 | 11 | { |
308 | 11 | GRealClosure *real_closure; |
309 | | |
310 | 11 | g_return_if_fail (closure != NULL); |
311 | 11 | g_return_if_fail (va_meta_marshal != NULL); |
312 | 11 | g_return_if_fail (closure->is_invalid == FALSE); |
313 | 11 | g_return_if_fail (closure->in_marshal == FALSE); |
314 | | |
315 | 11 | real_closure = G_REAL_CLOSURE (closure); |
316 | | |
317 | 11 | g_return_if_fail (real_closure->meta_marshal != NULL); |
318 | | |
319 | 11 | real_closure->va_meta_marshal = va_meta_marshal; |
320 | 11 | } |
321 | | |
322 | | /** |
323 | | * g_closure_set_meta_marshal: (skip) |
324 | | * @closure: a #GClosure |
325 | | * @marshal_data: (closure meta_marshal): context-dependent data to pass |
326 | | * to @meta_marshal |
327 | | * @meta_marshal: a #GClosureMarshal function |
328 | | * |
329 | | * Sets the meta marshaller of @closure. |
330 | | * |
331 | | * A meta marshaller wraps the @closure's marshal and modifies the way |
332 | | * it is called in some fashion. The most common use of this facility |
333 | | * is for C callbacks. |
334 | | * |
335 | | * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]), |
336 | | * are used everywhere, but the way that we get the callback function |
337 | | * differs. In most cases we want to use the @closure's callback, but in |
338 | | * other cases we want to use some different technique to retrieve the |
339 | | * callback function. |
340 | | * |
341 | | * For example, class closures for signals (see |
342 | | * g_signal_type_cclosure_new()) retrieve the callback function from a |
343 | | * fixed offset in the class structure. The meta marshaller retrieves |
344 | | * the right callback and passes it to the marshaller as the |
345 | | * @marshal_data argument. |
346 | | */ |
347 | | void |
348 | | g_closure_set_meta_marshal (GClosure *closure, |
349 | | gpointer marshal_data, |
350 | | GClosureMarshal meta_marshal) |
351 | 11 | { |
352 | 11 | GRealClosure *real_closure; |
353 | | |
354 | 11 | g_return_if_fail (closure != NULL); |
355 | 11 | g_return_if_fail (meta_marshal != NULL); |
356 | 11 | g_return_if_fail (closure->is_invalid == FALSE); |
357 | 11 | g_return_if_fail (closure->in_marshal == FALSE); |
358 | | |
359 | 11 | real_closure = G_REAL_CLOSURE (closure); |
360 | | |
361 | 11 | g_return_if_fail (real_closure->meta_marshal == NULL); |
362 | | |
363 | 11 | real_closure->meta_marshal = meta_marshal; |
364 | 11 | real_closure->meta_marshal_data = marshal_data; |
365 | 11 | } |
366 | | |
367 | | /** |
368 | | * g_closure_add_marshal_guards: (skip) |
369 | | * @closure: a #GClosure |
370 | | * @pre_marshal_data: (closure pre_marshal_notify): data to pass |
371 | | * to @pre_marshal_notify |
372 | | * @pre_marshal_notify: a function to call before the closure callback |
373 | | * @post_marshal_data: (closure post_marshal_notify): data to pass |
374 | | * to @post_marshal_notify |
375 | | * @post_marshal_notify: a function to call after the closure callback |
376 | | * |
377 | | * Adds a pair of notifiers which get invoked before and after the |
378 | | * closure callback, respectively. |
379 | | * |
380 | | * This is typically used to protect the extra arguments for the |
381 | | * duration of the callback. See g_object_watch_closure() for an |
382 | | * example of marshal guards. |
383 | | */ |
384 | | void |
385 | | g_closure_add_marshal_guards (GClosure *closure, |
386 | | gpointer pre_marshal_data, |
387 | | GClosureNotify pre_marshal_notify, |
388 | | gpointer post_marshal_data, |
389 | | GClosureNotify post_marshal_notify) |
390 | 0 | { |
391 | 0 | guint i; |
392 | |
|
393 | 0 | g_return_if_fail (closure != NULL); |
394 | 0 | g_return_if_fail (pre_marshal_notify != NULL); |
395 | 0 | g_return_if_fail (post_marshal_notify != NULL); |
396 | 0 | g_return_if_fail (closure->is_invalid == FALSE); |
397 | 0 | g_return_if_fail (closure->in_marshal == FALSE); |
398 | 0 | g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS); |
399 | | |
400 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2); |
401 | 0 | if (closure->n_inotifiers) |
402 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
403 | 0 | closure->n_fnotifiers + |
404 | 0 | closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
405 | 0 | closure->n_fnotifiers + 0)]; |
406 | 0 | if (closure->n_inotifiers > 1) |
407 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
408 | 0 | closure->n_fnotifiers + |
409 | 0 | closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
410 | 0 | closure->n_fnotifiers + 1)]; |
411 | 0 | if (closure->n_fnotifiers) |
412 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
413 | 0 | closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0]; |
414 | 0 | if (closure->n_fnotifiers > 1) |
415 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
416 | 0 | closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1]; |
417 | 0 | if (closure->n_guards) |
418 | 0 | closure->notifiers[(closure->n_guards + |
419 | 0 | closure->n_guards + 1)] = closure->notifiers[closure->n_guards]; |
420 | 0 | i = closure->n_guards; |
421 | 0 | closure->notifiers[i].data = pre_marshal_data; |
422 | 0 | closure->notifiers[i].notify = pre_marshal_notify; |
423 | 0 | closure->notifiers[i + 1].data = post_marshal_data; |
424 | 0 | closure->notifiers[i + 1].notify = post_marshal_notify; |
425 | 0 | ATOMIC_INC (closure, n_guards); |
426 | 0 | } |
427 | | |
428 | | /** |
429 | | * g_closure_add_finalize_notifier: (skip) |
430 | | * @closure: a #GClosure |
431 | | * @notify_data: (closure notify_func): data to pass to @notify_func |
432 | | * @notify_func: the callback function to register |
433 | | * |
434 | | * Registers a finalization notifier which will be called when the |
435 | | * reference count of @closure goes down to 0. |
436 | | * |
437 | | * Multiple finalization notifiers on a single closure are invoked in |
438 | | * unspecified order. If a single call to g_closure_unref() results in |
439 | | * the closure being both invalidated and finalized, then the invalidate |
440 | | * notifiers will be run before the finalize notifiers. |
441 | | */ |
442 | | void |
443 | | g_closure_add_finalize_notifier (GClosure *closure, |
444 | | gpointer notify_data, |
445 | | GClosureNotify notify_func) |
446 | 0 | { |
447 | 0 | guint i; |
448 | |
|
449 | 0 | g_return_if_fail (closure != NULL); |
450 | 0 | g_return_if_fail (notify_func != NULL); |
451 | 0 | g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS); |
452 | | |
453 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); |
454 | 0 | if (closure->n_inotifiers) |
455 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
456 | 0 | closure->n_fnotifiers + |
457 | 0 | closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
458 | 0 | closure->n_fnotifiers + 0)]; |
459 | 0 | i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers; |
460 | 0 | closure->notifiers[i].data = notify_data; |
461 | 0 | closure->notifiers[i].notify = notify_func; |
462 | 0 | ATOMIC_INC (closure, n_fnotifiers); |
463 | 0 | } |
464 | | |
465 | | /** |
466 | | * g_closure_add_invalidate_notifier: (skip) |
467 | | * @closure: a #GClosure |
468 | | * @notify_data: (closure notify_func): data to pass to @notify_func |
469 | | * @notify_func: the callback function to register |
470 | | * |
471 | | * Registers an invalidation notifier which will be called when the |
472 | | * @closure is invalidated with g_closure_invalidate(). |
473 | | * |
474 | | * Invalidation notifiers are invoked before finalization notifiers, |
475 | | * in an unspecified order. |
476 | | */ |
477 | | void |
478 | | g_closure_add_invalidate_notifier (GClosure *closure, |
479 | | gpointer notify_data, |
480 | | GClosureNotify notify_func) |
481 | 0 | { |
482 | 0 | guint i; |
483 | |
|
484 | 0 | g_return_if_fail (closure != NULL); |
485 | 0 | g_return_if_fail (notify_func != NULL); |
486 | 0 | g_return_if_fail (closure->is_invalid == FALSE); |
487 | 0 | g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS); |
488 | | |
489 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); |
490 | 0 | i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers; |
491 | 0 | closure->notifiers[i].data = notify_data; |
492 | 0 | closure->notifiers[i].notify = notify_func; |
493 | 0 | ATOMIC_INC (closure, n_inotifiers); |
494 | 0 | } |
495 | | |
496 | | static inline gboolean |
497 | | closure_try_remove_inotify (GClosure *closure, |
498 | | gpointer notify_data, |
499 | | GClosureNotify notify_func) |
500 | 0 | { |
501 | 0 | GClosureNotifyData *ndata, *nlast; |
502 | |
|
503 | 0 | nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1; |
504 | 0 | for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++) |
505 | 0 | if (ndata->notify == notify_func && ndata->data == notify_data) |
506 | 0 | { |
507 | 0 | ATOMIC_DEC (closure, n_inotifiers); |
508 | 0 | if (ndata < nlast) |
509 | 0 | *ndata = *nlast; |
510 | |
|
511 | 0 | return TRUE; |
512 | 0 | } |
513 | 0 | return FALSE; |
514 | 0 | } |
515 | | |
516 | | static inline gboolean |
517 | | closure_try_remove_fnotify (GClosure *closure, |
518 | | gpointer notify_data, |
519 | | GClosureNotify notify_func) |
520 | 0 | { |
521 | 0 | GClosureNotifyData *ndata, *nlast; |
522 | |
|
523 | 0 | nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1; |
524 | 0 | for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++) |
525 | 0 | if (ndata->notify == notify_func && ndata->data == notify_data) |
526 | 0 | { |
527 | 0 | ATOMIC_DEC (closure, n_fnotifiers); |
528 | 0 | if (ndata < nlast) |
529 | 0 | *ndata = *nlast; |
530 | 0 | if (closure->n_inotifiers) |
531 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
532 | 0 | closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
533 | 0 | closure->n_fnotifiers + |
534 | 0 | closure->n_inotifiers)]; |
535 | 0 | return TRUE; |
536 | 0 | } |
537 | 0 | return FALSE; |
538 | 0 | } |
539 | | |
540 | | /** |
541 | | * g_closure_ref: |
542 | | * @closure: #GClosure to increment the reference count on |
543 | | * |
544 | | * Increments the reference count on a closure to force it staying |
545 | | * alive while the caller holds a pointer to it. |
546 | | * |
547 | | * Returns: (transfer none): The @closure passed in, for convenience |
548 | | */ |
549 | | GClosure* |
550 | | g_closure_ref (GClosure *closure) |
551 | 14.2k | { |
552 | 14.2k | guint new_ref_count; |
553 | 14.2k | g_return_val_if_fail (closure != NULL, NULL); |
554 | 14.2k | g_return_val_if_fail (closure->ref_count > 0, NULL); |
555 | 14.2k | g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL); |
556 | | |
557 | 14.2k | ATOMIC_INC_ASSIGN (closure, ref_count, &new_ref_count); |
558 | 14.2k | g_return_val_if_fail (new_ref_count > 1, NULL); |
559 | | |
560 | 14.2k | return closure; |
561 | 14.2k | } |
562 | | |
563 | | static void |
564 | | closure_invalidate_internal (GClosure *closure) |
565 | 14.2k | { |
566 | 14.2k | gboolean was_invalid; |
567 | | |
568 | 14.2k | ATOMIC_SWAP (closure, is_invalid, TRUE, &was_invalid); |
569 | | /* invalidate only once */ |
570 | 14.2k | if (!was_invalid) |
571 | 14.2k | closure_invoke_notifiers (closure, INOTIFY); |
572 | 14.2k | } |
573 | | |
574 | | /** |
575 | | * g_closure_invalidate: |
576 | | * @closure: #GClosure to invalidate |
577 | | * |
578 | | * Sets a flag on the closure to indicate that its calling |
579 | | * environment has become invalid, and thus causes any future |
580 | | * invocations of g_closure_invoke() on this @closure to be |
581 | | * ignored. |
582 | | * |
583 | | * Also, invalidation notifiers installed on the closure will |
584 | | * be called at this point. Note that unless you are holding a |
585 | | * reference to the closure yourself, the invalidation notifiers may |
586 | | * unref the closure and cause it to be destroyed, so if you need to |
587 | | * access the closure after calling g_closure_invalidate(), make sure |
588 | | * that you've previously called g_closure_ref(). |
589 | | * |
590 | | * Note that g_closure_invalidate() will also be called when the |
591 | | * reference count of a closure drops to zero (unless it has already |
592 | | * been invalidated before). |
593 | | */ |
594 | | void |
595 | | g_closure_invalidate (GClosure *closure) |
596 | 0 | { |
597 | 0 | g_return_if_fail (closure != NULL); |
598 | | |
599 | 0 | if (!closure->is_invalid) |
600 | 0 | { |
601 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
602 | 0 | closure_invalidate_internal (closure); |
603 | 0 | g_closure_unref (closure); |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | | /** |
608 | | * g_closure_unref: |
609 | | * @closure: #GClosure to decrement the reference count on |
610 | | * |
611 | | * Decrements the reference count of a closure after it was previously |
612 | | * incremented by the same caller. |
613 | | * |
614 | | * If no other callers are using the closure, then the closure will be |
615 | | * destroyed and freed. |
616 | | */ |
617 | | void |
618 | | g_closure_unref (GClosure *closure) |
619 | 28.4k | { |
620 | 28.4k | guint new_ref_count; |
621 | | |
622 | 28.4k | g_return_if_fail (closure != NULL); |
623 | 28.4k | g_return_if_fail (closure->ref_count > 0); |
624 | | |
625 | | /* last unref, invalidate first */ |
626 | 28.4k | if (closure->ref_count == 1 && !closure->is_invalid) |
627 | 14.2k | closure_invalidate_internal (closure); |
628 | | |
629 | 28.4k | ATOMIC_DEC_ASSIGN (closure, ref_count, &new_ref_count); |
630 | | |
631 | 28.4k | if (new_ref_count == 0) |
632 | 14.2k | { |
633 | 14.2k | closure_invoke_notifiers (closure, FNOTIFY); |
634 | 14.2k | g_free (closure->notifiers); |
635 | | |
636 | 14.2k | #ifdef ENABLE_VALGRIND |
637 | | /* See comments in gtype.c about what's going on here... */ |
638 | 14.2k | if (RUNNING_ON_VALGRIND) |
639 | 0 | { |
640 | 0 | gchar *allocated; |
641 | |
|
642 | 0 | allocated = (gchar *) G_REAL_CLOSURE (closure); |
643 | 0 | allocated -= sizeof (gpointer); |
644 | |
|
645 | 0 | g_free (allocated); |
646 | |
|
647 | 0 | VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0); |
648 | 0 | VALGRIND_FREELIKE_BLOCK (closure, 0); |
649 | 0 | } |
650 | 14.2k | else |
651 | 14.2k | #endif |
652 | 14.2k | g_free (G_REAL_CLOSURE (closure)); |
653 | 14.2k | } |
654 | 28.4k | } |
655 | | |
656 | | /** |
657 | | * g_closure_sink: |
658 | | * @closure: #GClosure to decrement the initial reference count on, if it's |
659 | | * still being held |
660 | | * |
661 | | * Takes over the initial ownership of a closure. |
662 | | * |
663 | | * Each closure is initially created in a "floating" state, which means |
664 | | * that the initial reference count is not owned by any caller. |
665 | | * |
666 | | * This function checks to see if the object is still floating, and if so, |
667 | | * unsets the floating state and decreases the reference count. If the |
668 | | * closure is not floating, g_closure_sink() does nothing. |
669 | | * |
670 | | * The reason for the existence of the floating state is to prevent |
671 | | * cumbersome code sequences like: |
672 | | * |
673 | | * |[<!-- language="C" --> |
674 | | * closure = g_cclosure_new (cb_func, cb_data); |
675 | | * g_source_set_closure (source, closure); |
676 | | * g_closure_unref (closure); // GObject doesn't really need this |
677 | | * ]| |
678 | | * |
679 | | * Because g_source_set_closure() (and similar functions) take ownership of the |
680 | | * initial reference count, if it is unowned, we instead can write: |
681 | | * |
682 | | * |[<!-- language="C" --> |
683 | | * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data)); |
684 | | * ]| |
685 | | * |
686 | | * Generally, this function is used together with g_closure_ref(). An example |
687 | | * of storing a closure for later notification looks like: |
688 | | * |
689 | | * |[<!-- language="C" --> |
690 | | * static GClosure *notify_closure = NULL; |
691 | | * void |
692 | | * foo_notify_set_closure (GClosure *closure) |
693 | | * { |
694 | | * if (notify_closure) |
695 | | * g_closure_unref (notify_closure); |
696 | | * notify_closure = closure; |
697 | | * if (notify_closure) |
698 | | * { |
699 | | * g_closure_ref (notify_closure); |
700 | | * g_closure_sink (notify_closure); |
701 | | * } |
702 | | * } |
703 | | * ]| |
704 | | * |
705 | | * Because g_closure_sink() may decrement the reference count of a closure |
706 | | * (if it hasn't been called on @closure yet) just like g_closure_unref(), |
707 | | * g_closure_ref() should be called prior to this function. |
708 | | */ |
709 | | void |
710 | | g_closure_sink (GClosure *closure) |
711 | 14.2k | { |
712 | 14.2k | g_return_if_fail (closure != NULL); |
713 | 14.2k | g_return_if_fail (closure->ref_count > 0); |
714 | | |
715 | | /* floating is basically a kludge to avoid creating closures |
716 | | * with a ref_count of 0. so the initial ref_count a closure has |
717 | | * is unowned. with invoking g_closure_sink() code may |
718 | | * indicate that it takes over that initial ref_count. |
719 | | */ |
720 | 14.2k | if (closure->floating) |
721 | 14.2k | { |
722 | 14.2k | gboolean was_floating; |
723 | 14.2k | ATOMIC_SWAP (closure, floating, FALSE, &was_floating); |
724 | | /* unref floating flag only once */ |
725 | 14.2k | if (was_floating) |
726 | 14.2k | g_closure_unref (closure); |
727 | 14.2k | } |
728 | 14.2k | } |
729 | | |
730 | | /** |
731 | | * g_closure_remove_invalidate_notifier: (skip) |
732 | | * @closure: a #GClosure |
733 | | * @notify_data: data which was passed to g_closure_add_invalidate_notifier() |
734 | | * when registering @notify_func |
735 | | * @notify_func: the callback function to remove |
736 | | * |
737 | | * Removes an invalidation notifier. |
738 | | * |
739 | | * Notice that notifiers are automatically removed after they are run. |
740 | | */ |
741 | | void |
742 | | g_closure_remove_invalidate_notifier (GClosure *closure, |
743 | | gpointer notify_data, |
744 | | GClosureNotify notify_func) |
745 | 0 | { |
746 | 0 | g_return_if_fail (closure != NULL); |
747 | 0 | g_return_if_fail (notify_func != NULL); |
748 | | |
749 | 0 | if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */ |
750 | 0 | ((gpointer) closure->marshal) == ((gpointer) notify_func) && |
751 | 0 | closure->data == notify_data) |
752 | 0 | closure->marshal = NULL; |
753 | 0 | else if (!closure_try_remove_inotify (closure, notify_data, notify_func)) |
754 | 0 | g_critical (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)", |
755 | 0 | notify_func, notify_data); |
756 | 0 | } |
757 | | |
758 | | /** |
759 | | * g_closure_remove_finalize_notifier: (skip) |
760 | | * @closure: a #GClosure |
761 | | * @notify_data: data which was passed to g_closure_add_finalize_notifier() |
762 | | * when registering @notify_func |
763 | | * @notify_func: the callback function to remove |
764 | | * |
765 | | * Removes a finalization notifier. |
766 | | * |
767 | | * Notice that notifiers are automatically removed after they are run. |
768 | | */ |
769 | | void |
770 | | g_closure_remove_finalize_notifier (GClosure *closure, |
771 | | gpointer notify_data, |
772 | | GClosureNotify notify_func) |
773 | 0 | { |
774 | 0 | g_return_if_fail (closure != NULL); |
775 | 0 | g_return_if_fail (notify_func != NULL); |
776 | | |
777 | 0 | if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */ |
778 | 0 | ((gpointer) closure->marshal) == ((gpointer) notify_func) && |
779 | 0 | closure->data == notify_data) |
780 | 0 | closure->marshal = NULL; |
781 | 0 | else if (!closure_try_remove_fnotify (closure, notify_data, notify_func)) |
782 | 0 | g_critical (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)", |
783 | 0 | notify_func, notify_data); |
784 | 0 | } |
785 | | |
786 | | /** |
787 | | * g_closure_invoke: |
788 | | * @closure: a #GClosure |
789 | | * @return_value: (optional) (out): a #GValue to store the return |
790 | | * value. May be %NULL if the callback of @closure |
791 | | * doesn't return a value. |
792 | | * @n_param_values: the length of the @param_values array |
793 | | * @param_values: (array length=n_param_values): an array of |
794 | | * #GValues holding the arguments on which to |
795 | | * invoke the callback of @closure |
796 | | * @invocation_hint: (nullable): a context-dependent invocation hint |
797 | | * |
798 | | * Invokes the closure, i.e. executes the callback represented by the @closure. |
799 | | */ |
800 | | void |
801 | | g_closure_invoke (GClosure *closure, |
802 | | GValue /*out*/ *return_value, |
803 | | guint n_param_values, |
804 | | const GValue *param_values, |
805 | | gpointer invocation_hint) |
806 | 0 | { |
807 | 0 | GRealClosure *real_closure; |
808 | |
|
809 | 0 | g_return_if_fail (closure != NULL); |
810 | | |
811 | 0 | real_closure = G_REAL_CLOSURE (closure); |
812 | |
|
813 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
814 | 0 | if (!closure->is_invalid) |
815 | 0 | { |
816 | 0 | GClosureMarshal marshal; |
817 | 0 | gpointer marshal_data; |
818 | 0 | gboolean in_marshal = closure->in_marshal; |
819 | |
|
820 | 0 | g_return_if_fail (closure->marshal || real_closure->meta_marshal); |
821 | | |
822 | 0 | ATOMIC_SET (closure, in_marshal, TRUE); |
823 | 0 | if (real_closure->meta_marshal) |
824 | 0 | { |
825 | 0 | marshal_data = real_closure->meta_marshal_data; |
826 | 0 | marshal = real_closure->meta_marshal; |
827 | 0 | } |
828 | 0 | else |
829 | 0 | { |
830 | 0 | marshal_data = NULL; |
831 | 0 | marshal = closure->marshal; |
832 | 0 | } |
833 | 0 | if (!in_marshal) |
834 | 0 | closure_invoke_notifiers (closure, PRE_NOTIFY); |
835 | 0 | marshal (closure, |
836 | 0 | return_value, |
837 | 0 | n_param_values, param_values, |
838 | 0 | invocation_hint, |
839 | 0 | marshal_data); |
840 | 0 | if (!in_marshal) |
841 | 0 | closure_invoke_notifiers (closure, POST_NOTIFY); |
842 | 0 | ATOMIC_SET (closure, in_marshal, in_marshal); |
843 | 0 | } |
844 | 0 | g_closure_unref (closure); |
845 | 0 | } |
846 | | |
847 | | gboolean |
848 | | _g_closure_supports_invoke_va (GClosure *closure) |
849 | 0 | { |
850 | 0 | GRealClosure *real_closure; |
851 | |
|
852 | 0 | g_return_val_if_fail (closure != NULL, FALSE); |
853 | | |
854 | 0 | real_closure = G_REAL_CLOSURE (closure); |
855 | |
|
856 | 0 | return |
857 | 0 | real_closure->va_marshal != NULL && |
858 | 0 | (real_closure->meta_marshal == NULL || |
859 | 0 | real_closure->va_meta_marshal != NULL); |
860 | 0 | } |
861 | | |
862 | | void |
863 | | _g_closure_invoke_va (GClosure *closure, |
864 | | GValue /*out*/ *return_value, |
865 | | gpointer instance, |
866 | | va_list args, |
867 | | int n_params, |
868 | | GType *param_types) |
869 | 0 | { |
870 | 0 | GRealClosure *real_closure; |
871 | |
|
872 | 0 | g_return_if_fail (closure != NULL); |
873 | | |
874 | 0 | real_closure = G_REAL_CLOSURE (closure); |
875 | |
|
876 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
877 | 0 | if (!closure->is_invalid) |
878 | 0 | { |
879 | 0 | GVaClosureMarshal marshal; |
880 | 0 | gpointer marshal_data; |
881 | 0 | gboolean in_marshal = closure->in_marshal; |
882 | |
|
883 | 0 | g_return_if_fail (closure->marshal || real_closure->meta_marshal); |
884 | | |
885 | 0 | ATOMIC_SET (closure, in_marshal, TRUE); |
886 | 0 | if (real_closure->va_meta_marshal) |
887 | 0 | { |
888 | 0 | marshal_data = real_closure->meta_marshal_data; |
889 | 0 | marshal = real_closure->va_meta_marshal; |
890 | 0 | } |
891 | 0 | else |
892 | 0 | { |
893 | 0 | marshal_data = NULL; |
894 | 0 | marshal = real_closure->va_marshal; |
895 | 0 | } |
896 | 0 | if (!in_marshal) |
897 | 0 | closure_invoke_notifiers (closure, PRE_NOTIFY); |
898 | 0 | marshal (closure, |
899 | 0 | return_value, |
900 | 0 | instance, args, |
901 | 0 | marshal_data, |
902 | 0 | n_params, param_types); |
903 | 0 | if (!in_marshal) |
904 | 0 | closure_invoke_notifiers (closure, POST_NOTIFY); |
905 | 0 | ATOMIC_SET (closure, in_marshal, in_marshal); |
906 | 0 | } |
907 | 0 | g_closure_unref (closure); |
908 | 0 | } |
909 | | |
910 | | |
911 | | /** |
912 | | * g_closure_set_marshal: (skip) |
913 | | * @closure: a #GClosure |
914 | | * @marshal: a #GClosureMarshal function |
915 | | * |
916 | | * Sets the marshaller of @closure. |
917 | | * |
918 | | * The `marshal_data` of @marshal provides a way for a meta marshaller to |
919 | | * provide additional information to the marshaller. |
920 | | * |
921 | | * For GObject's C predefined marshallers (the `g_cclosure_marshal_*()` |
922 | | * functions), what it provides is a callback function to use instead of |
923 | | * @closure->callback. |
924 | | * |
925 | | * See also: g_closure_set_meta_marshal() |
926 | | */ |
927 | | void |
928 | | g_closure_set_marshal (GClosure *closure, |
929 | | GClosureMarshal marshal) |
930 | 14.2k | { |
931 | 14.2k | g_return_if_fail (closure != NULL); |
932 | 14.2k | g_return_if_fail (marshal != NULL); |
933 | | |
934 | 14.2k | if (closure->marshal && closure->marshal != marshal) |
935 | 0 | g_critical ("attempt to override closure->marshal (%p) with new marshal (%p)", |
936 | 14.2k | closure->marshal, marshal); |
937 | 14.2k | else |
938 | 14.2k | closure->marshal = marshal; |
939 | 14.2k | } |
940 | | |
941 | | void |
942 | | _g_closure_set_va_marshal (GClosure *closure, |
943 | | GVaClosureMarshal marshal) |
944 | 3 | { |
945 | 3 | GRealClosure *real_closure; |
946 | | |
947 | 3 | g_return_if_fail (closure != NULL); |
948 | 3 | g_return_if_fail (marshal != NULL); |
949 | | |
950 | 3 | real_closure = G_REAL_CLOSURE (closure); |
951 | | |
952 | 3 | if (real_closure->va_marshal && real_closure->va_marshal != marshal) |
953 | 0 | g_critical ("attempt to override closure->va_marshal (%p) with new marshal (%p)", |
954 | 3 | real_closure->va_marshal, marshal); |
955 | 3 | else |
956 | 3 | real_closure->va_marshal = marshal; |
957 | 3 | } |
958 | | |
959 | | /** |
960 | | * g_cclosure_new: (skip) |
961 | | * @callback_func: the function to invoke |
962 | | * @user_data: (closure callback_func): user data to pass to @callback_func |
963 | | * @destroy_data: destroy notify to be called when @user_data is no longer used |
964 | | * |
965 | | * Creates a new closure which invokes @callback_func with @user_data as |
966 | | * the last parameter. |
967 | | * |
968 | | * @destroy_data will be called as a finalize notifier on the #GClosure. |
969 | | * |
970 | | * Returns: (transfer floating): a floating reference to a new #GCClosure |
971 | | */ |
972 | | GClosure* |
973 | | g_cclosure_new (GCallback callback_func, |
974 | | gpointer user_data, |
975 | | GClosureNotify destroy_data) |
976 | 14.2k | { |
977 | 14.2k | GClosure *closure; |
978 | | |
979 | 14.2k | g_return_val_if_fail (callback_func != NULL, NULL); |
980 | | |
981 | 14.2k | closure = g_closure_new_simple (sizeof (GCClosure), user_data); |
982 | 14.2k | if (destroy_data) |
983 | 0 | g_closure_add_finalize_notifier (closure, user_data, destroy_data); |
984 | 14.2k | ((GCClosure*) closure)->callback = (gpointer) callback_func; |
985 | | |
986 | 14.2k | return closure; |
987 | 14.2k | } |
988 | | |
989 | | /** |
990 | | * g_cclosure_new_swap: (skip) |
991 | | * @callback_func: the function to invoke |
992 | | * @user_data: (closure callback_func): user data to pass to @callback_func |
993 | | * @destroy_data: destroy notify to be called when @user_data is no longer used |
994 | | * |
995 | | * Creates a new closure which invokes @callback_func with @user_data as |
996 | | * the first parameter. |
997 | | * |
998 | | * @destroy_data will be called as a finalize notifier on the #GClosure. |
999 | | * |
1000 | | * Returns: (transfer floating): a floating reference to a new #GCClosure |
1001 | | */ |
1002 | | GClosure* |
1003 | | g_cclosure_new_swap (GCallback callback_func, |
1004 | | gpointer user_data, |
1005 | | GClosureNotify destroy_data) |
1006 | 0 | { |
1007 | 0 | GClosure *closure; |
1008 | | |
1009 | 0 | g_return_val_if_fail (callback_func != NULL, NULL); |
1010 | | |
1011 | 0 | closure = g_closure_new_simple (sizeof (GCClosure), user_data); |
1012 | 0 | if (destroy_data) |
1013 | 0 | g_closure_add_finalize_notifier (closure, user_data, destroy_data); |
1014 | 0 | ((GCClosure*) closure)->callback = (gpointer) callback_func; |
1015 | 0 | ATOMIC_SET (closure, derivative_flag, TRUE); |
1016 | | |
1017 | 0 | return closure; |
1018 | 0 | } |
1019 | | |
1020 | | static void |
1021 | | g_type_class_meta_marshal (GClosure *closure, |
1022 | | GValue /*out*/ *return_value, |
1023 | | guint n_param_values, |
1024 | | const GValue *param_values, |
1025 | | gpointer invocation_hint, |
1026 | | gpointer marshal_data) |
1027 | 0 | { |
1028 | 0 | GTypeClass *class; |
1029 | 0 | gpointer callback; |
1030 | | /* GType itype = (GType) closure->data; */ |
1031 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1032 | | |
1033 | 0 | class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass); |
1034 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1035 | 0 | if (callback) |
1036 | 0 | closure->marshal (closure, |
1037 | 0 | return_value, |
1038 | 0 | n_param_values, param_values, |
1039 | 0 | invocation_hint, |
1040 | 0 | callback); |
1041 | 0 | } |
1042 | | |
1043 | | static void |
1044 | | g_type_class_meta_marshalv (GClosure *closure, |
1045 | | GValue *return_value, |
1046 | | gpointer instance, |
1047 | | va_list args, |
1048 | | gpointer marshal_data, |
1049 | | int n_params, |
1050 | | GType *param_types) |
1051 | 0 | { |
1052 | 0 | GRealClosure *real_closure; |
1053 | 0 | GTypeClass *class; |
1054 | 0 | gpointer callback; |
1055 | | /* GType itype = (GType) closure->data; */ |
1056 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1057 | |
|
1058 | 0 | real_closure = G_REAL_CLOSURE (closure); |
1059 | |
|
1060 | 0 | class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass); |
1061 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1062 | 0 | if (callback) |
1063 | 0 | real_closure->va_marshal (closure, |
1064 | 0 | return_value, |
1065 | 0 | instance, args, |
1066 | 0 | callback, |
1067 | 0 | n_params, |
1068 | 0 | param_types); |
1069 | 0 | } |
1070 | | |
1071 | | static void |
1072 | | g_type_iface_meta_marshal (GClosure *closure, |
1073 | | GValue /*out*/ *return_value, |
1074 | | guint n_param_values, |
1075 | | const GValue *param_values, |
1076 | | gpointer invocation_hint, |
1077 | | gpointer marshal_data) |
1078 | 0 | { |
1079 | 0 | GTypeClass *class; |
1080 | 0 | gpointer callback; |
1081 | 0 | GType itype = (GType) closure->data; |
1082 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1083 | | |
1084 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass); |
1085 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1086 | 0 | if (callback) |
1087 | 0 | closure->marshal (closure, |
1088 | 0 | return_value, |
1089 | 0 | n_param_values, param_values, |
1090 | 0 | invocation_hint, |
1091 | 0 | callback); |
1092 | 0 | } |
1093 | | |
1094 | | gboolean |
1095 | | _g_closure_is_void (GClosure *closure, |
1096 | | gpointer instance) |
1097 | 25.2k | { |
1098 | 25.2k | GRealClosure *real_closure; |
1099 | 25.2k | GTypeClass *class; |
1100 | 25.2k | gpointer callback; |
1101 | 25.2k | GType itype; |
1102 | 25.2k | guint offset; |
1103 | | |
1104 | 25.2k | if (closure->is_invalid) |
1105 | 0 | return TRUE; |
1106 | | |
1107 | 25.2k | real_closure = G_REAL_CLOSURE (closure); |
1108 | | |
1109 | 25.2k | if (real_closure->meta_marshal == g_type_iface_meta_marshal) |
1110 | 0 | { |
1111 | 0 | itype = (GType) closure->data; |
1112 | 0 | offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data); |
1113 | |
|
1114 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass); |
1115 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1116 | 0 | return callback == NULL; |
1117 | 0 | } |
1118 | 25.2k | else if (real_closure->meta_marshal == g_type_class_meta_marshal) |
1119 | 25.2k | { |
1120 | 25.2k | offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data); |
1121 | | |
1122 | 25.2k | class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass); |
1123 | 25.2k | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1124 | 25.2k | return callback == NULL; |
1125 | 25.2k | } |
1126 | | |
1127 | 0 | return FALSE; |
1128 | 25.2k | } |
1129 | | |
1130 | | static void |
1131 | | g_type_iface_meta_marshalv (GClosure *closure, |
1132 | | GValue *return_value, |
1133 | | gpointer instance, |
1134 | | va_list args, |
1135 | | gpointer marshal_data, |
1136 | | int n_params, |
1137 | | GType *param_types) |
1138 | 0 | { |
1139 | 0 | GRealClosure *real_closure; |
1140 | 0 | GTypeClass *class; |
1141 | 0 | gpointer callback; |
1142 | 0 | GType itype = (GType) closure->data; |
1143 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1144 | |
|
1145 | 0 | real_closure = G_REAL_CLOSURE (closure); |
1146 | |
|
1147 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass); |
1148 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1149 | 0 | if (callback) |
1150 | 0 | real_closure->va_marshal (closure, |
1151 | 0 | return_value, |
1152 | 0 | instance, args, |
1153 | 0 | callback, |
1154 | 0 | n_params, |
1155 | 0 | param_types); |
1156 | 0 | } |
1157 | | |
1158 | | /** |
1159 | | * g_signal_type_cclosure_new: |
1160 | | * @itype: the #GType identifier of an interface or classed type |
1161 | | * @struct_offset: the offset of the member function of @itype's class |
1162 | | * structure which is to be invoked by the new closure |
1163 | | * |
1164 | | * Creates a new closure which invokes the function found at the offset |
1165 | | * @struct_offset in the class structure of the interface or classed type |
1166 | | * identified by @itype. |
1167 | | * |
1168 | | * Returns: (transfer floating): a floating reference to a new #GCClosure |
1169 | | */ |
1170 | | GClosure* |
1171 | | g_signal_type_cclosure_new (GType itype, |
1172 | | guint struct_offset) |
1173 | 11 | { |
1174 | 11 | GClosure *closure; |
1175 | | |
1176 | 11 | g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL); |
1177 | 11 | g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL); |
1178 | | |
1179 | 11 | closure = g_closure_new_simple (sizeof (GClosure), GTYPE_TO_POINTER (itype)); |
1180 | 11 | if (G_TYPE_IS_INTERFACE (itype)) |
1181 | 0 | { |
1182 | 0 | g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal); |
1183 | 0 | g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv); |
1184 | 0 | } |
1185 | 11 | else |
1186 | 11 | { |
1187 | 11 | g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal); |
1188 | 11 | g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv); |
1189 | 11 | } |
1190 | 11 | return closure; |
1191 | 11 | } |
1192 | | |
1193 | | #include <ffi.h> |
1194 | | static ffi_type * |
1195 | | value_to_ffi_type (const GValue *gvalue, |
1196 | | gpointer *value, |
1197 | | gint *enum_tmpval, |
1198 | | gboolean *tmpval_used) |
1199 | 0 | { |
1200 | 0 | ffi_type *rettype = NULL; |
1201 | 0 | GType type = g_type_fundamental (G_VALUE_TYPE (gvalue)); |
1202 | 0 | g_assert (type != G_TYPE_INVALID); |
1203 | | |
1204 | 0 | if (enum_tmpval) |
1205 | 0 | { |
1206 | 0 | g_assert (tmpval_used != NULL); |
1207 | 0 | *tmpval_used = FALSE; |
1208 | 0 | } |
1209 | | |
1210 | 0 | switch (type) |
1211 | 0 | { |
1212 | 0 | case G_TYPE_BOOLEAN: |
1213 | 0 | case G_TYPE_CHAR: |
1214 | 0 | case G_TYPE_INT: |
1215 | 0 | rettype = &ffi_type_sint; |
1216 | 0 | *value = (gpointer)&(gvalue->data[0].v_int); |
1217 | 0 | break; |
1218 | 0 | case G_TYPE_ENUM: |
1219 | | /* enums are stored in v_long even though they are integers, which makes |
1220 | | * marshalling through libffi somewhat complicated. They need to be |
1221 | | * marshalled as signed ints, but we need to use a temporary int sized |
1222 | | * value to pass to libffi otherwise it'll pull the wrong value on |
1223 | | * BE machines with 32-bit integers when treating v_long as 32-bit int. |
1224 | | */ |
1225 | 0 | g_assert (enum_tmpval != NULL); |
1226 | 0 | rettype = &ffi_type_sint; |
1227 | 0 | *enum_tmpval = g_value_get_enum (gvalue); |
1228 | 0 | *value = enum_tmpval; |
1229 | 0 | *tmpval_used = TRUE; |
1230 | 0 | break; |
1231 | 0 | case G_TYPE_FLAGS: |
1232 | 0 | g_assert (enum_tmpval != NULL); |
1233 | 0 | rettype = &ffi_type_uint; |
1234 | 0 | *enum_tmpval = g_value_get_flags (gvalue); |
1235 | 0 | *value = enum_tmpval; |
1236 | 0 | *tmpval_used = TRUE; |
1237 | 0 | break; |
1238 | 0 | case G_TYPE_UCHAR: |
1239 | 0 | case G_TYPE_UINT: |
1240 | 0 | rettype = &ffi_type_uint; |
1241 | 0 | *value = (gpointer)&(gvalue->data[0].v_uint); |
1242 | 0 | break; |
1243 | 0 | case G_TYPE_STRING: |
1244 | 0 | case G_TYPE_OBJECT: |
1245 | 0 | case G_TYPE_BOXED: |
1246 | 0 | case G_TYPE_PARAM: |
1247 | 0 | case G_TYPE_POINTER: |
1248 | 0 | case G_TYPE_INTERFACE: |
1249 | 0 | case G_TYPE_VARIANT: |
1250 | 0 | rettype = &ffi_type_pointer; |
1251 | 0 | *value = (gpointer)&(gvalue->data[0].v_pointer); |
1252 | 0 | break; |
1253 | 0 | case G_TYPE_FLOAT: |
1254 | 0 | rettype = &ffi_type_float; |
1255 | 0 | *value = (gpointer)&(gvalue->data[0].v_float); |
1256 | 0 | break; |
1257 | 0 | case G_TYPE_DOUBLE: |
1258 | 0 | rettype = &ffi_type_double; |
1259 | 0 | *value = (gpointer)&(gvalue->data[0].v_double); |
1260 | 0 | break; |
1261 | 0 | case G_TYPE_LONG: |
1262 | 0 | rettype = &ffi_type_slong; |
1263 | 0 | *value = (gpointer)&(gvalue->data[0].v_long); |
1264 | 0 | break; |
1265 | 0 | case G_TYPE_ULONG: |
1266 | 0 | rettype = &ffi_type_ulong; |
1267 | 0 | *value = (gpointer)&(gvalue->data[0].v_ulong); |
1268 | 0 | break; |
1269 | 0 | case G_TYPE_INT64: |
1270 | 0 | rettype = &ffi_type_sint64; |
1271 | 0 | *value = (gpointer)&(gvalue->data[0].v_int64); |
1272 | 0 | break; |
1273 | 0 | case G_TYPE_UINT64: |
1274 | 0 | rettype = &ffi_type_uint64; |
1275 | 0 | *value = (gpointer)&(gvalue->data[0].v_uint64); |
1276 | 0 | break; |
1277 | 0 | default: |
1278 | 0 | rettype = &ffi_type_pointer; |
1279 | 0 | *value = NULL; |
1280 | 0 | g_critical ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type)); |
1281 | 0 | break; |
1282 | 0 | } |
1283 | 0 | return rettype; |
1284 | 0 | } |
1285 | | |
1286 | | static void |
1287 | | value_from_ffi_type (GValue *gvalue, gpointer *value) |
1288 | 0 | { |
1289 | 0 | ffi_arg *int_val = (ffi_arg*) value; |
1290 | 0 | GType type; |
1291 | |
|
1292 | 0 | type = G_VALUE_TYPE (gvalue); |
1293 | |
|
1294 | 0 | restart: |
1295 | 0 | switch (g_type_fundamental (type)) |
1296 | 0 | { |
1297 | 0 | case G_TYPE_INT: |
1298 | 0 | g_value_set_int (gvalue, (gint) *int_val); |
1299 | 0 | break; |
1300 | 0 | case G_TYPE_FLOAT: |
1301 | 0 | g_value_set_float (gvalue, *(gfloat*)value); |
1302 | 0 | break; |
1303 | 0 | case G_TYPE_DOUBLE: |
1304 | 0 | g_value_set_double (gvalue, *(gdouble*)value); |
1305 | 0 | break; |
1306 | 0 | case G_TYPE_BOOLEAN: |
1307 | 0 | g_value_set_boolean (gvalue, (gboolean) *int_val); |
1308 | 0 | break; |
1309 | 0 | case G_TYPE_STRING: |
1310 | 0 | g_value_take_string (gvalue, *(gchar**)value); |
1311 | 0 | break; |
1312 | 0 | case G_TYPE_CHAR: |
1313 | 0 | g_value_set_schar (gvalue, (gint8) *int_val); |
1314 | 0 | break; |
1315 | 0 | case G_TYPE_UCHAR: |
1316 | 0 | g_value_set_uchar (gvalue, (guchar) *int_val); |
1317 | 0 | break; |
1318 | 0 | case G_TYPE_UINT: |
1319 | 0 | g_value_set_uint (gvalue, (guint) *int_val); |
1320 | 0 | break; |
1321 | 0 | case G_TYPE_POINTER: |
1322 | 0 | g_value_set_pointer (gvalue, *(gpointer*)value); |
1323 | 0 | break; |
1324 | 0 | case G_TYPE_LONG: |
1325 | 0 | g_value_set_long (gvalue, (glong) *int_val); |
1326 | 0 | break; |
1327 | 0 | case G_TYPE_ULONG: |
1328 | 0 | g_value_set_ulong (gvalue, (gulong) *int_val); |
1329 | 0 | break; |
1330 | 0 | case G_TYPE_INT64: |
1331 | 0 | g_value_set_int64 (gvalue, (gint64) *int_val); |
1332 | 0 | break; |
1333 | 0 | case G_TYPE_UINT64: |
1334 | 0 | g_value_set_uint64 (gvalue, (guint64) *int_val); |
1335 | 0 | break; |
1336 | 0 | case G_TYPE_BOXED: |
1337 | 0 | g_value_take_boxed (gvalue, *(gpointer*)value); |
1338 | 0 | break; |
1339 | 0 | case G_TYPE_ENUM: |
1340 | 0 | g_value_set_enum (gvalue, (gint) *int_val); |
1341 | 0 | break; |
1342 | 0 | case G_TYPE_FLAGS: |
1343 | 0 | g_value_set_flags (gvalue, (guint) *int_val); |
1344 | 0 | break; |
1345 | 0 | case G_TYPE_PARAM: |
1346 | 0 | g_value_take_param (gvalue, *(gpointer*)value); |
1347 | 0 | break; |
1348 | 0 | case G_TYPE_OBJECT: |
1349 | 0 | g_value_take_object (gvalue, *(gpointer*)value); |
1350 | 0 | break; |
1351 | 0 | case G_TYPE_VARIANT: |
1352 | 0 | g_value_take_variant (gvalue, *(gpointer*)value); |
1353 | 0 | break; |
1354 | 0 | case G_TYPE_INTERFACE: |
1355 | 0 | type = g_type_interface_instantiatable_prerequisite (type); |
1356 | 0 | if (type) |
1357 | 0 | goto restart; |
1358 | 0 | G_GNUC_FALLTHROUGH; |
1359 | 0 | default: |
1360 | 0 | g_critical ("value_from_ffi_type: Unsupported fundamental type %s for type %s", |
1361 | 0 | g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))), |
1362 | 0 | g_type_name (G_VALUE_TYPE (gvalue))); |
1363 | 0 | } |
1364 | 0 | } |
1365 | | |
1366 | | typedef union { |
1367 | | gpointer _gpointer; |
1368 | | float _float; |
1369 | | double _double; |
1370 | | gint _gint; |
1371 | | guint _guint; |
1372 | | glong _glong; |
1373 | | gulong _gulong; |
1374 | | gint64 _gint64; |
1375 | | guint64 _guint64; |
1376 | | } va_arg_storage; |
1377 | | |
1378 | | static ffi_type * |
1379 | | va_to_ffi_type (GType gtype, |
1380 | | va_list *va, |
1381 | | va_arg_storage *storage) |
1382 | 0 | { |
1383 | 0 | ffi_type *rettype = NULL; |
1384 | 0 | GType type = g_type_fundamental (gtype); |
1385 | 0 | g_assert (type != G_TYPE_INVALID); |
1386 | | |
1387 | 0 | switch (type) |
1388 | 0 | { |
1389 | 0 | case G_TYPE_BOOLEAN: |
1390 | 0 | case G_TYPE_CHAR: |
1391 | 0 | case G_TYPE_INT: |
1392 | 0 | case G_TYPE_ENUM: |
1393 | 0 | rettype = &ffi_type_sint; |
1394 | 0 | storage->_gint = va_arg (*va, gint); |
1395 | 0 | break; |
1396 | 0 | case G_TYPE_UCHAR: |
1397 | 0 | case G_TYPE_UINT: |
1398 | 0 | case G_TYPE_FLAGS: |
1399 | 0 | rettype = &ffi_type_uint; |
1400 | 0 | storage->_guint = va_arg (*va, guint); |
1401 | 0 | break; |
1402 | 0 | case G_TYPE_STRING: |
1403 | 0 | case G_TYPE_OBJECT: |
1404 | 0 | case G_TYPE_BOXED: |
1405 | 0 | case G_TYPE_PARAM: |
1406 | 0 | case G_TYPE_POINTER: |
1407 | 0 | case G_TYPE_INTERFACE: |
1408 | 0 | case G_TYPE_VARIANT: |
1409 | 0 | rettype = &ffi_type_pointer; |
1410 | 0 | storage->_gpointer = va_arg (*va, gpointer); |
1411 | 0 | break; |
1412 | 0 | case G_TYPE_FLOAT: |
1413 | | /* Float args are passed as doubles in varargs */ |
1414 | 0 | rettype = &ffi_type_float; |
1415 | 0 | storage->_float = (float)va_arg (*va, double); |
1416 | 0 | break; |
1417 | 0 | case G_TYPE_DOUBLE: |
1418 | 0 | rettype = &ffi_type_double; |
1419 | 0 | storage->_double = va_arg (*va, double); |
1420 | 0 | break; |
1421 | 0 | case G_TYPE_LONG: |
1422 | 0 | rettype = &ffi_type_slong; |
1423 | 0 | storage->_glong = va_arg (*va, glong); |
1424 | 0 | break; |
1425 | 0 | case G_TYPE_ULONG: |
1426 | 0 | rettype = &ffi_type_ulong; |
1427 | 0 | storage->_gulong = va_arg (*va, gulong); |
1428 | 0 | break; |
1429 | 0 | case G_TYPE_INT64: |
1430 | 0 | rettype = &ffi_type_sint64; |
1431 | 0 | storage->_gint64 = va_arg (*va, gint64); |
1432 | 0 | break; |
1433 | 0 | case G_TYPE_UINT64: |
1434 | 0 | rettype = &ffi_type_uint64; |
1435 | 0 | storage->_guint64 = va_arg (*va, guint64); |
1436 | 0 | break; |
1437 | 0 | default: |
1438 | 0 | rettype = &ffi_type_pointer; |
1439 | 0 | storage->_guint64 = 0; |
1440 | 0 | g_critical ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type)); |
1441 | 0 | break; |
1442 | 0 | } |
1443 | 0 | return rettype; |
1444 | 0 | } |
1445 | | |
1446 | | /** |
1447 | | * g_cclosure_marshal_generic: |
1448 | | * @closure: A #GClosure. |
1449 | | * @return_gvalue: A #GValue to store the return value. May be %NULL |
1450 | | * if the callback of closure doesn't return a value. |
1451 | | * @n_param_values: The length of the @param_values array. |
1452 | | * @param_values: An array of #GValues holding the arguments |
1453 | | * on which to invoke the callback of closure. |
1454 | | * @invocation_hint: The invocation hint given as the last argument to |
1455 | | * g_closure_invoke(). |
1456 | | * @marshal_data: Additional data specified when registering the |
1457 | | * marshaller, see g_closure_set_marshal() and |
1458 | | * g_closure_set_meta_marshal() |
1459 | | * |
1460 | | * A generic marshaller function implemented via |
1461 | | * [libffi](http://sourceware.org/libffi/). |
1462 | | * |
1463 | | * Normally this function is not passed explicitly to g_signal_new(), |
1464 | | * but used automatically by GLib when specifying a %NULL marshaller. |
1465 | | * |
1466 | | * Since: 2.30 |
1467 | | */ |
1468 | | void |
1469 | | g_cclosure_marshal_generic (GClosure *closure, |
1470 | | GValue *return_gvalue, |
1471 | | guint n_param_values, |
1472 | | const GValue *param_values, |
1473 | | gpointer invocation_hint, |
1474 | | gpointer marshal_data) |
1475 | 0 | { |
1476 | 0 | ffi_type *rtype; |
1477 | 0 | void *rvalue; |
1478 | 0 | int n_args; |
1479 | 0 | ffi_type **atypes; |
1480 | 0 | void **args; |
1481 | 0 | int i; |
1482 | 0 | ffi_cif cif; |
1483 | 0 | GCClosure *cc = (GCClosure*) closure; |
1484 | 0 | gint *enum_tmpval; |
1485 | 0 | gboolean tmpval_used = FALSE; |
1486 | |
|
1487 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1488 | 0 | if (return_gvalue && G_VALUE_TYPE (return_gvalue)) |
1489 | 0 | { |
1490 | 0 | rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used); |
1491 | 0 | } |
1492 | 0 | else |
1493 | 0 | { |
1494 | 0 | rtype = &ffi_type_void; |
1495 | 0 | } |
1496 | |
|
1497 | 0 | rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg))); |
1498 | |
|
1499 | 0 | n_args = n_param_values + 1; |
1500 | 0 | atypes = g_alloca (sizeof (ffi_type *) * n_args); |
1501 | 0 | args = g_alloca (sizeof (gpointer) * n_args); |
1502 | |
|
1503 | 0 | if (tmpval_used) |
1504 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1505 | |
|
1506 | 0 | if (G_CCLOSURE_SWAP_DATA (closure)) |
1507 | 0 | { |
1508 | 0 | atypes[n_args-1] = value_to_ffi_type (param_values + 0, |
1509 | 0 | &args[n_args-1], |
1510 | 0 | enum_tmpval, |
1511 | 0 | &tmpval_used); |
1512 | 0 | atypes[0] = &ffi_type_pointer; |
1513 | 0 | args[0] = &closure->data; |
1514 | 0 | } |
1515 | 0 | else |
1516 | 0 | { |
1517 | 0 | atypes[0] = value_to_ffi_type (param_values + 0, |
1518 | 0 | &args[0], |
1519 | 0 | enum_tmpval, |
1520 | 0 | &tmpval_used); |
1521 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1522 | 0 | args[n_args-1] = &closure->data; |
1523 | 0 | } |
1524 | |
|
1525 | 0 | for (i = 1; i < n_args - 1; i++) |
1526 | 0 | { |
1527 | 0 | if (tmpval_used) |
1528 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1529 | |
|
1530 | 0 | atypes[i] = value_to_ffi_type (param_values + i, |
1531 | 0 | &args[i], |
1532 | 0 | enum_tmpval, |
1533 | 0 | &tmpval_used); |
1534 | 0 | } |
1535 | |
|
1536 | 0 | if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK) |
1537 | 0 | return; |
1538 | | |
1539 | 0 | ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args); |
1540 | |
|
1541 | 0 | if (return_gvalue && G_VALUE_TYPE (return_gvalue)) |
1542 | 0 | value_from_ffi_type (return_gvalue, rvalue); |
1543 | 0 | } |
1544 | | |
1545 | | /** |
1546 | | * g_cclosure_marshal_generic_va: |
1547 | | * @closure: the #GClosure to which the marshaller belongs |
1548 | | * @return_value: (nullable): a #GValue to store the return |
1549 | | * value. May be %NULL if the callback of @closure doesn't return a |
1550 | | * value. |
1551 | | * @instance: (type GObject.TypeInstance): the instance on which the closure is |
1552 | | * invoked. |
1553 | | * @args_list: va_list of arguments to be passed to the closure. |
1554 | | * @marshal_data: (nullable): additional data specified when |
1555 | | * registering the marshaller, see g_closure_set_marshal() and |
1556 | | * g_closure_set_meta_marshal() |
1557 | | * @n_params: the length of the @param_types array |
1558 | | * @param_types: (array length=n_params): the #GType of each argument from |
1559 | | * @args_list. |
1560 | | * |
1561 | | * A generic #GVaClosureMarshal function implemented via |
1562 | | * [libffi](http://sourceware.org/libffi/). |
1563 | | * |
1564 | | * Since: 2.30 |
1565 | | */ |
1566 | | void |
1567 | | g_cclosure_marshal_generic_va (GClosure *closure, |
1568 | | GValue *return_value, |
1569 | | gpointer instance, |
1570 | | va_list args_list, |
1571 | | gpointer marshal_data, |
1572 | | int n_params, |
1573 | | GType *param_types) |
1574 | 0 | { |
1575 | 0 | ffi_type *rtype; |
1576 | 0 | void *rvalue; |
1577 | 0 | int n_args; |
1578 | 0 | ffi_type **atypes; |
1579 | 0 | void **args; |
1580 | 0 | va_arg_storage *storage; |
1581 | 0 | int i; |
1582 | 0 | ffi_cif cif; |
1583 | 0 | GCClosure *cc = (GCClosure*) closure; |
1584 | 0 | gint *enum_tmpval; |
1585 | 0 | gboolean tmpval_used = FALSE; |
1586 | 0 | va_list args_copy; |
1587 | |
|
1588 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1589 | 0 | if (return_value && G_VALUE_TYPE (return_value)) |
1590 | 0 | { |
1591 | 0 | rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used); |
1592 | 0 | } |
1593 | 0 | else |
1594 | 0 | { |
1595 | 0 | rtype = &ffi_type_void; |
1596 | 0 | } |
1597 | |
|
1598 | 0 | rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg))); |
1599 | |
|
1600 | 0 | n_args = n_params + 2; |
1601 | 0 | atypes = g_alloca (sizeof (ffi_type *) * n_args); |
1602 | 0 | args = g_alloca (sizeof (gpointer) * n_args); |
1603 | 0 | storage = g_alloca (sizeof (va_arg_storage) * n_params); |
1604 | |
|
1605 | 0 | if (G_CCLOSURE_SWAP_DATA (closure)) |
1606 | 0 | { |
1607 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1608 | 0 | args[n_args-1] = &instance; |
1609 | 0 | atypes[0] = &ffi_type_pointer; |
1610 | 0 | args[0] = &closure->data; |
1611 | 0 | } |
1612 | 0 | else |
1613 | 0 | { |
1614 | 0 | atypes[0] = &ffi_type_pointer; |
1615 | 0 | args[0] = &instance; |
1616 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1617 | 0 | args[n_args-1] = &closure->data; |
1618 | 0 | } |
1619 | |
|
1620 | 0 | va_copy (args_copy, args_list); |
1621 | | |
1622 | | /* Box non-primitive arguments */ |
1623 | 0 | for (i = 0; i < n_params; i++) |
1624 | 0 | { |
1625 | 0 | GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
1626 | 0 | GType fundamental = G_TYPE_FUNDAMENTAL (type); |
1627 | |
|
1628 | 0 | atypes[i+1] = va_to_ffi_type (type, |
1629 | 0 | &args_copy, |
1630 | 0 | &storage[i]); |
1631 | 0 | args[i+1] = &storage[i]; |
1632 | |
|
1633 | 0 | if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0) |
1634 | 0 | { |
1635 | 0 | if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL) |
1636 | 0 | storage[i]._gpointer = g_strdup (storage[i]._gpointer); |
1637 | 0 | else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL) |
1638 | 0 | storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer); |
1639 | 0 | else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL) |
1640 | 0 | storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer); |
1641 | 0 | else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL) |
1642 | 0 | storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer); |
1643 | 0 | } |
1644 | 0 | if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL) |
1645 | 0 | storage[i]._gpointer = g_object_ref (storage[i]._gpointer); |
1646 | 0 | } |
1647 | |
|
1648 | 0 | va_end (args_copy); |
1649 | | |
1650 | 0 | if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK) |
1651 | 0 | return; |
1652 | | |
1653 | 0 | ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args); |
1654 | | |
1655 | | /* Unbox non-primitive arguments */ |
1656 | 0 | for (i = 0; i < n_params; i++) |
1657 | 0 | { |
1658 | 0 | GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
1659 | 0 | GType fundamental = G_TYPE_FUNDAMENTAL (type); |
1660 | |
|
1661 | 0 | if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0) |
1662 | 0 | { |
1663 | 0 | if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL) |
1664 | 0 | g_free (storage[i]._gpointer); |
1665 | 0 | else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL) |
1666 | 0 | g_param_spec_unref (storage[i]._gpointer); |
1667 | 0 | else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL) |
1668 | 0 | g_boxed_free (type, storage[i]._gpointer); |
1669 | 0 | else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL) |
1670 | 0 | g_variant_unref (storage[i]._gpointer); |
1671 | 0 | } |
1672 | 0 | if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL) |
1673 | 0 | g_object_unref (storage[i]._gpointer); |
1674 | 0 | } |
1675 | | |
1676 | 0 | if (return_value && G_VALUE_TYPE (return_value)) |
1677 | 0 | value_from_ffi_type (return_value, rvalue); |
1678 | 0 | } |