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