regex: Import PCRE 8.31
[glib.git] / gobject / gclosure.c
blobc0273154ba85be252b86ae03767a965f5f72d5ca
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * MT safe with regards to reference counting.
25 #include "config.h"
27 #include <string.h>
29 #include <ffi.h>
31 #include "gclosure.h"
32 #include "gboxed.h"
33 #include "gobject.h"
34 #include "genums.h"
35 #include "gvalue.h"
36 #include "gvaluetypes.h"
37 #include "gtype-private.h"
40 /**
41 * SECTION:gclosure
42 * @short_description: Functions as first-class objects
43 * @title: Closures
45 * A #GClosure represents a callback supplied by the programmer. It
46 * will generally comprise a function of some kind and a marshaller
47 * used to call it. It is the reponsibility of the marshaller to
48 * convert the arguments for the invocation from #GValue<!-- -->s into
49 * a suitable form, perform the callback on the converted arguments,
50 * and transform the return value back into a #GValue.
52 * In the case of C programs, a closure usually just holds a pointer
53 * to a function and maybe a data argument, and the marshaller
54 * converts between #GValue<!-- --> and native C types. The GObject
55 * library provides the #GCClosure type for this purpose. Bindings for
56 * other languages need marshallers which convert between #GValue<!--
57 * -->s and suitable representations in the runtime of the language in
58 * order to use functions written in that languages as callbacks.
60 * Within GObject, closures play an important role in the
61 * implementation of signals. When a signal is registered, the
62 * @c_marshaller argument to g_signal_new() specifies the default C
63 * marshaller for any closure which is connected to this
64 * signal. GObject provides a number of C marshallers for this
65 * purpose, see the g_cclosure_marshal_*() functions. Additional C
66 * marshallers can be generated with the <link
67 * linkend="glib-genmarshal">glib-genmarshal</link> utility. Closures
68 * 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:
76 * <itemizedlist>
77 * <listitem><para>
78 * Closures allow the callee to get the types of the callback parameters,
79 * which means that language bindings don't have to write individual glue
80 * for each callback type.
81 * </para></listitem>
82 * <listitem><para>
83 * The reference counting of #GClosure makes it easy to handle reentrancy
84 * right; if a callback is removed while it is being invoked, the closure
85 * and its parameters won't be freed until the invocation finishes.
86 * </para></listitem>
87 * <listitem><para>
88 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
89 * automatically removed when the objects they point to go away.
90 * </para></listitem>
91 * </itemizedlist>
94 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
95 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
96 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
97 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
98 #define CLOSURE_N_MFUNCS(cl) (((cl)->n_guards << 1L))
99 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
100 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
101 (cl)->n_fnotifiers + \
102 (cl)->n_inotifiers)
104 typedef union {
105 GClosure closure;
106 volatile gint vint;
107 } ClosureInt;
109 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
110 G_STMT_START { \
111 ClosureInt *cunion = (ClosureInt*) _closure; \
112 gint new_int, old_int, success; \
113 do \
115 ClosureInt tmp; \
116 tmp.vint = old_int = cunion->vint; \
117 _SET_OLD tmp.closure._field; \
118 tmp.closure._field _OP _value; \
119 _SET_NEW tmp.closure._field; \
120 new_int = tmp.vint; \
121 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
123 while (!success && _must_set); \
124 } G_STMT_END
126 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
127 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
128 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
129 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
130 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
131 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
133 #if 0 /* for non-thread-safe closures */
134 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
135 #define SET(cl,f,v) (void) (cl->f = v)
136 #define INC(cl,f) (void) (cl->f += 1)
137 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
138 #define DEC(cl,f) (void) (cl->f -= 1)
139 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
140 #endif
142 enum {
143 FNOTIFY,
144 INOTIFY,
145 PRE_NOTIFY,
146 POST_NOTIFY
150 /* --- functions --- */
152 * g_closure_new_simple:
153 * @sizeof_closure: the size of the structure to allocate, must be at least
154 * <literal>sizeof (GClosure)</literal>
155 * @data: data to store in the @data field of the newly allocated #GClosure
157 * Allocates a struct of the given size and initializes the initial
158 * part as a #GClosure. This function is mainly useful when
159 * implementing new types of closures.
161 * |[
162 * typedef struct _MyClosure MyClosure;
163 * struct _MyClosure
165 * GClosure closure;
166 * // extra data goes here
167 * };
169 * static void
170 * my_closure_finalize (gpointer notify_data,
171 * GClosure *closure)
173 * MyClosure *my_closure = (MyClosure *)closure;
175 * // free extra data here
178 * MyClosure *my_closure_new (gpointer data)
180 * GClosure *closure;
181 * MyClosure *my_closure;
183 * closure = g_closure_new_simple (sizeof (MyClosure), data);
184 * my_closure = (MyClosure *) closure;
186 * // initialize extra data here
188 * g_closure_add_finalize_notifier (closure, notify_data,
189 * my_closure_finalize);
190 * return my_closure;
192 * ]|
194 * Returns: (transfer full): a newly allocated #GClosure
196 GClosure*
197 g_closure_new_simple (guint sizeof_closure,
198 gpointer data)
200 GRealClosure *real_closure;
201 GClosure *closure;
203 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
204 sizeof_closure = sizeof_closure + sizeof (GRealClosure) - sizeof (GClosure);
206 real_closure = g_malloc0 (sizeof_closure);
207 closure = &real_closure->closure;
208 SET (closure, ref_count, 1);
209 SET (closure, floating, TRUE);
210 closure->data = data;
212 return closure;
215 static inline void
216 closure_invoke_notifiers (GClosure *closure,
217 guint notify_type)
219 /* notifier layout:
220 * n_guards n_guards n_fnotif. n_inotifiers
221 * ->[[pre_guards][post_guards][fnotifiers][inotifiers]]
223 * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards;
224 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
226 * constrains/catches:
227 * - closure->notifiers may be reloacted during callback
228 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
229 * - i.e. callbacks can be removed/added during invocation
230 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
231 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
232 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
233 * + none of the callbacks can cause recursion
234 * + closure->n_inotifiers is const 0 during FNOTIFY
236 switch (notify_type)
238 GClosureNotifyData *ndata;
239 guint i, offs;
240 case FNOTIFY:
241 while (closure->n_fnotifiers)
243 guint n;
244 DEC_ASSIGN (closure, n_fnotifiers, &n);
246 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
247 closure->marshal = (GClosureMarshal) ndata->notify;
248 closure->data = ndata->data;
249 ndata->notify (ndata->data, closure);
251 closure->marshal = NULL;
252 closure->data = NULL;
253 break;
254 case INOTIFY:
255 SET (closure, in_inotify, TRUE);
256 while (closure->n_inotifiers)
258 guint n;
259 DEC_ASSIGN (closure, n_inotifiers, &n);
261 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
262 closure->marshal = (GClosureMarshal) ndata->notify;
263 closure->data = ndata->data;
264 ndata->notify (ndata->data, closure);
266 closure->marshal = NULL;
267 closure->data = NULL;
268 SET (closure, in_inotify, FALSE);
269 break;
270 case PRE_NOTIFY:
271 i = closure->n_guards;
272 offs = 0;
273 while (i--)
275 ndata = closure->notifiers + offs + i;
276 ndata->notify (ndata->data, closure);
278 break;
279 case POST_NOTIFY:
280 i = closure->n_guards;
281 offs = i;
282 while (i--)
284 ndata = closure->notifiers + offs + i;
285 ndata->notify (ndata->data, closure);
287 break;
291 static void
292 g_closure_set_meta_va_marshal (GClosure *closure,
293 GVaClosureMarshal va_meta_marshal)
295 GRealClosure *real_closure;
297 g_return_if_fail (closure != NULL);
298 g_return_if_fail (va_meta_marshal != NULL);
299 g_return_if_fail (closure->is_invalid == FALSE);
300 g_return_if_fail (closure->in_marshal == FALSE);
302 real_closure = G_REAL_CLOSURE (closure);
304 g_return_if_fail (real_closure->meta_marshal != NULL);
306 real_closure->va_meta_marshal = va_meta_marshal;
310 * g_closure_set_meta_marshal: (skip)
311 * @closure: a #GClosure
312 * @marshal_data: context-dependent data to pass to @meta_marshal
313 * @meta_marshal: a #GClosureMarshal function
315 * Sets the meta marshaller of @closure. A meta marshaller wraps
316 * @closure->marshal and modifies the way it is called in some
317 * fashion. The most common use of this facility is for C callbacks.
318 * The same marshallers (generated by <link
319 * linkend="glib-genmarshal">glib-genmarshal</link>) are used
320 * everywhere, but the way that we get the callback function
321 * differs. In most cases we want to use @closure->callback, but in
322 * other cases we want to use some different technique to retrieve the
323 * callback function.
325 * For example, class closures for signals (see
326 * g_signal_type_cclosure_new()) retrieve the callback function from a
327 * fixed offset in the class structure. The meta marshaller retrieves
328 * the right callback and passes it to the marshaller as the
329 * @marshal_data argument.
331 void
332 g_closure_set_meta_marshal (GClosure *closure,
333 gpointer marshal_data,
334 GClosureMarshal meta_marshal)
336 GRealClosure *real_closure;
338 g_return_if_fail (closure != NULL);
339 g_return_if_fail (meta_marshal != NULL);
340 g_return_if_fail (closure->is_invalid == FALSE);
341 g_return_if_fail (closure->in_marshal == FALSE);
343 real_closure = G_REAL_CLOSURE (closure);
345 g_return_if_fail (real_closure->meta_marshal == NULL);
347 real_closure->meta_marshal = meta_marshal;
348 real_closure->meta_marshal_data = marshal_data;
352 * g_closure_add_marshal_guards: (skip)
353 * @closure: a #GClosure
354 * @pre_marshal_data: data to pass to @pre_marshal_notify
355 * @pre_marshal_notify: a function to call before the closure callback
356 * @post_marshal_data: data to pass to @post_marshal_notify
357 * @post_marshal_notify: a function to call after the closure callback
359 * Adds a pair of notifiers which get invoked before and after the
360 * closure callback, respectively. This is typically used to protect
361 * the extra arguments for the duration of the callback. See
362 * g_object_watch_closure() for an example of marshal guards.
364 void
365 g_closure_add_marshal_guards (GClosure *closure,
366 gpointer pre_marshal_data,
367 GClosureNotify pre_marshal_notify,
368 gpointer post_marshal_data,
369 GClosureNotify post_marshal_notify)
371 guint i;
373 g_return_if_fail (closure != NULL);
374 g_return_if_fail (pre_marshal_notify != NULL);
375 g_return_if_fail (post_marshal_notify != NULL);
376 g_return_if_fail (closure->is_invalid == FALSE);
377 g_return_if_fail (closure->in_marshal == FALSE);
378 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
380 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
381 if (closure->n_inotifiers)
382 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
383 closure->n_fnotifiers +
384 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
385 closure->n_fnotifiers + 0)];
386 if (closure->n_inotifiers > 1)
387 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
388 closure->n_fnotifiers +
389 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
390 closure->n_fnotifiers + 1)];
391 if (closure->n_fnotifiers)
392 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
393 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
394 if (closure->n_fnotifiers > 1)
395 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
396 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
397 if (closure->n_guards)
398 closure->notifiers[(closure->n_guards +
399 closure->n_guards + 1)] = closure->notifiers[closure->n_guards];
400 i = closure->n_guards;
401 closure->notifiers[i].data = pre_marshal_data;
402 closure->notifiers[i].notify = pre_marshal_notify;
403 closure->notifiers[i + 1].data = post_marshal_data;
404 closure->notifiers[i + 1].notify = post_marshal_notify;
405 INC (closure, n_guards);
409 * g_closure_add_finalize_notifier: (skip)
410 * @closure: a #GClosure
411 * @notify_data: data to pass to @notify_func
412 * @notify_func: the callback function to register
414 * Registers a finalization notifier which will be called when the
415 * reference count of @closure goes down to 0. Multiple finalization
416 * notifiers on a single closure are invoked in unspecified order. If
417 * a single call to g_closure_unref() results in the closure being
418 * both invalidated and finalized, then the invalidate notifiers will
419 * be run before the finalize notifiers.
421 void
422 g_closure_add_finalize_notifier (GClosure *closure,
423 gpointer notify_data,
424 GClosureNotify notify_func)
426 guint i;
428 g_return_if_fail (closure != NULL);
429 g_return_if_fail (notify_func != NULL);
430 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
432 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
433 if (closure->n_inotifiers)
434 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
435 closure->n_fnotifiers +
436 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
437 closure->n_fnotifiers + 0)];
438 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
439 closure->notifiers[i].data = notify_data;
440 closure->notifiers[i].notify = notify_func;
441 INC (closure, n_fnotifiers);
445 * g_closure_add_invalidate_notifier: (skip)
446 * @closure: a #GClosure
447 * @notify_data: data to pass to @notify_func
448 * @notify_func: the callback function to register
450 * Registers an invalidation notifier which will be called when the
451 * @closure is invalidated with g_closure_invalidate(). Invalidation
452 * notifiers are invoked before finalization notifiers, in an
453 * unspecified order.
455 void
456 g_closure_add_invalidate_notifier (GClosure *closure,
457 gpointer notify_data,
458 GClosureNotify notify_func)
460 guint i;
462 g_return_if_fail (closure != NULL);
463 g_return_if_fail (notify_func != NULL);
464 g_return_if_fail (closure->is_invalid == FALSE);
465 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
467 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
468 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
469 closure->notifiers[i].data = notify_data;
470 closure->notifiers[i].notify = notify_func;
471 INC (closure, n_inotifiers);
474 static inline gboolean
475 closure_try_remove_inotify (GClosure *closure,
476 gpointer notify_data,
477 GClosureNotify notify_func)
479 GClosureNotifyData *ndata, *nlast;
481 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
482 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
483 if (ndata->notify == notify_func && ndata->data == notify_data)
485 DEC (closure, n_inotifiers);
486 if (ndata < nlast)
487 *ndata = *nlast;
489 return TRUE;
491 return FALSE;
494 static inline gboolean
495 closure_try_remove_fnotify (GClosure *closure,
496 gpointer notify_data,
497 GClosureNotify notify_func)
499 GClosureNotifyData *ndata, *nlast;
501 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
502 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
503 if (ndata->notify == notify_func && ndata->data == notify_data)
505 DEC (closure, n_fnotifiers);
506 if (ndata < nlast)
507 *ndata = *nlast;
508 if (closure->n_inotifiers)
509 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
510 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
511 closure->n_fnotifiers +
512 closure->n_inotifiers)];
513 return TRUE;
515 return FALSE;
519 * g_closure_ref:
520 * @closure: #GClosure to increment the reference count on
522 * Increments the reference count on a closure to force it staying
523 * alive while the caller holds a pointer to it.
525 * Returns: (transfer none): The @closure passed in, for convenience
527 GClosure*
528 g_closure_ref (GClosure *closure)
530 guint new_ref_count;
531 g_return_val_if_fail (closure != NULL, NULL);
532 g_return_val_if_fail (closure->ref_count > 0, NULL);
533 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
535 INC_ASSIGN (closure, ref_count, &new_ref_count);
536 g_return_val_if_fail (new_ref_count > 1, NULL);
538 return closure;
542 * g_closure_invalidate:
543 * @closure: GClosure to invalidate
545 * Sets a flag on the closure to indicate that its calling
546 * environment has become invalid, and thus causes any future
547 * invocations of g_closure_invoke() on this @closure to be
548 * ignored. Also, invalidation notifiers installed on the closure will
549 * be called at this point. Note that unless you are holding a
550 * reference to the closure yourself, the invalidation notifiers may
551 * unref the closure and cause it to be destroyed, so if you need to
552 * access the closure after calling g_closure_invalidate(), make sure
553 * that you've previously called g_closure_ref().
555 * Note that g_closure_invalidate() will also be called when the
556 * reference count of a closure drops to zero (unless it has already
557 * been invalidated before).
559 void
560 g_closure_invalidate (GClosure *closure)
562 g_return_if_fail (closure != NULL);
564 if (!closure->is_invalid)
566 gboolean was_invalid;
567 g_closure_ref (closure); /* preserve floating flag */
568 SWAP (closure, is_invalid, TRUE, &was_invalid);
569 /* invalidate only once */
570 if (!was_invalid)
571 closure_invoke_notifiers (closure, INOTIFY);
572 g_closure_unref (closure);
577 * g_closure_unref:
578 * @closure: #GClosure to decrement the reference count on
580 * Decrements the reference count of a closure after it was previously
581 * incremented by the same caller. If no other callers are using the
582 * closure, then the closure will be destroyed and freed.
584 void
585 g_closure_unref (GClosure *closure)
587 guint new_ref_count;
589 g_return_if_fail (closure != NULL);
590 g_return_if_fail (closure->ref_count > 0);
592 if (closure->ref_count == 1) /* last unref, invalidate first */
593 g_closure_invalidate (closure);
595 DEC_ASSIGN (closure, ref_count, &new_ref_count);
597 if (new_ref_count == 0)
599 closure_invoke_notifiers (closure, FNOTIFY);
600 g_free (closure->notifiers);
601 g_free (G_REAL_CLOSURE (closure));
606 * g_closure_sink:
607 * @closure: #GClosure to decrement the initial reference count on, if it's
608 * still being held
610 * Takes over the initial ownership of a closure. Each closure is
611 * initially created in a <firstterm>floating</firstterm> state, which
612 * means that the initial reference count is not owned by any caller.
613 * g_closure_sink() checks to see if the object is still floating, and
614 * if so, unsets the floating state and decreases the reference
615 * count. If the closure is not floating, g_closure_sink() does
616 * nothing. The reason for the existence of the floating state is to
617 * prevent cumbersome code sequences like:
618 * |[
619 * closure = g_cclosure_new (cb_func, cb_data);
620 * g_source_set_closure (source, closure);
621 * g_closure_unref (closure); // XXX GObject doesn't really need this
622 * ]|
623 * Because g_source_set_closure() (and similar functions) take ownership of the
624 * initial reference count, if it is unowned, we instead can write:
625 * |[
626 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
627 * ]|
629 * Generally, this function is used together with g_closure_ref(). Ane example
630 * of storing a closure for later notification looks like:
631 * |[
632 * static GClosure *notify_closure = NULL;
633 * void
634 * foo_notify_set_closure (GClosure *closure)
636 * if (notify_closure)
637 * g_closure_unref (notify_closure);
638 * notify_closure = closure;
639 * if (notify_closure)
641 * g_closure_ref (notify_closure);
642 * g_closure_sink (notify_closure);
645 * ]|
647 * Because g_closure_sink() may decrement the reference count of a closure
648 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
649 * g_closure_ref() should be called prior to this function.
651 void
652 g_closure_sink (GClosure *closure)
654 g_return_if_fail (closure != NULL);
655 g_return_if_fail (closure->ref_count > 0);
657 /* floating is basically a kludge to avoid creating closures
658 * with a ref_count of 0. so the initial ref_count a closure has
659 * is unowned. with invoking g_closure_sink() code may
660 * indicate that it takes over that intiial ref_count.
662 if (closure->floating)
664 gboolean was_floating;
665 SWAP (closure, floating, FALSE, &was_floating);
666 /* unref floating flag only once */
667 if (was_floating)
668 g_closure_unref (closure);
673 * g_closure_remove_invalidate_notifier: (skip)
674 * @closure: a #GClosure
675 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
676 * when registering @notify_func
677 * @notify_func: the callback function to remove
679 * Removes an invalidation notifier.
681 * Notice that notifiers are automatically removed after they are run.
683 void
684 g_closure_remove_invalidate_notifier (GClosure *closure,
685 gpointer notify_data,
686 GClosureNotify notify_func)
688 g_return_if_fail (closure != NULL);
689 g_return_if_fail (notify_func != NULL);
691 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */
692 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
693 closure->data == notify_data)
694 closure->marshal = NULL;
695 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
696 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
697 notify_func, notify_data);
701 * g_closure_remove_finalize_notifier: (skip)
702 * @closure: a #GClosure
703 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
704 * when registering @notify_func
705 * @notify_func: the callback function to remove
707 * Removes a finalization notifier.
709 * Notice that notifiers are automatically removed after they are run.
711 void
712 g_closure_remove_finalize_notifier (GClosure *closure,
713 gpointer notify_data,
714 GClosureNotify notify_func)
716 g_return_if_fail (closure != NULL);
717 g_return_if_fail (notify_func != NULL);
719 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */
720 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
721 closure->data == notify_data)
722 closure->marshal = NULL;
723 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
724 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
725 notify_func, notify_data);
729 * g_closure_invoke:
730 * @closure: a #GClosure
731 * @return_value: (allow-none): a #GValue to store the return
732 * value. May be %NULL if the callback of @closure
733 * doesn't return a value.
734 * @n_param_values: the length of the @param_values array
735 * @param_values: (array length=n_param_values): an array of
736 * #GValue<!-- -->s holding the arguments on which to
737 * invoke the callback of @closure
738 * @invocation_hint: (allow-none): a context-dependent invocation hint
740 * Invokes the closure, i.e. executes the callback represented by the @closure.
742 void
743 g_closure_invoke (GClosure *closure,
744 GValue /*out*/ *return_value,
745 guint n_param_values,
746 const GValue *param_values,
747 gpointer invocation_hint)
749 GRealClosure *real_closure;
751 g_return_if_fail (closure != NULL);
753 real_closure = G_REAL_CLOSURE (closure);
755 g_closure_ref (closure); /* preserve floating flag */
756 if (!closure->is_invalid)
758 GClosureMarshal marshal;
759 gpointer marshal_data;
760 gboolean in_marshal = closure->in_marshal;
762 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
764 SET (closure, in_marshal, TRUE);
765 if (real_closure->meta_marshal)
767 marshal_data = real_closure->meta_marshal_data;
768 marshal = real_closure->meta_marshal;
770 else
772 marshal_data = NULL;
773 marshal = closure->marshal;
775 if (!in_marshal)
776 closure_invoke_notifiers (closure, PRE_NOTIFY);
777 marshal (closure,
778 return_value,
779 n_param_values, param_values,
780 invocation_hint,
781 marshal_data);
782 if (!in_marshal)
783 closure_invoke_notifiers (closure, POST_NOTIFY);
784 SET (closure, in_marshal, in_marshal);
786 g_closure_unref (closure);
789 gboolean
790 _g_closure_supports_invoke_va (GClosure *closure)
792 GRealClosure *real_closure;
794 g_return_val_if_fail (closure != NULL, FALSE);
796 real_closure = G_REAL_CLOSURE (closure);
798 return
799 real_closure->va_marshal != NULL &&
800 (real_closure->meta_marshal == NULL ||
801 real_closure->va_meta_marshal != NULL);
804 void
805 _g_closure_invoke_va (GClosure *closure,
806 GValue /*out*/ *return_value,
807 gpointer instance,
808 va_list args,
809 int n_params,
810 GType *param_types)
812 GRealClosure *real_closure;
814 g_return_if_fail (closure != NULL);
816 real_closure = G_REAL_CLOSURE (closure);
818 g_closure_ref (closure); /* preserve floating flag */
819 if (!closure->is_invalid)
821 GVaClosureMarshal marshal;
822 gpointer marshal_data;
823 gboolean in_marshal = closure->in_marshal;
825 g_return_if_fail (closure->marshal || real_closure->meta_marshal);
827 SET (closure, in_marshal, TRUE);
828 if (real_closure->va_meta_marshal)
830 marshal_data = real_closure->meta_marshal_data;
831 marshal = real_closure->va_meta_marshal;
833 else
835 marshal_data = NULL;
836 marshal = real_closure->va_marshal;
838 if (!in_marshal)
839 closure_invoke_notifiers (closure, PRE_NOTIFY);
840 marshal (closure,
841 return_value,
842 instance, args,
843 marshal_data,
844 n_params, param_types);
845 if (!in_marshal)
846 closure_invoke_notifiers (closure, POST_NOTIFY);
847 SET (closure, in_marshal, in_marshal);
849 g_closure_unref (closure);
854 * g_closure_set_marshal: (skip)
855 * @closure: a #GClosure
856 * @marshal: a #GClosureMarshal function
858 * Sets the marshaller of @closure. The <literal>marshal_data</literal>
859 * of @marshal provides a way for a meta marshaller to provide additional
860 * information to the marshaller. (See g_closure_set_meta_marshal().) For
861 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
862 * functions), what it provides is a callback function to use instead of
863 * @closure->callback.
865 void
866 g_closure_set_marshal (GClosure *closure,
867 GClosureMarshal marshal)
869 g_return_if_fail (closure != NULL);
870 g_return_if_fail (marshal != NULL);
872 if (closure->marshal && closure->marshal != marshal)
873 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
874 closure->marshal, marshal);
875 else
876 closure->marshal = marshal;
879 void
880 _g_closure_set_va_marshal (GClosure *closure,
881 GVaClosureMarshal marshal)
883 GRealClosure *real_closure;
885 g_return_if_fail (closure != NULL);
886 g_return_if_fail (marshal != NULL);
888 real_closure = G_REAL_CLOSURE (closure);
890 if (real_closure->va_marshal && real_closure->va_marshal != marshal)
891 g_warning ("attempt to override closure->va_marshal (%p) with new marshal (%p)",
892 real_closure->va_marshal, marshal);
893 else
894 real_closure->va_marshal = marshal;
898 * g_cclosure_new: (skip)
899 * @callback_func: the function to invoke
900 * @user_data: user data to pass to @callback_func
901 * @destroy_data: destroy notify to be called when @user_data is no longer used
903 * Creates a new closure which invokes @callback_func with @user_data as
904 * the last parameter.
906 * Returns: a new #GCClosure
908 GClosure*
909 g_cclosure_new (GCallback callback_func,
910 gpointer user_data,
911 GClosureNotify destroy_data)
913 GClosure *closure;
915 g_return_val_if_fail (callback_func != NULL, NULL);
917 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
918 if (destroy_data)
919 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
920 ((GCClosure*) closure)->callback = (gpointer) callback_func;
922 return closure;
926 * g_cclosure_new_swap: (skip)
927 * @callback_func: the function to invoke
928 * @user_data: user data to pass to @callback_func
929 * @destroy_data: destroy notify to be called when @user_data is no longer used
931 * Creates a new closure which invokes @callback_func with @user_data as
932 * the first parameter.
934 * Returns: (transfer full): a new #GCClosure
936 GClosure*
937 g_cclosure_new_swap (GCallback callback_func,
938 gpointer user_data,
939 GClosureNotify destroy_data)
941 GClosure *closure;
943 g_return_val_if_fail (callback_func != NULL, NULL);
945 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
946 if (destroy_data)
947 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
948 ((GCClosure*) closure)->callback = (gpointer) callback_func;
949 SET (closure, derivative_flag, TRUE);
951 return closure;
954 static void
955 g_type_class_meta_marshal (GClosure *closure,
956 GValue /*out*/ *return_value,
957 guint n_param_values,
958 const GValue *param_values,
959 gpointer invocation_hint,
960 gpointer marshal_data)
962 GTypeClass *class;
963 gpointer callback;
964 /* GType itype = (GType) closure->data; */
965 guint offset = GPOINTER_TO_UINT (marshal_data);
967 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
968 callback = G_STRUCT_MEMBER (gpointer, class, offset);
969 if (callback)
970 closure->marshal (closure,
971 return_value,
972 n_param_values, param_values,
973 invocation_hint,
974 callback);
977 static void
978 g_type_class_meta_marshalv (GClosure *closure,
979 GValue *return_value,
980 gpointer instance,
981 va_list args,
982 gpointer marshal_data,
983 int n_params,
984 GType *param_types)
986 GRealClosure *real_closure;
987 GTypeClass *class;
988 gpointer callback;
989 /* GType itype = (GType) closure->data; */
990 guint offset = GPOINTER_TO_UINT (marshal_data);
992 real_closure = G_REAL_CLOSURE (closure);
994 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
995 callback = G_STRUCT_MEMBER (gpointer, class, offset);
996 if (callback)
997 real_closure->va_marshal (closure,
998 return_value,
999 instance, args,
1000 callback,
1001 n_params,
1002 param_types);
1005 static void
1006 g_type_iface_meta_marshal (GClosure *closure,
1007 GValue /*out*/ *return_value,
1008 guint n_param_values,
1009 const GValue *param_values,
1010 gpointer invocation_hint,
1011 gpointer marshal_data)
1013 GTypeClass *class;
1014 gpointer callback;
1015 GType itype = (GType) closure->data;
1016 guint offset = GPOINTER_TO_UINT (marshal_data);
1018 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
1019 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1020 if (callback)
1021 closure->marshal (closure,
1022 return_value,
1023 n_param_values, param_values,
1024 invocation_hint,
1025 callback);
1028 gboolean
1029 _g_closure_is_void (GClosure *closure,
1030 gpointer instance)
1032 GRealClosure *real_closure;
1033 GTypeClass *class;
1034 gpointer callback;
1035 GType itype;
1036 guint offset;
1038 if (closure->is_invalid)
1039 return TRUE;
1041 real_closure = G_REAL_CLOSURE (closure);
1043 if (real_closure->meta_marshal == g_type_iface_meta_marshal)
1045 itype = (GType) closure->data;
1046 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1048 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1049 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1050 return callback == NULL;
1052 else if (real_closure->meta_marshal == g_type_class_meta_marshal)
1054 offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data);
1056 class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass);
1057 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1058 return callback == NULL;
1061 return FALSE;
1064 static void
1065 g_type_iface_meta_marshalv (GClosure *closure,
1066 GValue *return_value,
1067 gpointer instance,
1068 va_list args,
1069 gpointer marshal_data,
1070 int n_params,
1071 GType *param_types)
1073 GRealClosure *real_closure;
1074 GTypeClass *class;
1075 gpointer callback;
1076 GType itype = (GType) closure->data;
1077 guint offset = GPOINTER_TO_UINT (marshal_data);
1079 real_closure = G_REAL_CLOSURE (closure);
1081 class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass);
1082 callback = G_STRUCT_MEMBER (gpointer, class, offset);
1083 if (callback)
1084 real_closure->va_marshal (closure,
1085 return_value,
1086 instance, args,
1087 callback,
1088 n_params,
1089 param_types);
1093 * g_signal_type_cclosure_new:
1094 * @itype: the #GType identifier of an interface or classed type
1095 * @struct_offset: the offset of the member function of @itype's class
1096 * structure which is to be invoked by the new closure
1098 * Creates a new closure which invokes the function found at the offset
1099 * @struct_offset in the class structure of the interface or classed type
1100 * identified by @itype.
1102 * Returns: a new #GCClosure
1104 GClosure*
1105 g_signal_type_cclosure_new (GType itype,
1106 guint struct_offset)
1108 GClosure *closure;
1110 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1111 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
1113 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
1114 if (G_TYPE_IS_INTERFACE (itype))
1116 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
1117 g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv);
1119 else
1121 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
1122 g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv);
1124 return closure;
1127 #include <ffi.h>
1128 static ffi_type *
1129 value_to_ffi_type (const GValue *gvalue,
1130 gpointer *value,
1131 gint *enum_tmpval,
1132 gboolean *tmpval_used)
1134 ffi_type *rettype = NULL;
1135 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
1136 g_assert (type != G_TYPE_INVALID);
1138 if (enum_tmpval)
1140 g_assert (tmpval_used != NULL);
1141 *tmpval_used = FALSE;
1144 switch (type)
1146 case G_TYPE_BOOLEAN:
1147 case G_TYPE_CHAR:
1148 case G_TYPE_INT:
1149 rettype = &ffi_type_sint;
1150 *value = (gpointer)&(gvalue->data[0].v_int);
1151 break;
1152 case G_TYPE_ENUM:
1153 /* enums are stored in v_long even though they are integers, which makes
1154 * marshalling through libffi somewhat complicated. They need to be
1155 * marshalled as signed ints, but we need to use a temporary int sized
1156 * value to pass to libffi otherwise it'll pull the wrong value on
1157 * BE machines with 32-bit integers when treating v_long as 32-bit int.
1159 g_assert (enum_tmpval != NULL);
1160 rettype = &ffi_type_sint;
1161 *enum_tmpval = g_value_get_enum (gvalue);
1162 *value = enum_tmpval;
1163 *tmpval_used = TRUE;
1164 break;
1165 case G_TYPE_UCHAR:
1166 case G_TYPE_UINT:
1167 case G_TYPE_FLAGS:
1168 rettype = &ffi_type_uint;
1169 *value = (gpointer)&(gvalue->data[0].v_uint);
1170 break;
1171 case G_TYPE_STRING:
1172 case G_TYPE_OBJECT:
1173 case G_TYPE_BOXED:
1174 case G_TYPE_PARAM:
1175 case G_TYPE_POINTER:
1176 case G_TYPE_INTERFACE:
1177 case G_TYPE_VARIANT:
1178 rettype = &ffi_type_pointer;
1179 *value = (gpointer)&(gvalue->data[0].v_pointer);
1180 break;
1181 case G_TYPE_FLOAT:
1182 rettype = &ffi_type_float;
1183 *value = (gpointer)&(gvalue->data[0].v_float);
1184 break;
1185 case G_TYPE_DOUBLE:
1186 rettype = &ffi_type_double;
1187 *value = (gpointer)&(gvalue->data[0].v_double);
1188 break;
1189 case G_TYPE_LONG:
1190 rettype = &ffi_type_slong;
1191 *value = (gpointer)&(gvalue->data[0].v_long);
1192 break;
1193 case G_TYPE_ULONG:
1194 rettype = &ffi_type_ulong;
1195 *value = (gpointer)&(gvalue->data[0].v_ulong);
1196 break;
1197 case G_TYPE_INT64:
1198 rettype = &ffi_type_sint64;
1199 *value = (gpointer)&(gvalue->data[0].v_int64);
1200 break;
1201 case G_TYPE_UINT64:
1202 rettype = &ffi_type_uint64;
1203 *value = (gpointer)&(gvalue->data[0].v_uint64);
1204 break;
1205 default:
1206 rettype = &ffi_type_pointer;
1207 *value = NULL;
1208 g_warning ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1209 break;
1211 return rettype;
1214 static void
1215 value_from_ffi_type (GValue *gvalue, gpointer *value)
1217 ffi_arg *int_val = (ffi_arg*) value;
1219 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
1221 case G_TYPE_INT:
1222 g_value_set_int (gvalue, (gint) *int_val);
1223 break;
1224 case G_TYPE_FLOAT:
1225 g_value_set_float (gvalue, *(gfloat*)value);
1226 break;
1227 case G_TYPE_DOUBLE:
1228 g_value_set_double (gvalue, *(gdouble*)value);
1229 break;
1230 case G_TYPE_BOOLEAN:
1231 g_value_set_boolean (gvalue, (gboolean) *int_val);
1232 break;
1233 case G_TYPE_STRING:
1234 g_value_set_string (gvalue, *(gchar**)value);
1235 break;
1236 case G_TYPE_CHAR:
1237 g_value_set_schar (gvalue, (gint8) *int_val);
1238 break;
1239 case G_TYPE_UCHAR:
1240 g_value_set_uchar (gvalue, (guchar) *int_val);
1241 break;
1242 case G_TYPE_UINT:
1243 g_value_set_uint (gvalue, (guint) *int_val);
1244 break;
1245 case G_TYPE_POINTER:
1246 g_value_set_pointer (gvalue, *(gpointer*)value);
1247 break;
1248 case G_TYPE_LONG:
1249 g_value_set_long (gvalue, (glong) *int_val);
1250 break;
1251 case G_TYPE_ULONG:
1252 g_value_set_ulong (gvalue, (gulong) *int_val);
1253 break;
1254 case G_TYPE_INT64:
1255 g_value_set_int64 (gvalue, (gint64) *int_val);
1256 break;
1257 case G_TYPE_UINT64:
1258 g_value_set_uint64 (gvalue, (guint64) *int_val);
1259 break;
1260 case G_TYPE_BOXED:
1261 g_value_set_boxed (gvalue, *(gpointer*)value);
1262 break;
1263 case G_TYPE_ENUM:
1264 g_value_set_enum (gvalue, (gint) *int_val);
1265 break;
1266 case G_TYPE_FLAGS:
1267 g_value_set_flags (gvalue, (guint) *int_val);
1268 break;
1269 case G_TYPE_PARAM:
1270 g_value_set_param (gvalue, *(gpointer*)value);
1271 break;
1272 case G_TYPE_OBJECT:
1273 g_value_set_object (gvalue, *(gpointer*)value);
1274 break;
1275 case G_TYPE_VARIANT:
1276 g_value_set_variant (gvalue, *(gpointer*)value);
1277 break;
1278 default:
1279 g_warning ("value_from_ffi_type: Unsupported fundamental type: %s",
1280 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
1284 typedef union {
1285 gpointer _gpointer;
1286 float _float;
1287 double _double;
1288 gint _gint;
1289 guint _guint;
1290 glong _glong;
1291 gulong _gulong;
1292 gint64 _gint64;
1293 guint64 _guint64;
1294 } va_arg_storage;
1296 static ffi_type *
1297 va_to_ffi_type (GType gtype,
1298 va_list *va,
1299 va_arg_storage *storage)
1301 ffi_type *rettype = NULL;
1302 GType type = g_type_fundamental (gtype);
1303 g_assert (type != G_TYPE_INVALID);
1305 switch (type)
1307 case G_TYPE_BOOLEAN:
1308 case G_TYPE_CHAR:
1309 case G_TYPE_INT:
1310 case G_TYPE_ENUM:
1311 rettype = &ffi_type_sint;
1312 storage->_gint = va_arg (*va, gint);
1313 break;
1314 case G_TYPE_UCHAR:
1315 case G_TYPE_UINT:
1316 case G_TYPE_FLAGS:
1317 rettype = &ffi_type_uint;
1318 storage->_guint = va_arg (*va, guint);
1319 break;
1320 case G_TYPE_STRING:
1321 case G_TYPE_OBJECT:
1322 case G_TYPE_BOXED:
1323 case G_TYPE_PARAM:
1324 case G_TYPE_POINTER:
1325 case G_TYPE_INTERFACE:
1326 case G_TYPE_VARIANT:
1327 rettype = &ffi_type_pointer;
1328 storage->_gpointer = va_arg (*va, gpointer);
1329 break;
1330 case G_TYPE_FLOAT:
1331 /* Float args are passed as doubles in varargs */
1332 rettype = &ffi_type_float;
1333 storage->_float = (float)va_arg (*va, double);
1334 break;
1335 case G_TYPE_DOUBLE:
1336 rettype = &ffi_type_double;
1337 storage->_double = va_arg (*va, double);
1338 break;
1339 case G_TYPE_LONG:
1340 rettype = &ffi_type_slong;
1341 storage->_glong = va_arg (*va, glong);
1342 break;
1343 case G_TYPE_ULONG:
1344 rettype = &ffi_type_ulong;
1345 storage->_gulong = va_arg (*va, gulong);
1346 break;
1347 case G_TYPE_INT64:
1348 rettype = &ffi_type_sint64;
1349 storage->_gint64 = va_arg (*va, gint64);
1350 break;
1351 case G_TYPE_UINT64:
1352 rettype = &ffi_type_uint64;
1353 storage->_guint64 = va_arg (*va, guint64);
1354 break;
1355 default:
1356 rettype = &ffi_type_pointer;
1357 storage->_guint64 = 0;
1358 g_warning ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));
1359 break;
1361 return rettype;
1365 * g_cclosure_marshal_generic:
1366 * @closure: A #GClosure.
1367 * @return_gvalue: A #GValue to store the return value. May be %NULL
1368 * if the callback of closure doesn't return a value.
1369 * @n_param_values: The length of the @param_values array.
1370 * @param_values: An array of #GValue<!-- -->s holding the arguments
1371 * on which to invoke the callback of closure.
1372 * @invocation_hint: The invocation hint given as the last argument to
1373 * g_closure_invoke().
1374 * @marshal_data: Additional data specified when registering the
1375 * marshaller, see g_closure_set_marshal() and
1376 * g_closure_set_meta_marshal()
1378 * A generic marshaller function implemented via <ulink
1379 * url="http://sourceware.org/libffi/">libffi</ulink>.
1381 * Since: 2.30
1383 void
1384 g_cclosure_marshal_generic (GClosure *closure,
1385 GValue *return_gvalue,
1386 guint n_param_values,
1387 const GValue *param_values,
1388 gpointer invocation_hint,
1389 gpointer marshal_data)
1391 ffi_type *rtype;
1392 void *rvalue;
1393 int n_args;
1394 ffi_type **atypes;
1395 void **args;
1396 int i;
1397 ffi_cif cif;
1398 GCClosure *cc = (GCClosure*) closure;
1399 gint *enum_tmpval;
1400 gboolean tmpval_used = FALSE;
1402 enum_tmpval = g_alloca (sizeof (gint));
1403 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1405 rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used);
1407 else
1409 rtype = &ffi_type_void;
1412 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1414 n_args = n_param_values + 1;
1415 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1416 args = g_alloca (sizeof (gpointer) * n_args);
1418 if (tmpval_used)
1419 enum_tmpval = g_alloca (sizeof (gint));
1421 if (G_CCLOSURE_SWAP_DATA (closure))
1423 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
1424 &args[n_args-1],
1425 enum_tmpval,
1426 &tmpval_used);
1427 atypes[0] = &ffi_type_pointer;
1428 args[0] = &closure->data;
1430 else
1432 atypes[0] = value_to_ffi_type (param_values + 0,
1433 &args[0],
1434 enum_tmpval,
1435 &tmpval_used);
1436 atypes[n_args-1] = &ffi_type_pointer;
1437 args[n_args-1] = &closure->data;
1440 for (i = 1; i < n_args - 1; i++)
1442 if (tmpval_used)
1443 enum_tmpval = g_alloca (sizeof (gint));
1445 atypes[i] = value_to_ffi_type (param_values + i,
1446 &args[i],
1447 enum_tmpval,
1448 &tmpval_used);
1451 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1452 return;
1454 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1456 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
1457 value_from_ffi_type (return_gvalue, rvalue);
1460 void
1461 g_cclosure_marshal_generic_va (GClosure *closure,
1462 GValue *return_value,
1463 gpointer instance,
1464 va_list args_list,
1465 gpointer marshal_data,
1466 int n_params,
1467 GType *param_types)
1469 ffi_type *rtype;
1470 void *rvalue;
1471 int n_args;
1472 ffi_type **atypes;
1473 void **args;
1474 va_arg_storage *storage;
1475 int i;
1476 ffi_cif cif;
1477 GCClosure *cc = (GCClosure*) closure;
1478 gint *enum_tmpval;
1479 gboolean tmpval_used = FALSE;
1480 va_list args_copy;
1482 enum_tmpval = g_alloca (sizeof (gint));
1483 if (return_value && G_VALUE_TYPE (return_value))
1485 rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used);
1487 else
1489 rtype = &ffi_type_void;
1492 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
1494 n_args = n_params + 2;
1495 atypes = g_alloca (sizeof (ffi_type *) * n_args);
1496 args = g_alloca (sizeof (gpointer) * n_args);
1497 storage = g_alloca (sizeof (va_arg_storage) * n_params);
1499 if (tmpval_used)
1500 enum_tmpval = g_alloca (sizeof (gint));
1502 if (G_CCLOSURE_SWAP_DATA (closure))
1504 atypes[n_args-1] = &ffi_type_pointer;
1505 args[n_args-1] = &instance;
1506 atypes[0] = &ffi_type_pointer;
1507 args[0] = &closure->data;
1509 else
1511 atypes[0] = &ffi_type_pointer;
1512 args[0] = &instance;
1513 atypes[n_args-1] = &ffi_type_pointer;
1514 args[n_args-1] = &closure->data;
1517 G_VA_COPY (args_copy, args_list);
1519 /* Box non-primitive arguments */
1520 for (i = 0; i < n_params; i++)
1522 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1523 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1525 atypes[i+1] = va_to_ffi_type (type,
1526 &args_copy,
1527 &storage[i]);
1528 args[i+1] = &storage[i];
1530 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1532 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1533 storage[i]._gpointer = g_strdup (storage[i]._gpointer);
1534 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1535 storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer);
1536 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1537 storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer);
1538 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1539 storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer);
1541 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1542 storage[i]._gpointer = g_object_ref (storage[i]._gpointer);
1545 va_end (args_copy);
1547 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
1548 return;
1550 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
1552 /* Unbox non-primitive arguments */
1553 for (i = 0; i < n_params; i++)
1555 GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1556 GType fundamental = G_TYPE_FUNDAMENTAL (type);
1558 if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0)
1560 if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL)
1561 g_free (storage[i]._gpointer);
1562 else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL)
1563 g_param_spec_unref (storage[i]._gpointer);
1564 else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL)
1565 g_boxed_free (type, storage[i]._gpointer);
1566 else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL)
1567 g_variant_unref (storage[i]._gpointer);
1569 if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL)
1570 g_object_unref (storage[i]._gpointer);
1573 if (return_value && G_VALUE_TYPE (return_value))
1574 value_from_ffi_type (return_value, rvalue);
1578 * g_cclosure_marshal_VOID__VOID:
1579 * @closure: the #GClosure to which the marshaller belongs
1580 * @return_value: ignored
1581 * @n_param_values: 1
1582 * @param_values: a #GValue array holding only the instance
1583 * @invocation_hint: the invocation hint given as the last argument
1584 * to g_closure_invoke()
1585 * @marshal_data: additional data specified when registering the marshaller
1587 * A marshaller for a #GCClosure with a callback of type
1588 * <literal>void (*callback) (gpointer instance, gpointer user_data)</literal>.
1592 * g_cclosure_marshal_VOID__BOOLEAN:
1593 * @closure: the #GClosure to which the marshaller belongs
1594 * @return_value: ignored
1595 * @n_param_values: 2
1596 * @param_values: a #GValue array holding the instance and the #gboolean parameter
1597 * @invocation_hint: the invocation hint given as the last argument
1598 * to g_closure_invoke()
1599 * @marshal_data: additional data specified when registering the marshaller
1601 * A marshaller for a #GCClosure with a callback of type
1602 * <literal>void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)</literal>.
1606 * g_cclosure_marshal_VOID__CHAR:
1607 * @closure: the #GClosure to which the marshaller belongs
1608 * @return_value: ignored
1609 * @n_param_values: 2
1610 * @param_values: a #GValue array holding the instance and the #gchar parameter
1611 * @invocation_hint: the invocation hint given as the last argument
1612 * to g_closure_invoke()
1613 * @marshal_data: additional data specified when registering the marshaller
1615 * A marshaller for a #GCClosure with a callback of type
1616 * <literal>void (*callback) (gpointer instance, gchar arg1, gpointer user_data)</literal>.
1620 * g_cclosure_marshal_VOID__UCHAR:
1621 * @closure: the #GClosure to which the marshaller belongs
1622 * @return_value: ignored
1623 * @n_param_values: 2
1624 * @param_values: a #GValue array holding the instance and the #guchar parameter
1625 * @invocation_hint: the invocation hint given as the last argument
1626 * to g_closure_invoke()
1627 * @marshal_data: additional data specified when registering the marshaller
1629 * A marshaller for a #GCClosure with a callback of type
1630 * <literal>void (*callback) (gpointer instance, guchar arg1, gpointer user_data)</literal>.
1634 * g_cclosure_marshal_VOID__INT:
1635 * @closure: the #GClosure to which the marshaller belongs
1636 * @return_value: ignored
1637 * @n_param_values: 2
1638 * @param_values: a #GValue array holding the instance and the #gint parameter
1639 * @invocation_hint: the invocation hint given as the last argument
1640 * to g_closure_invoke()
1641 * @marshal_data: additional data specified when registering the marshaller
1643 * A marshaller for a #GCClosure with a callback of type
1644 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal>.
1648 * g_cclosure_marshal_VOID__UINT:
1649 * @closure: the #GClosure to which the marshaller belongs
1650 * @return_value: ignored
1651 * @n_param_values: 2
1652 * @param_values: a #GValue array holding the instance and the #guint parameter
1653 * @invocation_hint: the invocation hint given as the last argument
1654 * to g_closure_invoke()
1655 * @marshal_data: additional data specified when registering the marshaller
1657 * A marshaller for a #GCClosure with a callback of type
1658 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer user_data)</literal>.
1662 * g_cclosure_marshal_VOID__LONG:
1663 * @closure: the #GClosure to which the marshaller belongs
1664 * @return_value: ignored
1665 * @n_param_values: 2
1666 * @param_values: a #GValue array holding the instance and the #glong parameter
1667 * @invocation_hint: the invocation hint given as the last argument
1668 * to g_closure_invoke()
1669 * @marshal_data: additional data specified when registering the marshaller
1671 * A marshaller for a #GCClosure with a callback of type
1672 * <literal>void (*callback) (gpointer instance, glong arg1, gpointer user_data)</literal>.
1676 * g_cclosure_marshal_VOID__ULONG:
1677 * @closure: the #GClosure to which the marshaller belongs
1678 * @return_value: ignored
1679 * @n_param_values: 2
1680 * @param_values: a #GValue array holding the instance and the #gulong parameter
1681 * @invocation_hint: the invocation hint given as the last argument
1682 * to g_closure_invoke()
1683 * @marshal_data: additional data specified when registering the marshaller
1685 * A marshaller for a #GCClosure with a callback of type
1686 * <literal>void (*callback) (gpointer instance, gulong arg1, gpointer user_data)</literal>.
1690 * g_cclosure_marshal_VOID__ENUM:
1691 * @closure: the #GClosure to which the marshaller belongs
1692 * @return_value: ignored
1693 * @n_param_values: 2
1694 * @param_values: a #GValue array holding the instance and the enumeration parameter
1695 * @invocation_hint: the invocation hint given as the last argument
1696 * to g_closure_invoke()
1697 * @marshal_data: additional data specified when registering the marshaller
1699 * A marshaller for a #GCClosure with a callback of type
1700 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes an enumeration type..
1704 * g_cclosure_marshal_VOID__FLAGS:
1705 * @closure: the #GClosure to which the marshaller belongs
1706 * @return_value: ignored
1707 * @n_param_values: 2
1708 * @param_values: a #GValue array holding the instance and the flags parameter
1709 * @invocation_hint: the invocation hint given as the last argument
1710 * to g_closure_invoke()
1711 * @marshal_data: additional data specified when registering the marshaller
1713 * A marshaller for a #GCClosure with a callback of type
1714 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes a flags type.
1718 * g_cclosure_marshal_VOID__FLOAT:
1719 * @closure: the #GClosure to which the marshaller belongs
1720 * @return_value: ignored
1721 * @n_param_values: 2
1722 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1723 * @invocation_hint: the invocation hint given as the last argument
1724 * to g_closure_invoke()
1725 * @marshal_data: additional data specified when registering the marshaller
1727 * A marshaller for a #GCClosure with a callback of type
1728 * <literal>void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)</literal>.
1732 * g_cclosure_marshal_VOID__DOUBLE:
1733 * @closure: the #GClosure to which the marshaller belongs
1734 * @return_value: ignored
1735 * @n_param_values: 2
1736 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1737 * @invocation_hint: the invocation hint given as the last argument
1738 * to g_closure_invoke()
1739 * @marshal_data: additional data specified when registering the marshaller
1741 * A marshaller for a #GCClosure with a callback of type
1742 * <literal>void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)</literal>.
1746 * g_cclosure_marshal_VOID__STRING:
1747 * @closure: the #GClosure to which the marshaller belongs
1748 * @return_value: ignored
1749 * @n_param_values: 2
1750 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1751 * @invocation_hint: the invocation hint given as the last argument
1752 * to g_closure_invoke()
1753 * @marshal_data: additional data specified when registering the marshaller
1755 * A marshaller for a #GCClosure with a callback of type
1756 * <literal>void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)</literal>.
1760 * g_cclosure_marshal_VOID__PARAM:
1761 * @closure: the #GClosure to which the marshaller belongs
1762 * @return_value: ignored
1763 * @n_param_values: 2
1764 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1765 * @invocation_hint: the invocation hint given as the last argument
1766 * to g_closure_invoke()
1767 * @marshal_data: additional data specified when registering the marshaller
1769 * A marshaller for a #GCClosure with a callback of type
1770 * <literal>void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)</literal>.
1774 * g_cclosure_marshal_VOID__BOXED:
1775 * @closure: the #GClosure to which the marshaller belongs
1776 * @return_value: ignored
1777 * @n_param_values: 2
1778 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1779 * @invocation_hint: the invocation hint given as the last argument
1780 * to g_closure_invoke()
1781 * @marshal_data: additional data specified when registering the marshaller
1783 * A marshaller for a #GCClosure with a callback of type
1784 * <literal>void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)</literal>.
1788 * g_cclosure_marshal_VOID__POINTER:
1789 * @closure: the #GClosure to which the marshaller belongs
1790 * @return_value: ignored
1791 * @n_param_values: 2
1792 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1793 * @invocation_hint: the invocation hint given as the last argument
1794 * to g_closure_invoke()
1795 * @marshal_data: additional data specified when registering the marshaller
1797 * A marshaller for a #GCClosure with a callback of type
1798 * <literal>void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)</literal>.
1802 * g_cclosure_marshal_VOID__OBJECT:
1803 * @closure: the #GClosure to which the marshaller belongs
1804 * @return_value: ignored
1805 * @n_param_values: 2
1806 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1807 * @invocation_hint: the invocation hint given as the last argument
1808 * to g_closure_invoke()
1809 * @marshal_data: additional data specified when registering the marshaller
1811 * A marshaller for a #GCClosure with a callback of type
1812 * <literal>void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)</literal>.
1816 * g_cclosure_marshal_VOID__VARIANT:
1817 * @closure: the #GClosure to which the marshaller belongs
1818 * @return_value: ignored
1819 * @n_param_values: 2
1820 * @param_values: a #GValue array holding the instance and the #GVariant* parameter
1821 * @invocation_hint: the invocation hint given as the last argument
1822 * to g_closure_invoke()
1823 * @marshal_data: additional data specified when registering the marshaller
1825 * A marshaller for a #GCClosure with a callback of type
1826 * <literal>void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)</literal>.
1828 * Since: 2.26
1832 * g_cclosure_marshal_VOID__UINT_POINTER:
1833 * @closure: the #GClosure to which the marshaller belongs
1834 * @return_value: ignored
1835 * @n_param_values: 3
1836 * @param_values: a #GValue array holding instance, arg1 and arg2
1837 * @invocation_hint: the invocation hint given as the last argument
1838 * to g_closure_invoke()
1839 * @marshal_data: additional data specified when registering the marshaller
1841 * A marshaller for a #GCClosure with a callback of type
1842 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)</literal>.
1846 * g_cclosure_marshal_BOOLEAN__FLAGS:
1847 * @closure: the #GClosure to which the marshaller belongs
1848 * @return_value: a #GValue which can store the returned #gboolean
1849 * @n_param_values: 2
1850 * @param_values: a #GValue array holding instance and arg1
1851 * @invocation_hint: the invocation hint given as the last argument
1852 * to g_closure_invoke()
1853 * @marshal_data: additional data specified when registering the marshaller
1855 * A marshaller for a #GCClosure with a callback of type
1856 * <literal>gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter
1857 * denotes a flags type.
1861 * g_cclosure_marshal_BOOL__FLAGS:
1863 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1866 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1867 * @closure: the #GClosure to which the marshaller belongs
1868 * @return_value: a #GValue, which can store the returned string
1869 * @n_param_values: 3
1870 * @param_values: a #GValue array holding instance, arg1 and arg2
1871 * @invocation_hint: the invocation hint given as the last argument
1872 * to g_closure_invoke()
1873 * @marshal_data: additional data specified when registering the marshaller
1875 * A marshaller for a #GCClosure with a callback of type
1876 * <literal>gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)</literal>.
1879 * g_cclosure_marshal_BOOLEAN__OBJECT_BOXED_BOXED:
1880 * @closure: the #GClosure to which the marshaller belongs
1881 * @return_value: a #GValue, which can store the returned string
1882 * @n_param_values: 3
1883 * @param_values: a #GValue array holding instance, arg1 and arg2
1884 * @invocation_hint: the invocation hint given as the last argument
1885 * to g_closure_invoke()
1886 * @marshal_data: additional data specified when registering the marshaller
1888 * A marshaller for a #GCClosure with a callback of type
1889 * <literal>gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)</literal>.
1891 * Since: 2.26