Merge branch 'fix-gdbus-unix-address' into 'master'
[glib.git] / gobject / gclosure.c
blob188c74ad8c4486f20214dc459a765ab9e8d92f70
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 * Copyright (C) 2005 Imendio AB
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.1 of the License, or (at your option) any later version.
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.
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/>.
20 * MT safe with regards to reference counting.
23 #include "config.h"
25 #include "../glib/gvalgrind.h"
26 #include <string.h>
28 #include <ffi.h>
30 #include "gclosure.h"
31 #include "gboxed.h"
32 #include "gobject.h"
33 #include "genums.h"
34 #include "gvalue.h"
35 #include "gvaluetypes.h"
36 #include "gtype-private.h"
39 /**
40 * SECTION:gclosure
41 * @short_description: Functions as first-class objects
42 * @title: Closures
44 * A #GClosure represents a callback supplied by the programmer. It
45 * will generally comprise a function of some kind and a marshaller
46 * used to call it. It is the responsibility of the marshaller to
47 * convert the arguments for the invocation from #GValues into
48 * a suitable form, perform the callback on the converted arguments,
49 * and transform the return value back into a #GValue.
51 * In the case of C programs, a closure usually just holds a pointer
52 * to a function and maybe a data argument, and the marshaller
53 * converts between #GValue and native C types. The GObject
54 * library provides the #GCClosure type for this purpose. Bindings for
55 * other languages need marshallers which convert between #GValues
56 * and suitable representations in the runtime of the language in
57 * order to use functions written in that language as callbacks. Use
58 * g_closure_set_marshal() to set the marshaller on such a custom
59 * closure implementation.
61 * Within GObject, closures play an important role in the
62 * implementation of signals. When a signal is registered, the
63 * @c_marshaller argument to g_signal_new() specifies the default C
64 * marshaller for any closure which is connected to this
65 * signal. GObject provides a number of C marshallers for this
66 * purpose, see the g_cclosure_marshal_*() functions. Additional C
67 * marshallers can be generated with the [glib-genmarshal][glib-genmarshal]
68 * utility. Closures can be explicitly connected to signals with
69 * g_signal_connect_closure(), but it usually more convenient to let
70 * GObject create a closure automatically by using one of the
71 * g_signal_connect_*() functions which take a callback function/user
72 * data pair.
74 * Using closures has a number of important advantages over a simple
75 * callback function/data pointer combination:
77 * - Closures allow the callee to get the types of the callback parameters,
78 * which means that language bindings don't have to write individual glue
79 * for each callback type.
81 * - The reference counting of #GClosure makes it easy to handle reentrancy
82 * right; if a callback is removed while it is being invoked, the closure
83 * and its parameters won't be freed until the invocation finishes.
85 * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
86 * automatically removed when the objects they point to go away.
89 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
90 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
91 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
92 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
93 #define CLOSURE_N_MFUNCS(cl) (((cl)->n_guards << 1L))
94 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
95 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
96 (cl)->n_fnotifiers + \
97 (cl)->n_inotifiers)
99 typedef union {
100 GClosure closure;
101 volatile gint vint;
102 } ClosureInt;
104 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
105 G_STMT_START { \
106 ClosureInt *cunion = (ClosureInt*) _closure; \
107 gint new_int, old_int, success; \
108 do \
110 ClosureInt tmp; \
111 tmp.vint = old_int = cunion->vint; \
112 _SET_OLD tmp.closure._field; \
113 tmp.closure._field _OP _value; \
114 _SET_NEW tmp.closure._field; \
115 new_int = tmp.vint; \
116 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
118 while (!success && _must_set); \
119 } G_STMT_END
121 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
122 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
123 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
124 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
125 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
126 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
128 #if 0 /* for non-thread-safe closures */
129 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
130 #define SET(cl,f,v) (void) (cl->f = v)
131 #define INC(cl,f) (void) (cl->f += 1)
132 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
133 #define DEC(cl,f) (void) (cl->f -= 1)
134 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
135 #endif
137 enum {
138 FNOTIFY,
139 INOTIFY,
140 PRE_NOTIFY,
141 POST_NOTIFY
145 /* --- functions --- */
147 * g_closure_new_simple:
148 * @sizeof_closure: the size of the structure to allocate, must be at least
149 * `sizeof (GClosure)`
150 * @data: data to store in the @data field of the newly allocated #GClosure
152 * Allocates a struct of the given size and initializes the initial
153 * part as a #GClosure. This function is mainly useful when
154 * implementing new types of closures.
156 * |[<!-- language="C" -->
157 * typedef struct _MyClosure MyClosure;
158 * struct _MyClosure
160 * GClosure closure;
161 * // extra data goes here
162 * };
164 * static void
165 * my_closure_finalize (gpointer notify_data,
166 * GClosure *closure)
168 * MyClosure *my_closure = (MyClosure *)closure;
170 * // free extra data here
173 * MyClosure *my_closure_new (gpointer data)
175 * GClosure *closure;
176 * MyClosure *my_closure;
178 * closure = g_closure_new_simple (sizeof (MyClosure), data);
179 * my_closure = (MyClosure *) closure;
181 * // initialize extra data here
183 * g_closure_add_finalize_notifier (closure, notify_data,
184 * my_closure_finalize);
185 * return my_closure;
187 * ]|
189 * Returns: (transfer none): a floating reference to a new #GClosure
191 GClosure*
192 g_closure_new_simple (guint sizeof_closure,
193 gpointer data)
195 GClosure *closure;
196 gint private_size;
197 gchar *allocated;
199 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
201 private_size = sizeof (GRealClosure) - sizeof (GClosure);
203 #ifdef ENABLE_VALGRIND
204 /* See comments in gtype.c about what's going on here... */
205 if (RUNNING_ON_VALGRIND)
207 private_size += sizeof (gpointer);
209 allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer));
211 *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer);
213 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, sizeof_closure + sizeof (gpointer), 0, TRUE);
214 VALGRIND_MALLOCLIKE_BLOCK (allocated + sizeof (gpointer), private_size - sizeof (gpointer), 0, TRUE);
216 else
217 #endif
218 allocated = g_malloc0 (private_size + sizeof_closure);
220 closure = (GClosure *) (allocated + private_size);
222 SET (closure, ref_count, 1);
223 SET (closure, floating, TRUE);
224 closure->data = data;
226 return closure;
229 static inline void
230 closure_invoke_notifiers (GClosure *closure,
231 guint notify_type)
233 /* notifier layout:
234 * n_guards n_guards n_fnotif. n_inotifiers
235 * ->[[pre_guards][post_guards][fnotifiers][inotifiers]]
237 * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards;
238 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
240 * constrains/catches:
241 * - closure->notifiers may be reloacted during callback
242 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
243 * - i.e. callbacks can be removed/added during invocation
244 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
245 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
246 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
247 * + none of the callbacks can cause recursion
248 * + closure->n_inotifiers is const 0 during FNOTIFY
250 switch (notify_type)
252 GClosureNotifyData *ndata;
253 guint i, offs;
254 case FNOTIFY:
255 while (closure->n_fnotifiers)
257 guint n;
258 DEC_ASSIGN (closure, n_fnotifiers, &n);
260 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
261 closure->marshal = (GClosureMarshal) ndata->notify;
262 closure->data = ndata->data;
263 ndata->notify (ndata->data, closure);
265 closure->marshal = NULL;
266 closure->data = NULL;
267 break;
268 case INOTIFY:
269 SET (closure, in_inotify, TRUE);
270 while (closure->n_inotifiers)
272 guint n;
273 DEC_ASSIGN (closure, n_inotifiers, &n);
275 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
276 closure->marshal = (GClosureMarshal) ndata->notify;
277 closure->data = ndata->data;
278 ndata->notify (ndata->data, closure);
280 closure->marshal = NULL;
281 closure->data = NULL;
282 SET (closure, in_inotify, FALSE);
283 break;
284 case PRE_NOTIFY:
285 i = closure->n_guards;
286 offs = 0;
287 while (i--)
289 ndata = closure->notifiers + offs + i;
290 ndata->notify (ndata->data, closure);
292 break;
293 case POST_NOTIFY:
294 i = closure->n_guards;
295 offs = i;
296 while (i--)
298 ndata = closure->notifiers + offs + i;
299 ndata->notify (ndata->data, closure);
301 break;
305 static void
306 g_closure_set_meta_va_marshal (GClosure *closure,
307 GVaClosureMarshal va_meta_marshal)
309 GRealClosure *real_closure;
311 g_return_if_fail (closure != NULL);
312 g_return_if_fail (va_meta_marshal != NULL);
313 g_return_if_fail (closure->is_invalid == FALSE);
314 g_return_if_fail (closure->in_marshal == FALSE);
316 real_closure = G_REAL_CLOSURE (closure);
318 g_return_if_fail (real_closure->meta_marshal != NULL);
320 real_closure->va_meta_marshal = va_meta_marshal;
324 * g_closure_set_meta_marshal: (skip)
325 * @closure: a #GClosure
326 * @marshal_data: (closure meta_marshal): context-dependent data to pass
327 * to @meta_marshal
328 * @meta_marshal: a #GClosureMarshal function
330 * Sets the meta marshaller of @closure. A meta marshaller wraps
331 * @closure->marshal and modifies the way it is called in some
332 * fashion. The most common use of this facility is for C callbacks.
333 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
334 * are used everywhere, but the way that we get the callback function
335 * differs. In most cases we want to use @closure->callback, but in
336 * other cases we want to use some different technique to retrieve the
337 * callback function.
339 * For example, class closures for signals (see
340 * g_signal_type_cclosure_new()) retrieve the callback function from a
341 * fixed offset in the class structure. The meta marshaller retrieves
342 * the right callback and passes it to the marshaller as the
343 * @marshal_data argument.
345 void
346 g_closure_set_meta_marshal (GClosure *closure,
347 gpointer marshal_data,
348 GClosureMarshal meta_marshal)
350 GRealClosure *real_closure;
352 g_return_if_fail (closure != NULL);
353 g_return_if_fail (meta_marshal != NULL);
354 g_return_if_fail (closure->is_invalid == FALSE);
355 g_return_if_fail (closure->in_marshal == FALSE);
357 real_closure = G_REAL_CLOSURE (closure);
359 g_return_if_fail (real_closure->meta_marshal == NULL);
361 real_closure->meta_marshal = meta_marshal;
362 real_closure->meta_marshal_data = marshal_data;
366 * g_closure_add_marshal_guards: (skip)
367 * @closure: a #GClosure
368 * @pre_marshal_data: (closure pre_marshal_notify): data to pass
369 * to @pre_marshal_notify
370 * @pre_marshal_notify: a function to call before the closure callback
371 * @post_marshal_data: (closure post_marshal_notify): data to pass
372 * to @post_marshal_notify
373 * @post_marshal_notify: a function to call after the closure callback
375 * Adds a pair of notifiers which get invoked before and after the
376 * closure callback, respectively. This is typically used to protect
377 * the extra arguments for the duration of the callback. See
378 * g_object_watch_closure() for an example of marshal guards.
380 void
381 g_closure_add_marshal_guards (GClosure *closure,
382 gpointer pre_marshal_data,
383 GClosureNotify pre_marshal_notify,
384 gpointer post_marshal_data,
385 GClosureNotify post_marshal_notify)
387 guint i;
389 g_return_if_fail (closure != NULL);
390 g_return_if_fail (pre_marshal_notify != NULL);
391 g_return_if_fail (post_marshal_notify != NULL);
392 g_return_if_fail (closure->is_invalid == FALSE);
393 g_return_if_fail (closure->in_marshal == FALSE);
394 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
396 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
397 if (closure->n_inotifiers)
398 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
399 closure->n_fnotifiers +
400 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
401 closure->n_fnotifiers + 0)];
402 if (closure->n_inotifiers > 1)
403 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
404 closure->n_fnotifiers +
405 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
406 closure->n_fnotifiers + 1)];
407 if (closure->n_fnotifiers)
408 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
409 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
410 if (closure->n_fnotifiers > 1)
411 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
412 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
413 if (closure->n_guards)
414 closure->notifiers[(closure->n_guards +
415 closure->n_guards + 1)] = closure->notifiers[closure->n_guards];
416 i = closure->n_guards;
417 closure->notifiers[i].data = pre_marshal_data;
418 closure->notifiers[i].notify = pre_marshal_notify;
419 closure->notifiers[i + 1].data = post_marshal_data;
420 closure->notifiers[i + 1].notify = post_marshal_notify;
421 INC (closure, n_guards);
425 * g_closure_add_finalize_notifier: (skip)
426 * @closure: a #GClosure
427 * @notify_data: (closure notify_func): data to pass to @notify_func
428 * @notify_func: the callback function to register
430 * Registers a finalization notifier which will be called when the
431 * reference count of @closure goes down to 0. Multiple finalization
432 * notifiers on a single closure are invoked in unspecified order. If
433 * a single call to g_closure_unref() results in the closure being
434 * both invalidated and finalized, then the invalidate notifiers will
435 * be run before the finalize notifiers.
437 void
438 g_closure_add_finalize_notifier (GClosure *closure,
439 gpointer notify_data,
440 GClosureNotify notify_func)
442 guint i;
444 g_return_if_fail (closure != NULL);
445 g_return_if_fail (notify_func != NULL);
446 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
448 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
449 if (closure->n_inotifiers)
450 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
451 closure->n_fnotifiers +
452 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
453 closure->n_fnotifiers + 0)];
454 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
455 closure->notifiers[i].data = notify_data;
456 closure->notifiers[i].notify = notify_func;
457 INC (closure, n_fnotifiers);
461 * g_closure_add_invalidate_notifier: (skip)
462 * @closure: a #GClosure
463 * @notify_data: (closure notify_func): data to pass to @notify_func
464 * @notify_func: the callback function to register
466 * Registers an invalidation notifier which will be called when the
467 * @closure is invalidated with g_closure_invalidate(). Invalidation
468 * notifiers are invoked before finalization notifiers, in an
469 * unspecified order.
471 void
472 g_closure_add_invalidate_notifier (GClosure *closure,
473 gpointer notify_data,
474 GClosureNotify notify_func)
476 guint i;
478 g_return_if_fail (closure != NULL);
479 g_return_if_fail (notify_func != NULL);
480 g_return_if_fail (closure->is_invalid == FALSE);
481 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
483 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
484 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
485 closure->notifiers[i].data = notify_data;
486 closure->notifiers[i].notify = notify_func;
487 INC (closure, n_inotifiers);
490 static inline gboolean
491 closure_try_remove_inotify (GClosure *closure,
492 gpointer notify_data,
493 GClosureNotify notify_func)
495 GClosureNotifyData *ndata, *nlast;
497 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
498 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
499 if (ndata->notify == notify_func && ndata->data == notify_data)
501 DEC (closure, n_inotifiers);
502 if (ndata < nlast)
503 *ndata = *nlast;
505 return TRUE;
507 return FALSE;
510 static inline gboolean
511 closure_try_remove_fnotify (GClosure *closure,
512 gpointer notify_data,
513 GClosureNotify notify_func)
515 GClosureNotifyData *ndata, *nlast;
517 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
518 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
519 if (ndata->notify == notify_func && ndata->data == notify_data)
521 DEC (closure, n_fnotifiers);
522 if (ndata < nlast)
523 *ndata = *nlast;
524 if (closure->n_inotifiers)
525 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
526 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
527 closure->n_fnotifiers +
528 closure->n_inotifiers)];
529 return TRUE;
531 return FALSE;
535 * g_closure_ref:
536 * @closure: #GClosure to increment the reference count on
538 * Increments the reference count on a closure to force it staying
539 * alive while the caller holds a pointer to it.
541 * Returns: (transfer none): The @closure passed in, for convenience
543 GClosure*
544 g_closure_ref (GClosure *closure)
546 guint new_ref_count;
547 g_return_val_if_fail (closure != NULL, NULL);
548 g_return_val_if_fail (closure->ref_count > 0, NULL);
549 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
551 INC_ASSIGN (closure, ref_count, &new_ref_count);
552 g_return_val_if_fail (new_ref_count > 1, NULL);
554 return closure;
558 * g_closure_invalidate:
559 * @closure: GClosure to invalidate
561 * Sets a flag on the closure to indicate that its calling
562 * environment has become invalid, and thus causes any future
563 * invocations of g_closure_invoke() on this @closure to be
564 * ignored. Also, invalidation notifiers installed on the closure will
565 * be called at this point. Note that unless you are holding a
566 * reference to the closure yourself, the invalidation notifiers may
567 * unref the closure and cause it to be destroyed, so if you need to
568 * access the closure after calling g_closure_invalidate(), make sure
569 * that you've previously called g_closure_ref().
571 * Note that g_closure_invalidate() will also be called when the
572 * reference count of a closure drops to zero (unless it has already
573 * been invalidated before).
575 void
576 g_closure_invalidate (GClosure *closure)
578 g_return_if_fail (closure != NULL);
580 if (!closure->is_invalid)
582 gboolean was_invalid;
583 g_closure_ref (closure); /* preserve floating flag */
584 SWAP (closure, is_invalid, TRUE, &was_invalid);
585 /* invalidate only once */
586 if (!was_invalid)
587 closure_invoke_notifiers (closure, INOTIFY);
588 g_closure_unref (closure);
593 * g_closure_unref:
594 * @closure: #GClosure to decrement the reference count on
596 * Decrements the reference count of a closure after it was previously
597 * incremented by the same caller. If no other callers are using the
598 * closure, then the closure will be destroyed and freed.
600 void
601 g_closure_unref (GClosure *closure)
603 guint new_ref_count;
605 g_return_if_fail (closure != NULL);
606 g_return_if_fail (closure->ref_count > 0);
608 if (closure->ref_count == 1) /* last unref, invalidate first */
609 g_closure_invalidate (closure);
611 DEC_ASSIGN (closure, ref_count, &new_ref_count);
613 if (new_ref_count == 0)
615 closure_invoke_notifiers (closure, FNOTIFY);
616 g_free (closure->notifiers);
618 #ifdef ENABLE_VALGRIND
619 /* See comments in gtype.c about what's going on here... */
620 if (RUNNING_ON_VALGRIND)
622 gchar *allocated;
624 allocated = (gchar *) G_REAL_CLOSURE (closure);
625 allocated -= sizeof (gpointer);
627 g_free (allocated);
629 VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0);
630 VALGRIND_FREELIKE_BLOCK (closure, 0);
632 else
633 #endif
634 g_free (G_REAL_CLOSURE (closure));
639 * g_closure_sink:
640 * @closure: #GClosure to decrement the initial reference count on, if it's
641 * still being held
643 * Takes over the initial ownership of a closure. Each closure is
644 * initially created in a "floating" state, which means that the initial
645 * reference count is not owned by any caller. g_closure_sink() checks
646 * to see if the object is still floating, and if so, unsets the
647 * floating state and decreases the reference count. If the closure
648 * is not floating, g_closure_sink() does nothing. The reason for the
649 * existence of the floating state is to prevent cumbersome code
650 * sequences like:
651 * |[<!-- language="C" -->
652 * closure = g_cclosure_new (cb_func, cb_data);
653 * g_source_set_closure (source, closure);
654 * g_closure_unref (closure); // GObject doesn't really need this
655 * ]|
656 * Because g_source_set_closure() (and similar functions) take ownership of the
657 * initial reference count, if it is unowned, we instead can write:
658 * |[<!-- language="C" -->
659 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
660 * ]|
662 * Generally, this function is used together with g_closure_ref(). Ane example
663 * of storing a closure for later notification looks like:
664 * |[<!-- language="C" -->
665 * static GClosure *notify_closure = NULL;
666 * void
667 * foo_notify_set_closure (GClosure *closure)
669 * if (notify_closure)
670 * g_closure_unref (notify_closure);
671 * notify_closure = closure;
672 * if (notify_closure)
674 * g_closure_ref (notify_closure);
675 * g_closure_sink (notify_closure);
678 * ]|
680 * Because g_closure_sink() may decrement the reference count of a closure
681 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
682 * g_closure_ref() should be called prior to this function.
684 void
685 g_closure_sink (GClosure *closure)
687 g_return_if_fail (closure != NULL);
688 g_return_if_fail (closure->ref_count > 0);
690 /* floating is basically a kludge to avoid creating closures
691 * with a ref_count of 0. so the initial ref_count a closure has
692 * is unowned. with invoking g_closure_sink() code may
693 * indicate that it takes over that intiial ref_count.
695 if (closure->floating)
697 gboolean was_floating;
698 SWAP (closure, floating, FALSE, &was_floating);
699 /* unref floating flag only once */
700 if (was_floating)
701 g_closure_unref (closure);
706 * g_closure_remove_invalidate_notifier: (skip)
707 * @closure: a #GClosure
708 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
709 * when registering @notify_func
710 * @notify_func: the callback function to remove
712 * Removes an invalidation notifier.
714 * Notice that notifiers are automatically removed after they are run.
716 void
717 g_closure_remove_invalidate_notifier (GClosure *closure,
718 gpointer notify_data,
719 GClosureNotify notify_func)
721 g_return_if_fail (closure != NULL);
722 g_return_if_fail (notify_func != NULL);
724 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */
725 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
726 closure->data == notify_data)
727 closure->marshal = NULL;
728 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
729 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
730 notify_func, notify_data);
734 * g_closure_remove_finalize_notifier: (skip)
735 * @closure: a #GClosure
736 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
737 * when registering @notify_func
738 * @notify_func: the callback function to remove
740 * Removes a finalization notifier.
742 * Notice that notifiers are automatically removed after they are run.
744 void
745 g_closure_remove_finalize_notifier (GClosure *closure,
746 gpointer notify_data,
747 GClosureNotify notify_func)
749 g_return_if_fail (closure != NULL);
750 g_return_if_fail (notify_func != NULL);
752 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */
753 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
754 closure->data == notify_data)
755 closure->marshal = NULL;
756 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
757 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
758 notify_func, notify_data);
762 * g_closure_invoke:
763 * @closure: a #GClosure
764 * @return_value: (optional) (out): a #GValue to store the return
765 * value. May be %NULL if the callback of @closure
766 * doesn't return a value.
767 * @n_param_values: the length of the @param_values array
768 * @param_values: (array length=n_param_values): an array of
769 * #GValues holding the arguments on which to
770 * invoke the callback of @closure
771 * @invocation_hint: (nullable): a context-dependent invocation hint
773 * Invokes the closure, i.e. executes the callback represented by the @closure.
775 void
776 g_closure_invoke (GClosure *closure,
777 GValue /*out*/ *return_value,
778 guint n_param_values,
779 const GValue *param_values,
780 gpointer invocation_hint)
782 GRealClosure *real_closure;
784 g_return_if_fail (closure != NULL);
786 real_closure = G_REAL_CLOSURE (closure);
788 g_closure_ref (closure); /* preserve floating flag */
789 if (!closure->is_invalid)
791 GClosureMarshal marshal;
792 gpointer marshal_data;
793 gboolean in_marshal = closure->in_marshal;
795 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
797 SET (closure, in_marshal, TRUE);
798 if (real_closure->meta_marshal)
800 marshal_data = real_closure->meta_marshal_data;
801 marshal = real_closure->meta_marshal;
803 else
805 marshal_data = NULL;
806 marshal = closure->marshal;
808 if (!in_marshal)
809 closure_invoke_notifiers (closure, PRE_NOTIFY);
810 marshal (closure,
811 return_value,
812 n_param_values, param_values,
813 invocation_hint,
814 marshal_data);
815 if (!in_marshal)
816 closure_invoke_notifiers (closure, POST_NOTIFY);
817 SET (closure, in_marshal, in_marshal);
819 g_closure_unref (closure);
822 gboolean
823 _g_closure_supports_invoke_va (GClosure *closure)
825 GRealClosure *real_closure;
827 g_return_val_if_fail (closure != NULL, FALSE);
829 real_closure = G_REAL_CLOSURE (closure);
831 return
832 real_closure->va_marshal != NULL &&
833 (real_closure->meta_marshal == NULL ||
834 real_closure->va_meta_marshal != NULL);
837 void
838 _g_closure_invoke_va (GClosure *closure,
839 GValue /*out*/ *return_value,
840 gpointer instance,
841 va_list args,
842 int n_params,
843 GType *param_types)
845 GRealClosure *real_closure;
847 g_return_if_fail (closure != NULL);
849 real_closure = G_REAL_CLOSURE (closure);
851 g_closure_ref (closure); /* preserve floating flag */
852 if (!closure->is_invalid)
854 GVaClosureMarshal marshal;
855 gpointer marshal_data;
856 gboolean in_marshal = closure->in_marshal;
858 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
860 SET (closure, in_marshal, TRUE);
861 if (real_closure->va_meta_marshal)
863 marshal_data = real_closure->meta_marshal_data;
864 marshal = real_closure->va_meta_marshal;
866 else
868 marshal_data = NULL;
869 marshal = real_closure->va_marshal;
871 if (!in_marshal)
872 closure_invoke_notifiers (closure, PRE_NOTIFY);
873 marshal (closure,
874 return_value,
875 instance, args,
876 marshal_data,
877 n_params, param_types);
878 if (!in_marshal)
879 closure_invoke_notifiers (closure, POST_NOTIFY);
880 SET (closure, in_marshal, in_marshal);
882 g_closure_unref (closure);
887 * g_closure_set_marshal: (skip)
888 * @closure: a #GClosure
889 * @marshal: a #GClosureMarshal function
891 * Sets the marshaller of @closure. The `marshal_data`
892 * of @marshal provides a way for a meta marshaller to provide additional
893 * information to the marshaller. (See g_closure_set_meta_marshal().) For
894 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
895 * functions), what it provides is a callback function to use instead of
896 * @closure->callback.
898 void
899 g_closure_set_marshal (GClosure *closure,
900 GClosureMarshal marshal)
902 g_return_if_fail (closure != NULL);
903 g_return_if_fail (marshal != NULL);
905 if (closure->marshal && closure->marshal != marshal)
906 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
907 closure->marshal, marshal);
908 else
909 closure->marshal = marshal;
912 void
913 _g_closure_set_va_marshal (GClosure *closure,
914 GVaClosureMarshal marshal)
916 GRealClosure *real_closure;
918 g_return_if_fail (closure != NULL);
919 g_return_if_fail (marshal != NULL);
921 real_closure = G_REAL_CLOSURE (closure);
923 if (real_closure->va_marshal && real_closure->va_marshal != marshal)
924 g_warning ("attempt to override closure->va_marshal (%p) with new marshal (%p)",
925 real_closure->va_marshal, marshal);
926 else
927 real_closure->va_marshal = marshal;
931 * g_cclosure_new: (skip)
932 * @callback_func: the function to invoke
933 * @user_data: (closure callback_func): user data to pass to @callback_func
934 * @destroy_data: destroy notify to be called when @user_data is no longer used
936 * Creates a new closure which invokes @callback_func with @user_data as
937 * the last parameter.
939 * Returns: (transfer none): a floating reference to a new #GCClosure
941 GClosure*
942 g_cclosure_new (GCallback callback_func,
943 gpointer user_data,
944 GClosureNotify destroy_data)
946 GClosure *closure;
948 g_return_val_if_fail (callback_func != NULL, NULL);
950 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
951 if (destroy_data)
952 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
953 ((GCClosure*) closure)->callback = (gpointer) callback_func;
955 return closure;
959 * g_cclosure_new_swap: (skip)
960 * @callback_func: the function to invoke
961 * @user_data: (closure callback_func): user data to pass to @callback_func
962 * @destroy_data: destroy notify to be called when @user_data is no longer used
964 * Creates a new closure which invokes @callback_func with @user_data as
965 * the first parameter.
967 * Returns: (transfer none): a floating reference to a new #GCClosure
969 GClosure*
970 g_cclosure_new_swap (GCallback callback_func,
971 gpointer user_data,
972 GClosureNotify destroy_data)
974 GClosure *closure;
976 g_return_val_if_fail (callback_func != NULL, NULL);
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 SET (closure, derivative_flag, TRUE);
984 return closure;
987 static void
988 g_type_class_meta_marshal (GClosure *closure,
989 GValue /*out*/ *return_value,
990 guint n_param_values,
991 const GValue *param_values,
992 gpointer invocation_hint,
993 gpointer marshal_data)
995 GTypeClass *class;
996 gpointer callback;
997 /* GType itype = (GType) closure->data; */
998 guint offset = GPOINTER_TO_UINT (marshal_data);
1000 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1001 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1002 if (callback)
1003 closure->marshal (closure,
1004 return_value,
1005 n_param_values, param_values,
1006 invocation_hint,
1007 callback);
1010 static void
1011 g_type_class_meta_marshalv (GClosure *closure,
1012 GValue *return_value,
1013 gpointer instance,
1014 va_list args,
1015 gpointer marshal_data,
1016 int n_params,
1017 GType *param_types)
1019 GRealClosure *real_closure;
1020 GTypeClass *class;
1021 gpointer callback;
1022 /* GType itype = (GType) closure->data; */
1023 guint offset = GPOINTER_TO_UINT (marshal_data);
1025 real_closure = G_REAL_CLOSURE (closure);
1027 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1028 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1029 if (callback)
1030 real_closure->va_marshal (closure,
1031 return_value,
1032 instance, args,
1033 callback,
1034 n_params,
1035 param_types);
1038 static void
1039 g_type_iface_meta_marshal (GClosure *closure,
1040 GValue /*out*/ *return_value,
1041 guint n_param_values,
1042 const GValue *param_values,
1043 gpointer invocation_hint,
1044 gpointer marshal_data)
1046 GTypeClass *class;
1047 gpointer callback;
1048 GType itype = (GType) closure->data;
1049 guint offset = GPOINTER_TO_UINT (marshal_data);
1051 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1052 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1053 if (callback)
1054 closure->marshal (closure,
1055 return_value,
1056 n_param_values, param_values,
1057 invocation_hint,
1058 callback);
1061 gboolean
1062 _g_closure_is_void (GClosure *closure,
1063 gpointer instance)
1065 GRealClosure *real_closure;
1066 GTypeClass *class;
1067 gpointer callback;
1068 GType itype;
1069 guint offset;
1071 if (closure->is_invalid)
1072 return TRUE;
1074 real_closure = G_REAL_CLOSURE (closure);
1076 if (real_closure->meta_marshal == g_type_iface_meta_marshal)
1078 itype = (GType) closure->data;
1079 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1081 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1082 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1083 return callback == NULL;
1085 else if (real_closure->meta_marshal == g_type_class_meta_marshal)
1087 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1089 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1090 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1091 return callback == NULL;
1094 return FALSE;
1097 static void
1098 g_type_iface_meta_marshalv (GClosure *closure,
1099 GValue *return_value,
1100 gpointer instance,
1101 va_list args,
1102 gpointer marshal_data,
1103 int n_params,
1104 GType *param_types)
1106 GRealClosure *real_closure;
1107 GTypeClass *class;
1108 gpointer callback;
1109 GType itype = (GType) closure->data;
1110 guint offset = GPOINTER_TO_UINT (marshal_data);
1112 real_closure = G_REAL_CLOSURE (closure);
1114 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1115 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1116 if (callback)
1117 real_closure->va_marshal (closure,
1118 return_value,
1119 instance, args,
1120 callback,
1121 n_params,
1122 param_types);
1126 * g_signal_type_cclosure_new:
1127 * @itype: the #GType identifier of an interface or classed type
1128 * @struct_offset: the offset of the member function of @itype's class
1129 * structure which is to be invoked by the new closure
1131 * Creates a new closure which invokes the function found at the offset
1132 * @struct_offset in the class structure of the interface or classed type
1133 * identified by @itype.
1135 * Returns: (transfer none): a floating reference to a new #GCClosure
1137 GClosure*
1138 g_signal_type_cclosure_new (GType itype,
1139 guint struct_offset)
1141 GClosure *closure;
1143 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1144 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
1146 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
1147 if (G_TYPE_IS_INTERFACE (itype))
1149 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
1150 g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv);
1152 else
1154 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
1155 g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv);
1157 return closure;
1160 #include <ffi.h>
1161 static ffi_type *
1162 value_to_ffi_type (const GValue *gvalue,
1163 gpointer *value,
1164 gint *enum_tmpval,
1165 gboolean *tmpval_used)
1167 ffi_type *rettype = NULL;
1168 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
1169 g_assert (type != G_TYPE_INVALID);
1171 if (enum_tmpval)
1173 g_assert (tmpval_used != NULL);
1174 *tmpval_used = FALSE;
1177 switch (type)
1179 case G_TYPE_BOOLEAN:
1180 case G_TYPE_CHAR:
1181 case G_TYPE_INT:
1182 rettype = &ffi_type_sint;
1183 *value = (gpointer)&(gvalue->data[0].v_int);
1184 break;
1185 case G_TYPE_ENUM:
1186 /* enums are stored in v_long even though they are integers, which makes
1187 * marshalling through libffi somewhat complicated. They need to be
1188 * marshalled as signed ints, but we need to use a temporary int sized
1189 * value to pass to libffi otherwise it'll pull the wrong value on
1190 * BE machines with 32-bit integers when treating v_long as 32-bit int.
1192 g_assert (enum_tmpval != NULL);
1193 rettype = &ffi_type_sint;
1194 *enum_tmpval = g_value_get_enum (gvalue);
1195 *value = enum_tmpval;
1196 *tmpval_used = TRUE;
1197 break;
1198 case G_TYPE_FLAGS:
1199 g_assert (enum_tmpval != NULL);
1200 rettype = &ffi_type_uint;
1201 *enum_tmpval = g_value_get_flags (gvalue);
1202 *value = enum_tmpval;
1203 *tmpval_used = TRUE;
1204 break;
1205 case G_TYPE_UCHAR:
1206 case G_TYPE_UINT:
1207 rettype = &ffi_type_uint;
1208 *value = (gpointer)&(gvalue->data[0].v_uint);
1209 break;
1210 case G_TYPE_STRING:
1211 case G_TYPE_OBJECT:
1212 case G_TYPE_BOXED:
1213 case G_TYPE_PARAM:
1214 case G_TYPE_POINTER:
1215 case G_TYPE_INTERFACE:
1216 case G_TYPE_VARIANT:
1217 rettype = &ffi_type_pointer;
1218 *value = (gpointer)&(gvalue->data[0].v_pointer);
1219 break;
1220 case G_TYPE_FLOAT:
1221 rettype = &ffi_type_float;
1222 *value = (gpointer)&(gvalue->data[0].v_float);
1223 break;
1224 case G_TYPE_DOUBLE:
1225 rettype = &ffi_type_double;
1226 *value = (gpointer)&(gvalue->data[0].v_double);
1227 break;
1228 case G_TYPE_LONG:
1229 rettype = &ffi_type_slong;
1230 *value = (gpointer)&(gvalue->data[0].v_long);
1231 break;
1232 case G_TYPE_ULONG:
1233 rettype = &ffi_type_ulong;
1234 *value = (gpointer)&(gvalue->data[0].v_ulong);
1235 break;
1236 case G_TYPE_INT64:
1237 rettype = &ffi_type_sint64;
1238 *value = (gpointer)&(gvalue->data[0].v_int64);
1239 break;
1240 case G_TYPE_UINT64:
1241 rettype = &ffi_type_uint64;
1242 *value = (gpointer)&(gvalue->data[0].v_uint64);
1243 break;
1244 default:
1245 rettype = &ffi_type_pointer;
1246 *value = NULL;
1247 g_warning ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1248 break;
1250 return rettype;
1253 static void
1254 value_from_ffi_type (GValue *gvalue, gpointer *value)
1256 ffi_arg *int_val = (ffi_arg*) value;
1258 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
1260 case G_TYPE_INT:
1261 g_value_set_int (gvalue, (gint) *int_val);
1262 break;
1263 case G_TYPE_FLOAT:
1264 g_value_set_float (gvalue, *(gfloat*)value);
1265 break;
1266 case G_TYPE_DOUBLE:
1267 g_value_set_double (gvalue, *(gdouble*)value);
1268 break;
1269 case G_TYPE_BOOLEAN:
1270 g_value_set_boolean (gvalue, (gboolean) *int_val);
1271 break;
1272 case G_TYPE_STRING:
1273 g_value_take_string (gvalue, *(gchar**)value);
1274 break;
1275 case G_TYPE_CHAR:
1276 g_value_set_schar (gvalue, (gint8) *int_val);
1277 break;
1278 case G_TYPE_UCHAR:
1279 g_value_set_uchar (gvalue, (guchar) *int_val);
1280 break;
1281 case G_TYPE_UINT:
1282 g_value_set_uint (gvalue, (guint) *int_val);
1283 break;
1284 case G_TYPE_POINTER:
1285 g_value_set_pointer (gvalue, *(gpointer*)value);
1286 break;
1287 case G_TYPE_LONG:
1288 g_value_set_long (gvalue, (glong) *int_val);
1289 break;
1290 case G_TYPE_ULONG:
1291 g_value_set_ulong (gvalue, (gulong) *int_val);
1292 break;
1293 case G_TYPE_INT64:
1294 g_value_set_int64 (gvalue, (gint64) *int_val);
1295 break;
1296 case G_TYPE_UINT64:
1297 g_value_set_uint64 (gvalue, (guint64) *int_val);
1298 break;
1299 case G_TYPE_BOXED:
1300 g_value_take_boxed (gvalue, *(gpointer*)value);
1301 break;
1302 case G_TYPE_ENUM:
1303 g_value_set_enum (gvalue, (gint) *int_val);
1304 break;
1305 case G_TYPE_FLAGS:
1306 g_value_set_flags (gvalue, (guint) *int_val);
1307 break;
1308 case G_TYPE_PARAM:
1309 g_value_take_param (gvalue, *(gpointer*)value);
1310 break;
1311 case G_TYPE_OBJECT:
1312 g_value_take_object (gvalue, *(gpointer*)value);
1313 break;
1314 case G_TYPE_VARIANT:
1315 g_value_take_variant (gvalue, *(gpointer*)value);
1316 break;
1317 default:
1318 g_warning ("value_from_ffi_type: Unsupported fundamental type: %s",
1319 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
1323 typedef union {
1324 gpointer _gpointer;
1325 float _float;
1326 double _double;
1327 gint _gint;
1328 guint _guint;
1329 glong _glong;
1330 gulong _gulong;
1331 gint64 _gint64;
1332 guint64 _guint64;
1333 } va_arg_storage;
1335 static ffi_type *
1336 va_to_ffi_type (GType gtype,
1337 va_list *va,
1338 va_arg_storage *storage)
1340 ffi_type *rettype = NULL;
1341 GType type = g_type_fundamental (gtype);
1342 g_assert (type != G_TYPE_INVALID);
1344 switch (type)
1346 case G_TYPE_BOOLEAN:
1347 case G_TYPE_CHAR:
1348 case G_TYPE_INT:
1349 case G_TYPE_ENUM:
1350 rettype = &ffi_type_sint;
1351 storage->_gint = va_arg (*va, gint);
1352 break;
1353 case G_TYPE_UCHAR:
1354 case G_TYPE_UINT:
1355 case G_TYPE_FLAGS:
1356 rettype = &ffi_type_uint;
1357 storage->_guint = va_arg (*va, guint);
1358 break;
1359 case G_TYPE_STRING:
1360 case G_TYPE_OBJECT:
1361 case G_TYPE_BOXED:
1362 case G_TYPE_PARAM:
1363 case G_TYPE_POINTER:
1364 case G_TYPE_INTERFACE:
1365 case G_TYPE_VARIANT:
1366 rettype = &ffi_type_pointer;
1367 storage->_gpointer = va_arg (*va, gpointer);
1368 break;
1369 case G_TYPE_FLOAT:
1370 /* Float args are passed as doubles in varargs */
1371 rettype = &ffi_type_float;
1372 storage->_float = (float)va_arg (*va, double);
1373 break;
1374 case G_TYPE_DOUBLE:
1375 rettype = &ffi_type_double;
1376 storage->_double = va_arg (*va, double);
1377 break;
1378 case G_TYPE_LONG:
1379 rettype = &ffi_type_slong;
1380 storage->_glong = va_arg (*va, glong);
1381 break;
1382 case G_TYPE_ULONG:
1383 rettype = &ffi_type_ulong;
1384 storage->_gulong = va_arg (*va, gulong);
1385 break;
1386 case G_TYPE_INT64:
1387 rettype = &ffi_type_sint64;
1388 storage->_gint64 = va_arg (*va, gint64);
1389 break;
1390 case G_TYPE_UINT64:
1391 rettype = &ffi_type_uint64;
1392 storage->_guint64 = va_arg (*va, guint64);
1393 break;
1394 default:
1395 rettype = &ffi_type_pointer;
1396 storage->_guint64 = 0;
1397 g_warning ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1398 break;
1400 return rettype;
1404 * g_cclosure_marshal_generic:
1405 * @closure: A #GClosure.
1406 * @return_gvalue: A #GValue to store the return value. May be %NULL
1407 * if the callback of closure doesn't return a value.
1408 * @n_param_values: The length of the @param_values array.
1409 * @param_values: An array of #GValues holding the arguments
1410 * on which to invoke the callback of closure.
1411 * @invocation_hint: The invocation hint given as the last argument to
1412 * g_closure_invoke().
1413 * @marshal_data: Additional data specified when registering the
1414 * marshaller, see g_closure_set_marshal() and
1415 * g_closure_set_meta_marshal()
1417 * A generic marshaller function implemented via
1418 * [libffi](http://sourceware.org/libffi/).
1420 * Normally this function is not passed explicitly to g_signal_new(),
1421 * but used automatically by GLib when specifying a %NULL marshaller.
1423 * Since: 2.30
1425 void
1426 g_cclosure_marshal_generic (GClosure *closure,
1427 GValue *return_gvalue,
1428 guint n_param_values,
1429 const GValue *param_values,
1430 gpointer invocation_hint,
1431 gpointer marshal_data)
1433 ffi_type *rtype;
1434 void *rvalue;
1435 int n_args;
1436 ffi_type **atypes;
1437 void **args;
1438 int i;
1439 ffi_cif cif;
1440 GCClosure *cc = (GCClosure*) closure;
1441 gint *enum_tmpval;
1442 gboolean tmpval_used = FALSE;
1444 enum_tmpval = g_alloca (sizeof (gint));
1445 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1447 rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used);
1449 else
1451 rtype = &ffi_type_void;
1454 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1456 n_args = n_param_values + 1;
1457 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1458 args = g_alloca (sizeof (gpointer) * n_args);
1460 if (tmpval_used)
1461 enum_tmpval = g_alloca (sizeof (gint));
1463 if (G_CCLOSURE_SWAP_DATA (closure))
1465 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
1466 &args[n_args-1],
1467 enum_tmpval,
1468 &tmpval_used);
1469 atypes[0] = &ffi_type_pointer;
1470 args[0] = &closure->data;
1472 else
1474 atypes[0] = value_to_ffi_type (param_values + 0,
1475 &args[0],
1476 enum_tmpval,
1477 &tmpval_used);
1478 atypes[n_args-1] = &ffi_type_pointer;
1479 args[n_args-1] = &closure->data;
1482 for (i = 1; i < n_args - 1; i++)
1484 if (tmpval_used)
1485 enum_tmpval = g_alloca (sizeof (gint));
1487 atypes[i] = value_to_ffi_type (param_values + i,
1488 &args[i],
1489 enum_tmpval,
1490 &tmpval_used);
1493 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1494 return;
1496 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1498 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1499 value_from_ffi_type (return_gvalue, rvalue);
1503 * g_cclosure_marshal_generic_va:
1504 * @closure: the #GClosure to which the marshaller belongs
1505 * @return_value: (nullable): a #GValue to store the return
1506 * value. May be %NULL if the callback of @closure doesn't return a
1507 * value.
1508 * @instance: (type GObject.TypeInstance): the instance on which the closure is
1509 * invoked.
1510 * @args_list: va_list of arguments to be passed to the closure.
1511 * @marshal_data: (nullable): additional data specified when
1512 * registering the marshaller, see g_closure_set_marshal() and
1513 * g_closure_set_meta_marshal()
1514 * @n_params: the length of the @param_types array
1515 * @param_types: (array length=n_params): the #GType of each argument from
1516 * @args_list.
1518 * A generic #GVaClosureMarshal function implemented via
1519 * [libffi](http://sourceware.org/libffi/).
1521 * Since: 2.30
1523 void
1524 g_cclosure_marshal_generic_va (GClosure *closure,
1525 GValue *return_value,
1526 gpointer instance,
1527 va_list args_list,
1528 gpointer marshal_data,
1529 int n_params,
1530 GType *param_types)
1532 ffi_type *rtype;
1533 void *rvalue;
1534 int n_args;
1535 ffi_type **atypes;
1536 void **args;
1537 va_arg_storage *storage;
1538 int i;
1539 ffi_cif cif;
1540 GCClosure *cc = (GCClosure*) closure;
1541 gint *enum_tmpval;
1542 gboolean tmpval_used = FALSE;
1543 va_list args_copy;
1545 enum_tmpval = g_alloca (sizeof (gint));
1546 if (return_value && G_VALUE_TYPE (return_value))
1548 rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used);
1550 else
1552 rtype = &ffi_type_void;
1555 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1557 n_args = n_params + 2;
1558 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1559 args = g_alloca (sizeof (gpointer) * n_args);
1560 storage = g_alloca (sizeof (va_arg_storage) * n_params);
1562 if (G_CCLOSURE_SWAP_DATA (closure))
1564 atypes[n_args-1] = &ffi_type_pointer;
1565 args[n_args-1] = &instance;
1566 atypes[0] = &ffi_type_pointer;
1567 args[0] = &closure->data;
1569 else
1571 atypes[0] = &ffi_type_pointer;
1572 args[0] = &instance;
1573 atypes[n_args-1] = &ffi_type_pointer;
1574 args[n_args-1] = &closure->data;
1577 G_VA_COPY (args_copy, args_list);
1579 /* Box non-primitive arguments */
1580 for (i = 0; i < n_params; i++)
1582 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1583 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1585 atypes[i+1] = va_to_ffi_type (type,
1586 &args_copy,
1587 &storage[i]);
1588 args[i+1] = &storage[i];
1590 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1592 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1593 storage[i]._gpointer = g_strdup (storage[i]._gpointer);
1594 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1595 storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer);
1596 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1597 storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer);
1598 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1599 storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer);
1601 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1602 storage[i]._gpointer = g_object_ref (storage[i]._gpointer);
1605 va_end (args_copy);
1607 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1608 return;
1610 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1612 /* Unbox non-primitive arguments */
1613 for (i = 0; i < n_params; i++)
1615 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1616 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1618 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1620 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1621 g_free (storage[i]._gpointer);
1622 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1623 g_param_spec_unref (storage[i]._gpointer);
1624 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1625 g_boxed_free (type, storage[i]._gpointer);
1626 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1627 g_variant_unref (storage[i]._gpointer);
1629 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1630 g_object_unref (storage[i]._gpointer);
1633 if (return_value && G_VALUE_TYPE (return_value))
1634 value_from_ffi_type (return_value, rvalue);
1638 * g_cclosure_marshal_VOID__VOID:
1639 * @closure: the #GClosure to which the marshaller belongs
1640 * @return_value: ignored
1641 * @n_param_values: 1
1642 * @param_values: a #GValue array holding only the instance
1643 * @invocation_hint: the invocation hint given as the last argument
1644 * to g_closure_invoke()
1645 * @marshal_data: additional data specified when registering the marshaller
1647 * A marshaller for a #GCClosure with a callback of type
1648 * `void (*callback) (gpointer instance, gpointer user_data)`.
1652 * g_cclosure_marshal_VOID__BOOLEAN:
1653 * @closure: the #GClosure to which the marshaller belongs
1654 * @return_value: ignored
1655 * @n_param_values: 2
1656 * @param_values: a #GValue array holding the instance and the #gboolean parameter
1657 * @invocation_hint: the invocation hint given as the last argument
1658 * to g_closure_invoke()
1659 * @marshal_data: additional data specified when registering the marshaller
1661 * A marshaller for a #GCClosure with a callback of type
1662 * `void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)`.
1666 * g_cclosure_marshal_VOID__CHAR:
1667 * @closure: the #GClosure to which the marshaller belongs
1668 * @return_value: ignored
1669 * @n_param_values: 2
1670 * @param_values: a #GValue array holding the instance and the #gchar parameter
1671 * @invocation_hint: the invocation hint given as the last argument
1672 * to g_closure_invoke()
1673 * @marshal_data: additional data specified when registering the marshaller
1675 * A marshaller for a #GCClosure with a callback of type
1676 * `void (*callback) (gpointer instance, gchar arg1, gpointer user_data)`.
1680 * g_cclosure_marshal_VOID__UCHAR:
1681 * @closure: the #GClosure to which the marshaller belongs
1682 * @return_value: ignored
1683 * @n_param_values: 2
1684 * @param_values: a #GValue array holding the instance and the #guchar parameter
1685 * @invocation_hint: the invocation hint given as the last argument
1686 * to g_closure_invoke()
1687 * @marshal_data: additional data specified when registering the marshaller
1689 * A marshaller for a #GCClosure with a callback of type
1690 * `void (*callback) (gpointer instance, guchar arg1, gpointer user_data)`.
1694 * g_cclosure_marshal_VOID__INT:
1695 * @closure: the #GClosure to which the marshaller belongs
1696 * @return_value: ignored
1697 * @n_param_values: 2
1698 * @param_values: a #GValue array holding the instance and the #gint parameter
1699 * @invocation_hint: the invocation hint given as the last argument
1700 * to g_closure_invoke()
1701 * @marshal_data: additional data specified when registering the marshaller
1703 * A marshaller for a #GCClosure with a callback of type
1704 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)`.
1708 * g_cclosure_marshal_VOID__UINT:
1709 * @closure: the #GClosure to which the marshaller belongs
1710 * @return_value: ignored
1711 * @n_param_values: 2
1712 * @param_values: a #GValue array holding the instance and the #guint parameter
1713 * @invocation_hint: the invocation hint given as the last argument
1714 * to g_closure_invoke()
1715 * @marshal_data: additional data specified when registering the marshaller
1717 * A marshaller for a #GCClosure with a callback of type
1718 * `void (*callback) (gpointer instance, guint arg1, gpointer user_data)`.
1722 * g_cclosure_marshal_VOID__LONG:
1723 * @closure: the #GClosure to which the marshaller belongs
1724 * @return_value: ignored
1725 * @n_param_values: 2
1726 * @param_values: a #GValue array holding the instance and the #glong parameter
1727 * @invocation_hint: the invocation hint given as the last argument
1728 * to g_closure_invoke()
1729 * @marshal_data: additional data specified when registering the marshaller
1731 * A marshaller for a #GCClosure with a callback of type
1732 * `void (*callback) (gpointer instance, glong arg1, gpointer user_data)`.
1736 * g_cclosure_marshal_VOID__ULONG:
1737 * @closure: the #GClosure to which the marshaller belongs
1738 * @return_value: ignored
1739 * @n_param_values: 2
1740 * @param_values: a #GValue array holding the instance and the #gulong parameter
1741 * @invocation_hint: the invocation hint given as the last argument
1742 * to g_closure_invoke()
1743 * @marshal_data: additional data specified when registering the marshaller
1745 * A marshaller for a #GCClosure with a callback of type
1746 * `void (*callback) (gpointer instance, gulong arg1, gpointer user_data)`.
1750 * g_cclosure_marshal_VOID__ENUM:
1751 * @closure: the #GClosure to which the marshaller belongs
1752 * @return_value: ignored
1753 * @n_param_values: 2
1754 * @param_values: a #GValue array holding the instance and the enumeration parameter
1755 * @invocation_hint: the invocation hint given as the last argument
1756 * to g_closure_invoke()
1757 * @marshal_data: additional data specified when registering the marshaller
1759 * A marshaller for a #GCClosure with a callback of type
1760 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes an enumeration type..
1764 * g_cclosure_marshal_VOID__FLAGS:
1765 * @closure: the #GClosure to which the marshaller belongs
1766 * @return_value: ignored
1767 * @n_param_values: 2
1768 * @param_values: a #GValue array holding the instance and the flags parameter
1769 * @invocation_hint: the invocation hint given as the last argument
1770 * to g_closure_invoke()
1771 * @marshal_data: additional data specified when registering the marshaller
1773 * A marshaller for a #GCClosure with a callback of type
1774 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes a flags type.
1778 * g_cclosure_marshal_VOID__FLOAT:
1779 * @closure: the #GClosure to which the marshaller belongs
1780 * @return_value: ignored
1781 * @n_param_values: 2
1782 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1783 * @invocation_hint: the invocation hint given as the last argument
1784 * to g_closure_invoke()
1785 * @marshal_data: additional data specified when registering the marshaller
1787 * A marshaller for a #GCClosure with a callback of type
1788 * `void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)`.
1792 * g_cclosure_marshal_VOID__DOUBLE:
1793 * @closure: the #GClosure to which the marshaller belongs
1794 * @return_value: ignored
1795 * @n_param_values: 2
1796 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1797 * @invocation_hint: the invocation hint given as the last argument
1798 * to g_closure_invoke()
1799 * @marshal_data: additional data specified when registering the marshaller
1801 * A marshaller for a #GCClosure with a callback of type
1802 * `void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)`.
1806 * g_cclosure_marshal_VOID__STRING:
1807 * @closure: the #GClosure to which the marshaller belongs
1808 * @return_value: ignored
1809 * @n_param_values: 2
1810 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1811 * @invocation_hint: the invocation hint given as the last argument
1812 * to g_closure_invoke()
1813 * @marshal_data: additional data specified when registering the marshaller
1815 * A marshaller for a #GCClosure with a callback of type
1816 * `void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)`.
1820 * g_cclosure_marshal_VOID__PARAM:
1821 * @closure: the #GClosure to which the marshaller belongs
1822 * @return_value: ignored
1823 * @n_param_values: 2
1824 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1825 * @invocation_hint: the invocation hint given as the last argument
1826 * to g_closure_invoke()
1827 * @marshal_data: additional data specified when registering the marshaller
1829 * A marshaller for a #GCClosure with a callback of type
1830 * `void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)`.
1834 * g_cclosure_marshal_VOID__BOXED:
1835 * @closure: the #GClosure to which the marshaller belongs
1836 * @return_value: ignored
1837 * @n_param_values: 2
1838 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1839 * @invocation_hint: the invocation hint given as the last argument
1840 * to g_closure_invoke()
1841 * @marshal_data: additional data specified when registering the marshaller
1843 * A marshaller for a #GCClosure with a callback of type
1844 * `void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)`.
1848 * g_cclosure_marshal_VOID__POINTER:
1849 * @closure: the #GClosure to which the marshaller belongs
1850 * @return_value: ignored
1851 * @n_param_values: 2
1852 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1853 * @invocation_hint: the invocation hint given as the last argument
1854 * to g_closure_invoke()
1855 * @marshal_data: additional data specified when registering the marshaller
1857 * A marshaller for a #GCClosure with a callback of type
1858 * `void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)`.
1862 * g_cclosure_marshal_VOID__OBJECT:
1863 * @closure: the #GClosure to which the marshaller belongs
1864 * @return_value: ignored
1865 * @n_param_values: 2
1866 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1867 * @invocation_hint: the invocation hint given as the last argument
1868 * to g_closure_invoke()
1869 * @marshal_data: additional data specified when registering the marshaller
1871 * A marshaller for a #GCClosure with a callback of type
1872 * `void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)`.
1876 * g_cclosure_marshal_VOID__VARIANT:
1877 * @closure: the #GClosure to which the marshaller belongs
1878 * @return_value: ignored
1879 * @n_param_values: 2
1880 * @param_values: a #GValue array holding the instance and the #GVariant* parameter
1881 * @invocation_hint: the invocation hint given as the last argument
1882 * to g_closure_invoke()
1883 * @marshal_data: additional data specified when registering the marshaller
1885 * A marshaller for a #GCClosure with a callback of type
1886 * `void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)`.
1888 * Since: 2.26
1892 * g_cclosure_marshal_VOID__UINT_POINTER:
1893 * @closure: the #GClosure to which the marshaller belongs
1894 * @return_value: ignored
1895 * @n_param_values: 3
1896 * @param_values: a #GValue array holding instance, arg1 and arg2
1897 * @invocation_hint: the invocation hint given as the last argument
1898 * to g_closure_invoke()
1899 * @marshal_data: additional data specified when registering the marshaller
1901 * A marshaller for a #GCClosure with a callback of type
1902 * `void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)`.
1906 * g_cclosure_marshal_BOOLEAN__FLAGS:
1907 * @closure: the #GClosure to which the marshaller belongs
1908 * @return_value: a #GValue which can store the returned #gboolean
1909 * @n_param_values: 2
1910 * @param_values: a #GValue array holding instance and arg1
1911 * @invocation_hint: the invocation hint given as the last argument
1912 * to g_closure_invoke()
1913 * @marshal_data: additional data specified when registering the marshaller
1915 * A marshaller for a #GCClosure with a callback of type
1916 * `gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter
1917 * denotes a flags type.
1921 * g_cclosure_marshal_BOOL__FLAGS:
1923 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1926 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1927 * @closure: the #GClosure to which the marshaller belongs
1928 * @return_value: a #GValue, which can store the returned string
1929 * @n_param_values: 3
1930 * @param_values: a #GValue array holding instance, arg1 and arg2
1931 * @invocation_hint: the invocation hint given as the last argument
1932 * to g_closure_invoke()
1933 * @marshal_data: additional data specified when registering the marshaller
1935 * A marshaller for a #GCClosure with a callback of type
1936 * `gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)`.
1939 * g_cclosure_marshal_BOOLEAN__OBJECT_BOXED_BOXED:
1940 * @closure: the #GClosure to which the marshaller belongs
1941 * @return_value: a #GValue, which can store the returned string
1942 * @n_param_values: 3
1943 * @param_values: a #GValue array holding instance, arg1 and arg2
1944 * @invocation_hint: the invocation hint given as the last argument
1945 * to g_closure_invoke()
1946 * @marshal_data: additional data specified when registering the marshaller
1948 * A marshaller for a #GCClosure with a callback of type
1949 * `gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)`.
1951 * Since: 2.26