Coverage Report

Created: 2026-10-28 06:22

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