1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 * Copyright (C) 2005 Imendio AB
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
22 * MT safe with regards to reference counting.
27 #include "../glib/gvalgrind.h"
37 #include "gvaluetypes.h"
38 #include "gtype-private.h"
43 * @short_description: Functions as first-class objects
46 * A #GClosure represents a callback supplied by the programmer.
48 * It will generally comprise a function of some kind and a marshaller
49 * used to call it. It is the responsibility of the marshaller to
50 * convert the arguments for the invocation from #GValues into
51 * a suitable form, perform the callback on the converted arguments,
52 * and transform the return value back into a #GValue.
54 * In the case of C programs, a closure usually just holds a pointer
55 * to a function and maybe a data argument, and the marshaller
56 * converts between #GValue and native C types. The GObject
57 * library provides the #GCClosure type for this purpose. Bindings for
58 * other languages need marshallers which convert between #GValues
59 * and suitable representations in the runtime of the language in
60 * order to use functions written in that language as callbacks. Use
61 * g_closure_set_marshal() to set the marshaller on such a custom
62 * closure implementation.
64 * Within GObject, closures play an important role in the
65 * implementation of signals. When a signal is registered, the
66 * @c_marshaller argument to g_signal_new() specifies the default C
67 * marshaller for any closure which is connected to this
68 * signal. GObject provides a number of C marshallers for this
69 * purpose, see the g_cclosure_marshal_*() functions. Additional C
70 * marshallers can be generated with the [glib-genmarshal][glib-genmarshal]
71 * utility. Closures can be explicitly connected to signals with
72 * g_signal_connect_closure(), but it usually more convenient to let
73 * GObject create a closure automatically by using one of the
74 * g_signal_connect_*() functions which take a callback function/user
77 * Using closures has a number of important advantages over a simple
78 * callback function/data pointer combination:
80 * - Closures allow the callee to get the types of the callback parameters,
81 * which means that language bindings don't have to write individual glue
82 * for each callback type.
84 * - The reference counting of #GClosure makes it easy to handle reentrancy
85 * right; if a callback is removed while it is being invoked, the closure
86 * and its parameters won't be freed until the invocation finishes.
88 * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
89 * automatically removed when the objects they point to go away.
92 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
93 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
94 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
95 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
96 #define CLOSURE_N_MFUNCS(cl) (((cl)->n_guards << 1L))
97 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
98 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
99 (cl)->n_fnotifiers + \
107 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
109 ClosureInt *cunion = (ClosureInt*) _closure; \
110 gint new_int, old_int, success; \
114 tmp.vint = old_int = cunion->vint; \
115 _SET_OLD tmp.closure._field; \
116 tmp.closure._field _OP _value; \
117 _SET_NEW tmp.closure._field; \
118 new_int = tmp.vint; \
119 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
121 while (!success && _must_set); \
124 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
125 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
126 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
127 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
128 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
129 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
131 #if 0 /* for non-thread-safe closures */
132 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
133 #define SET(cl,f,v) (void) (cl->f = v)
134 #define INC(cl,f) (void) (cl->f += 1)
135 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
136 #define DEC(cl,f) (void) (cl->f -= 1)
137 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
148 /* --- functions --- */
150 * g_closure_new_simple:
151 * @sizeof_closure: the size of the structure to allocate, must be at least
152 * `sizeof (GClosure)`
153 * @data: data to store in the @data field of the newly allocated #GClosure
155 * Allocates a struct of the given size and initializes the initial
156 * part as a #GClosure.
158 * This function is mainly useful when implementing new types of closures:
160 * |[<!-- language="C" -->
161 * typedef struct _MyClosure MyClosure;
165 * // extra data goes here
169 * my_closure_finalize (gpointer notify_data,
172 * MyClosure *my_closure = (MyClosure *)closure;
174 * // free extra data here
177 * MyClosure *my_closure_new (gpointer data)
180 * MyClosure *my_closure;
182 * closure = g_closure_new_simple (sizeof (MyClosure), data);
183 * my_closure = (MyClosure *) closure;
185 * // initialize extra data here
187 * g_closure_add_finalize_notifier (closure, notify_data,
188 * my_closure_finalize);
193 * Returns: (transfer floating): a floating reference to a new #GClosure
196 g_closure_new_simple (guint sizeof_closure,
203 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
205 private_size = sizeof (GRealClosure) - sizeof (GClosure);
207 #ifdef ENABLE_VALGRIND
208 /* See comments in gtype.c about what's going on here... */
209 if (RUNNING_ON_VALGRIND)
211 private_size += sizeof (gpointer);
213 allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer));
215 *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer);
217 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, sizeof_closure + sizeof (gpointer), 0, TRUE);
218 VALGRIND_MALLOCLIKE_BLOCK (allocated + sizeof (gpointer), private_size - sizeof (gpointer), 0, TRUE);
222 allocated = g_malloc0 (private_size + sizeof_closure);
224 closure = (GClosure *) (allocated + private_size);
226 SET (closure, ref_count, 1);
227 SET (closure, floating, TRUE);
228 closure->data = data;
234 closure_invoke_notifiers (GClosure *closure,
238 * n_guards n_guards n_fnotif. n_inotifiers
239 * ->[[pre_guards][post_guards][fnotifiers][inotifiers]]
241 * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards;
242 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
244 * constrains/catches:
245 * - closure->notifiers may be reloacted during callback
246 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
247 * - i.e. callbacks can be removed/added during invocation
248 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
249 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
250 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
251 * + none of the callbacks can cause recursion
252 * + closure->n_inotifiers is const 0 during FNOTIFY
256 GClosureNotifyData *ndata;
259 while (closure->n_fnotifiers)
262 DEC_ASSIGN (closure, n_fnotifiers, &n);
264 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
265 closure->marshal = (GClosureMarshal) ndata->notify;
266 closure->data = ndata->data;
267 ndata->notify (ndata->data, closure);
269 closure->marshal = NULL;
270 closure->data = NULL;
273 SET (closure, in_inotify, TRUE);
274 while (closure->n_inotifiers)
277 DEC_ASSIGN (closure, n_inotifiers, &n);
279 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
280 closure->marshal = (GClosureMarshal) ndata->notify;
281 closure->data = ndata->data;
282 ndata->notify (ndata->data, closure);
284 closure->marshal = NULL;
285 closure->data = NULL;
286 SET (closure, in_inotify, FALSE);
289 i = closure->n_guards;
293 ndata = closure->notifiers + offs + i;
294 ndata->notify (ndata->data, closure);
298 i = closure->n_guards;
302 ndata = closure->notifiers + offs + i;
303 ndata->notify (ndata->data, closure);
310 g_closure_set_meta_va_marshal (GClosure *closure,
311 GVaClosureMarshal va_meta_marshal)
313 GRealClosure *real_closure;
315 g_return_if_fail (closure != NULL);
316 g_return_if_fail (va_meta_marshal != NULL);
317 g_return_if_fail (closure->is_invalid == FALSE);
318 g_return_if_fail (closure->in_marshal == FALSE);
320 real_closure = G_REAL_CLOSURE (closure);
322 g_return_if_fail (real_closure->meta_marshal != NULL);
324 real_closure->va_meta_marshal = va_meta_marshal;
328 * g_closure_set_meta_marshal: (skip)
329 * @closure: a #GClosure
330 * @marshal_data: (closure meta_marshal): context-dependent data to pass
332 * @meta_marshal: a #GClosureMarshal function
334 * Sets the meta marshaller of @closure.
336 * A meta marshaller wraps the @closure's marshal and modifies the way
337 * it is called in some fashion. The most common use of this facility
338 * is for C callbacks.
340 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
341 * are used everywhere, but the way that we get the callback function
342 * differs. In most cases we want to use the @closure's callback, but in
343 * other cases we want to use some different technique to retrieve the
346 * For example, class closures for signals (see
347 * g_signal_type_cclosure_new()) retrieve the callback function from a
348 * fixed offset in the class structure. The meta marshaller retrieves
349 * the right callback and passes it to the marshaller as the
350 * @marshal_data argument.
353 g_closure_set_meta_marshal (GClosure *closure,
354 gpointer marshal_data,
355 GClosureMarshal meta_marshal)
357 GRealClosure *real_closure;
359 g_return_if_fail (closure != NULL);
360 g_return_if_fail (meta_marshal != NULL);
361 g_return_if_fail (closure->is_invalid == FALSE);
362 g_return_if_fail (closure->in_marshal == FALSE);
364 real_closure = G_REAL_CLOSURE (closure);
366 g_return_if_fail (real_closure->meta_marshal == NULL);
368 real_closure->meta_marshal = meta_marshal;
369 real_closure->meta_marshal_data = marshal_data;
373 * g_closure_add_marshal_guards: (skip)
374 * @closure: a #GClosure
375 * @pre_marshal_data: (closure pre_marshal_notify): data to pass
376 * to @pre_marshal_notify
377 * @pre_marshal_notify: a function to call before the closure callback
378 * @post_marshal_data: (closure post_marshal_notify): data to pass
379 * to @post_marshal_notify
380 * @post_marshal_notify: a function to call after the closure callback
382 * Adds a pair of notifiers which get invoked before and after the
383 * closure callback, respectively.
385 * This is typically used to protect the extra arguments for the
386 * duration of the callback. See g_object_watch_closure() for an
387 * example of marshal guards.
390 g_closure_add_marshal_guards (GClosure *closure,
391 gpointer pre_marshal_data,
392 GClosureNotify pre_marshal_notify,
393 gpointer post_marshal_data,
394 GClosureNotify post_marshal_notify)
398 g_return_if_fail (closure != NULL);
399 g_return_if_fail (pre_marshal_notify != NULL);
400 g_return_if_fail (post_marshal_notify != NULL);
401 g_return_if_fail (closure->is_invalid == FALSE);
402 g_return_if_fail (closure->in_marshal == FALSE);
403 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
405 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
406 if (closure->n_inotifiers)
407 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
408 closure->n_fnotifiers +
409 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
410 closure->n_fnotifiers + 0)];
411 if (closure->n_inotifiers > 1)
412 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
413 closure->n_fnotifiers +
414 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
415 closure->n_fnotifiers + 1)];
416 if (closure->n_fnotifiers)
417 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
418 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
419 if (closure->n_fnotifiers > 1)
420 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
421 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
422 if (closure->n_guards)
423 closure->notifiers[(closure->n_guards +
424 closure->n_guards + 1)] = closure->notifiers[closure->n_guards];
425 i = closure->n_guards;
426 closure->notifiers[i].data = pre_marshal_data;
427 closure->notifiers[i].notify = pre_marshal_notify;
428 closure->notifiers[i + 1].data = post_marshal_data;
429 closure->notifiers[i + 1].notify = post_marshal_notify;
430 INC (closure, n_guards);
434 * g_closure_add_finalize_notifier: (skip)
435 * @closure: a #GClosure
436 * @notify_data: (closure notify_func): data to pass to @notify_func
437 * @notify_func: the callback function to register
439 * Registers a finalization notifier which will be called when the
440 * reference count of @closure goes down to 0.
442 * Multiple finalization notifiers on a single closure are invoked in
443 * unspecified order. If a single call to g_closure_unref() results in
444 * the closure being both invalidated and finalized, then the invalidate
445 * notifiers will be run before the finalize notifiers.
448 g_closure_add_finalize_notifier (GClosure *closure,
449 gpointer notify_data,
450 GClosureNotify notify_func)
454 g_return_if_fail (closure != NULL);
455 g_return_if_fail (notify_func != NULL);
456 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
458 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
459 if (closure->n_inotifiers)
460 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
461 closure->n_fnotifiers +
462 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
463 closure->n_fnotifiers + 0)];
464 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
465 closure->notifiers[i].data = notify_data;
466 closure->notifiers[i].notify = notify_func;
467 INC (closure, n_fnotifiers);
471 * g_closure_add_invalidate_notifier: (skip)
472 * @closure: a #GClosure
473 * @notify_data: (closure notify_func): data to pass to @notify_func
474 * @notify_func: the callback function to register
476 * Registers an invalidation notifier which will be called when the
477 * @closure is invalidated with g_closure_invalidate().
479 * Invalidation notifiers are invoked before finalization notifiers,
480 * in an unspecified order.
483 g_closure_add_invalidate_notifier (GClosure *closure,
484 gpointer notify_data,
485 GClosureNotify notify_func)
489 g_return_if_fail (closure != NULL);
490 g_return_if_fail (notify_func != NULL);
491 g_return_if_fail (closure->is_invalid == FALSE);
492 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
494 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
495 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
496 closure->notifiers[i].data = notify_data;
497 closure->notifiers[i].notify = notify_func;
498 INC (closure, n_inotifiers);
501 static inline gboolean
502 closure_try_remove_inotify (GClosure *closure,
503 gpointer notify_data,
504 GClosureNotify notify_func)
506 GClosureNotifyData *ndata, *nlast;
508 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
509 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
510 if (ndata->notify == notify_func && ndata->data == notify_data)
512 DEC (closure, n_inotifiers);
521 static inline gboolean
522 closure_try_remove_fnotify (GClosure *closure,
523 gpointer notify_data,
524 GClosureNotify notify_func)
526 GClosureNotifyData *ndata, *nlast;
528 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
529 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
530 if (ndata->notify == notify_func && ndata->data == notify_data)
532 DEC (closure, n_fnotifiers);
535 if (closure->n_inotifiers)
536 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
537 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
538 closure->n_fnotifiers +
539 closure->n_inotifiers)];
547 * @closure: #GClosure to increment the reference count on
549 * Increments the reference count on a closure to force it staying
550 * alive while the caller holds a pointer to it.
552 * Returns: (transfer none): The @closure passed in, for convenience
555 g_closure_ref (GClosure *closure)
558 g_return_val_if_fail (closure != NULL, NULL);
559 g_return_val_if_fail (closure->ref_count > 0, NULL);
560 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
562 INC_ASSIGN (closure, ref_count, &new_ref_count);
563 g_return_val_if_fail (new_ref_count > 1, NULL);
569 * g_closure_invalidate:
570 * @closure: #GClosure to invalidate
572 * Sets a flag on the closure to indicate that its calling
573 * environment has become invalid, and thus causes any future
574 * invocations of g_closure_invoke() on this @closure to be
577 * Also, invalidation notifiers installed on the closure will
578 * be called at this point. Note that unless you are holding a
579 * reference to the closure yourself, the invalidation notifiers may
580 * unref the closure and cause it to be destroyed, so if you need to
581 * access the closure after calling g_closure_invalidate(), make sure
582 * that you've previously called g_closure_ref().
584 * Note that g_closure_invalidate() will also be called when the
585 * reference count of a closure drops to zero (unless it has already
586 * been invalidated before).
589 g_closure_invalidate (GClosure *closure)
591 g_return_if_fail (closure != NULL);
593 if (!closure->is_invalid)
595 gboolean was_invalid;
596 g_closure_ref (closure); /* preserve floating flag */
597 SWAP (closure, is_invalid, TRUE, &was_invalid);
598 /* invalidate only once */
600 closure_invoke_notifiers (closure, INOTIFY);
601 g_closure_unref (closure);
607 * @closure: #GClosure to decrement the reference count on
609 * Decrements the reference count of a closure after it was previously
610 * incremented by the same caller.
612 * If no other callers are using the closure, then the closure will be
613 * destroyed and freed.
616 g_closure_unref (GClosure *closure)
620 g_return_if_fail (closure != NULL);
621 g_return_if_fail (closure->ref_count > 0);
623 if (closure->ref_count == 1) /* last unref, invalidate first */
624 g_closure_invalidate (closure);
626 DEC_ASSIGN (closure, ref_count, &new_ref_count);
628 if (new_ref_count == 0)
630 closure_invoke_notifiers (closure, FNOTIFY);
631 g_free (closure->notifiers);
633 #ifdef ENABLE_VALGRIND
634 /* See comments in gtype.c about what's going on here... */
635 if (RUNNING_ON_VALGRIND)
639 allocated = (gchar *) G_REAL_CLOSURE (closure);
640 allocated -= sizeof (gpointer);
644 VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0);
645 VALGRIND_FREELIKE_BLOCK (closure, 0);
649 g_free (G_REAL_CLOSURE (closure));
655 * @closure: #GClosure to decrement the initial reference count on, if it's
658 * Takes over the initial ownership of a closure.
660 * Each closure is initially created in a "floating" state, which means
661 * that the initial reference count is not owned by any caller.
663 * This function checks to see if the object is still floating, and if so,
664 * unsets the floating state and decreases the reference count. If the
665 * closure is not floating, g_closure_sink() does nothing.
667 * The reason for the existence of the floating state is to prevent
668 * cumbersome code sequences like:
670 * |[<!-- language="C" -->
671 * closure = g_cclosure_new (cb_func, cb_data);
672 * g_source_set_closure (source, closure);
673 * g_closure_unref (closure); // GObject doesn't really need this
676 * Because g_source_set_closure() (and similar functions) take ownership of the
677 * initial reference count, if it is unowned, we instead can write:
679 * |[<!-- language="C" -->
680 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
683 * Generally, this function is used together with g_closure_ref(). An example
684 * of storing a closure for later notification looks like:
686 * |[<!-- language="C" -->
687 * static GClosure *notify_closure = NULL;
689 * foo_notify_set_closure (GClosure *closure)
691 * if (notify_closure)
692 * g_closure_unref (notify_closure);
693 * notify_closure = closure;
694 * if (notify_closure)
696 * g_closure_ref (notify_closure);
697 * g_closure_sink (notify_closure);
702 * Because g_closure_sink() may decrement the reference count of a closure
703 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
704 * g_closure_ref() should be called prior to this function.
707 g_closure_sink (GClosure *closure)
709 g_return_if_fail (closure != NULL);
710 g_return_if_fail (closure->ref_count > 0);
712 /* floating is basically a kludge to avoid creating closures
713 * with a ref_count of 0. so the initial ref_count a closure has
714 * is unowned. with invoking g_closure_sink() code may
715 * indicate that it takes over that initial ref_count.
717 if (closure->floating)
719 gboolean was_floating;
720 SWAP (closure, floating, FALSE, &was_floating);
721 /* unref floating flag only once */
723 g_closure_unref (closure);
728 * g_closure_remove_invalidate_notifier: (skip)
729 * @closure: a #GClosure
730 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
731 * when registering @notify_func
732 * @notify_func: the callback function to remove
734 * Removes an invalidation notifier.
736 * Notice that notifiers are automatically removed after they are run.
739 g_closure_remove_invalidate_notifier (GClosure *closure,
740 gpointer notify_data,
741 GClosureNotify notify_func)
743 g_return_if_fail (closure != NULL);
744 g_return_if_fail (notify_func != NULL);
746 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */
747 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
748 closure->data == notify_data)
749 closure->marshal = NULL;
750 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
751 g_critical (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
752 notify_func, notify_data);
756 * g_closure_remove_finalize_notifier: (skip)
757 * @closure: a #GClosure
758 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
759 * when registering @notify_func
760 * @notify_func: the callback function to remove
762 * Removes a finalization notifier.
764 * Notice that notifiers are automatically removed after they are run.
767 g_closure_remove_finalize_notifier (GClosure *closure,
768 gpointer notify_data,
769 GClosureNotify notify_func)
771 g_return_if_fail (closure != NULL);
772 g_return_if_fail (notify_func != NULL);
774 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */
775 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
776 closure->data == notify_data)
777 closure->marshal = NULL;
778 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
779 g_critical (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
780 notify_func, notify_data);
785 * @closure: a #GClosure
786 * @return_value: (optional) (out): a #GValue to store the return
787 * value. May be %NULL if the callback of @closure
788 * doesn't return a value.
789 * @n_param_values: the length of the @param_values array
790 * @param_values: (array length=n_param_values): an array of
791 * #GValues holding the arguments on which to
792 * invoke the callback of @closure
793 * @invocation_hint: (nullable): a context-dependent invocation hint
795 * Invokes the closure, i.e. executes the callback represented by the @closure.
798 g_closure_invoke (GClosure *closure,
799 GValue /*out*/ *return_value,
800 guint n_param_values,
801 const GValue *param_values,
802 gpointer invocation_hint)
804 GRealClosure *real_closure;
806 g_return_if_fail (closure != NULL);
808 real_closure = G_REAL_CLOSURE (closure);
810 g_closure_ref (closure); /* preserve floating flag */
811 if (!closure->is_invalid)
813 GClosureMarshal marshal;
814 gpointer marshal_data;
815 gboolean in_marshal = closure->in_marshal;
817 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
819 SET (closure, in_marshal, TRUE);
820 if (real_closure->meta_marshal)
822 marshal_data = real_closure->meta_marshal_data;
823 marshal = real_closure->meta_marshal;
828 marshal = closure->marshal;
831 closure_invoke_notifiers (closure, PRE_NOTIFY);
834 n_param_values, param_values,
838 closure_invoke_notifiers (closure, POST_NOTIFY);
839 SET (closure, in_marshal, in_marshal);
841 g_closure_unref (closure);
845 _g_closure_supports_invoke_va (GClosure *closure)
847 GRealClosure *real_closure;
849 g_return_val_if_fail (closure != NULL, FALSE);
851 real_closure = G_REAL_CLOSURE (closure);
854 real_closure->va_marshal != NULL &&
855 (real_closure->meta_marshal == NULL ||
856 real_closure->va_meta_marshal != NULL);
860 _g_closure_invoke_va (GClosure *closure,
861 GValue /*out*/ *return_value,
867 GRealClosure *real_closure;
869 g_return_if_fail (closure != NULL);
871 real_closure = G_REAL_CLOSURE (closure);
873 g_closure_ref (closure); /* preserve floating flag */
874 if (!closure->is_invalid)
876 GVaClosureMarshal marshal;
877 gpointer marshal_data;
878 gboolean in_marshal = closure->in_marshal;
880 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
882 SET (closure, in_marshal, TRUE);
883 if (real_closure->va_meta_marshal)
885 marshal_data = real_closure->meta_marshal_data;
886 marshal = real_closure->va_meta_marshal;
891 marshal = real_closure->va_marshal;
894 closure_invoke_notifiers (closure, PRE_NOTIFY);
899 n_params, param_types);
901 closure_invoke_notifiers (closure, POST_NOTIFY);
902 SET (closure, in_marshal, in_marshal);
904 g_closure_unref (closure);
909 * g_closure_set_marshal: (skip)
910 * @closure: a #GClosure
911 * @marshal: a #GClosureMarshal function
913 * Sets the marshaller of @closure.
915 * The `marshal_data` of @marshal provides a way for a meta marshaller to
916 * provide additional information to the marshaller.
918 * For GObject's C predefined marshallers (the `g_cclosure_marshal_*()`
919 * functions), what it provides is a callback function to use instead of
920 * @closure->callback.
922 * See also: g_closure_set_meta_marshal()
925 g_closure_set_marshal (GClosure *closure,
926 GClosureMarshal marshal)
928 g_return_if_fail (closure != NULL);
929 g_return_if_fail (marshal != NULL);
931 if (closure->marshal && closure->marshal != marshal)
932 g_critical ("attempt to override closure->marshal (%p) with new marshal (%p)",
933 closure->marshal, marshal);
935 closure->marshal = marshal;
939 _g_closure_set_va_marshal (GClosure *closure,
940 GVaClosureMarshal marshal)
942 GRealClosure *real_closure;
944 g_return_if_fail (closure != NULL);
945 g_return_if_fail (marshal != NULL);
947 real_closure = G_REAL_CLOSURE (closure);
949 if (real_closure->va_marshal && real_closure->va_marshal != marshal)
950 g_critical ("attempt to override closure->va_marshal (%p) with new marshal (%p)",
951 real_closure->va_marshal, marshal);
953 real_closure->va_marshal = marshal;
957 * g_cclosure_new: (skip)
958 * @callback_func: the function to invoke
959 * @user_data: (closure callback_func): user data to pass to @callback_func
960 * @destroy_data: destroy notify to be called when @user_data is no longer used
962 * Creates a new closure which invokes @callback_func with @user_data as
963 * the last parameter.
965 * @destroy_data will be called as a finalize notifier on the #GClosure.
967 * Returns: (transfer floating): a floating reference to a new #GCClosure
970 g_cclosure_new (GCallback callback_func,
972 GClosureNotify destroy_data)
976 g_return_val_if_fail (callback_func != NULL, NULL);
978 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
980 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
981 ((GCClosure*) closure)->callback = (gpointer) callback_func;
987 * g_cclosure_new_swap: (skip)
988 * @callback_func: the function to invoke
989 * @user_data: (closure callback_func): user data to pass to @callback_func
990 * @destroy_data: destroy notify to be called when @user_data is no longer used
992 * Creates a new closure which invokes @callback_func with @user_data as
993 * the first parameter.
995 * @destroy_data will be called as a finalize notifier on the #GClosure.
997 * Returns: (transfer floating): a floating reference to a new #GCClosure
1000 g_cclosure_new_swap (GCallback callback_func,
1002 GClosureNotify destroy_data)
1006 g_return_val_if_fail (callback_func != NULL, NULL);
1008 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
1010 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
1011 ((GCClosure*) closure)->callback = (gpointer) callback_func;
1012 SET (closure, derivative_flag, TRUE);
1018 g_type_class_meta_marshal (GClosure *closure,
1019 GValue /*out*/ *return_value,
1020 guint n_param_values,
1021 const GValue *param_values,
1022 gpointer invocation_hint,
1023 gpointer marshal_data)
1027 /* GType itype = (GType) closure->data; */
1028 guint offset = GPOINTER_TO_UINT (marshal_data);
1030 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1031 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1033 closure->marshal (closure,
1035 n_param_values, param_values,
1041 g_type_class_meta_marshalv (GClosure *closure,
1042 GValue *return_value,
1045 gpointer marshal_data,
1049 GRealClosure *real_closure;
1052 /* GType itype = (GType) closure->data; */
1053 guint offset = GPOINTER_TO_UINT (marshal_data);
1055 real_closure = G_REAL_CLOSURE (closure);
1057 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1058 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1060 real_closure->va_marshal (closure,
1069 g_type_iface_meta_marshal (GClosure *closure,
1070 GValue /*out*/ *return_value,
1071 guint n_param_values,
1072 const GValue *param_values,
1073 gpointer invocation_hint,
1074 gpointer marshal_data)
1078 GType itype = (GType) closure->data;
1079 guint offset = GPOINTER_TO_UINT (marshal_data);
1081 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1082 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1084 closure->marshal (closure,
1086 n_param_values, param_values,
1092 _g_closure_is_void (GClosure *closure,
1095 GRealClosure *real_closure;
1101 if (closure->is_invalid)
1104 real_closure = G_REAL_CLOSURE (closure);
1106 if (real_closure->meta_marshal == g_type_iface_meta_marshal)
1108 itype = (GType) closure->data;
1109 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1111 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1112 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1113 return callback == NULL;
1115 else if (real_closure->meta_marshal == g_type_class_meta_marshal)
1117 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1119 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1120 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1121 return callback == NULL;
1128 g_type_iface_meta_marshalv (GClosure *closure,
1129 GValue *return_value,
1132 gpointer marshal_data,
1136 GRealClosure *real_closure;
1139 GType itype = (GType) closure->data;
1140 guint offset = GPOINTER_TO_UINT (marshal_data);
1142 real_closure = G_REAL_CLOSURE (closure);
1144 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1145 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1147 real_closure->va_marshal (closure,
1156 * g_signal_type_cclosure_new:
1157 * @itype: the #GType identifier of an interface or classed type
1158 * @struct_offset: the offset of the member function of @itype's class
1159 * structure which is to be invoked by the new closure
1161 * Creates a new closure which invokes the function found at the offset
1162 * @struct_offset in the class structure of the interface or classed type
1163 * identified by @itype.
1165 * Returns: (transfer floating): a floating reference to a new #GCClosure
1168 g_signal_type_cclosure_new (GType itype,
1169 guint struct_offset)
1173 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1174 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
1176 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
1177 if (G_TYPE_IS_INTERFACE (itype))
1179 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
1180 g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv);
1184 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
1185 g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv);
1192 value_to_ffi_type (const GValue *gvalue,
1195 gboolean *tmpval_used)
1197 ffi_type *rettype = NULL;
1198 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
1199 g_assert (type != G_TYPE_INVALID);
1203 g_assert (tmpval_used != NULL);
1204 *tmpval_used = FALSE;
1209 case G_TYPE_BOOLEAN:
1212 rettype = &ffi_type_sint;
1213 *value = (gpointer)&(gvalue->data[0].v_int);
1216 /* enums are stored in v_long even though they are integers, which makes
1217 * marshalling through libffi somewhat complicated. They need to be
1218 * marshalled as signed ints, but we need to use a temporary int sized
1219 * value to pass to libffi otherwise it'll pull the wrong value on
1220 * BE machines with 32-bit integers when treating v_long as 32-bit int.
1222 g_assert (enum_tmpval != NULL);
1223 rettype = &ffi_type_sint;
1224 *enum_tmpval = g_value_get_enum (gvalue);
1225 *value = enum_tmpval;
1226 *tmpval_used = TRUE;
1229 g_assert (enum_tmpval != NULL);
1230 rettype = &ffi_type_uint;
1231 *enum_tmpval = g_value_get_flags (gvalue);
1232 *value = enum_tmpval;
1233 *tmpval_used = TRUE;
1237 rettype = &ffi_type_uint;
1238 *value = (gpointer)&(gvalue->data[0].v_uint);
1244 case G_TYPE_POINTER:
1245 case G_TYPE_INTERFACE:
1246 case G_TYPE_VARIANT:
1247 rettype = &ffi_type_pointer;
1248 *value = (gpointer)&(gvalue->data[0].v_pointer);
1251 rettype = &ffi_type_float;
1252 *value = (gpointer)&(gvalue->data[0].v_float);
1255 rettype = &ffi_type_double;
1256 *value = (gpointer)&(gvalue->data[0].v_double);
1259 rettype = &ffi_type_slong;
1260 *value = (gpointer)&(gvalue->data[0].v_long);
1263 rettype = &ffi_type_ulong;
1264 *value = (gpointer)&(gvalue->data[0].v_ulong);
1267 rettype = &ffi_type_sint64;
1268 *value = (gpointer)&(gvalue->data[0].v_int64);
1271 rettype = &ffi_type_uint64;
1272 *value = (gpointer)&(gvalue->data[0].v_uint64);
1275 rettype = &ffi_type_pointer;
1277 g_critical ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1284 value_from_ffi_type (GValue *gvalue, gpointer *value)
1286 ffi_arg *int_val = (ffi_arg*) value;
1289 type = G_VALUE_TYPE (gvalue);
1292 switch (g_type_fundamental (type))
1295 g_value_set_int (gvalue, (gint) *int_val);
1298 g_value_set_float (gvalue, *(gfloat*)value);
1301 g_value_set_double (gvalue, *(gdouble*)value);
1303 case G_TYPE_BOOLEAN:
1304 g_value_set_boolean (gvalue, (gboolean) *int_val);
1307 g_value_take_string (gvalue, *(gchar**)value);
1310 g_value_set_schar (gvalue, (gint8) *int_val);
1313 g_value_set_uchar (gvalue, (guchar) *int_val);
1316 g_value_set_uint (gvalue, (guint) *int_val);
1318 case G_TYPE_POINTER:
1319 g_value_set_pointer (gvalue, *(gpointer*)value);
1322 g_value_set_long (gvalue, (glong) *int_val);
1325 g_value_set_ulong (gvalue, (gulong) *int_val);
1328 g_value_set_int64 (gvalue, (gint64) *int_val);
1331 g_value_set_uint64 (gvalue, (guint64) *int_val);
1334 g_value_take_boxed (gvalue, *(gpointer*)value);
1337 g_value_set_enum (gvalue, (gint) *int_val);
1340 g_value_set_flags (gvalue, (guint) *int_val);
1343 g_value_take_param (gvalue, *(gpointer*)value);
1346 g_value_take_object (gvalue, *(gpointer*)value);
1348 case G_TYPE_VARIANT:
1349 g_value_take_variant (gvalue, *(gpointer*)value);
1351 case G_TYPE_INTERFACE:
1352 type = g_type_interface_instantiatable_prerequisite (type);
1357 g_critical ("value_from_ffi_type: Unsupported fundamental type %s for type %s",
1358 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))),
1359 g_type_name (G_VALUE_TYPE (gvalue)));
1376 va_to_ffi_type (GType gtype,
1378 va_arg_storage *storage)
1380 ffi_type *rettype = NULL;
1381 GType type = g_type_fundamental (gtype);
1382 g_assert (type != G_TYPE_INVALID);
1386 case G_TYPE_BOOLEAN:
1390 rettype = &ffi_type_sint;
1391 storage->_gint = va_arg (*va, gint);
1396 rettype = &ffi_type_uint;
1397 storage->_guint = va_arg (*va, guint);
1403 case G_TYPE_POINTER:
1404 case G_TYPE_INTERFACE:
1405 case G_TYPE_VARIANT:
1406 rettype = &ffi_type_pointer;
1407 storage->_gpointer = va_arg (*va, gpointer);
1410 /* Float args are passed as doubles in varargs */
1411 rettype = &ffi_type_float;
1412 storage->_float = (float)va_arg (*va, double);
1415 rettype = &ffi_type_double;
1416 storage->_double = va_arg (*va, double);
1419 rettype = &ffi_type_slong;
1420 storage->_glong = va_arg (*va, glong);
1423 rettype = &ffi_type_ulong;
1424 storage->_gulong = va_arg (*va, gulong);
1427 rettype = &ffi_type_sint64;
1428 storage->_gint64 = va_arg (*va, gint64);
1431 rettype = &ffi_type_uint64;
1432 storage->_guint64 = va_arg (*va, guint64);
1435 rettype = &ffi_type_pointer;
1436 storage->_guint64 = 0;
1437 g_critical ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1444 * g_cclosure_marshal_generic:
1445 * @closure: A #GClosure.
1446 * @return_gvalue: A #GValue to store the return value. May be %NULL
1447 * if the callback of closure doesn't return a value.
1448 * @n_param_values: The length of the @param_values array.
1449 * @param_values: An array of #GValues holding the arguments
1450 * on which to invoke the callback of closure.
1451 * @invocation_hint: The invocation hint given as the last argument to
1452 * g_closure_invoke().
1453 * @marshal_data: Additional data specified when registering the
1454 * marshaller, see g_closure_set_marshal() and
1455 * g_closure_set_meta_marshal()
1457 * A generic marshaller function implemented via
1458 * [libffi](http://sourceware.org/libffi/).
1460 * Normally this function is not passed explicitly to g_signal_new(),
1461 * but used automatically by GLib when specifying a %NULL marshaller.
1466 g_cclosure_marshal_generic (GClosure *closure,
1467 GValue *return_gvalue,
1468 guint n_param_values,
1469 const GValue *param_values,
1470 gpointer invocation_hint,
1471 gpointer marshal_data)
1480 GCClosure *cc = (GCClosure*) closure;
1482 gboolean tmpval_used = FALSE;
1484 enum_tmpval = g_alloca (sizeof (gint));
1485 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1487 rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used);
1491 rtype = &ffi_type_void;
1494 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1496 n_args = n_param_values + 1;
1497 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1498 args = g_alloca (sizeof (gpointer) * n_args);
1501 enum_tmpval = g_alloca (sizeof (gint));
1503 if (G_CCLOSURE_SWAP_DATA (closure))
1505 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
1509 atypes[0] = &ffi_type_pointer;
1510 args[0] = &closure->data;
1514 atypes[0] = value_to_ffi_type (param_values + 0,
1518 atypes[n_args-1] = &ffi_type_pointer;
1519 args[n_args-1] = &closure->data;
1522 for (i = 1; i < n_args - 1; i++)
1525 enum_tmpval = g_alloca (sizeof (gint));
1527 atypes[i] = value_to_ffi_type (param_values + i,
1533 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1536 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1538 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1539 value_from_ffi_type (return_gvalue, rvalue);
1543 * g_cclosure_marshal_generic_va:
1544 * @closure: the #GClosure to which the marshaller belongs
1545 * @return_value: (nullable): a #GValue to store the return
1546 * value. May be %NULL if the callback of @closure doesn't return a
1548 * @instance: (type GObject.TypeInstance): the instance on which the closure is
1550 * @args_list: va_list of arguments to be passed to the closure.
1551 * @marshal_data: (nullable): additional data specified when
1552 * registering the marshaller, see g_closure_set_marshal() and
1553 * g_closure_set_meta_marshal()
1554 * @n_params: the length of the @param_types array
1555 * @param_types: (array length=n_params): the #GType of each argument from
1558 * A generic #GVaClosureMarshal function implemented via
1559 * [libffi](http://sourceware.org/libffi/).
1564 g_cclosure_marshal_generic_va (GClosure *closure,
1565 GValue *return_value,
1568 gpointer marshal_data,
1577 va_arg_storage *storage;
1580 GCClosure *cc = (GCClosure*) closure;
1582 gboolean tmpval_used = FALSE;
1585 enum_tmpval = g_alloca (sizeof (gint));
1586 if (return_value && G_VALUE_TYPE (return_value))
1588 rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used);
1592 rtype = &ffi_type_void;
1595 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1597 n_args = n_params + 2;
1598 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1599 args = g_alloca (sizeof (gpointer) * n_args);
1600 storage = g_alloca (sizeof (va_arg_storage) * n_params);
1602 if (G_CCLOSURE_SWAP_DATA (closure))
1604 atypes[n_args-1] = &ffi_type_pointer;
1605 args[n_args-1] = &instance;
1606 atypes[0] = &ffi_type_pointer;
1607 args[0] = &closure->data;
1611 atypes[0] = &ffi_type_pointer;
1612 args[0] = &instance;
1613 atypes[n_args-1] = &ffi_type_pointer;
1614 args[n_args-1] = &closure->data;
1617 va_copy (args_copy, args_list);
1619 /* Box non-primitive arguments */
1620 for (i = 0; i < n_params; i++)
1622 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1623 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1625 atypes[i+1] = va_to_ffi_type (type,
1628 args[i+1] = &storage[i];
1630 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1632 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1633 storage[i]._gpointer = g_strdup (storage[i]._gpointer);
1634 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1635 storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer);
1636 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1637 storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer);
1638 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1639 storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer);
1641 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1642 storage[i]._gpointer = g_object_ref (storage[i]._gpointer);
1647 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1650 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1652 /* Unbox non-primitive arguments */
1653 for (i = 0; i < n_params; i++)
1655 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1656 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1658 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1660 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1661 g_free (storage[i]._gpointer);
1662 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1663 g_param_spec_unref (storage[i]._gpointer);
1664 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1665 g_boxed_free (type, storage[i]._gpointer);
1666 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1667 g_variant_unref (storage[i]._gpointer);
1669 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1670 g_object_unref (storage[i]._gpointer);
1673 if (return_value && G_VALUE_TYPE (return_value))
1674 value_from_ffi_type (return_value, rvalue);
1678 * g_cclosure_marshal_VOID__VOID:
1679 * @closure: the #GClosure to which the marshaller belongs
1680 * @return_value: ignored
1681 * @n_param_values: 1
1682 * @param_values: a #GValue array holding only the instance
1683 * @invocation_hint: the invocation hint given as the last argument
1684 * to g_closure_invoke()
1685 * @marshal_data: additional data specified when registering the marshaller
1687 * A marshaller for a #GCClosure with a callback of type
1688 * `void (*callback) (gpointer instance, gpointer user_data)`.
1692 * g_cclosure_marshal_VOID__BOOLEAN:
1693 * @closure: the #GClosure to which the marshaller belongs
1694 * @return_value: ignored
1695 * @n_param_values: 2
1696 * @param_values: a #GValue array holding the instance and the #gboolean parameter
1697 * @invocation_hint: the invocation hint given as the last argument
1698 * to g_closure_invoke()
1699 * @marshal_data: additional data specified when registering the marshaller
1701 * A marshaller for a #GCClosure with a callback of type
1702 * `void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)`.
1706 * g_cclosure_marshal_VOID__CHAR:
1707 * @closure: the #GClosure to which the marshaller belongs
1708 * @return_value: ignored
1709 * @n_param_values: 2
1710 * @param_values: a #GValue array holding the instance and the #gchar parameter
1711 * @invocation_hint: the invocation hint given as the last argument
1712 * to g_closure_invoke()
1713 * @marshal_data: additional data specified when registering the marshaller
1715 * A marshaller for a #GCClosure with a callback of type
1716 * `void (*callback) (gpointer instance, gchar arg1, gpointer user_data)`.
1720 * g_cclosure_marshal_VOID__UCHAR:
1721 * @closure: the #GClosure to which the marshaller belongs
1722 * @return_value: ignored
1723 * @n_param_values: 2
1724 * @param_values: a #GValue array holding the instance and the #guchar parameter
1725 * @invocation_hint: the invocation hint given as the last argument
1726 * to g_closure_invoke()
1727 * @marshal_data: additional data specified when registering the marshaller
1729 * A marshaller for a #GCClosure with a callback of type
1730 * `void (*callback) (gpointer instance, guchar arg1, gpointer user_data)`.
1734 * g_cclosure_marshal_VOID__INT:
1735 * @closure: the #GClosure to which the marshaller belongs
1736 * @return_value: ignored
1737 * @n_param_values: 2
1738 * @param_values: a #GValue array holding the instance and the #gint parameter
1739 * @invocation_hint: the invocation hint given as the last argument
1740 * to g_closure_invoke()
1741 * @marshal_data: additional data specified when registering the marshaller
1743 * A marshaller for a #GCClosure with a callback of type
1744 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)`.
1748 * g_cclosure_marshal_VOID__UINT:
1749 * @closure: the #GClosure to which the marshaller belongs
1750 * @return_value: ignored
1751 * @n_param_values: 2
1752 * @param_values: a #GValue array holding the instance and the #guint parameter
1753 * @invocation_hint: the invocation hint given as the last argument
1754 * to g_closure_invoke()
1755 * @marshal_data: additional data specified when registering the marshaller
1757 * A marshaller for a #GCClosure with a callback of type
1758 * `void (*callback) (gpointer instance, guint arg1, gpointer user_data)`.
1762 * g_cclosure_marshal_VOID__LONG:
1763 * @closure: the #GClosure to which the marshaller belongs
1764 * @return_value: ignored
1765 * @n_param_values: 2
1766 * @param_values: a #GValue array holding the instance and the #glong parameter
1767 * @invocation_hint: the invocation hint given as the last argument
1768 * to g_closure_invoke()
1769 * @marshal_data: additional data specified when registering the marshaller
1771 * A marshaller for a #GCClosure with a callback of type
1772 * `void (*callback) (gpointer instance, glong arg1, gpointer user_data)`.
1776 * g_cclosure_marshal_VOID__ULONG:
1777 * @closure: the #GClosure to which the marshaller belongs
1778 * @return_value: ignored
1779 * @n_param_values: 2
1780 * @param_values: a #GValue array holding the instance and the #gulong parameter
1781 * @invocation_hint: the invocation hint given as the last argument
1782 * to g_closure_invoke()
1783 * @marshal_data: additional data specified when registering the marshaller
1785 * A marshaller for a #GCClosure with a callback of type
1786 * `void (*callback) (gpointer instance, gulong arg1, gpointer user_data)`.
1790 * g_cclosure_marshal_VOID__ENUM:
1791 * @closure: the #GClosure to which the marshaller belongs
1792 * @return_value: ignored
1793 * @n_param_values: 2
1794 * @param_values: a #GValue array holding the instance and the enumeration parameter
1795 * @invocation_hint: the invocation hint given as the last argument
1796 * to g_closure_invoke()
1797 * @marshal_data: additional data specified when registering the marshaller
1799 * A marshaller for a #GCClosure with a callback of type
1800 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes an enumeration type..
1804 * g_cclosure_marshal_VOID__FLAGS:
1805 * @closure: the #GClosure to which the marshaller belongs
1806 * @return_value: ignored
1807 * @n_param_values: 2
1808 * @param_values: a #GValue array holding the instance and the flags parameter
1809 * @invocation_hint: the invocation hint given as the last argument
1810 * to g_closure_invoke()
1811 * @marshal_data: additional data specified when registering the marshaller
1813 * A marshaller for a #GCClosure with a callback of type
1814 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes a flags type.
1818 * g_cclosure_marshal_VOID__FLOAT:
1819 * @closure: the #GClosure to which the marshaller belongs
1820 * @return_value: ignored
1821 * @n_param_values: 2
1822 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1823 * @invocation_hint: the invocation hint given as the last argument
1824 * to g_closure_invoke()
1825 * @marshal_data: additional data specified when registering the marshaller
1827 * A marshaller for a #GCClosure with a callback of type
1828 * `void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)`.
1832 * g_cclosure_marshal_VOID__DOUBLE:
1833 * @closure: the #GClosure to which the marshaller belongs
1834 * @return_value: ignored
1835 * @n_param_values: 2
1836 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1837 * @invocation_hint: the invocation hint given as the last argument
1838 * to g_closure_invoke()
1839 * @marshal_data: additional data specified when registering the marshaller
1841 * A marshaller for a #GCClosure with a callback of type
1842 * `void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)`.
1846 * g_cclosure_marshal_VOID__STRING:
1847 * @closure: the #GClosure to which the marshaller belongs
1848 * @return_value: ignored
1849 * @n_param_values: 2
1850 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1851 * @invocation_hint: the invocation hint given as the last argument
1852 * to g_closure_invoke()
1853 * @marshal_data: additional data specified when registering the marshaller
1855 * A marshaller for a #GCClosure with a callback of type
1856 * `void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)`.
1860 * g_cclosure_marshal_VOID__PARAM:
1861 * @closure: the #GClosure to which the marshaller belongs
1862 * @return_value: ignored
1863 * @n_param_values: 2
1864 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1865 * @invocation_hint: the invocation hint given as the last argument
1866 * to g_closure_invoke()
1867 * @marshal_data: additional data specified when registering the marshaller
1869 * A marshaller for a #GCClosure with a callback of type
1870 * `void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)`.
1874 * g_cclosure_marshal_VOID__BOXED:
1875 * @closure: the #GClosure to which the marshaller belongs
1876 * @return_value: ignored
1877 * @n_param_values: 2
1878 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1879 * @invocation_hint: the invocation hint given as the last argument
1880 * to g_closure_invoke()
1881 * @marshal_data: additional data specified when registering the marshaller
1883 * A marshaller for a #GCClosure with a callback of type
1884 * `void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)`.
1888 * g_cclosure_marshal_VOID__POINTER:
1889 * @closure: the #GClosure to which the marshaller belongs
1890 * @return_value: ignored
1891 * @n_param_values: 2
1892 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1893 * @invocation_hint: the invocation hint given as the last argument
1894 * to g_closure_invoke()
1895 * @marshal_data: additional data specified when registering the marshaller
1897 * A marshaller for a #GCClosure with a callback of type
1898 * `void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)`.
1902 * g_cclosure_marshal_VOID__OBJECT:
1903 * @closure: the #GClosure to which the marshaller belongs
1904 * @return_value: ignored
1905 * @n_param_values: 2
1906 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1907 * @invocation_hint: the invocation hint given as the last argument
1908 * to g_closure_invoke()
1909 * @marshal_data: additional data specified when registering the marshaller
1911 * A marshaller for a #GCClosure with a callback of type
1912 * `void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)`.
1916 * g_cclosure_marshal_VOID__VARIANT:
1917 * @closure: the #GClosure to which the marshaller belongs
1918 * @return_value: ignored
1919 * @n_param_values: 2
1920 * @param_values: a #GValue array holding the instance and the #GVariant* parameter
1921 * @invocation_hint: the invocation hint given as the last argument
1922 * to g_closure_invoke()
1923 * @marshal_data: additional data specified when registering the marshaller
1925 * A marshaller for a #GCClosure with a callback of type
1926 * `void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)`.
1932 * g_cclosure_marshal_VOID__UINT_POINTER:
1933 * @closure: the #GClosure to which the marshaller belongs
1934 * @return_value: ignored
1935 * @n_param_values: 3
1936 * @param_values: a #GValue array holding instance, arg1 and arg2
1937 * @invocation_hint: the invocation hint given as the last argument
1938 * to g_closure_invoke()
1939 * @marshal_data: additional data specified when registering the marshaller
1941 * A marshaller for a #GCClosure with a callback of type
1942 * `void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)`.
1946 * g_cclosure_marshal_BOOLEAN__FLAGS:
1947 * @closure: the #GClosure to which the marshaller belongs
1948 * @return_value: a #GValue which can store the returned #gboolean
1949 * @n_param_values: 2
1950 * @param_values: a #GValue array holding instance and arg1
1951 * @invocation_hint: the invocation hint given as the last argument
1952 * to g_closure_invoke()
1953 * @marshal_data: additional data specified when registering the marshaller
1955 * A marshaller for a #GCClosure with a callback of type
1956 * `gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter
1957 * denotes a flags type.
1961 * g_cclosure_marshal_BOOL__FLAGS:
1963 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1966 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1967 * @closure: the #GClosure to which the marshaller belongs
1968 * @return_value: a #GValue, which can store the returned string
1969 * @n_param_values: 3
1970 * @param_values: a #GValue array holding instance, arg1 and arg2
1971 * @invocation_hint: the invocation hint given as the last argument
1972 * to g_closure_invoke()
1973 * @marshal_data: additional data specified when registering the marshaller
1975 * A marshaller for a #GCClosure with a callback of type
1976 * `gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)`.
1979 * g_cclosure_marshal_BOOLEAN__OBJECT_BOXED_BOXED:
1980 * @closure: the #GClosure to which the marshaller belongs
1981 * @return_value: a #GValue, which can store the returned string
1982 * @n_param_values: 3
1983 * @param_values: a #GValue array holding instance, arg1 and arg2
1984 * @invocation_hint: the invocation hint given as the last argument
1985 * to g_closure_invoke()
1986 * @marshal_data: additional data specified when registering the marshaller
1988 * A marshaller for a #GCClosure with a callback of type
1989 * `gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)`.