Update German translation
[glib.git] / gobject / gclosure.c
blobcced5f90b38627a0aa10a08fe81db29ea21e7103
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 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/valgrind.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 #GValue<!--
56 * -->s and suitable representations in the runtime of the language in
57 * order to use functions written in that languages as callbacks.
59 * Within GObject, closures play an important role in the
60 * implementation of signals. When a signal is registered, the
61 * @c_marshaller argument to g_signal_new() specifies the default C
62 * marshaller for any closure which is connected to this
63 * signal. GObject provides a number of C marshallers for this
64 * purpose, see the g_cclosure_marshal_*() functions. Additional C
65 * marshallers can be generated with the [glib-genmarshal][glib-genmarshal]
66 * utility. Closures 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.
72 * Using closures has a number of important advantages over a simple
73 * callback function/data pointer combination:
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.
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.
83 * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
84 * automatically removed when the objects they point to go away.
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)
97 typedef union {
98 GClosure closure;
99 volatile gint vint;
100 } ClosureInt;
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 \
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); \
116 while (!success && _must_set); \
117 } G_STMT_END
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) = )
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
135 enum {
136 FNOTIFY,
137 INOTIFY,
138 PRE_NOTIFY,
139 POST_NOTIFY
143 /* --- functions --- */
145 * g_closure_new_simple:
146 * @sizeof_closure: the size of the structure to allocate, must be at least
147 * `sizeof (GClosure)`
148 * @data: data to store in the @data field of the newly allocated #GClosure
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.
154 * |[<!-- language="C" -->
155 * typedef struct _MyClosure MyClosure;
156 * struct _MyClosure
158 * GClosure closure;
159 * // extra data goes here
160 * };
162 * static void
163 * my_closure_finalize (gpointer notify_data,
164 * GClosure *closure)
166 * MyClosure *my_closure = (MyClosure *)closure;
168 * // free extra data here
171 * MyClosure *my_closure_new (gpointer data)
173 * GClosure *closure;
174 * MyClosure *my_closure;
176 * closure = g_closure_new_simple (sizeof (MyClosure), data);
177 * my_closure = (MyClosure *) closure;
179 * // initialize extra data here
181 * g_closure_add_finalize_notifier (closure, notify_data,
182 * my_closure_finalize);
183 * return my_closure;
185 * ]|
187 * Returns: (transfer full): a newly allocated #GClosure
189 GClosure*
190 g_closure_new_simple (guint sizeof_closure,
191 gpointer data)
193 GClosure *closure;
194 gint private_size;
195 gchar *allocated;
197 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
199 private_size = sizeof (GRealClosure) - sizeof (GClosure);
201 /* See comments in gtype.c about what's going on here... */
202 if (RUNNING_ON_VALGRIND)
204 private_size += sizeof (gpointer);
206 allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer));
208 *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer);
210 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, sizeof_closure + sizeof (gpointer), 0, TRUE);
211 VALGRIND_MALLOCLIKE_BLOCK (allocated + sizeof (gpointer), private_size - sizeof (gpointer), 0, TRUE);
213 else
214 allocated = g_malloc0 (private_size + sizeof_closure);
216 closure = (GClosure *) (allocated + private_size);
218 SET (closure, ref_count, 1);
219 SET (closure, floating, TRUE);
220 closure->data = data;
222 return closure;
225 static inline void
226 closure_invoke_notifiers (GClosure *closure,
227 guint notify_type)
229 /* notifier layout:
230 * n_guards n_guards n_fnotif. n_inotifiers
231 * ->[[pre_guards][post_guards][fnotifiers][inotifiers]]
233 * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards;
234 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
236 * constrains/catches:
237 * - closure->notifiers may be reloacted during callback
238 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
239 * - i.e. callbacks can be removed/added during invocation
240 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
241 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
242 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
243 * + none of the callbacks can cause recursion
244 * + closure->n_inotifiers is const 0 during FNOTIFY
246 switch (notify_type)
248 GClosureNotifyData *ndata;
249 guint i, offs;
250 case FNOTIFY:
251 while (closure->n_fnotifiers)
253 guint n;
254 DEC_ASSIGN (closure, n_fnotifiers, &n);
256 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
257 closure->marshal = (GClosureMarshal) ndata->notify;
258 closure->data = ndata->data;
259 ndata->notify (ndata->data, closure);
261 closure->marshal = NULL;
262 closure->data = NULL;
263 break;
264 case INOTIFY:
265 SET (closure, in_inotify, TRUE);
266 while (closure->n_inotifiers)
268 guint n;
269 DEC_ASSIGN (closure, n_inotifiers, &n);
271 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
272 closure->marshal = (GClosureMarshal) ndata->notify;
273 closure->data = ndata->data;
274 ndata->notify (ndata->data, closure);
276 closure->marshal = NULL;
277 closure->data = NULL;
278 SET (closure, in_inotify, FALSE);
279 break;
280 case PRE_NOTIFY:
281 i = closure->n_guards;
282 offs = 0;
283 while (i--)
285 ndata = closure->notifiers + offs + i;
286 ndata->notify (ndata->data, closure);
288 break;
289 case POST_NOTIFY:
290 i = closure->n_guards;
291 offs = i;
292 while (i--)
294 ndata = closure->notifiers + offs + i;
295 ndata->notify (ndata->data, closure);
297 break;
301 static void
302 g_closure_set_meta_va_marshal (GClosure *closure,
303 GVaClosureMarshal va_meta_marshal)
305 GRealClosure *real_closure;
307 g_return_if_fail (closure != NULL);
308 g_return_if_fail (va_meta_marshal != NULL);
309 g_return_if_fail (closure->is_invalid == FALSE);
310 g_return_if_fail (closure->in_marshal == FALSE);
312 real_closure = G_REAL_CLOSURE (closure);
314 g_return_if_fail (real_closure->meta_marshal != NULL);
316 real_closure->va_meta_marshal = va_meta_marshal;
320 * g_closure_set_meta_marshal: (skip)
321 * @closure: a #GClosure
322 * @marshal_data: (closure meta_marshal): context-dependent data to pass
323 * to @meta_marshal
324 * @meta_marshal: a #GClosureMarshal function
326 * Sets the meta marshaller of @closure. A meta marshaller wraps
327 * @closure->marshal and modifies the way it is called in some
328 * fashion. The most common use of this facility is for C callbacks.
329 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
330 * are used everywhere, but the way that we get the callback function
331 * differs. In most cases we want to use @closure->callback, but in
332 * other cases we want to use some different technique to retrieve the
333 * callback function.
335 * For example, class closures for signals (see
336 * g_signal_type_cclosure_new()) retrieve the callback function from a
337 * fixed offset in the class structure. The meta marshaller retrieves
338 * the right callback and passes it to the marshaller as the
339 * @marshal_data argument.
341 void
342 g_closure_set_meta_marshal (GClosure *closure,
343 gpointer marshal_data,
344 GClosureMarshal meta_marshal)
346 GRealClosure *real_closure;
348 g_return_if_fail (closure != NULL);
349 g_return_if_fail (meta_marshal != NULL);
350 g_return_if_fail (closure->is_invalid == FALSE);
351 g_return_if_fail (closure->in_marshal == FALSE);
353 real_closure = G_REAL_CLOSURE (closure);
355 g_return_if_fail (real_closure->meta_marshal == NULL);
357 real_closure->meta_marshal = meta_marshal;
358 real_closure->meta_marshal_data = marshal_data;
362 * g_closure_add_marshal_guards: (skip)
363 * @closure: a #GClosure
364 * @pre_marshal_data: (closure pre_marshal_notify): data to pass
365 * to @pre_marshal_notify
366 * @pre_marshal_notify: a function to call before the closure callback
367 * @post_marshal_data: (closure post_marshal_notify): data to pass
368 * to @post_marshal_notify
369 * @post_marshal_notify: a function to call after the closure callback
371 * Adds a pair of notifiers which get invoked before and after the
372 * closure callback, respectively. This is typically used to protect
373 * the extra arguments for the duration of the callback. See
374 * g_object_watch_closure() for an example of marshal guards.
376 void
377 g_closure_add_marshal_guards (GClosure *closure,
378 gpointer pre_marshal_data,
379 GClosureNotify pre_marshal_notify,
380 gpointer post_marshal_data,
381 GClosureNotify post_marshal_notify)
383 guint i;
385 g_return_if_fail (closure != NULL);
386 g_return_if_fail (pre_marshal_notify != NULL);
387 g_return_if_fail (post_marshal_notify != NULL);
388 g_return_if_fail (closure->is_invalid == FALSE);
389 g_return_if_fail (closure->in_marshal == FALSE);
390 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
392 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
393 if (closure->n_inotifiers)
394 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
395 closure->n_fnotifiers +
396 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
397 closure->n_fnotifiers + 0)];
398 if (closure->n_inotifiers > 1)
399 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
400 closure->n_fnotifiers +
401 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
402 closure->n_fnotifiers + 1)];
403 if (closure->n_fnotifiers)
404 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
405 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
406 if (closure->n_fnotifiers > 1)
407 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
408 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
409 if (closure->n_guards)
410 closure->notifiers[(closure->n_guards +
411 closure->n_guards + 1)] = closure->notifiers[closure->n_guards];
412 i = closure->n_guards;
413 closure->notifiers[i].data = pre_marshal_data;
414 closure->notifiers[i].notify = pre_marshal_notify;
415 closure->notifiers[i + 1].data = post_marshal_data;
416 closure->notifiers[i + 1].notify = post_marshal_notify;
417 INC (closure, n_guards);
421 * g_closure_add_finalize_notifier: (skip)
422 * @closure: a #GClosure
423 * @notify_data: (closure notify_func): data to pass to @notify_func
424 * @notify_func: the callback function to register
426 * Registers a finalization notifier which will be called when the
427 * reference count of @closure goes down to 0. Multiple finalization
428 * notifiers on a single closure are invoked in unspecified order. If
429 * a single call to g_closure_unref() results in the closure being
430 * both invalidated and finalized, then the invalidate notifiers will
431 * be run before the finalize notifiers.
433 void
434 g_closure_add_finalize_notifier (GClosure *closure,
435 gpointer notify_data,
436 GClosureNotify notify_func)
438 guint i;
440 g_return_if_fail (closure != NULL);
441 g_return_if_fail (notify_func != NULL);
442 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
444 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
445 if (closure->n_inotifiers)
446 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
447 closure->n_fnotifiers +
448 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
449 closure->n_fnotifiers + 0)];
450 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
451 closure->notifiers[i].data = notify_data;
452 closure->notifiers[i].notify = notify_func;
453 INC (closure, n_fnotifiers);
457 * g_closure_add_invalidate_notifier: (skip)
458 * @closure: a #GClosure
459 * @notify_data: (closure notify_func): data to pass to @notify_func
460 * @notify_func: the callback function to register
462 * Registers an invalidation notifier which will be called when the
463 * @closure is invalidated with g_closure_invalidate(). Invalidation
464 * notifiers are invoked before finalization notifiers, in an
465 * unspecified order.
467 void
468 g_closure_add_invalidate_notifier (GClosure *closure,
469 gpointer notify_data,
470 GClosureNotify notify_func)
472 guint i;
474 g_return_if_fail (closure != NULL);
475 g_return_if_fail (notify_func != NULL);
476 g_return_if_fail (closure->is_invalid == FALSE);
477 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
479 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
480 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
481 closure->notifiers[i].data = notify_data;
482 closure->notifiers[i].notify = notify_func;
483 INC (closure, n_inotifiers);
486 static inline gboolean
487 closure_try_remove_inotify (GClosure *closure,
488 gpointer notify_data,
489 GClosureNotify notify_func)
491 GClosureNotifyData *ndata, *nlast;
493 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
494 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
495 if (ndata->notify == notify_func && ndata->data == notify_data)
497 DEC (closure, n_inotifiers);
498 if (ndata < nlast)
499 *ndata = *nlast;
501 return TRUE;
503 return FALSE;
506 static inline gboolean
507 closure_try_remove_fnotify (GClosure *closure,
508 gpointer notify_data,
509 GClosureNotify notify_func)
511 GClosureNotifyData *ndata, *nlast;
513 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
514 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
515 if (ndata->notify == notify_func && ndata->data == notify_data)
517 DEC (closure, n_fnotifiers);
518 if (ndata < nlast)
519 *ndata = *nlast;
520 if (closure->n_inotifiers)
521 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
522 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
523 closure->n_fnotifiers +
524 closure->n_inotifiers)];
525 return TRUE;
527 return FALSE;
531 * g_closure_ref:
532 * @closure: #GClosure to increment the reference count on
534 * Increments the reference count on a closure to force it staying
535 * alive while the caller holds a pointer to it.
537 * Returns: (transfer none): The @closure passed in, for convenience
539 GClosure*
540 g_closure_ref (GClosure *closure)
542 guint new_ref_count;
543 g_return_val_if_fail (closure != NULL, NULL);
544 g_return_val_if_fail (closure->ref_count > 0, NULL);
545 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
547 INC_ASSIGN (closure, ref_count, &new_ref_count);
548 g_return_val_if_fail (new_ref_count > 1, NULL);
550 return closure;
554 * g_closure_invalidate:
555 * @closure: GClosure to invalidate
557 * Sets a flag on the closure to indicate that its calling
558 * environment has become invalid, and thus causes any future
559 * invocations of g_closure_invoke() on this @closure to be
560 * ignored. Also, invalidation notifiers installed on the closure will
561 * be called at this point. Note that unless you are holding a
562 * reference to the closure yourself, the invalidation notifiers may
563 * unref the closure and cause it to be destroyed, so if you need to
564 * access the closure after calling g_closure_invalidate(), make sure
565 * that you've previously called g_closure_ref().
567 * Note that g_closure_invalidate() will also be called when the
568 * reference count of a closure drops to zero (unless it has already
569 * been invalidated before).
571 void
572 g_closure_invalidate (GClosure *closure)
574 g_return_if_fail (closure != NULL);
576 if (!closure->is_invalid)
578 gboolean was_invalid;
579 g_closure_ref (closure); /* preserve floating flag */
580 SWAP (closure, is_invalid, TRUE, &was_invalid);
581 /* invalidate only once */
582 if (!was_invalid)
583 closure_invoke_notifiers (closure, INOTIFY);
584 g_closure_unref (closure);
589 * g_closure_unref:
590 * @closure: #GClosure to decrement the reference count on
592 * Decrements the reference count of a closure after it was previously
593 * incremented by the same caller. If no other callers are using the
594 * closure, then the closure will be destroyed and freed.
596 void
597 g_closure_unref (GClosure *closure)
599 guint new_ref_count;
601 g_return_if_fail (closure != NULL);
602 g_return_if_fail (closure->ref_count > 0);
604 if (closure->ref_count == 1) /* last unref, invalidate first */
605 g_closure_invalidate (closure);
607 DEC_ASSIGN (closure, ref_count, &new_ref_count);
609 if (new_ref_count == 0)
611 closure_invoke_notifiers (closure, FNOTIFY);
612 g_free (closure->notifiers);
614 /* See comments in gtype.c about what's going on here... */
615 if (RUNNING_ON_VALGRIND)
617 gchar *allocated;
619 allocated = (gchar *) G_REAL_CLOSURE (closure);
620 allocated -= sizeof (gpointer);
622 g_free (allocated);
624 VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0);
625 VALGRIND_FREELIKE_BLOCK (closure, 0);
627 else
628 g_free (G_REAL_CLOSURE (closure));
633 * g_closure_sink:
634 * @closure: #GClosure to decrement the initial reference count on, if it's
635 * still being held
637 * Takes over the initial ownership of a closure. Each closure is
638 * initially created in a "floating" state, which means that the initial
639 * reference count is not owned by any caller. g_closure_sink() checks
640 * to see if the object is still floating, and if so, unsets the
641 * floating state and decreases the reference count. If the closure
642 * is not floating, g_closure_sink() does nothing. The reason for the
643 * existence of the floating state is to prevent cumbersome code
644 * sequences like:
645 * |[<!-- language="C" -->
646 * closure = g_cclosure_new (cb_func, cb_data);
647 * g_source_set_closure (source, closure);
648 * g_closure_unref (closure); // GObject doesn't really need this
649 * ]|
650 * Because g_source_set_closure() (and similar functions) take ownership of the
651 * initial reference count, if it is unowned, we instead can write:
652 * |[<!-- language="C" -->
653 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
654 * ]|
656 * Generally, this function is used together with g_closure_ref(). Ane example
657 * of storing a closure for later notification looks like:
658 * |[<!-- language="C" -->
659 * static GClosure *notify_closure = NULL;
660 * void
661 * foo_notify_set_closure (GClosure *closure)
663 * if (notify_closure)
664 * g_closure_unref (notify_closure);
665 * notify_closure = closure;
666 * if (notify_closure)
668 * g_closure_ref (notify_closure);
669 * g_closure_sink (notify_closure);
672 * ]|
674 * Because g_closure_sink() may decrement the reference count of a closure
675 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
676 * g_closure_ref() should be called prior to this function.
678 void
679 g_closure_sink (GClosure *closure)
681 g_return_if_fail (closure != NULL);
682 g_return_if_fail (closure->ref_count > 0);
684 /* floating is basically a kludge to avoid creating closures
685 * with a ref_count of 0. so the initial ref_count a closure has
686 * is unowned. with invoking g_closure_sink() code may
687 * indicate that it takes over that intiial ref_count.
689 if (closure->floating)
691 gboolean was_floating;
692 SWAP (closure, floating, FALSE, &was_floating);
693 /* unref floating flag only once */
694 if (was_floating)
695 g_closure_unref (closure);
700 * g_closure_remove_invalidate_notifier: (skip)
701 * @closure: a #GClosure
702 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
703 * when registering @notify_func
704 * @notify_func: the callback function to remove
706 * Removes an invalidation notifier.
708 * Notice that notifiers are automatically removed after they are run.
710 void
711 g_closure_remove_invalidate_notifier (GClosure *closure,
712 gpointer notify_data,
713 GClosureNotify notify_func)
715 g_return_if_fail (closure != NULL);
716 g_return_if_fail (notify_func != NULL);
718 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */
719 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
720 closure->data == notify_data)
721 closure->marshal = NULL;
722 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
723 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
724 notify_func, notify_data);
728 * g_closure_remove_finalize_notifier: (skip)
729 * @closure: a #GClosure
730 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
731 * when registering @notify_func
732 * @notify_func: the callback function to remove
734 * Removes a finalization notifier.
736 * Notice that notifiers are automatically removed after they are run.
738 void
739 g_closure_remove_finalize_notifier (GClosure *closure,
740 gpointer notify_data,
741 GClosureNotify notify_func)
743 g_return_if_fail (closure != NULL);
744 g_return_if_fail (notify_func != NULL);
746 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */
747 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
748 closure->data == notify_data)
749 closure->marshal = NULL;
750 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
751 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
752 notify_func, notify_data);
756 * g_closure_invoke:
757 * @closure: a #GClosure
758 * @return_value: (optional) (out): a #GValue to store the return
759 * value. May be %NULL if the callback of @closure
760 * doesn't return a value.
761 * @n_param_values: the length of the @param_values array
762 * @param_values: (array length=n_param_values): an array of
763 * #GValues holding the arguments on which to
764 * invoke the callback of @closure
765 * @invocation_hint: (nullable): a context-dependent invocation hint
767 * Invokes the closure, i.e. executes the callback represented by the @closure.
769 void
770 g_closure_invoke (GClosure *closure,
771 GValue /*out*/ *return_value,
772 guint n_param_values,
773 const GValue *param_values,
774 gpointer invocation_hint)
776 GRealClosure *real_closure;
778 g_return_if_fail (closure != NULL);
780 real_closure = G_REAL_CLOSURE (closure);
782 g_closure_ref (closure); /* preserve floating flag */
783 if (!closure->is_invalid)
785 GClosureMarshal marshal;
786 gpointer marshal_data;
787 gboolean in_marshal = closure->in_marshal;
789 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
791 SET (closure, in_marshal, TRUE);
792 if (real_closure->meta_marshal)
794 marshal_data = real_closure->meta_marshal_data;
795 marshal = real_closure->meta_marshal;
797 else
799 marshal_data = NULL;
800 marshal = closure->marshal;
802 if (!in_marshal)
803 closure_invoke_notifiers (closure, PRE_NOTIFY);
804 marshal (closure,
805 return_value,
806 n_param_values, param_values,
807 invocation_hint,
808 marshal_data);
809 if (!in_marshal)
810 closure_invoke_notifiers (closure, POST_NOTIFY);
811 SET (closure, in_marshal, in_marshal);
813 g_closure_unref (closure);
816 gboolean
817 _g_closure_supports_invoke_va (GClosure *closure)
819 GRealClosure *real_closure;
821 g_return_val_if_fail (closure != NULL, FALSE);
823 real_closure = G_REAL_CLOSURE (closure);
825 return
826 real_closure->va_marshal != NULL &&
827 (real_closure->meta_marshal == NULL ||
828 real_closure->va_meta_marshal != NULL);
831 void
832 _g_closure_invoke_va (GClosure *closure,
833 GValue /*out*/ *return_value,
834 gpointer instance,
835 va_list args,
836 int n_params,
837 GType *param_types)
839 GRealClosure *real_closure;
841 g_return_if_fail (closure != NULL);
843 real_closure = G_REAL_CLOSURE (closure);
845 g_closure_ref (closure); /* preserve floating flag */
846 if (!closure->is_invalid)
848 GVaClosureMarshal marshal;
849 gpointer marshal_data;
850 gboolean in_marshal = closure->in_marshal;
852 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
854 SET (closure, in_marshal, TRUE);
855 if (real_closure->va_meta_marshal)
857 marshal_data = real_closure->meta_marshal_data;
858 marshal = real_closure->va_meta_marshal;
860 else
862 marshal_data = NULL;
863 marshal = real_closure->va_marshal;
865 if (!in_marshal)
866 closure_invoke_notifiers (closure, PRE_NOTIFY);
867 marshal (closure,
868 return_value,
869 instance, args,
870 marshal_data,
871 n_params, param_types);
872 if (!in_marshal)
873 closure_invoke_notifiers (closure, POST_NOTIFY);
874 SET (closure, in_marshal, in_marshal);
876 g_closure_unref (closure);
881 * g_closure_set_marshal: (skip)
882 * @closure: a #GClosure
883 * @marshal: a #GClosureMarshal function
885 * Sets the marshaller of @closure. The `marshal_data`
886 * of @marshal provides a way for a meta marshaller to provide additional
887 * information to the marshaller. (See g_closure_set_meta_marshal().) For
888 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
889 * functions), what it provides is a callback function to use instead of
890 * @closure->callback.
892 void
893 g_closure_set_marshal (GClosure *closure,
894 GClosureMarshal marshal)
896 g_return_if_fail (closure != NULL);
897 g_return_if_fail (marshal != NULL);
899 if (closure->marshal && closure->marshal != marshal)
900 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
901 closure->marshal, marshal);
902 else
903 closure->marshal = marshal;
906 void
907 _g_closure_set_va_marshal (GClosure *closure,
908 GVaClosureMarshal marshal)
910 GRealClosure *real_closure;
912 g_return_if_fail (closure != NULL);
913 g_return_if_fail (marshal != NULL);
915 real_closure = G_REAL_CLOSURE (closure);
917 if (real_closure->va_marshal && real_closure->va_marshal != marshal)
918 g_warning ("attempt to override closure->va_marshal (%p) with new marshal (%p)",
919 real_closure->va_marshal, marshal);
920 else
921 real_closure->va_marshal = marshal;
925 * g_cclosure_new: (skip)
926 * @callback_func: the function to invoke
927 * @user_data: (closure callback_func): user data to pass to @callback_func
928 * @destroy_data: destroy notify to be called when @user_data is no longer used
930 * Creates a new closure which invokes @callback_func with @user_data as
931 * the last parameter.
933 * Returns: a new #GCClosure
935 GClosure*
936 g_cclosure_new (GCallback callback_func,
937 gpointer user_data,
938 GClosureNotify destroy_data)
940 GClosure *closure;
942 g_return_val_if_fail (callback_func != NULL, NULL);
944 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
945 if (destroy_data)
946 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
947 ((GCClosure*) closure)->callback = (gpointer) callback_func;
949 return closure;
953 * g_cclosure_new_swap: (skip)
954 * @callback_func: the function to invoke
955 * @user_data: (closure callback_func): user data to pass to @callback_func
956 * @destroy_data: destroy notify to be called when @user_data is no longer used
958 * Creates a new closure which invokes @callback_func with @user_data as
959 * the first parameter.
961 * Returns: (transfer full): a new #GCClosure
963 GClosure*
964 g_cclosure_new_swap (GCallback callback_func,
965 gpointer user_data,
966 GClosureNotify destroy_data)
968 GClosure *closure;
970 g_return_val_if_fail (callback_func != NULL, NULL);
972 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
973 if (destroy_data)
974 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
975 ((GCClosure*) closure)->callback = (gpointer) callback_func;
976 SET (closure, derivative_flag, TRUE);
978 return closure;
981 static void
982 g_type_class_meta_marshal (GClosure *closure,
983 GValue /*out*/ *return_value,
984 guint n_param_values,
985 const GValue *param_values,
986 gpointer invocation_hint,
987 gpointer marshal_data)
989 GTypeClass *class;
990 gpointer callback;
991 /* GType itype = (GType) closure->data; */
992 guint offset = GPOINTER_TO_UINT (marshal_data);
994 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
995 callback = G_STRUCT_MEMBER (gpointer, class, offset);
996 if (callback)
997 closure->marshal (closure,
998 return_value,
999 n_param_values, param_values,
1000 invocation_hint,
1001 callback);
1004 static void
1005 g_type_class_meta_marshalv (GClosure *closure,
1006 GValue *return_value,
1007 gpointer instance,
1008 va_list args,
1009 gpointer marshal_data,
1010 int n_params,
1011 GType *param_types)
1013 GRealClosure *real_closure;
1014 GTypeClass *class;
1015 gpointer callback;
1016 /* GType itype = (GType) closure->data; */
1017 guint offset = GPOINTER_TO_UINT (marshal_data);
1019 real_closure = G_REAL_CLOSURE (closure);
1021 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1022 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1023 if (callback)
1024 real_closure->va_marshal (closure,
1025 return_value,
1026 instance, args,
1027 callback,
1028 n_params,
1029 param_types);
1032 static void
1033 g_type_iface_meta_marshal (GClosure *closure,
1034 GValue /*out*/ *return_value,
1035 guint n_param_values,
1036 const GValue *param_values,
1037 gpointer invocation_hint,
1038 gpointer marshal_data)
1040 GTypeClass *class;
1041 gpointer callback;
1042 GType itype = (GType) closure->data;
1043 guint offset = GPOINTER_TO_UINT (marshal_data);
1045 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1046 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1047 if (callback)
1048 closure->marshal (closure,
1049 return_value,
1050 n_param_values, param_values,
1051 invocation_hint,
1052 callback);
1055 gboolean
1056 _g_closure_is_void (GClosure *closure,
1057 gpointer instance)
1059 GRealClosure *real_closure;
1060 GTypeClass *class;
1061 gpointer callback;
1062 GType itype;
1063 guint offset;
1065 if (closure->is_invalid)
1066 return TRUE;
1068 real_closure = G_REAL_CLOSURE (closure);
1070 if (real_closure->meta_marshal == g_type_iface_meta_marshal)
1072 itype = (GType) closure->data;
1073 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1075 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1076 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1077 return callback == NULL;
1079 else if (real_closure->meta_marshal == g_type_class_meta_marshal)
1081 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1083 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1084 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1085 return callback == NULL;
1088 return FALSE;
1091 static void
1092 g_type_iface_meta_marshalv (GClosure *closure,
1093 GValue *return_value,
1094 gpointer instance,
1095 va_list args,
1096 gpointer marshal_data,
1097 int n_params,
1098 GType *param_types)
1100 GRealClosure *real_closure;
1101 GTypeClass *class;
1102 gpointer callback;
1103 GType itype = (GType) closure->data;
1104 guint offset = GPOINTER_TO_UINT (marshal_data);
1106 real_closure = G_REAL_CLOSURE (closure);
1108 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1109 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1110 if (callback)
1111 real_closure->va_marshal (closure,
1112 return_value,
1113 instance, args,
1114 callback,
1115 n_params,
1116 param_types);
1120 * g_signal_type_cclosure_new:
1121 * @itype: the #GType identifier of an interface or classed type
1122 * @struct_offset: the offset of the member function of @itype's class
1123 * structure which is to be invoked by the new closure
1125 * Creates a new closure which invokes the function found at the offset
1126 * @struct_offset in the class structure of the interface or classed type
1127 * identified by @itype.
1129 * Returns: a new #GCClosure
1131 GClosure*
1132 g_signal_type_cclosure_new (GType itype,
1133 guint struct_offset)
1135 GClosure *closure;
1137 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1138 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
1140 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
1141 if (G_TYPE_IS_INTERFACE (itype))
1143 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
1144 g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv);
1146 else
1148 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
1149 g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv);
1151 return closure;
1154 #include <ffi.h>
1155 static ffi_type *
1156 value_to_ffi_type (const GValue *gvalue,
1157 gpointer *value,
1158 gint *enum_tmpval,
1159 gboolean *tmpval_used)
1161 ffi_type *rettype = NULL;
1162 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
1163 g_assert (type != G_TYPE_INVALID);
1165 if (enum_tmpval)
1167 g_assert (tmpval_used != NULL);
1168 *tmpval_used = FALSE;
1171 switch (type)
1173 case G_TYPE_BOOLEAN:
1174 case G_TYPE_CHAR:
1175 case G_TYPE_INT:
1176 rettype = &ffi_type_sint;
1177 *value = (gpointer)&(gvalue->data[0].v_int);
1178 break;
1179 case G_TYPE_ENUM:
1180 /* enums are stored in v_long even though they are integers, which makes
1181 * marshalling through libffi somewhat complicated. They need to be
1182 * marshalled as signed ints, but we need to use a temporary int sized
1183 * value to pass to libffi otherwise it'll pull the wrong value on
1184 * BE machines with 32-bit integers when treating v_long as 32-bit int.
1186 g_assert (enum_tmpval != NULL);
1187 rettype = &ffi_type_sint;
1188 *enum_tmpval = g_value_get_enum (gvalue);
1189 *value = enum_tmpval;
1190 *tmpval_used = TRUE;
1191 break;
1192 case G_TYPE_FLAGS:
1193 g_assert (enum_tmpval != NULL);
1194 rettype = &ffi_type_uint;
1195 *enum_tmpval = g_value_get_flags (gvalue);
1196 *value = enum_tmpval;
1197 *tmpval_used = TRUE;
1198 break;
1199 case G_TYPE_UCHAR:
1200 case G_TYPE_UINT:
1201 rettype = &ffi_type_uint;
1202 *value = (gpointer)&(gvalue->data[0].v_uint);
1203 break;
1204 case G_TYPE_STRING:
1205 case G_TYPE_OBJECT:
1206 case G_TYPE_BOXED:
1207 case G_TYPE_PARAM:
1208 case G_TYPE_POINTER:
1209 case G_TYPE_INTERFACE:
1210 case G_TYPE_VARIANT:
1211 rettype = &ffi_type_pointer;
1212 *value = (gpointer)&(gvalue->data[0].v_pointer);
1213 break;
1214 case G_TYPE_FLOAT:
1215 rettype = &ffi_type_float;
1216 *value = (gpointer)&(gvalue->data[0].v_float);
1217 break;
1218 case G_TYPE_DOUBLE:
1219 rettype = &ffi_type_double;
1220 *value = (gpointer)&(gvalue->data[0].v_double);
1221 break;
1222 case G_TYPE_LONG:
1223 rettype = &ffi_type_slong;
1224 *value = (gpointer)&(gvalue->data[0].v_long);
1225 break;
1226 case G_TYPE_ULONG:
1227 rettype = &ffi_type_ulong;
1228 *value = (gpointer)&(gvalue->data[0].v_ulong);
1229 break;
1230 case G_TYPE_INT64:
1231 rettype = &ffi_type_sint64;
1232 *value = (gpointer)&(gvalue->data[0].v_int64);
1233 break;
1234 case G_TYPE_UINT64:
1235 rettype = &ffi_type_uint64;
1236 *value = (gpointer)&(gvalue->data[0].v_uint64);
1237 break;
1238 default:
1239 rettype = &ffi_type_pointer;
1240 *value = NULL;
1241 g_warning ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1242 break;
1244 return rettype;
1247 static void
1248 value_from_ffi_type (GValue *gvalue, gpointer *value)
1250 ffi_arg *int_val = (ffi_arg*) value;
1252 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
1254 case G_TYPE_INT:
1255 g_value_set_int (gvalue, (gint) *int_val);
1256 break;
1257 case G_TYPE_FLOAT:
1258 g_value_set_float (gvalue, *(gfloat*)value);
1259 break;
1260 case G_TYPE_DOUBLE:
1261 g_value_set_double (gvalue, *(gdouble*)value);
1262 break;
1263 case G_TYPE_BOOLEAN:
1264 g_value_set_boolean (gvalue, (gboolean) *int_val);
1265 break;
1266 case G_TYPE_STRING:
1267 g_value_take_string (gvalue, *(gchar**)value);
1268 break;
1269 case G_TYPE_CHAR:
1270 g_value_set_schar (gvalue, (gint8) *int_val);
1271 break;
1272 case G_TYPE_UCHAR:
1273 g_value_set_uchar (gvalue, (guchar) *int_val);
1274 break;
1275 case G_TYPE_UINT:
1276 g_value_set_uint (gvalue, (guint) *int_val);
1277 break;
1278 case G_TYPE_POINTER:
1279 g_value_set_pointer (gvalue, *(gpointer*)value);
1280 break;
1281 case G_TYPE_LONG:
1282 g_value_set_long (gvalue, (glong) *int_val);
1283 break;
1284 case G_TYPE_ULONG:
1285 g_value_set_ulong (gvalue, (gulong) *int_val);
1286 break;
1287 case G_TYPE_INT64:
1288 g_value_set_int64 (gvalue, (gint64) *int_val);
1289 break;
1290 case G_TYPE_UINT64:
1291 g_value_set_uint64 (gvalue, (guint64) *int_val);
1292 break;
1293 case G_TYPE_BOXED:
1294 g_value_take_boxed (gvalue, *(gpointer*)value);
1295 break;
1296 case G_TYPE_ENUM:
1297 g_value_set_enum (gvalue, (gint) *int_val);
1298 break;
1299 case G_TYPE_FLAGS:
1300 g_value_set_flags (gvalue, (guint) *int_val);
1301 break;
1302 case G_TYPE_PARAM:
1303 g_value_take_param (gvalue, *(gpointer*)value);
1304 break;
1305 case G_TYPE_OBJECT:
1306 g_value_take_object (gvalue, *(gpointer*)value);
1307 break;
1308 case G_TYPE_VARIANT:
1309 g_value_take_variant (gvalue, *(gpointer*)value);
1310 break;
1311 default:
1312 g_warning ("value_from_ffi_type: Unsupported fundamental type: %s",
1313 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
1317 typedef union {
1318 gpointer _gpointer;
1319 float _float;
1320 double _double;
1321 gint _gint;
1322 guint _guint;
1323 glong _glong;
1324 gulong _gulong;
1325 gint64 _gint64;
1326 guint64 _guint64;
1327 } va_arg_storage;
1329 static ffi_type *
1330 va_to_ffi_type (GType gtype,
1331 va_list *va,
1332 va_arg_storage *storage)
1334 ffi_type *rettype = NULL;
1335 GType type = g_type_fundamental (gtype);
1336 g_assert (type != G_TYPE_INVALID);
1338 switch (type)
1340 case G_TYPE_BOOLEAN:
1341 case G_TYPE_CHAR:
1342 case G_TYPE_INT:
1343 case G_TYPE_ENUM:
1344 rettype = &ffi_type_sint;
1345 storage->_gint = va_arg (*va, gint);
1346 break;
1347 case G_TYPE_UCHAR:
1348 case G_TYPE_UINT:
1349 case G_TYPE_FLAGS:
1350 rettype = &ffi_type_uint;
1351 storage->_guint = va_arg (*va, guint);
1352 break;
1353 case G_TYPE_STRING:
1354 case G_TYPE_OBJECT:
1355 case G_TYPE_BOXED:
1356 case G_TYPE_PARAM:
1357 case G_TYPE_POINTER:
1358 case G_TYPE_INTERFACE:
1359 case G_TYPE_VARIANT:
1360 rettype = &ffi_type_pointer;
1361 storage->_gpointer = va_arg (*va, gpointer);
1362 break;
1363 case G_TYPE_FLOAT:
1364 /* Float args are passed as doubles in varargs */
1365 rettype = &ffi_type_float;
1366 storage->_float = (float)va_arg (*va, double);
1367 break;
1368 case G_TYPE_DOUBLE:
1369 rettype = &ffi_type_double;
1370 storage->_double = va_arg (*va, double);
1371 break;
1372 case G_TYPE_LONG:
1373 rettype = &ffi_type_slong;
1374 storage->_glong = va_arg (*va, glong);
1375 break;
1376 case G_TYPE_ULONG:
1377 rettype = &ffi_type_ulong;
1378 storage->_gulong = va_arg (*va, gulong);
1379 break;
1380 case G_TYPE_INT64:
1381 rettype = &ffi_type_sint64;
1382 storage->_gint64 = va_arg (*va, gint64);
1383 break;
1384 case G_TYPE_UINT64:
1385 rettype = &ffi_type_uint64;
1386 storage->_guint64 = va_arg (*va, guint64);
1387 break;
1388 default:
1389 rettype = &ffi_type_pointer;
1390 storage->_guint64 = 0;
1391 g_warning ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1392 break;
1394 return rettype;
1398 * g_cclosure_marshal_generic:
1399 * @closure: A #GClosure.
1400 * @return_gvalue: A #GValue to store the return value. May be %NULL
1401 * if the callback of closure doesn't return a value.
1402 * @n_param_values: The length of the @param_values array.
1403 * @param_values: An array of #GValues holding the arguments
1404 * on which to invoke the callback of closure.
1405 * @invocation_hint: The invocation hint given as the last argument to
1406 * g_closure_invoke().
1407 * @marshal_data: Additional data specified when registering the
1408 * marshaller, see g_closure_set_marshal() and
1409 * g_closure_set_meta_marshal()
1411 * A generic marshaller function implemented via
1412 * [libffi](http://sourceware.org/libffi/).
1414 * Normally this function is not passed explicitly to g_signal_new(),
1415 * but used automatically by GLib when specifying a %NULL marshaller.
1417 * Since: 2.30
1419 void
1420 g_cclosure_marshal_generic (GClosure *closure,
1421 GValue *return_gvalue,
1422 guint n_param_values,
1423 const GValue *param_values,
1424 gpointer invocation_hint,
1425 gpointer marshal_data)
1427 ffi_type *rtype;
1428 void *rvalue;
1429 int n_args;
1430 ffi_type **atypes;
1431 void **args;
1432 int i;
1433 ffi_cif cif;
1434 GCClosure *cc = (GCClosure*) closure;
1435 gint *enum_tmpval;
1436 gboolean tmpval_used = FALSE;
1438 enum_tmpval = g_alloca (sizeof (gint));
1439 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1441 rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used);
1443 else
1445 rtype = &ffi_type_void;
1448 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1450 n_args = n_param_values + 1;
1451 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1452 args = g_alloca (sizeof (gpointer) * n_args);
1454 if (tmpval_used)
1455 enum_tmpval = g_alloca (sizeof (gint));
1457 if (G_CCLOSURE_SWAP_DATA (closure))
1459 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
1460 &args[n_args-1],
1461 enum_tmpval,
1462 &tmpval_used);
1463 atypes[0] = &ffi_type_pointer;
1464 args[0] = &closure->data;
1466 else
1468 atypes[0] = value_to_ffi_type (param_values + 0,
1469 &args[0],
1470 enum_tmpval,
1471 &tmpval_used);
1472 atypes[n_args-1] = &ffi_type_pointer;
1473 args[n_args-1] = &closure->data;
1476 for (i = 1; i < n_args - 1; i++)
1478 if (tmpval_used)
1479 enum_tmpval = g_alloca (sizeof (gint));
1481 atypes[i] = value_to_ffi_type (param_values + i,
1482 &args[i],
1483 enum_tmpval,
1484 &tmpval_used);
1487 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1488 return;
1490 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1492 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1493 value_from_ffi_type (return_gvalue, rvalue);
1497 * g_cclosure_marshal_generic_va:
1498 * @closure: the #GClosure to which the marshaller belongs
1499 * @return_value: (nullable): a #GValue to store the return
1500 * value. May be %NULL if the callback of @closure doesn't return a
1501 * value.
1502 * @instance: (type GObject.TypeInstance): the instance on which the closure is
1503 * invoked.
1504 * @args_list: va_list of arguments to be passed to the closure.
1505 * @marshal_data: (nullable): additional data specified when
1506 * registering the marshaller, see g_closure_set_marshal() and
1507 * g_closure_set_meta_marshal()
1508 * @n_params: the length of the @param_types array
1509 * @param_types: (array length=n_params): the #GType of each argument from
1510 * @args_list.
1512 * A generic #GVaClosureMarshal function implemented via
1513 * [libffi](http://sourceware.org/libffi/).
1515 * Since: 2.30
1517 void
1518 g_cclosure_marshal_generic_va (GClosure *closure,
1519 GValue *return_value,
1520 gpointer instance,
1521 va_list args_list,
1522 gpointer marshal_data,
1523 int n_params,
1524 GType *param_types)
1526 ffi_type *rtype;
1527 void *rvalue;
1528 int n_args;
1529 ffi_type **atypes;
1530 void **args;
1531 va_arg_storage *storage;
1532 int i;
1533 ffi_cif cif;
1534 GCClosure *cc = (GCClosure*) closure;
1535 gint *enum_tmpval;
1536 gboolean tmpval_used = FALSE;
1537 va_list args_copy;
1539 enum_tmpval = g_alloca (sizeof (gint));
1540 if (return_value && G_VALUE_TYPE (return_value))
1542 rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used);
1544 else
1546 rtype = &ffi_type_void;
1549 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1551 n_args = n_params + 2;
1552 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1553 args = g_alloca (sizeof (gpointer) * n_args);
1554 storage = g_alloca (sizeof (va_arg_storage) * n_params);
1556 if (G_CCLOSURE_SWAP_DATA (closure))
1558 atypes[n_args-1] = &ffi_type_pointer;
1559 args[n_args-1] = &instance;
1560 atypes[0] = &ffi_type_pointer;
1561 args[0] = &closure->data;
1563 else
1565 atypes[0] = &ffi_type_pointer;
1566 args[0] = &instance;
1567 atypes[n_args-1] = &ffi_type_pointer;
1568 args[n_args-1] = &closure->data;
1571 G_VA_COPY (args_copy, args_list);
1573 /* Box non-primitive arguments */
1574 for (i = 0; i < n_params; i++)
1576 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1577 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1579 atypes[i+1] = va_to_ffi_type (type,
1580 &args_copy,
1581 &storage[i]);
1582 args[i+1] = &storage[i];
1584 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1586 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1587 storage[i]._gpointer = g_strdup (storage[i]._gpointer);
1588 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1589 storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer);
1590 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1591 storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer);
1592 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1593 storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer);
1595 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1596 storage[i]._gpointer = g_object_ref (storage[i]._gpointer);
1599 va_end (args_copy);
1601 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1602 return;
1604 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1606 /* Unbox non-primitive arguments */
1607 for (i = 0; i < n_params; i++)
1609 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1610 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1612 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1614 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1615 g_free (storage[i]._gpointer);
1616 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1617 g_param_spec_unref (storage[i]._gpointer);
1618 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1619 g_boxed_free (type, storage[i]._gpointer);
1620 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1621 g_variant_unref (storage[i]._gpointer);
1623 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1624 g_object_unref (storage[i]._gpointer);
1627 if (return_value && G_VALUE_TYPE (return_value))
1628 value_from_ffi_type (return_value, rvalue);
1632 * g_cclosure_marshal_VOID__VOID:
1633 * @closure: the #GClosure to which the marshaller belongs
1634 * @return_value: ignored
1635 * @n_param_values: 1
1636 * @param_values: a #GValue array holding only the instance
1637 * @invocation_hint: the invocation hint given as the last argument
1638 * to g_closure_invoke()
1639 * @marshal_data: additional data specified when registering the marshaller
1641 * A marshaller for a #GCClosure with a callback of type
1642 * `void (*callback) (gpointer instance, gpointer user_data)`.
1646 * g_cclosure_marshal_VOID__BOOLEAN:
1647 * @closure: the #GClosure to which the marshaller belongs
1648 * @return_value: ignored
1649 * @n_param_values: 2
1650 * @param_values: a #GValue array holding the instance and the #gboolean parameter
1651 * @invocation_hint: the invocation hint given as the last argument
1652 * to g_closure_invoke()
1653 * @marshal_data: additional data specified when registering the marshaller
1655 * A marshaller for a #GCClosure with a callback of type
1656 * `void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)`.
1660 * g_cclosure_marshal_VOID__CHAR:
1661 * @closure: the #GClosure to which the marshaller belongs
1662 * @return_value: ignored
1663 * @n_param_values: 2
1664 * @param_values: a #GValue array holding the instance and the #gchar parameter
1665 * @invocation_hint: the invocation hint given as the last argument
1666 * to g_closure_invoke()
1667 * @marshal_data: additional data specified when registering the marshaller
1669 * A marshaller for a #GCClosure with a callback of type
1670 * `void (*callback) (gpointer instance, gchar arg1, gpointer user_data)`.
1674 * g_cclosure_marshal_VOID__UCHAR:
1675 * @closure: the #GClosure to which the marshaller belongs
1676 * @return_value: ignored
1677 * @n_param_values: 2
1678 * @param_values: a #GValue array holding the instance and the #guchar parameter
1679 * @invocation_hint: the invocation hint given as the last argument
1680 * to g_closure_invoke()
1681 * @marshal_data: additional data specified when registering the marshaller
1683 * A marshaller for a #GCClosure with a callback of type
1684 * `void (*callback) (gpointer instance, guchar arg1, gpointer user_data)`.
1688 * g_cclosure_marshal_VOID__INT:
1689 * @closure: the #GClosure to which the marshaller belongs
1690 * @return_value: ignored
1691 * @n_param_values: 2
1692 * @param_values: a #GValue array holding the instance and the #gint parameter
1693 * @invocation_hint: the invocation hint given as the last argument
1694 * to g_closure_invoke()
1695 * @marshal_data: additional data specified when registering the marshaller
1697 * A marshaller for a #GCClosure with a callback of type
1698 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)`.
1702 * g_cclosure_marshal_VOID__UINT:
1703 * @closure: the #GClosure to which the marshaller belongs
1704 * @return_value: ignored
1705 * @n_param_values: 2
1706 * @param_values: a #GValue array holding the instance and the #guint parameter
1707 * @invocation_hint: the invocation hint given as the last argument
1708 * to g_closure_invoke()
1709 * @marshal_data: additional data specified when registering the marshaller
1711 * A marshaller for a #GCClosure with a callback of type
1712 * `void (*callback) (gpointer instance, guint arg1, gpointer user_data)`.
1716 * g_cclosure_marshal_VOID__LONG:
1717 * @closure: the #GClosure to which the marshaller belongs
1718 * @return_value: ignored
1719 * @n_param_values: 2
1720 * @param_values: a #GValue array holding the instance and the #glong parameter
1721 * @invocation_hint: the invocation hint given as the last argument
1722 * to g_closure_invoke()
1723 * @marshal_data: additional data specified when registering the marshaller
1725 * A marshaller for a #GCClosure with a callback of type
1726 * `void (*callback) (gpointer instance, glong arg1, gpointer user_data)`.
1730 * g_cclosure_marshal_VOID__ULONG:
1731 * @closure: the #GClosure to which the marshaller belongs
1732 * @return_value: ignored
1733 * @n_param_values: 2
1734 * @param_values: a #GValue array holding the instance and the #gulong parameter
1735 * @invocation_hint: the invocation hint given as the last argument
1736 * to g_closure_invoke()
1737 * @marshal_data: additional data specified when registering the marshaller
1739 * A marshaller for a #GCClosure with a callback of type
1740 * `void (*callback) (gpointer instance, gulong arg1, gpointer user_data)`.
1744 * g_cclosure_marshal_VOID__ENUM:
1745 * @closure: the #GClosure to which the marshaller belongs
1746 * @return_value: ignored
1747 * @n_param_values: 2
1748 * @param_values: a #GValue array holding the instance and the enumeration parameter
1749 * @invocation_hint: the invocation hint given as the last argument
1750 * to g_closure_invoke()
1751 * @marshal_data: additional data specified when registering the marshaller
1753 * A marshaller for a #GCClosure with a callback of type
1754 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes an enumeration type..
1758 * g_cclosure_marshal_VOID__FLAGS:
1759 * @closure: the #GClosure to which the marshaller belongs
1760 * @return_value: ignored
1761 * @n_param_values: 2
1762 * @param_values: a #GValue array holding the instance and the flags parameter
1763 * @invocation_hint: the invocation hint given as the last argument
1764 * to g_closure_invoke()
1765 * @marshal_data: additional data specified when registering the marshaller
1767 * A marshaller for a #GCClosure with a callback of type
1768 * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes a flags type.
1772 * g_cclosure_marshal_VOID__FLOAT:
1773 * @closure: the #GClosure to which the marshaller belongs
1774 * @return_value: ignored
1775 * @n_param_values: 2
1776 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1777 * @invocation_hint: the invocation hint given as the last argument
1778 * to g_closure_invoke()
1779 * @marshal_data: additional data specified when registering the marshaller
1781 * A marshaller for a #GCClosure with a callback of type
1782 * `void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)`.
1786 * g_cclosure_marshal_VOID__DOUBLE:
1787 * @closure: the #GClosure to which the marshaller belongs
1788 * @return_value: ignored
1789 * @n_param_values: 2
1790 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1791 * @invocation_hint: the invocation hint given as the last argument
1792 * to g_closure_invoke()
1793 * @marshal_data: additional data specified when registering the marshaller
1795 * A marshaller for a #GCClosure with a callback of type
1796 * `void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)`.
1800 * g_cclosure_marshal_VOID__STRING:
1801 * @closure: the #GClosure to which the marshaller belongs
1802 * @return_value: ignored
1803 * @n_param_values: 2
1804 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1805 * @invocation_hint: the invocation hint given as the last argument
1806 * to g_closure_invoke()
1807 * @marshal_data: additional data specified when registering the marshaller
1809 * A marshaller for a #GCClosure with a callback of type
1810 * `void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)`.
1814 * g_cclosure_marshal_VOID__PARAM:
1815 * @closure: the #GClosure to which the marshaller belongs
1816 * @return_value: ignored
1817 * @n_param_values: 2
1818 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1819 * @invocation_hint: the invocation hint given as the last argument
1820 * to g_closure_invoke()
1821 * @marshal_data: additional data specified when registering the marshaller
1823 * A marshaller for a #GCClosure with a callback of type
1824 * `void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)`.
1828 * g_cclosure_marshal_VOID__BOXED:
1829 * @closure: the #GClosure to which the marshaller belongs
1830 * @return_value: ignored
1831 * @n_param_values: 2
1832 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1833 * @invocation_hint: the invocation hint given as the last argument
1834 * to g_closure_invoke()
1835 * @marshal_data: additional data specified when registering the marshaller
1837 * A marshaller for a #GCClosure with a callback of type
1838 * `void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)`.
1842 * g_cclosure_marshal_VOID__POINTER:
1843 * @closure: the #GClosure to which the marshaller belongs
1844 * @return_value: ignored
1845 * @n_param_values: 2
1846 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1847 * @invocation_hint: the invocation hint given as the last argument
1848 * to g_closure_invoke()
1849 * @marshal_data: additional data specified when registering the marshaller
1851 * A marshaller for a #GCClosure with a callback of type
1852 * `void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)`.
1856 * g_cclosure_marshal_VOID__OBJECT:
1857 * @closure: the #GClosure to which the marshaller belongs
1858 * @return_value: ignored
1859 * @n_param_values: 2
1860 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1861 * @invocation_hint: the invocation hint given as the last argument
1862 * to g_closure_invoke()
1863 * @marshal_data: additional data specified when registering the marshaller
1865 * A marshaller for a #GCClosure with a callback of type
1866 * `void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)`.
1870 * g_cclosure_marshal_VOID__VARIANT:
1871 * @closure: the #GClosure to which the marshaller belongs
1872 * @return_value: ignored
1873 * @n_param_values: 2
1874 * @param_values: a #GValue array holding the instance and the #GVariant* parameter
1875 * @invocation_hint: the invocation hint given as the last argument
1876 * to g_closure_invoke()
1877 * @marshal_data: additional data specified when registering the marshaller
1879 * A marshaller for a #GCClosure with a callback of type
1880 * `void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)`.
1882 * Since: 2.26
1886 * g_cclosure_marshal_VOID__UINT_POINTER:
1887 * @closure: the #GClosure to which the marshaller belongs
1888 * @return_value: ignored
1889 * @n_param_values: 3
1890 * @param_values: a #GValue array holding instance, arg1 and arg2
1891 * @invocation_hint: the invocation hint given as the last argument
1892 * to g_closure_invoke()
1893 * @marshal_data: additional data specified when registering the marshaller
1895 * A marshaller for a #GCClosure with a callback of type
1896 * `void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)`.
1900 * g_cclosure_marshal_BOOLEAN__FLAGS:
1901 * @closure: the #GClosure to which the marshaller belongs
1902 * @return_value: a #GValue which can store the returned #gboolean
1903 * @n_param_values: 2
1904 * @param_values: a #GValue array holding instance and arg1
1905 * @invocation_hint: the invocation hint given as the last argument
1906 * to g_closure_invoke()
1907 * @marshal_data: additional data specified when registering the marshaller
1909 * A marshaller for a #GCClosure with a callback of type
1910 * `gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter
1911 * denotes a flags type.
1915 * g_cclosure_marshal_BOOL__FLAGS:
1917 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1920 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1921 * @closure: the #GClosure to which the marshaller belongs
1922 * @return_value: a #GValue, which can store the returned string
1923 * @n_param_values: 3
1924 * @param_values: a #GValue array holding instance, arg1 and arg2
1925 * @invocation_hint: the invocation hint given as the last argument
1926 * to g_closure_invoke()
1927 * @marshal_data: additional data specified when registering the marshaller
1929 * A marshaller for a #GCClosure with a callback of type
1930 * `gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)`.
1933 * g_cclosure_marshal_BOOLEAN__OBJECT_BOXED_BOXED:
1934 * @closure: the #GClosure to which the marshaller belongs
1935 * @return_value: a #GValue, which can store the returned string
1936 * @n_param_values: 3
1937 * @param_values: a #GValue array holding instance, arg1 and arg2
1938 * @invocation_hint: the invocation hint given as the last argument
1939 * to g_closure_invoke()
1940 * @marshal_data: additional data specified when registering the marshaller
1942 * A marshaller for a #GCClosure with a callback of type
1943 * `gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)`.
1945 * Since: 2.26