gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gobject / gclosure.c
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  * SECTION:gclosure
43  * @short_description: Functions as first-class objects
44  * @title: Closures
45  *
46  * A #GClosure represents a callback supplied by the programmer.
47  *
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.
53  *
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.
63  *
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
75  * data pair.
76  *
77  * Using closures has a number of important advantages over a simple
78  * callback function/data pointer combination:
79  * 
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.
83  *
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.
87  *
88  * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
89  *   automatically removed when the objects they point to go away.
90  */
91
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 + \
100                                          (cl)->n_inotifiers)
101
102 typedef union {
103   GClosure closure;
104   gint vint;
105 } ClosureInt;
106
107 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW)      \
108 G_STMT_START {                                                                          \
109   ClosureInt *cunion = (ClosureInt*) _closure;                                          \
110   gint new_int, old_int, success;                                                       \
111   do                                                                                    \
112     {                                                                                   \
113       ClosureInt tmp;                                                                   \
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);    \
120     }                                                                                   \
121   while (!success && _must_set);                                                        \
122 } G_STMT_END
123
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) = )
130
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)
138 #endif
139
140 enum {
141   FNOTIFY,
142   INOTIFY,
143   PRE_NOTIFY,
144   POST_NOTIFY
145 };
146
147
148 /* --- functions --- */
149 /**
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
154  *
155  * Allocates a struct of the given size and initializes the initial
156  * part as a #GClosure.
157  *
158  * This function is mainly useful when implementing new types of closures:
159  *
160  * |[<!-- language="C" --> 
161  * typedef struct _MyClosure MyClosure;
162  * struct _MyClosure
163  * {
164  *   GClosure closure;
165  *   // extra data goes here
166  * };
167  *
168  * static void
169  * my_closure_finalize (gpointer  notify_data,
170  *                      GClosure *closure)
171  * {
172  *   MyClosure *my_closure = (MyClosure *)closure;
173  *
174  *   // free extra data here
175  * }
176  *
177  * MyClosure *my_closure_new (gpointer data)
178  * {
179  *   GClosure *closure;
180  *   MyClosure *my_closure;
181  *
182  *   closure = g_closure_new_simple (sizeof (MyClosure), data);
183  *   my_closure = (MyClosure *) closure;
184  *
185  *   // initialize extra data here
186  *
187  *   g_closure_add_finalize_notifier (closure, notify_data,
188  *                                    my_closure_finalize);
189  *   return my_closure;
190  * }
191  * ]|
192  *
193  * Returns: (transfer floating): a floating reference to a new #GClosure
194  */
195 GClosure*
196 g_closure_new_simple (guint           sizeof_closure,
197                       gpointer        data)
198 {
199   GClosure *closure;
200   gint private_size;
201   gchar *allocated;
202
203   g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
204
205   private_size = sizeof (GRealClosure) - sizeof (GClosure);
206
207 #ifdef ENABLE_VALGRIND
208   /* See comments in gtype.c about what's going on here... */
209   if (RUNNING_ON_VALGRIND)
210     {
211       private_size += sizeof (gpointer);
212
213       allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer));
214
215       *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer);
216
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);
219     }
220   else
221 #endif
222     allocated = g_malloc0 (private_size + sizeof_closure);
223
224   closure = (GClosure *) (allocated + private_size);
225
226   SET (closure, ref_count, 1);
227   SET (closure, floating, TRUE);
228   closure->data = data;
229
230   return closure;
231 }
232
233 static inline void
234 closure_invoke_notifiers (GClosure *closure,
235                           guint     notify_type)
236 {
237   /* notifier layout:
238    *     n_guards    n_guards     n_fnotif.  n_inotifiers
239    * ->[[pre_guards][post_guards][fnotifiers][inotifiers]]
240    *
241    * CLOSURE_N_MFUNCS(cl)    = n_guards + n_guards;
242    * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
243    *
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
253    */
254   switch (notify_type)
255     {
256       GClosureNotifyData *ndata;
257       guint i, offs;
258     case FNOTIFY:
259       while (closure->n_fnotifiers)
260         {
261           guint n;
262           DEC_ASSIGN (closure, n_fnotifiers, &n);
263
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);
268         }
269       closure->marshal = NULL;
270       closure->data = NULL;
271       break;
272     case INOTIFY:
273       SET (closure, in_inotify, TRUE);
274       while (closure->n_inotifiers)
275         {
276           guint n;
277           DEC_ASSIGN (closure, n_inotifiers, &n);
278
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);
283         }
284       closure->marshal = NULL;
285       closure->data = NULL;
286       SET (closure, in_inotify, FALSE);
287       break;
288     case PRE_NOTIFY:
289       i = closure->n_guards;
290       offs = 0;
291       while (i--)
292         {
293           ndata = closure->notifiers + offs + i;
294           ndata->notify (ndata->data, closure);
295         }
296       break;
297     case POST_NOTIFY:
298       i = closure->n_guards;
299       offs = i;
300       while (i--)
301         {
302           ndata = closure->notifiers + offs + i;
303           ndata->notify (ndata->data, closure);
304         }
305       break;
306     }
307 }
308
309 static void
310 g_closure_set_meta_va_marshal (GClosure       *closure,
311                                GVaClosureMarshal va_meta_marshal)
312 {
313   GRealClosure *real_closure;
314
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);
319
320   real_closure = G_REAL_CLOSURE (closure);
321
322   g_return_if_fail (real_closure->meta_marshal != NULL);
323
324   real_closure->va_meta_marshal = va_meta_marshal;
325 }
326
327 /**
328  * g_closure_set_meta_marshal: (skip)
329  * @closure: a #GClosure
330  * @marshal_data: (closure meta_marshal): context-dependent data to pass
331  *  to @meta_marshal
332  * @meta_marshal: a #GClosureMarshal function
333  *
334  * Sets the meta marshaller of @closure.
335  *
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.
339  *
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
344  * callback function.
345  *
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.
351  */
352 void
353 g_closure_set_meta_marshal (GClosure       *closure,
354                             gpointer        marshal_data,
355                             GClosureMarshal meta_marshal)
356 {
357   GRealClosure *real_closure;
358
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);
363
364   real_closure = G_REAL_CLOSURE (closure);
365
366   g_return_if_fail (real_closure->meta_marshal == NULL);
367
368   real_closure->meta_marshal = meta_marshal;
369   real_closure->meta_marshal_data = marshal_data;
370 }
371
372 /**
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
381  *
382  * Adds a pair of notifiers which get invoked before and after the
383  * closure callback, respectively.
384  *
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.
388  */
389 void
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)
395 {
396   guint i;
397
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);
404
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);
431 }
432
433 /**
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
438  *
439  * Registers a finalization notifier which will be called when the
440  * reference count of @closure goes down to 0.
441  *
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.
446  */
447 void
448 g_closure_add_finalize_notifier (GClosure      *closure,
449                                  gpointer       notify_data,
450                                  GClosureNotify notify_func)
451 {
452   guint i;
453
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);
457
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);
468 }
469
470 /**
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
475  *
476  * Registers an invalidation notifier which will be called when the
477  * @closure is invalidated with g_closure_invalidate().
478  *
479  * Invalidation notifiers are invoked before finalization notifiers,
480  * in an unspecified order.
481  */
482 void
483 g_closure_add_invalidate_notifier (GClosure      *closure,
484                                    gpointer       notify_data,
485                                    GClosureNotify notify_func)
486 {
487   guint i;
488
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);
493
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);
499 }
500
501 static inline gboolean
502 closure_try_remove_inotify (GClosure       *closure,
503                             gpointer       notify_data,
504                             GClosureNotify notify_func)
505 {
506   GClosureNotifyData *ndata, *nlast;
507
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)
511       {
512         DEC (closure, n_inotifiers);
513         if (ndata < nlast)
514           *ndata = *nlast;
515
516         return TRUE;
517       }
518   return FALSE;
519 }
520
521 static inline gboolean
522 closure_try_remove_fnotify (GClosure       *closure,
523                             gpointer       notify_data,
524                             GClosureNotify notify_func)
525 {
526   GClosureNotifyData *ndata, *nlast;
527
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)
531       {
532         DEC (closure, n_fnotifiers);
533         if (ndata < nlast)
534           *ndata = *nlast;
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)];
540         return TRUE;
541       }
542   return FALSE;
543 }
544
545 /**
546  * g_closure_ref:
547  * @closure: #GClosure to increment the reference count on
548  *
549  * Increments the reference count on a closure to force it staying
550  * alive while the caller holds a pointer to it.
551  *
552  * Returns: (transfer none): The @closure passed in, for convenience
553  */
554 GClosure*
555 g_closure_ref (GClosure *closure)
556 {
557   guint new_ref_count;
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);
561
562   INC_ASSIGN (closure, ref_count, &new_ref_count);
563   g_return_val_if_fail (new_ref_count > 1, NULL);
564
565   return closure;
566 }
567
568 /**
569  * g_closure_invalidate:
570  * @closure: #GClosure to invalidate
571  *
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
575  * ignored.
576  *
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().
583  *
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).
587  */
588 void
589 g_closure_invalidate (GClosure *closure)
590 {
591   g_return_if_fail (closure != NULL);
592
593   if (!closure->is_invalid)
594     {
595       gboolean was_invalid;
596       g_closure_ref (closure);           /* preserve floating flag */
597       SWAP (closure, is_invalid, TRUE, &was_invalid);
598       /* invalidate only once */
599       if (!was_invalid)
600         closure_invoke_notifiers (closure, INOTIFY);
601       g_closure_unref (closure);
602     }
603 }
604
605 /**
606  * g_closure_unref:
607  * @closure: #GClosure to decrement the reference count on
608  *
609  * Decrements the reference count of a closure after it was previously
610  * incremented by the same caller.
611  *
612  * If no other callers are using the closure, then the closure will be
613  * destroyed and freed.
614  */
615 void
616 g_closure_unref (GClosure *closure)
617 {
618   guint new_ref_count;
619
620   g_return_if_fail (closure != NULL);
621   g_return_if_fail (closure->ref_count > 0);
622
623   if (closure->ref_count == 1)  /* last unref, invalidate first */
624     g_closure_invalidate (closure);
625
626   DEC_ASSIGN (closure, ref_count, &new_ref_count);
627
628   if (new_ref_count == 0)
629     {
630       closure_invoke_notifiers (closure, FNOTIFY);
631       g_free (closure->notifiers);
632
633 #ifdef ENABLE_VALGRIND
634       /* See comments in gtype.c about what's going on here... */
635       if (RUNNING_ON_VALGRIND)
636         {
637           gchar *allocated;
638
639           allocated = (gchar *) G_REAL_CLOSURE (closure);
640           allocated -= sizeof (gpointer);
641
642           g_free (allocated);
643
644           VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0);
645           VALGRIND_FREELIKE_BLOCK (closure, 0);
646         }
647       else
648 #endif
649         g_free (G_REAL_CLOSURE (closure));
650     }
651 }
652
653 /**
654  * g_closure_sink:
655  * @closure: #GClosure to decrement the initial reference count on, if it's
656  *           still being held
657  *
658  * Takes over the initial ownership of a closure.
659  *
660  * Each closure is initially created in a "floating" state, which means
661  * that the initial reference count is not owned by any caller.
662  *
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.
666  *
667  * The reason for the existence of the floating state is to prevent
668  * cumbersome code sequences like:
669  *
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
674  * ]|
675  *
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:
678  *
679  * |[<!-- language="C" --> 
680  * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
681  * ]|
682  *
683  * Generally, this function is used together with g_closure_ref(). An example
684  * of storing a closure for later notification looks like:
685  *
686  * |[<!-- language="C" --> 
687  * static GClosure *notify_closure = NULL;
688  * void
689  * foo_notify_set_closure (GClosure *closure)
690  * {
691  *   if (notify_closure)
692  *     g_closure_unref (notify_closure);
693  *   notify_closure = closure;
694  *   if (notify_closure)
695  *     {
696  *       g_closure_ref (notify_closure);
697  *       g_closure_sink (notify_closure);
698  *     }
699  * }
700  * ]|
701  *
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.
705  */
706 void
707 g_closure_sink (GClosure *closure)
708 {
709   g_return_if_fail (closure != NULL);
710   g_return_if_fail (closure->ref_count > 0);
711
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.
716    */
717   if (closure->floating)
718     {
719       gboolean was_floating;
720       SWAP (closure, floating, FALSE, &was_floating);
721       /* unref floating flag only once */
722       if (was_floating)
723         g_closure_unref (closure);
724     }
725 }
726
727 /**
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
733  *
734  * Removes an invalidation notifier.
735  *
736  * Notice that notifiers are automatically removed after they are run.
737  */
738 void
739 g_closure_remove_invalidate_notifier (GClosure      *closure,
740                                       gpointer       notify_data,
741                                       GClosureNotify notify_func)
742 {
743   g_return_if_fail (closure != NULL);
744   g_return_if_fail (notify_func != NULL);
745
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);
753 }
754
755 /**
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
761  *
762  * Removes a finalization notifier.
763  *
764  * Notice that notifiers are automatically removed after they are run.
765  */
766 void
767 g_closure_remove_finalize_notifier (GClosure      *closure,
768                                     gpointer       notify_data,
769                                     GClosureNotify notify_func)
770 {
771   g_return_if_fail (closure != NULL);
772   g_return_if_fail (notify_func != NULL);
773
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);
781 }
782
783 /**
784  * g_closure_invoke:
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
794  *
795  * Invokes the closure, i.e. executes the callback represented by the @closure.
796  */
797 void
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)
803 {
804   GRealClosure *real_closure;
805
806   g_return_if_fail (closure != NULL);
807
808   real_closure = G_REAL_CLOSURE (closure);
809
810   g_closure_ref (closure);      /* preserve floating flag */
811   if (!closure->is_invalid)
812     {
813       GClosureMarshal marshal;
814       gpointer marshal_data;
815       gboolean in_marshal = closure->in_marshal;
816
817       g_return_if_fail (closure->marshal || real_closure->meta_marshal);
818
819       SET (closure, in_marshal, TRUE);
820       if (real_closure->meta_marshal)
821         {
822           marshal_data = real_closure->meta_marshal_data;
823           marshal = real_closure->meta_marshal;
824         }
825       else
826         {
827           marshal_data = NULL;
828           marshal = closure->marshal;
829         }
830       if (!in_marshal)
831         closure_invoke_notifiers (closure, PRE_NOTIFY);
832       marshal (closure,
833                return_value,
834                n_param_values, param_values,
835                invocation_hint,
836                marshal_data);
837       if (!in_marshal)
838         closure_invoke_notifiers (closure, POST_NOTIFY);
839       SET (closure, in_marshal, in_marshal);
840     }
841   g_closure_unref (closure);
842 }
843
844 gboolean
845 _g_closure_supports_invoke_va (GClosure       *closure)
846 {
847   GRealClosure *real_closure;
848
849   g_return_val_if_fail (closure != NULL, FALSE);
850
851   real_closure = G_REAL_CLOSURE (closure);
852
853   return
854     real_closure->va_marshal != NULL &&
855     (real_closure->meta_marshal == NULL ||
856      real_closure->va_meta_marshal != NULL);
857 }
858
859 void
860 _g_closure_invoke_va (GClosure       *closure,
861                       GValue /*out*/ *return_value,
862                       gpointer        instance,
863                       va_list         args,
864                       int             n_params,
865                       GType          *param_types)
866 {
867   GRealClosure *real_closure;
868
869   g_return_if_fail (closure != NULL);
870
871   real_closure = G_REAL_CLOSURE (closure);
872
873   g_closure_ref (closure);      /* preserve floating flag */
874   if (!closure->is_invalid)
875     {
876       GVaClosureMarshal marshal;
877       gpointer marshal_data;
878       gboolean in_marshal = closure->in_marshal;
879
880       g_return_if_fail (closure->marshal || real_closure->meta_marshal);
881
882       SET (closure, in_marshal, TRUE);
883       if (real_closure->va_meta_marshal)
884         {
885           marshal_data = real_closure->meta_marshal_data;
886           marshal = real_closure->va_meta_marshal;
887         }
888       else
889         {
890           marshal_data = NULL;
891           marshal = real_closure->va_marshal;
892         }
893       if (!in_marshal)
894         closure_invoke_notifiers (closure, PRE_NOTIFY);
895       marshal (closure,
896                return_value,
897                instance, args,
898                marshal_data,
899                n_params, param_types);
900       if (!in_marshal)
901         closure_invoke_notifiers (closure, POST_NOTIFY);
902       SET (closure, in_marshal, in_marshal);
903     }
904   g_closure_unref (closure);
905 }
906
907
908 /**
909  * g_closure_set_marshal: (skip)
910  * @closure: a #GClosure
911  * @marshal: a #GClosureMarshal function
912  *
913  * Sets the marshaller of @closure.
914  *
915  * The `marshal_data` of @marshal provides a way for a meta marshaller to
916  * provide additional information to the marshaller.
917  *
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.
921  *
922  * See also: g_closure_set_meta_marshal()
923  */
924 void
925 g_closure_set_marshal (GClosure       *closure,
926                        GClosureMarshal marshal)
927 {
928   g_return_if_fail (closure != NULL);
929   g_return_if_fail (marshal != NULL);
930
931   if (closure->marshal && closure->marshal != marshal)
932     g_critical ("attempt to override closure->marshal (%p) with new marshal (%p)",
933                 closure->marshal, marshal);
934   else
935     closure->marshal = marshal;
936 }
937
938 void
939 _g_closure_set_va_marshal (GClosure       *closure,
940                            GVaClosureMarshal marshal)
941 {
942   GRealClosure *real_closure;
943
944   g_return_if_fail (closure != NULL);
945   g_return_if_fail (marshal != NULL);
946
947   real_closure = G_REAL_CLOSURE (closure);
948
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);
952   else
953     real_closure->va_marshal = marshal;
954 }
955
956 /**
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
961  *
962  * Creates a new closure which invokes @callback_func with @user_data as
963  * the last parameter.
964  *
965  * @destroy_data will be called as a finalize notifier on the #GClosure.
966  *
967  * Returns: (transfer floating): a floating reference to a new #GCClosure
968  */
969 GClosure*
970 g_cclosure_new (GCallback      callback_func,
971                 gpointer       user_data,
972                 GClosureNotify destroy_data)
973 {
974   GClosure *closure;
975   
976   g_return_val_if_fail (callback_func != NULL, NULL);
977   
978   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
979   if (destroy_data)
980     g_closure_add_finalize_notifier (closure, user_data, destroy_data);
981   ((GCClosure*) closure)->callback = (gpointer) callback_func;
982   
983   return closure;
984 }
985
986 /**
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
991  *
992  * Creates a new closure which invokes @callback_func with @user_data as
993  * the first parameter.
994  *
995  * @destroy_data will be called as a finalize notifier on the #GClosure.
996  *
997  * Returns: (transfer floating): a floating reference to a new #GCClosure
998  */
999 GClosure*
1000 g_cclosure_new_swap (GCallback      callback_func,
1001                      gpointer       user_data,
1002                      GClosureNotify destroy_data)
1003 {
1004   GClosure *closure;
1005   
1006   g_return_val_if_fail (callback_func != NULL, NULL);
1007   
1008   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
1009   if (destroy_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);
1013   
1014   return closure;
1015 }
1016
1017 static void
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)
1024 {
1025   GTypeClass *class;
1026   gpointer callback;
1027   /* GType itype = (GType) closure->data; */
1028   guint offset = GPOINTER_TO_UINT (marshal_data);
1029   
1030   class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1031   callback = G_STRUCT_MEMBER (gpointer, class, offset);
1032   if (callback)
1033     closure->marshal (closure,
1034                       return_value,
1035                       n_param_values, param_values,
1036                       invocation_hint,
1037                       callback);
1038 }
1039
1040 static void
1041 g_type_class_meta_marshalv (GClosure *closure,
1042                             GValue   *return_value,
1043                             gpointer  instance,
1044                             va_list   args,
1045                             gpointer  marshal_data,
1046                             int       n_params,
1047                             GType    *param_types)
1048 {
1049   GRealClosure *real_closure;
1050   GTypeClass *class;
1051   gpointer callback;
1052   /* GType itype = (GType) closure->data; */
1053   guint offset = GPOINTER_TO_UINT (marshal_data);
1054
1055   real_closure = G_REAL_CLOSURE (closure);
1056
1057   class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1058   callback = G_STRUCT_MEMBER (gpointer, class, offset);
1059   if (callback)
1060     real_closure->va_marshal (closure,
1061                               return_value,
1062                               instance, args,
1063                               callback,
1064                               n_params,
1065                               param_types);
1066 }
1067
1068 static void
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)
1075 {
1076   GTypeClass *class;
1077   gpointer callback;
1078   GType itype = (GType) closure->data;
1079   guint offset = GPOINTER_TO_UINT (marshal_data);
1080   
1081   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1082   callback = G_STRUCT_MEMBER (gpointer, class, offset);
1083   if (callback)
1084     closure->marshal (closure,
1085                       return_value,
1086                       n_param_values, param_values,
1087                       invocation_hint,
1088                       callback);
1089 }
1090
1091 gboolean
1092 _g_closure_is_void (GClosure *closure,
1093                     gpointer instance)
1094 {
1095   GRealClosure *real_closure;
1096   GTypeClass *class;
1097   gpointer callback;
1098   GType itype;
1099   guint offset;
1100
1101   if (closure->is_invalid)
1102     return TRUE;
1103
1104   real_closure = G_REAL_CLOSURE (closure);
1105
1106   if (real_closure->meta_marshal == g_type_iface_meta_marshal)
1107     {
1108       itype = (GType) closure->data;
1109       offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1110
1111       class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1112       callback = G_STRUCT_MEMBER (gpointer, class, offset);
1113       return callback == NULL;
1114     }
1115   else if (real_closure->meta_marshal == g_type_class_meta_marshal)
1116     {
1117       offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1118
1119       class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1120       callback = G_STRUCT_MEMBER (gpointer, class, offset);
1121       return callback == NULL;
1122     }
1123
1124   return FALSE;
1125 }
1126
1127 static void
1128 g_type_iface_meta_marshalv (GClosure *closure,
1129                             GValue   *return_value,
1130                             gpointer  instance,
1131                             va_list   args,
1132                             gpointer  marshal_data,
1133                             int       n_params,
1134                             GType    *param_types)
1135 {
1136   GRealClosure *real_closure;
1137   GTypeClass *class;
1138   gpointer callback;
1139   GType itype = (GType) closure->data;
1140   guint offset = GPOINTER_TO_UINT (marshal_data);
1141
1142   real_closure = G_REAL_CLOSURE (closure);
1143
1144   class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1145   callback = G_STRUCT_MEMBER (gpointer, class, offset);
1146   if (callback)
1147     real_closure->va_marshal (closure,
1148                               return_value,
1149                               instance, args,
1150                               callback,
1151                               n_params,
1152                               param_types);
1153 }
1154
1155 /**
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
1160  *
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.
1164  *
1165  * Returns: (transfer floating): a floating reference to a new #GCClosure
1166  */
1167 GClosure*
1168 g_signal_type_cclosure_new (GType    itype,
1169                             guint    struct_offset)
1170 {
1171   GClosure *closure;
1172   
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);
1175   
1176   closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
1177   if (G_TYPE_IS_INTERFACE (itype))
1178     {
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);
1181     }
1182   else
1183     {
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);
1186     }
1187   return closure;
1188 }
1189
1190 #include <ffi.h>
1191 static ffi_type *
1192 value_to_ffi_type (const GValue *gvalue,
1193                    gpointer *value,
1194                    gint *enum_tmpval,
1195                    gboolean *tmpval_used)
1196 {
1197   ffi_type *rettype = NULL;
1198   GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
1199   g_assert (type != G_TYPE_INVALID);
1200
1201   if (enum_tmpval)
1202     {
1203       g_assert (tmpval_used != NULL);
1204       *tmpval_used = FALSE;
1205     }
1206
1207   switch (type)
1208     {
1209     case G_TYPE_BOOLEAN:
1210     case G_TYPE_CHAR:
1211     case G_TYPE_INT:
1212       rettype = &ffi_type_sint;
1213       *value = (gpointer)&(gvalue->data[0].v_int);
1214       break;
1215     case G_TYPE_ENUM:
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.
1221        */
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;
1227       break;
1228     case G_TYPE_FLAGS:
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;
1234       break;
1235     case G_TYPE_UCHAR:
1236     case G_TYPE_UINT:
1237       rettype = &ffi_type_uint;
1238       *value = (gpointer)&(gvalue->data[0].v_uint);
1239       break;
1240     case G_TYPE_STRING:
1241     case G_TYPE_OBJECT:
1242     case G_TYPE_BOXED:
1243     case G_TYPE_PARAM:
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);
1249       break;
1250     case G_TYPE_FLOAT:
1251       rettype = &ffi_type_float;
1252       *value = (gpointer)&(gvalue->data[0].v_float);
1253       break;
1254     case G_TYPE_DOUBLE:
1255       rettype = &ffi_type_double;
1256       *value = (gpointer)&(gvalue->data[0].v_double);
1257       break;
1258     case G_TYPE_LONG:
1259       rettype = &ffi_type_slong;
1260       *value = (gpointer)&(gvalue->data[0].v_long);
1261       break;
1262     case G_TYPE_ULONG:
1263       rettype = &ffi_type_ulong;
1264       *value = (gpointer)&(gvalue->data[0].v_ulong);
1265       break;
1266     case G_TYPE_INT64:
1267       rettype = &ffi_type_sint64;
1268       *value = (gpointer)&(gvalue->data[0].v_int64);
1269       break;
1270     case G_TYPE_UINT64:
1271       rettype = &ffi_type_uint64;
1272       *value = (gpointer)&(gvalue->data[0].v_uint64);
1273       break;
1274     default:
1275       rettype = &ffi_type_pointer;
1276       *value = NULL;
1277       g_critical ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1278       break;
1279     }
1280   return rettype;
1281 }
1282
1283 static void
1284 value_from_ffi_type (GValue *gvalue, gpointer *value)
1285 {
1286   ffi_arg *int_val = (ffi_arg*) value;
1287   GType type;
1288
1289   type = G_VALUE_TYPE (gvalue);
1290
1291 restart:
1292   switch (g_type_fundamental (type))
1293     {
1294     case G_TYPE_INT:
1295       g_value_set_int (gvalue, (gint) *int_val);
1296       break;
1297     case G_TYPE_FLOAT:
1298       g_value_set_float (gvalue, *(gfloat*)value);
1299       break;
1300     case G_TYPE_DOUBLE:
1301       g_value_set_double (gvalue, *(gdouble*)value);
1302       break;
1303     case G_TYPE_BOOLEAN:
1304       g_value_set_boolean (gvalue, (gboolean) *int_val);
1305       break;
1306     case G_TYPE_STRING:
1307       g_value_take_string (gvalue, *(gchar**)value);
1308       break;
1309     case G_TYPE_CHAR:
1310       g_value_set_schar (gvalue, (gint8) *int_val);
1311       break;
1312     case G_TYPE_UCHAR:
1313       g_value_set_uchar (gvalue, (guchar) *int_val);
1314       break;
1315     case G_TYPE_UINT:
1316       g_value_set_uint (gvalue, (guint) *int_val);
1317       break;
1318     case G_TYPE_POINTER:
1319       g_value_set_pointer (gvalue, *(gpointer*)value);
1320       break;
1321     case G_TYPE_LONG:
1322       g_value_set_long (gvalue, (glong) *int_val);
1323       break;
1324     case G_TYPE_ULONG:
1325       g_value_set_ulong (gvalue, (gulong) *int_val);
1326       break;
1327     case G_TYPE_INT64:
1328       g_value_set_int64 (gvalue, (gint64) *int_val);
1329       break;
1330     case G_TYPE_UINT64:
1331       g_value_set_uint64 (gvalue, (guint64) *int_val);
1332       break;
1333     case G_TYPE_BOXED:
1334       g_value_take_boxed (gvalue, *(gpointer*)value);
1335       break;
1336     case G_TYPE_ENUM:
1337       g_value_set_enum (gvalue, (gint) *int_val);
1338       break;
1339     case G_TYPE_FLAGS:
1340       g_value_set_flags (gvalue, (guint) *int_val);
1341       break;
1342     case G_TYPE_PARAM:
1343       g_value_take_param (gvalue, *(gpointer*)value);
1344       break;
1345     case G_TYPE_OBJECT:
1346       g_value_take_object (gvalue, *(gpointer*)value);
1347       break;
1348     case G_TYPE_VARIANT:
1349       g_value_take_variant (gvalue, *(gpointer*)value);
1350       break;
1351     case G_TYPE_INTERFACE:
1352       type = g_type_interface_instantiatable_prerequisite (type);
1353       if (type)
1354         goto restart;
1355       G_GNUC_FALLTHROUGH;
1356     default:
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)));
1360     }
1361 }
1362
1363 typedef union {
1364   gpointer _gpointer;
1365   float _float;
1366   double _double;
1367   gint _gint;
1368   guint _guint;
1369   glong _glong;
1370   gulong _gulong;
1371   gint64 _gint64;
1372   guint64 _guint64;
1373 } va_arg_storage;
1374
1375 static ffi_type *
1376 va_to_ffi_type (GType gtype,
1377                 va_list *va,
1378                 va_arg_storage *storage)
1379 {
1380   ffi_type *rettype = NULL;
1381   GType type = g_type_fundamental (gtype);
1382   g_assert (type != G_TYPE_INVALID);
1383
1384   switch (type)
1385     {
1386     case G_TYPE_BOOLEAN:
1387     case G_TYPE_CHAR:
1388     case G_TYPE_INT:
1389     case G_TYPE_ENUM:
1390       rettype = &ffi_type_sint;
1391       storage->_gint = va_arg (*va, gint);
1392       break;
1393     case G_TYPE_UCHAR:
1394     case G_TYPE_UINT:
1395     case G_TYPE_FLAGS:
1396       rettype = &ffi_type_uint;
1397       storage->_guint = va_arg (*va, guint);
1398       break;
1399     case G_TYPE_STRING:
1400     case G_TYPE_OBJECT:
1401     case G_TYPE_BOXED:
1402     case G_TYPE_PARAM:
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);
1408       break;
1409     case G_TYPE_FLOAT:
1410       /* Float args are passed as doubles in varargs */
1411       rettype = &ffi_type_float;
1412       storage->_float = (float)va_arg (*va, double);
1413       break;
1414     case G_TYPE_DOUBLE:
1415       rettype = &ffi_type_double;
1416       storage->_double = va_arg (*va, double);
1417       break;
1418     case G_TYPE_LONG:
1419       rettype = &ffi_type_slong;
1420       storage->_glong = va_arg (*va, glong);
1421       break;
1422     case G_TYPE_ULONG:
1423       rettype = &ffi_type_ulong;
1424       storage->_gulong = va_arg (*va, gulong);
1425       break;
1426     case G_TYPE_INT64:
1427       rettype = &ffi_type_sint64;
1428       storage->_gint64 = va_arg (*va, gint64);
1429       break;
1430     case G_TYPE_UINT64:
1431       rettype = &ffi_type_uint64;
1432       storage->_guint64 = va_arg (*va, guint64);
1433       break;
1434     default:
1435       rettype = &ffi_type_pointer;
1436       storage->_guint64  = 0;
1437       g_critical ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1438       break;
1439     }
1440   return rettype;
1441 }
1442
1443 /**
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()
1456  *
1457  * A generic marshaller function implemented via
1458  * [libffi](http://sourceware.org/libffi/).
1459  *
1460  * Normally this function is not passed explicitly to g_signal_new(),
1461  * but used automatically by GLib when specifying a %NULL marshaller.
1462  *
1463  * Since: 2.30
1464  */
1465 void
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)
1472 {
1473   ffi_type *rtype;
1474   void *rvalue;
1475   int n_args;
1476   ffi_type **atypes;
1477   void **args;
1478   int i;
1479   ffi_cif cif;
1480   GCClosure *cc = (GCClosure*) closure;
1481   gint *enum_tmpval;
1482   gboolean tmpval_used = FALSE;
1483
1484   enum_tmpval = g_alloca (sizeof (gint));
1485   if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1486     {
1487       rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used);
1488     }
1489   else
1490     {
1491       rtype = &ffi_type_void;
1492     }
1493
1494   rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1495
1496   n_args = n_param_values + 1;
1497   atypes = g_alloca (sizeof (ffi_type *) * n_args);
1498   args =  g_alloca (sizeof (gpointer) * n_args);
1499
1500   if (tmpval_used)
1501     enum_tmpval = g_alloca (sizeof (gint));
1502
1503   if (G_CCLOSURE_SWAP_DATA (closure))
1504     {
1505       atypes[n_args-1] = value_to_ffi_type (param_values + 0,
1506                                             &args[n_args-1],
1507                                             enum_tmpval,
1508                                             &tmpval_used);
1509       atypes[0] = &ffi_type_pointer;
1510       args[0] = &closure->data;
1511     }
1512   else
1513     {
1514       atypes[0] = value_to_ffi_type (param_values + 0,
1515                                      &args[0],
1516                                      enum_tmpval,
1517                                      &tmpval_used);
1518       atypes[n_args-1] = &ffi_type_pointer;
1519       args[n_args-1] = &closure->data;
1520     }
1521
1522   for (i = 1; i < n_args - 1; i++)
1523     {
1524       if (tmpval_used)
1525         enum_tmpval = g_alloca (sizeof (gint));
1526
1527       atypes[i] = value_to_ffi_type (param_values + i,
1528                                      &args[i],
1529                                      enum_tmpval,
1530                                      &tmpval_used);
1531     }
1532
1533   if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1534     return;
1535
1536   ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1537
1538   if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1539     value_from_ffi_type (return_gvalue, rvalue);
1540 }
1541
1542 /**
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
1547  *  value.
1548  * @instance: (type GObject.TypeInstance): the instance on which the closure is
1549  *  invoked.
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
1556  *  @args_list.
1557  *
1558  * A generic #GVaClosureMarshal function implemented via
1559  * [libffi](http://sourceware.org/libffi/).
1560  *
1561  * Since: 2.30
1562  */
1563 void
1564 g_cclosure_marshal_generic_va (GClosure *closure,
1565                                GValue   *return_value,
1566                                gpointer  instance,
1567                                va_list   args_list,
1568                                gpointer  marshal_data,
1569                                int       n_params,
1570                                GType    *param_types)
1571 {
1572   ffi_type *rtype;
1573   void *rvalue;
1574   int n_args;
1575   ffi_type **atypes;
1576   void **args;
1577   va_arg_storage *storage;
1578   int i;
1579   ffi_cif cif;
1580   GCClosure *cc = (GCClosure*) closure;
1581   gint *enum_tmpval;
1582   gboolean tmpval_used = FALSE;
1583   va_list args_copy;
1584
1585   enum_tmpval = g_alloca (sizeof (gint));
1586   if (return_value && G_VALUE_TYPE (return_value))
1587     {
1588       rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used);
1589     }
1590   else
1591     {
1592       rtype = &ffi_type_void;
1593     }
1594
1595   rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1596
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);
1601
1602   if (G_CCLOSURE_SWAP_DATA (closure))
1603     {
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;
1608     }
1609   else
1610     {
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;
1615     }
1616
1617   va_copy (args_copy, args_list);
1618
1619   /* Box non-primitive arguments */
1620   for (i = 0; i < n_params; i++)
1621     {
1622       GType type = param_types[i]  & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1623       GType fundamental = G_TYPE_FUNDAMENTAL (type);
1624
1625       atypes[i+1] = va_to_ffi_type (type,
1626                                     &args_copy,
1627                                     &storage[i]);
1628       args[i+1] = &storage[i];
1629
1630       if ((param_types[i]  & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1631         {
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);
1640         }
1641       if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1642         storage[i]._gpointer = g_object_ref (storage[i]._gpointer);
1643     }
1644
1645   va_end (args_copy);
1646   
1647   if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1648     return;
1649
1650   ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1651
1652   /* Unbox non-primitive arguments */
1653   for (i = 0; i < n_params; i++)
1654     {
1655       GType type = param_types[i]  & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1656       GType fundamental = G_TYPE_FUNDAMENTAL (type);
1657
1658       if ((param_types[i]  & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1659         {
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);
1668         }
1669       if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1670         g_object_unref (storage[i]._gpointer);
1671     }
1672   
1673   if (return_value && G_VALUE_TYPE (return_value))
1674     value_from_ffi_type (return_value, rvalue);
1675 }
1676
1677 /**
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
1686  *
1687  * A marshaller for a #GCClosure with a callback of type
1688  * `void (*callback) (gpointer instance, gpointer user_data)`.
1689  */
1690
1691 /**
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
1700  *
1701  * A marshaller for a #GCClosure with a callback of type
1702  * `void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)`.
1703  */
1704
1705 /**
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
1714  *
1715  * A marshaller for a #GCClosure with a callback of type
1716  * `void (*callback) (gpointer instance, gchar arg1, gpointer user_data)`.
1717  */
1718
1719 /**
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
1728  *
1729  * A marshaller for a #GCClosure with a callback of type
1730  * `void (*callback) (gpointer instance, guchar arg1, gpointer user_data)`.
1731  */
1732
1733 /**
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
1742  *
1743  * A marshaller for a #GCClosure with a callback of type
1744  * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)`.
1745  */
1746
1747 /**
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
1756  *
1757  * A marshaller for a #GCClosure with a callback of type
1758  * `void (*callback) (gpointer instance, guint arg1, gpointer user_data)`.
1759  */
1760
1761 /**
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
1770  *
1771  * A marshaller for a #GCClosure with a callback of type
1772  * `void (*callback) (gpointer instance, glong arg1, gpointer user_data)`.
1773  */
1774
1775 /**
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
1784  *
1785  * A marshaller for a #GCClosure with a callback of type
1786  * `void (*callback) (gpointer instance, gulong arg1, gpointer user_data)`.
1787  */
1788
1789 /**
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
1798  *
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..
1801  */
1802
1803 /**
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
1812  *
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.
1815  */
1816
1817 /**
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
1826  *
1827  * A marshaller for a #GCClosure with a callback of type
1828  * `void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)`.
1829  */
1830
1831 /**
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
1840  *
1841  * A marshaller for a #GCClosure with a callback of type
1842  * `void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)`.
1843  */
1844
1845 /**
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
1854  *
1855  * A marshaller for a #GCClosure with a callback of type
1856  * `void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)`.
1857  */
1858
1859 /**
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
1868  *
1869  * A marshaller for a #GCClosure with a callback of type
1870  * `void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)`.
1871  */
1872
1873 /**
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
1882  *
1883  * A marshaller for a #GCClosure with a callback of type
1884  * `void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)`.
1885  */
1886
1887 /**
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
1896  *
1897  * A marshaller for a #GCClosure with a callback of type
1898  * `void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)`.
1899  */
1900
1901 /**
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
1910  *
1911  * A marshaller for a #GCClosure with a callback of type
1912  * `void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)`.
1913  */
1914
1915 /**
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
1924  *
1925  * A marshaller for a #GCClosure with a callback of type
1926  * `void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)`.
1927  *
1928  * Since: 2.26
1929  */
1930
1931 /**
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
1940  *
1941  * A marshaller for a #GCClosure with a callback of type
1942  * `void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)`.
1943  */
1944
1945 /**
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
1954  *
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.
1958  */
1959
1960 /**
1961  * g_cclosure_marshal_BOOL__FLAGS:
1962  *
1963  * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1964  */
1965 /**
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
1974  *
1975  * A marshaller for a #GCClosure with a callback of type
1976  * `gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)`.
1977  */
1978 /**
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
1987  *
1988  * A marshaller for a #GCClosure with a callback of type
1989  * `gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)`.
1990  *
1991  * Since: 2.26
1992  */