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