Coverage Report

Created: 2025-06-13 06:21

/src/glib/gobject/gobject.c
Line
Count
Source (jump to first uncovered line)
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * MT safe with regards to reference counting.
22
 */
23
24
#include "config.h"
25
26
#include <string.h>
27
#include <signal.h>
28
29
#include "../glib/glib-private.h"
30
31
#include "gobject.h"
32
#include "gtype-private.h"
33
#include "gvaluecollector.h"
34
#include "gsignal.h"
35
#include "gparamspecs.h"
36
#include "gvaluetypes.h"
37
#include "gobject_trace.h"
38
#include "gconstructor.h"
39
40
/**
41
 * GObject:
42
 *
43
 * The base object type.
44
 *
45
 * `GObject` is the fundamental type providing the common attributes and
46
 * methods for all object types in GTK, Pango and other libraries
47
 * based on GObject. The `GObject` class provides methods for object
48
 * construction and destruction, property access methods, and signal
49
 * support. Signals are described in detail [here][gobject-Signals].
50
 *
51
 * For a tutorial on implementing a new `GObject` class, see [How to define and
52
 * implement a new GObject](tutorial.html#how-to-define-and-implement-a-new-gobject).
53
 * For a list of naming conventions for GObjects and their methods, see the
54
 * [GType conventions](concepts.html#conventions). For the high-level concepts
55
 * behind GObject, read
56
 * [Instantiatable classed types: Objects](concepts.html#instantiatable-classed-types-objects).
57
 *
58
 * Since GLib 2.72, all `GObject`s are guaranteed to be aligned to at least the
59
 * alignment of the largest basic GLib type (typically this is `guint64` or
60
 * `gdouble`). If you need larger alignment for an element in a `GObject`, you
61
 * should allocate it on the heap (aligned), or arrange for your `GObject` to be
62
 * appropriately padded. This guarantee applies to the `GObject` (or derived)
63
 * struct, the `GObjectClass` (or derived) struct, and any private data allocated
64
 * by `G_ADD_PRIVATE()`.
65
 */
66
67
/* --- macros --- */
68
6.17k
#define PARAM_SPEC_PARAM_ID(pspec)    ((pspec)->param_id)
69
66
#define PARAM_SPEC_SET_PARAM_ID(pspec, id)  ((pspec)->param_id = (id))
70
71
0
#define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
72
#define OBJECT_HAS_TOGGLE_REF(object) \
73
    ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
74
0
#define OBJECT_FLOATING_FLAG 0x2
75
76
13.0k
#define CLASS_HAS_PROPS_FLAG 0x1
77
#define CLASS_HAS_PROPS(class) \
78
19.4k
    ((class)->flags & CLASS_HAS_PROPS_FLAG)
79
#define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
80
    ((class)->constructor != g_object_constructor)
81
#define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
82
6.48k
    ((class)->constructed != g_object_constructed)
83
29.4k
#define CLASS_HAS_NOTIFY(class) ((class)->notify != NULL)
84
#define CLASS_HAS_CUSTOM_DISPATCH(class) \
85
14.7k
    ((class)->dispatch_properties_changed != g_object_dispatch_properties_changed)
86
#define CLASS_NEEDS_NOTIFY(class) \
87
24.0k
    (CLASS_HAS_NOTIFY(class) || CLASS_HAS_CUSTOM_DISPATCH(class))
88
89
123
#define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
90
#define CLASS_HAS_DERIVED_CLASS(class) \
91
66
    ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
92
93
/* --- signals --- */
94
enum {
95
  NOTIFY,
96
  LAST_SIGNAL
97
};
98
99
100
/* --- properties --- */
101
enum {
102
  PROP_NONE
103
};
104
105
23.3k
#define OPTIONAL_FLAG_IN_CONSTRUCTION    (1 << 0)
106
0
#define OPTIONAL_FLAG_HAS_SIGNAL_HANDLER (1 << 1) /* Set if object ever had a signal handler */
107
9.29k
#define OPTIONAL_FLAG_HAS_NOTIFY_HANDLER (1 << 2) /* Same, specifically for "notify" */
108
12.9k
#define OPTIONAL_FLAG_EVER_HAD_WEAK_REF  (1 << 4) /* whether on the object ever g_weak_ref_set() was called. */
109
110
#if SIZEOF_INT == 4 && GLIB_SIZEOF_VOID_P >= 8
111
#define HAVE_OPTIONAL_FLAGS_IN_GOBJECT 1
112
#else
113
#define HAVE_OPTIONAL_FLAGS_IN_GOBJECT 0
114
#endif
115
116
/* For now we only create a private struct if we don't have optional flags in
117
 * GObject. Currently we don't need it otherwise. In the future we might
118
 * always add a private struct. */
119
#define HAVE_PRIVATE (!HAVE_OPTIONAL_FLAGS_IN_GOBJECT)
120
121
#if HAVE_PRIVATE
122
typedef struct {
123
#if !HAVE_OPTIONAL_FLAGS_IN_GOBJECT
124
  guint optional_flags; /* (atomic) */
125
#endif
126
} GObjectPrivate;
127
128
static int GObject_private_offset;
129
#endif
130
131
typedef struct
132
{
133
  GTypeInstance  g_type_instance;
134
135
  /*< private >*/
136
  guint          ref_count;  /* (atomic) */
137
#if HAVE_OPTIONAL_FLAGS_IN_GOBJECT
138
  guint          optional_flags;  /* (atomic) */
139
#endif
140
  GData         *qdata;
141
} GObjectReal;
142
143
G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal));
144
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count));
145
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, qdata) == G_STRUCT_OFFSET(GObjectReal, qdata));
146
147
148
/* --- prototypes --- */
149
static void g_object_base_class_init    (GObjectClass *class);
150
static void g_object_base_class_finalize    (GObjectClass *class);
151
static void g_object_do_class_init      (GObjectClass *class);
152
static void g_object_init       (GObject  *object,
153
               GObjectClass *class);
154
static GObject* g_object_constructor      (GType                  type,
155
               guint                  n_construct_properties,
156
               GObjectConstructParam *construct_params);
157
static void     g_object_constructed                    (GObject        *object);
158
static void g_object_real_dispose     (GObject  *object);
159
static void g_object_finalize     (GObject  *object);
160
static void g_object_do_set_property    (GObject        *object,
161
               guint           property_id,
162
               const GValue   *value,
163
               GParamSpec     *pspec);
164
static void g_object_do_get_property    (GObject        *object,
165
               guint           property_id,
166
               GValue         *value,
167
               GParamSpec     *pspec);
168
static void g_value_object_init     (GValue   *value);
169
static void g_value_object_free_value   (GValue   *value);
170
static void g_value_object_copy_value   (const GValue *src_value,
171
               GValue   *dest_value);
172
static void g_value_object_transform_value    (const GValue *src_value,
173
               GValue   *dest_value);
174
static gpointer g_value_object_peek_pointer             (const GValue   *value);
175
static gchar* g_value_object_collect_value    (GValue   *value,
176
               guint           n_collect_values,
177
               GTypeCValue    *collect_values,
178
               guint           collect_flags);
179
static gchar* g_value_object_lcopy_value    (const GValue *value,
180
               guint           n_collect_values,
181
               GTypeCValue    *collect_values,
182
               guint           collect_flags);
183
static void g_object_dispatch_properties_changed  (GObject  *object,
184
               guint     n_pspecs,
185
               GParamSpec    **pspecs);
186
static void closure_array_destroy_all (GObject *object);
187
static guint               object_floating_flag_handler (GObject        *object,
188
                                                         gint            job);
189
static inline void object_set_optional_flags (GObject *object,
190
                                              guint flags);
191
static void g_object_weak_release_all (GObject *object, gboolean release_all);
192
193
static void object_interface_check_properties           (gpointer        check_data,
194
               gpointer        g_iface);
195
196
/* --- typedefs --- */
197
198
typedef struct
199
{
200
  guint16 freeze_count;
201
  guint16 len;
202
  guint16 alloc;
203
  GParamSpec *pspecs[];
204
} GObjectNotifyQueue;
205
206
/* --- variables --- */
207
static GQuark             quark_closure_array = 0;
208
static GQuark             quark_weak_notifies = 0;
209
static GQuark             quark_toggle_refs = 0;
210
static GQuark               quark_notify_queue;
211
static GParamSpecPool      *pspec_pool = NULL; /* atomic */
212
static gulong             gobject_signals[LAST_SIGNAL] = { 0, };
213
static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
214
static GQuark             quark_weak_locations = 0;
215
216
static gpointer (*_local_g_datalist_id_update_atomic) (GData **datalist,
217
                                                       GQuark key_id,
218
                                                       gboolean already_locked,
219
                                                       GDataListUpdateAtomicFunc callback,
220
                                                       gpointer user_data) = NULL;
221
#undef _g_datalist_id_update_atomic_full
222
32.4k
#define _g_datalist_id_update_atomic_full(...) ((_local_g_datalist_id_update_atomic) (__VA_ARGS__))
223
224
#if HAVE_PRIVATE
225
G_ALWAYS_INLINE static inline GObjectPrivate *
226
g_object_get_instance_private (GObject *object)
227
{
228
  return G_STRUCT_MEMBER_P (object, GObject_private_offset);
229
}
230
#endif
231
232
G_ALWAYS_INLINE static inline guint *
233
object_get_optional_flags_p (GObject *object)
234
41.7k
{
235
41.7k
#if HAVE_OPTIONAL_FLAGS_IN_GOBJECT
236
41.7k
  return &(((GObjectReal *) object)->optional_flags);
237
#else
238
  return &g_object_get_instance_private (object)->optional_flags;
239
#endif
240
41.7k
}
241
242
/*****************************************************************************/
243
244
/* For GWeakRef, we need to take a lock per-object. However, in various cases
245
 * we cannot take a strong reference on the object to keep it alive. So the
246
 * mutex cannot be in the object itself, because when we want to release the
247
 * lock, we can no longer access object.
248
 *
249
 * Instead, the mutex is on the WeakRefData, which is itself ref-counted
250
 * and has a separate lifetime from the object. */
251
typedef struct
252
{
253
  /* This is both an atomic ref-count and bit 30 (WEAK_REF_DATA_LOCK_BIT) is
254
   * used for g_bit_lock(). */
255
  gint atomic_field;
256
257
  guint16 len;
258
259
  /* Only relevant when len > 1. In that case, it's the allocated size of
260
   * "list.many" array.  */
261
  guint16 alloc;
262
263
  /* Only relevant when len > 0. In that case, either "one" or "many" union
264
   * field is in use. */
265
  union
266
  {
267
    GWeakRef *one;
268
    GWeakRef **many;
269
  } list;
270
} WeakRefData;
271
272
/* We choose bit 30, and not bit 31. Bit 31 would be the sign for gint, so it
273
 * a bit awkward to use. Note that it probably also would work fine.
274
 *
275
 * But 30 is ok, because it still leaves us space for 2^30-1 references, which
276
 * is more than we ever need. */
277
0
#define WEAK_REF_DATA_LOCK_BIT 30
278
279
static void weak_ref_data_clear_list (WeakRefData *wrdata, GObject *object);
280
281
static WeakRefData *
282
weak_ref_data_ref (WeakRefData *wrdata)
283
0
{
284
0
  gint ref;
285
286
0
#if G_ENABLE_DEBUG
287
0
  g_assert (wrdata);
288
0
#endif
289
290
0
  ref = g_atomic_int_add (&wrdata->atomic_field, 1);
291
292
0
#if G_ENABLE_DEBUG
293
  /* Overflow is almost impossible to happen, because the user would need to
294
   * spawn that many operating system threads, that all call
295
   * g_weak_ref_{set,get}() in parallel.
296
   *
297
   * Still, assert in debug mode. */
298
0
  g_assert (ref < G_MAXINT32);
299
300
  /* the real ref-count would be the following: */
301
0
  ref = (ref + 1) & ~(1 << WEAK_REF_DATA_LOCK_BIT);
302
303
  /* assert that the ref-count is still in the valid range. */
304
0
  g_assert (ref > 0 && ref < (1 << WEAK_REF_DATA_LOCK_BIT));
305
0
#endif
306
0
  (void) ref;
307
308
0
  return wrdata;
309
0
}
310
311
static void
312
weak_ref_data_unref (WeakRefData *wrdata)
313
0
{
314
0
  if (!wrdata)
315
0
    return;
316
317
  /* Note that we also use WEAK_REF_DATA_LOCK_BIT on "atomic_field" as a bit
318
   * lock. However, we will always keep the @wrdata alive (having a reference)
319
   * while holding a lock (otherwise, we couldn't unlock anymore). Thus, at the
320
   * point when we decrement the ref-count to zero, we surely also have the
321
   * @wrdata unlocked.
322
   *
323
   * This means, using "aomit_field" both as ref-count and the lock bit is
324
   * fine. */
325
326
0
  if (!g_atomic_int_dec_and_test (&wrdata->atomic_field))
327
0
    return;
328
329
0
#if G_ENABLE_DEBUG
330
  /* We expect that the list of weak locations is empty at this point.
331
   * During g_object_unref() (_object_unref_clear_weak_locations()) it
332
   * should have been cleared.
333
   *
334
   * Calling weak_ref_data_clear_list() should be unnecessary. */
335
0
  g_assert (wrdata->len == 0);
336
0
#endif
337
338
0
  g_free_sized (wrdata, sizeof (WeakRefData));
339
0
}
340
341
static void
342
weak_ref_data_lock (WeakRefData *wrdata)
343
0
{
344
  /* Note that while holding a _weak_ref_lock() on the @weak_ref, we MUST not acquire a
345
   * weak_ref_data_lock() on the @wrdata. The other way around! */
346
0
  if (wrdata)
347
0
    g_bit_lock (&wrdata->atomic_field, WEAK_REF_DATA_LOCK_BIT);
348
0
}
349
350
static void
351
weak_ref_data_unlock (WeakRefData *wrdata)
352
0
{
353
0
  if (wrdata)
354
0
    g_bit_unlock (&wrdata->atomic_field, WEAK_REF_DATA_LOCK_BIT);
355
0
}
356
357
static gpointer
358
weak_ref_data_get_or_create_cb (gpointer *data,
359
                                GDestroyNotify *destroy_notify,
360
                                gpointer user_data)
361
0
{
362
0
  WeakRefData *wrdata = *data;
363
0
  GObject *object = user_data;
364
365
0
  if (!wrdata)
366
0
    {
367
0
      wrdata = g_new (WeakRefData, 1);
368
369
      /* The initial ref-count is 1. This one is owned by the GData until the
370
       * object gets destroyed.
371
       *
372
       * The WEAK_REF_DATA_LOCK_BIT bit is of course initially unset.  */
373
0
      wrdata->atomic_field = 1;
374
0
      wrdata->len = 0;
375
      /* Other fields are left uninitialized. They are only considered with a positive @len. */
376
377
0
      *data = wrdata;
378
0
      *destroy_notify = (GDestroyNotify) weak_ref_data_unref;
379
380
      /* Mark the @object that it was ever involved with GWeakRef. This flag
381
       * will stick until @object gets destroyed, just like the WeakRefData
382
       * also won't be freed for the remainder of the life of @object. */
383
0
      object_set_optional_flags (object, OPTIONAL_FLAG_EVER_HAD_WEAK_REF);
384
0
    }
385
386
0
  return wrdata;
387
0
}
388
389
static WeakRefData *
390
weak_ref_data_get_or_create (GObject *object)
391
0
{
392
0
  if (!object)
393
0
    return NULL;
394
395
0
  return _g_datalist_id_update_atomic (&object->qdata,
396
0
                                       quark_weak_locations,
397
0
                                       weak_ref_data_get_or_create_cb,
398
0
                                       object);
399
0
}
400
401
static WeakRefData *
402
weak_ref_data_get (GObject *object)
403
0
{
404
0
  return g_datalist_id_get_data (&object->qdata, quark_weak_locations);
405
0
}
406
407
static WeakRefData *
408
weak_ref_data_get_surely (GObject *object)
409
0
{
410
0
  WeakRefData *wrdata;
411
412
  /* The "surely" part is about that we expect to have a WeakRefData.
413
   *
414
   * Note that once a GObject gets a WeakRefData (during g_weak_ref_set() and
415
   * weak_ref_data_get_or_create()), it sticks and is not freed until the
416
   * object gets destroyed.
417
   *
418
   * Maybe we could release the unused WeakRefData in g_weak_ref_set(), but
419
   * then we would always need to take a reference during weak_ref_data_get().
420
   * That is likely not worth it. */
421
422
0
  wrdata = weak_ref_data_get (object);
423
0
#if G_ENABLE_DEBUG
424
0
  g_assert (wrdata);
425
0
#endif
426
0
  return wrdata;
427
0
}
428
429
static gint32
430
weak_ref_data_list_find (WeakRefData *wrdata, GWeakRef *weak_ref)
431
0
{
432
0
  if (wrdata->len == 1u)
433
0
    {
434
0
      if (wrdata->list.one == weak_ref)
435
0
        return 0;
436
0
    }
437
0
  else
438
0
    {
439
0
      guint16 i;
440
441
0
      for (i = 0; i < wrdata->len; i++)
442
0
        {
443
0
          if (wrdata->list.many[i] == weak_ref)
444
0
            return i;
445
0
        }
446
0
    }
447
448
0
  return -1;
449
0
}
450
451
static gboolean
452
weak_ref_data_list_add (WeakRefData *wrdata, GWeakRef *weak_ref)
453
0
{
454
0
  if (wrdata->len == 0u)
455
0
    wrdata->list.one = weak_ref;
456
0
  else
457
0
    {
458
0
      if (wrdata->len == 1u)
459
0
        {
460
0
          GWeakRef *weak_ref2 = wrdata->list.one;
461
462
0
          wrdata->alloc = 4u;
463
0
          wrdata->list.many = g_new (GWeakRef *, wrdata->alloc);
464
0
          wrdata->list.many[0] = weak_ref2;
465
0
        }
466
0
      else if (wrdata->len == wrdata->alloc)
467
0
        {
468
0
          guint16 alloc;
469
470
0
          alloc = wrdata->alloc * 2u;
471
0
          if (G_UNLIKELY (alloc < wrdata->len))
472
0
            {
473
0
              if (wrdata->len == G_MAXUINT16)
474
0
                return FALSE;
475
0
              alloc = G_MAXUINT16;
476
0
            }
477
0
          wrdata->list.many = g_renew (GWeakRef *, wrdata->list.many, alloc);
478
0
          wrdata->alloc = alloc;
479
0
        }
480
481
0
      wrdata->list.many[wrdata->len] = weak_ref;
482
0
    }
483
484
0
  wrdata->len++;
485
0
  return TRUE;
486
0
}
487
488
static GWeakRef *
489
weak_ref_data_list_remove (WeakRefData *wrdata, guint16 idx, gboolean allow_shrink)
490
0
{
491
0
  GWeakRef *weak_ref;
492
493
0
#if G_ENABLE_DEBUG
494
0
  g_assert (idx < wrdata->len);
495
0
#endif
496
497
0
  wrdata->len--;
498
499
0
  if (wrdata->len == 0u)
500
0
    {
501
0
      weak_ref = wrdata->list.one;
502
0
    }
503
0
  else
504
0
    {
505
0
      weak_ref = wrdata->list.many[idx];
506
507
0
      if (wrdata->len == 1u)
508
0
        {
509
0
          GWeakRef *weak_ref2 = wrdata->list.many[idx == 0 ? 1 : 0];
510
511
0
          g_free (wrdata->list.many);
512
0
          wrdata->list.one = weak_ref2;
513
0
        }
514
0
      else
515
0
        {
516
0
          wrdata->list.many[idx] = wrdata->list.many[wrdata->len];
517
518
0
          if (allow_shrink && G_UNLIKELY (wrdata->len <= wrdata->alloc / 4u))
519
0
            {
520
              /* Shrink the buffer. When 75% are empty, shrink it to 50%. */
521
0
              if (wrdata->alloc == G_MAXUINT16)
522
0
                wrdata->alloc = ((guint32) G_MAXUINT16 + 1u) / 2u;
523
0
              else
524
0
                wrdata->alloc /= 2u;
525
0
              wrdata->list.many = g_renew (GWeakRef *, wrdata->list.many, wrdata->alloc);
526
0
            }
527
0
        }
528
0
    }
529
530
0
  return weak_ref;
531
0
}
532
533
static gboolean
534
weak_ref_data_has (GObject *object, WeakRefData *wrdata, WeakRefData **out_new_wrdata)
535
0
{
536
0
  WeakRefData *wrdata2;
537
538
  /* Check whether @object has @wrdata as WeakRefData. Note that an GObject's
539
   * WeakRefData never changes (until destruction, once it's allocated).
540
   *
541
   * If you thus hold a reference to a @wrdata, you can check that the @object
542
   * is still the same as the object where we got the @wrdata originally from.
543
   *
544
   * You couldn't do this check by using pointer equality of the GObject pointers,
545
   * when you cannot hold strong references on the objects involved. Because then
546
   * the object pointer might be dangling (and even destroyed and recreated as another
547
   * object at the same memory location).
548
   *
549
   * Basically, weak_ref_data_has() is to compare for equality of two GObject pointers,
550
   * when we cannot hold a strong reference on both. Instead, we earlier took a reference
551
   * on the @wrdata and compare that instead.
552
   */
553
554
0
  if (!object)
555
0
    {
556
      /* If @object is NULL, then it does have a NULL @wrdata, and we return
557
       * TRUE in the case.  That's a convenient special case for some callers.
558
       *
559
       * In other words, weak_ref_data_has(NULL, NULL, out_new_wrdata) is TRUE.
560
       */
561
0
#if G_ENABLE_DEBUG
562
0
      g_assert (!out_new_wrdata);
563
0
#endif
564
0
      return !wrdata;
565
0
    }
566
567
0
  if (!wrdata)
568
0
    {
569
      /* We only call this function with an @object that was previously
570
       * registered as GWeakRef.
571
       *
572
       * That means, our @object will have a wrdata, and the result of the
573
       * evaluation will be %FALSE. */
574
0
      if (out_new_wrdata)
575
0
        *out_new_wrdata = weak_ref_data_ref (weak_ref_data_get (object));
576
0
#if G_ENABLE_DEBUG
577
0
      g_assert (out_new_wrdata
578
0
                    ? *out_new_wrdata
579
0
                    : weak_ref_data_get (object));
580
0
#endif
581
0
      return FALSE;
582
0
    }
583
584
0
  wrdata2 = weak_ref_data_get_surely (object);
585
586
0
  if (wrdata == wrdata2)
587
0
    {
588
0
      if (out_new_wrdata)
589
0
        *out_new_wrdata = NULL;
590
0
      return TRUE;
591
0
    }
592
593
0
  if (out_new_wrdata)
594
0
    *out_new_wrdata = weak_ref_data_ref (wrdata2);
595
0
  return FALSE;
596
0
}
597
598
/*****************************************************************************/
599
600
/* --- functions --- */
601
602
static const GObjectNotifyQueue notify_queue_empty = {
603
  .freeze_count = 0,
604
};
605
606
G_ALWAYS_INLINE static inline gboolean
607
_is_notify_queue_empty (const GObjectNotifyQueue *nqueue)
608
0
{
609
  /* Only the notify_queue_empty instance has a zero freeze count. We check
610
   * here for that condition instead of pointer comparing to
611
   * &notify_queue_empty. That seems better because callers will afterwards
612
   * dereference "freeze_count", so the value is already loaded.
613
   *
614
   * In any case, both conditions must be equivalent.
615
   */
616
0
#ifdef G_ENABLE_DEBUG
617
0
  g_assert ((nqueue == &notify_queue_empty) == (nqueue->freeze_count == 0));
618
0
#endif
619
0
  return nqueue->freeze_count == 0;
620
0
}
621
622
G_ALWAYS_INLINE static inline gsize
623
g_object_notify_queue_alloc_size (gsize alloc)
624
0
{
625
0
  return G_STRUCT_OFFSET (GObjectNotifyQueue, pspecs) + (alloc * sizeof (GParamSpec *));
626
0
}
627
628
static GObjectNotifyQueue *
629
g_object_notify_queue_new_frozen (void)
630
0
{
631
0
  GObjectNotifyQueue *nqueue;
632
633
0
  nqueue = g_malloc (g_object_notify_queue_alloc_size (4));
634
635
0
  nqueue->freeze_count = 1;
636
0
  nqueue->alloc = 4;
637
0
  nqueue->len = 0;
638
639
0
  return nqueue;
640
0
}
641
642
static gpointer
643
g_object_notify_queue_freeze_cb (gpointer *data,
644
                                 GDestroyNotify *destroy_notify,
645
                                 gpointer user_data)
646
6.48k
{
647
6.48k
  GObject *object = ((gpointer *) user_data)[0];
648
6.48k
  gboolean freeze_always = GPOINTER_TO_INT (((gpointer *) user_data)[1]);
649
6.48k
  GObjectNotifyQueue *nqueue = *data;
650
651
6.48k
  if (!nqueue)
652
6.48k
    {
653
      /* The nqueue doesn't exist yet. We use the dummy object that is shared
654
       * by all instances. */
655
6.48k
      *data = (gpointer) &notify_queue_empty;
656
6.48k
      *destroy_notify = NULL;
657
6.48k
    }
658
0
  else if (!freeze_always)
659
0
    {
660
      /* The caller only wants to ensure we are frozen once. If we are already frozen,
661
       * don't freeze another time.
662
       *
663
       * This is only relevant during the object initialization. */
664
0
    }
665
0
  else
666
0
    {
667
0
      if (_is_notify_queue_empty (nqueue))
668
0
        {
669
0
          nqueue = g_object_notify_queue_new_frozen ();
670
0
          *data = nqueue;
671
0
          *destroy_notify = g_free;
672
0
          nqueue->freeze_count++;
673
0
        }
674
0
      else if (G_UNLIKELY (nqueue->freeze_count == G_MAXUINT16))
675
0
        {
676
0
          g_critical ("Free queue for %s (%p) is larger than 65535,"
677
0
                      " called g_object_freeze_notify() too often."
678
0
                      " Forgot to call g_object_thaw_notify() or infinite loop",
679
0
                      G_OBJECT_TYPE_NAME (object), object);
680
0
        }
681
0
      else
682
0
        nqueue->freeze_count++;
683
0
    }
684
685
6.48k
  return NULL;
686
6.48k
}
687
688
static void
689
g_object_notify_queue_freeze (GObject *object, gboolean freeze_always)
690
6.48k
{
691
6.48k
  _g_datalist_id_update_atomic (&object->qdata,
692
6.48k
                                quark_notify_queue,
693
6.48k
                                g_object_notify_queue_freeze_cb,
694
6.48k
                                ((gpointer[]){ object, GINT_TO_POINTER (!!freeze_always) }));
695
6.48k
}
696
697
static gpointer
698
g_object_notify_queue_thaw_cb (gpointer *data,
699
                               GDestroyNotify *destroy_notify,
700
                               gpointer user_data)
701
0
{
702
0
  GObject *object = user_data;
703
0
  GObjectNotifyQueue *nqueue = *data;
704
705
0
  if (G_UNLIKELY (!nqueue))
706
0
    {
707
0
      g_critical ("%s: property-changed notification for %s(%p) is not frozen",
708
0
                  G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
709
0
      return NULL;
710
0
    }
711
712
0
  if (_is_notify_queue_empty (nqueue))
713
0
    {
714
0
      *data = NULL;
715
0
      *destroy_notify = NULL;
716
0
      return NULL;
717
0
    }
718
719
0
  nqueue->freeze_count--;
720
721
0
  if (nqueue->freeze_count > 0)
722
0
    return NULL;
723
724
0
  *data = NULL;
725
0
  *destroy_notify = NULL;
726
0
  return nqueue;
727
0
}
728
729
static void
730
g_object_notify_queue_thaw (GObject *object, gboolean take_ref)
731
0
{
732
0
  GObjectNotifyQueue *nqueue;
733
734
0
  nqueue = _g_datalist_id_update_atomic (&object->qdata,
735
0
                                         quark_notify_queue,
736
0
                                         g_object_notify_queue_thaw_cb,
737
0
                                         object);
738
739
0
  if (!nqueue)
740
0
    return;
741
742
0
  if (nqueue->len > 0)
743
0
    {
744
0
      guint16 i;
745
0
      guint16 j;
746
747
      /* Reverse the list. This is the order that we historically had. */
748
0
      for (i = 0, j = nqueue->len - 1u; i < j; i++, j--)
749
0
        {
750
0
          GParamSpec *tmp;
751
752
0
          tmp = nqueue->pspecs[i];
753
0
          nqueue->pspecs[i] = nqueue->pspecs[j];
754
0
          nqueue->pspecs[j] = tmp;
755
0
        }
756
757
0
      if (take_ref)
758
0
        g_object_ref (object);
759
760
0
      G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, nqueue->len, nqueue->pspecs);
761
762
0
      if (take_ref)
763
0
        g_object_unref (object);
764
0
    }
765
766
0
  g_free (nqueue);
767
0
}
768
769
static gpointer
770
g_object_notify_queue_add_cb (gpointer *data,
771
                              GDestroyNotify *destroy_notify,
772
                              gpointer user_data)
773
0
{
774
0
  GParamSpec *pspec = ((gpointer *) user_data)[0];
775
0
  gboolean in_init = GPOINTER_TO_INT (((gpointer *) user_data)[1]);
776
0
  GObjectNotifyQueue *nqueue = *data;
777
0
  guint16 i;
778
779
0
  if (!nqueue)
780
0
    {
781
0
      if (!in_init)
782
0
        {
783
          /* We are not in-init and are currently not frozen. There is nothing
784
           * to do. We return FALSE to the caller, which then will dispatch
785
           * the event right away. */
786
0
          return GINT_TO_POINTER (FALSE);
787
0
        }
788
789
      /* If we are "in_init", we always want to create a queue now.
790
       *
791
       * Note in that case, the freeze will be balanced at the end of object
792
       * initialization.
793
       *
794
       * We only ensure that a nqueue exists. If it doesn't exist, we create
795
       * it (and freeze once). If it already exists (and is frozen), we don't
796
       * freeze an additional time. */
797
0
      nqueue = g_object_notify_queue_new_frozen ();
798
0
      *data = nqueue;
799
0
      *destroy_notify = g_free;
800
0
    }
801
0
  else if (_is_notify_queue_empty (nqueue))
802
0
    {
803
0
      nqueue = g_object_notify_queue_new_frozen ();
804
0
      *data = nqueue;
805
0
      *destroy_notify = g_free;
806
0
    }
807
0
  else
808
0
    {
809
0
      for (i = 0; i < nqueue->len; i++)
810
0
        {
811
0
          if (nqueue->pspecs[i] == pspec)
812
0
            goto out;
813
0
        }
814
815
0
      if (G_UNLIKELY (nqueue->len == nqueue->alloc))
816
0
        {
817
0
          guint32 alloc;
818
819
0
          alloc = ((guint32) nqueue->alloc) * 2u;
820
0
          if (alloc >= G_MAXUINT16)
821
0
            {
822
0
              if (G_UNLIKELY (nqueue->len >= G_MAXUINT16))
823
0
                g_error ("g_object_notify_queue_add_cb: cannot track more than 65535 properties for freeze notification");
824
0
              alloc = G_MAXUINT16;
825
0
            }
826
0
          nqueue = g_realloc (nqueue, g_object_notify_queue_alloc_size (alloc));
827
0
          nqueue->alloc = alloc;
828
829
0
          *data = nqueue;
830
0
        }
831
0
    }
832
833
0
  nqueue->pspecs[nqueue->len++] = pspec;
834
835
0
out:
836
0
  return GINT_TO_POINTER (TRUE);
837
0
}
838
839
static gboolean
840
g_object_notify_queue_add (GObject *object,
841
                           GParamSpec *pspec,
842
                           gboolean in_init)
843
0
{
844
0
  gpointer result;
845
846
0
  result = _g_datalist_id_update_atomic (&object->qdata,
847
0
                                         quark_notify_queue,
848
0
                                         g_object_notify_queue_add_cb,
849
0
                                         ((gpointer[]){ pspec, GINT_TO_POINTER (!!in_init) }));
850
851
0
  return GPOINTER_TO_INT (result);
852
0
}
853
854
#ifdef  G_ENABLE_DEBUG
855
G_LOCK_DEFINE_STATIC     (debug_objects);
856
static guint     debug_objects_count = 0;
857
static GHashTable *debug_objects_ht = NULL;
858
859
static void
860
debug_objects_foreach (gpointer key,
861
           gpointer value,
862
           gpointer user_data)
863
0
{
864
0
  GObject *object = value;
865
866
0
  g_message ("[%p] stale %s\tref_count=%u",
867
0
       object,
868
0
       G_OBJECT_TYPE_NAME (object),
869
0
       object->ref_count);
870
0
}
871
872
#ifdef G_HAS_CONSTRUCTORS
873
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
874
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
875
#endif
876
G_DEFINE_DESTRUCTOR(debug_objects_atexit)
877
#endif /* G_HAS_CONSTRUCTORS */
878
879
static void
880
debug_objects_atexit (void)
881
0
{
882
0
  GOBJECT_IF_DEBUG (OBJECTS,
883
0
    {
884
0
      G_LOCK (debug_objects);
885
0
      g_message ("stale GObjects: %u", debug_objects_count);
886
0
      g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
887
0
      G_UNLOCK (debug_objects);
888
0
    });
889
0
}
890
#endif  /* G_ENABLE_DEBUG */
891
892
void
893
_g_object_type_init (void)
894
20
{
895
20
  static gboolean initialized = FALSE;
896
20
  static const GTypeFundamentalInfo finfo = {
897
20
    G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
898
20
  };
899
20
  GTypeInfo info = {
900
20
    sizeof (GObjectClass),
901
20
    (GBaseInitFunc) g_object_base_class_init,
902
20
    (GBaseFinalizeFunc) g_object_base_class_finalize,
903
20
    (GClassInitFunc) g_object_do_class_init,
904
20
    NULL  /* class_destroy */,
905
20
    NULL  /* class_data */,
906
20
    sizeof (GObject),
907
20
    0   /* n_preallocs */,
908
20
    (GInstanceInitFunc) g_object_init,
909
20
    NULL, /* value_table */
910
20
  };
911
20
  static const GTypeValueTable value_table = {
912
20
    g_value_object_init,    /* value_init */
913
20
    g_value_object_free_value,    /* value_free */
914
20
    g_value_object_copy_value,    /* value_copy */
915
20
    g_value_object_peek_pointer,  /* value_peek_pointer */
916
20
    "p",        /* collect_format */
917
20
    g_value_object_collect_value, /* collect_value */
918
20
    "p",        /* lcopy_format */
919
20
    g_value_object_lcopy_value,   /* lcopy_value */
920
20
  };
921
20
  GType type G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
922
  
923
20
  g_return_if_fail (initialized == FALSE);
924
20
  initialized = TRUE;
925
  
926
  /* G_TYPE_OBJECT
927
   */
928
20
  info.value_table = &value_table;
929
20
  type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
930
20
  g_assert (type == G_TYPE_OBJECT);
931
20
  g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
932
933
20
#if G_ENABLE_DEBUG
934
  /* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS
935
   * conditional in between, as the C spec leaves conditionals inside macro
936
   * expansions as undefined behavior. Only GCC and Clang are known to work
937
   * but compilation breaks on MSVC.
938
   *
939
   * See: https://bugzilla.gnome.org/show_bug.cgi?id=769504
940
   */
941
20
  if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \
942
0
    {
943
0
      debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
944
# ifndef G_HAS_CONSTRUCTORS
945
      g_atexit (debug_objects_atexit);
946
# endif /* G_HAS_CONSTRUCTORS */
947
0
    }
948
20
#endif /* G_ENABLE_DEBUG */
949
950
#if HAVE_PRIVATE
951
  GObject_private_offset =
952
      g_type_add_instance_private (G_TYPE_OBJECT, sizeof (GObjectPrivate));
953
#endif
954
20
}
955
956
/* Initialize the global GParamSpecPool; this function needs to be
957
 * called whenever we access the GParamSpecPool and we cannot guarantee
958
 * that g_object_do_class_init() has been called: for instance, by the
959
 * interface property API.
960
 *
961
 * To avoid yet another global lock, we use atomic pointer checks: the
962
 * first caller of this function will win the race. Any other access to
963
 * the GParamSpecPool is done under its own mutex.
964
 */
965
static inline GParamSpecPool *
966
g_object_maybe_init_pspec_pool (void)
967
75
{
968
75
  GParamSpecPool *pool = g_atomic_pointer_get (&pspec_pool);
969
970
75
  if (G_UNLIKELY (pool == NULL))
971
9
    {
972
9
      GParamSpecPool *new_pool = g_param_spec_pool_new (TRUE);
973
9
      if (g_atomic_pointer_compare_and_exchange_full (&pspec_pool, NULL,
974
9
                                                      new_pool, &pool))
975
9
        pool = g_steal_pointer (&new_pool);
976
977
9
      g_clear_pointer (&new_pool, g_param_spec_pool_free);
978
9
    }
979
980
75
  return pool;
981
75
}
982
983
static void
984
g_object_base_class_init (GObjectClass *class)
985
33
{
986
33
  GObjectClass *pclass = g_type_class_peek_parent (class);
987
988
  /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
989
33
  class->flags &= (unsigned) ~CLASS_HAS_DERIVED_CLASS_FLAG;
990
991
33
  if (pclass)
992
24
    pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
993
994
  /* reset instance specific fields and methods that don't get inherited */
995
33
  class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
996
33
  class->n_construct_properties = g_slist_length (class->construct_properties);
997
33
  class->get_property = NULL;
998
33
  class->set_property = NULL;
999
33
  class->pspecs = NULL;
1000
33
  class->n_pspecs = 0;
1001
33
}
1002
1003
static void
1004
g_object_base_class_finalize (GObjectClass *class)
1005
0
{
1006
0
  GList *list, *node;
1007
0
  GParamSpecPool *param_spec_pool;
1008
  
1009
0
  _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
1010
1011
0
  g_slist_free (class->construct_properties);
1012
0
  class->construct_properties = NULL;
1013
0
  class->n_construct_properties = 0;
1014
0
  param_spec_pool = g_atomic_pointer_get (&pspec_pool);
1015
0
  list = g_param_spec_pool_list_owned (param_spec_pool, G_OBJECT_CLASS_TYPE (class));
1016
0
  for (node = list; node; node = node->next)
1017
0
    {
1018
0
      GParamSpec *pspec = node->data;
1019
0
      g_param_spec_pool_remove (param_spec_pool, pspec);
1020
0
      PARAM_SPEC_SET_PARAM_ID (pspec, 0);
1021
0
      g_param_spec_unref (pspec);
1022
0
    }
1023
0
  g_list_free (list);
1024
0
}
1025
1026
static void
1027
g_object_do_class_init (GObjectClass *class)
1028
9
{
1029
9
  quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
1030
9
  quark_weak_notifies = g_quark_from_static_string ("GObject-weak-notifies");
1031
9
  quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
1032
9
  quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
1033
9
  quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
1034
1035
9
  g_atomic_pointer_set (&_local_g_datalist_id_update_atomic, GLIB_PRIVATE_CALL (g_datalist_id_update_atomic));
1036
1037
9
  g_object_maybe_init_pspec_pool ();
1038
1039
9
  class->constructor = g_object_constructor;
1040
9
  class->constructed = g_object_constructed;
1041
9
  class->set_property = g_object_do_set_property;
1042
9
  class->get_property = g_object_do_get_property;
1043
9
  class->dispose = g_object_real_dispose;
1044
9
  class->finalize = g_object_finalize;
1045
9
  class->dispatch_properties_changed = g_object_dispatch_properties_changed;
1046
9
  class->notify = NULL;
1047
1048
  /**
1049
   * GObject::notify:
1050
   * @gobject: the object which received the signal.
1051
   * @pspec: the #GParamSpec of the property which changed.
1052
   *
1053
   * The notify signal is emitted on an object when one of its properties has
1054
   * its value set through g_object_set_property(), g_object_set(), et al.
1055
   *
1056
   * Note that getting this signal doesn’t itself guarantee that the value of
1057
   * the property has actually changed. When it is emitted is determined by the
1058
   * derived GObject class. If the implementor did not create the property with
1059
   * %G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results
1060
   * in ::notify being emitted, even if the new value is the same as the old.
1061
   * If they did pass %G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only
1062
   * when they explicitly call g_object_notify() or g_object_notify_by_pspec(),
1063
   * and common practice is to do that only when the value has actually changed.
1064
   *
1065
   * This signal is typically used to obtain change notification for a
1066
   * single property, by specifying the property name as a detail in the
1067
   * g_signal_connect() call, like this:
1068
   *
1069
   * |[<!-- language="C" --> 
1070
   * g_signal_connect (text_view->buffer, "notify::paste-target-list",
1071
   *                   G_CALLBACK (gtk_text_view_target_list_notify),
1072
   *                   text_view)
1073
   * ]|
1074
   *
1075
   * It is important to note that you must use
1076
   * [canonical parameter names][class@GObject.ParamSpec#parameter-names] as
1077
   * detail strings for the notify signal.
1078
   */
1079
9
  gobject_signals[NOTIFY] =
1080
9
    g_signal_new (g_intern_static_string ("notify"),
1081
9
      G_TYPE_FROM_CLASS (class),
1082
9
      G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
1083
9
      G_STRUCT_OFFSET (GObjectClass, notify),
1084
9
      NULL, NULL,
1085
9
      NULL,
1086
9
      G_TYPE_NONE,
1087
9
      1, G_TYPE_PARAM);
1088
1089
  /* Install a check function that we'll use to verify that classes that
1090
   * implement an interface implement all properties for that interface
1091
   */
1092
9
  g_type_add_interface_check (NULL, object_interface_check_properties);
1093
1094
#if HAVE_PRIVATE
1095
  g_type_class_adjust_private_offset (class, &GObject_private_offset);
1096
#endif
1097
9
}
1098
1099
/* Sinks @pspec if it’s a floating ref. */
1100
static inline gboolean
1101
install_property_internal (GType       g_type,
1102
         guint       property_id,
1103
         GParamSpec *pspec)
1104
66
{
1105
66
  GParamSpecPool *param_spec_pool;
1106
66
  g_param_spec_ref_sink (pspec);
1107
1108
66
  param_spec_pool = g_object_maybe_init_pspec_pool ();
1109
1110
66
  if (g_param_spec_pool_lookup (param_spec_pool, pspec->name, g_type, FALSE))
1111
0
    {
1112
0
      g_critical ("When installing property: type '%s' already has a property named '%s'",
1113
0
                  g_type_name (g_type),
1114
0
                  pspec->name);
1115
0
      g_param_spec_unref (pspec);
1116
0
      return FALSE;
1117
0
    }
1118
1119
66
  PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1120
66
  g_param_spec_pool_insert (param_spec_pool, g_steal_pointer (&pspec), g_type);
1121
66
  return TRUE;
1122
66
}
1123
1124
static gboolean
1125
validate_pspec_to_install (GParamSpec *pspec)
1126
66
{
1127
66
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1128
66
  g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */
1129
1130
66
  g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE);
1131
1132
66
  if (pspec->flags & G_PARAM_CONSTRUCT)
1133
66
    g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE);
1134
1135
66
  if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1136
66
    g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE);
1137
1138
66
  return TRUE;
1139
66
}
1140
1141
/* Sinks @pspec if it’s a floating ref. */
1142
static gboolean
1143
validate_and_install_class_property (GObjectClass *class,
1144
                                     GType         oclass_type,
1145
                                     GType         parent_type,
1146
                                     guint         property_id,
1147
                                     GParamSpec   *pspec)
1148
66
{
1149
66
  if (!validate_pspec_to_install (pspec))
1150
0
    {
1151
0
      g_param_spec_ref_sink (pspec);
1152
0
      g_param_spec_unref (pspec);
1153
0
      return FALSE;
1154
0
    }
1155
1156
66
  if (pspec->flags & G_PARAM_WRITABLE)
1157
66
    g_return_val_if_fail (class->set_property != NULL, FALSE);
1158
66
  if (pspec->flags & G_PARAM_READABLE)
1159
66
    g_return_val_if_fail (class->get_property != NULL, FALSE);
1160
1161
66
  class->flags |= CLASS_HAS_PROPS_FLAG;
1162
66
  if (install_property_internal (oclass_type, property_id, pspec))
1163
66
    {
1164
66
      if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1165
25
        {
1166
25
          class->construct_properties = g_slist_append (class->construct_properties, pspec);
1167
25
          class->n_construct_properties += 1;
1168
25
        }
1169
1170
      /* for property overrides of construct properties, we have to get rid
1171
       * of the overridden inherited construct property
1172
       */
1173
66
      pspec = g_param_spec_pool_lookup (g_atomic_pointer_get (&pspec_pool),
1174
66
                                        pspec->name, parent_type, TRUE);
1175
66
      if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1176
0
        {
1177
0
          class->construct_properties = g_slist_remove (class->construct_properties, pspec);
1178
0
          class->n_construct_properties -= 1;
1179
0
        }
1180
1181
66
      return TRUE;
1182
66
    }
1183
0
  else
1184
0
    return FALSE;
1185
66
}
1186
1187
/**
1188
 * g_object_class_install_property:
1189
 * @oclass: a #GObjectClass
1190
 * @property_id: the id for the new property
1191
 * @pspec: the #GParamSpec for the new property
1192
 *
1193
 * Installs a new property.
1194
 *
1195
 * All properties should be installed during the class initializer.  It
1196
 * is possible to install properties after that, but doing so is not
1197
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
1198
 * use of properties on the same type on other threads.
1199
 *
1200
 * Note that it is possible to redefine a property in a derived class,
1201
 * by installing a property with the same name. This can be useful at times,
1202
 * e.g. to change the range of allowed values or the default value.
1203
 */
1204
void
1205
g_object_class_install_property (GObjectClass *class,
1206
         guint         property_id,
1207
         GParamSpec   *pspec)
1208
66
{
1209
66
  GType oclass_type, parent_type;
1210
1211
66
  g_return_if_fail (G_IS_OBJECT_CLASS (class));
1212
66
  g_return_if_fail (property_id > 0);
1213
1214
66
  oclass_type = G_OBJECT_CLASS_TYPE (class);
1215
66
  parent_type = g_type_parent (oclass_type);
1216
1217
66
  if (CLASS_HAS_DERIVED_CLASS (class))
1218
66
    g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
1219
1220
66
  (void) validate_and_install_class_property (class,
1221
66
                                              oclass_type,
1222
66
                                              parent_type,
1223
66
                                              property_id,
1224
66
                                              pspec);
1225
66
}
1226
1227
typedef struct {
1228
  const char *name;
1229
  GParamSpec *pspec;
1230
} PspecEntry;
1231
1232
static int
1233
compare_pspec_entry (const void *a,
1234
                     const void *b)
1235
0
{
1236
0
  const PspecEntry *ae = a;
1237
0
  const PspecEntry *be = b;
1238
1239
0
  return ae->name < be->name ? -1 : (ae->name > be->name ? 1 : 0);
1240
0
}
1241
1242
/* This uses pointer comparisons with @property_name, so
1243
 * will only work with string literals. */
1244
static inline GParamSpec *
1245
find_pspec (GObjectClass *class,
1246
            const char   *property_name)
1247
3.88k
{
1248
3.88k
  const PspecEntry *pspecs = (const PspecEntry *)class->pspecs;
1249
3.88k
  gsize n_pspecs = class->n_pspecs;
1250
1251
3.88k
  g_assert (n_pspecs <= G_MAXSSIZE);
1252
1253
  /* The limit for choosing between linear and binary search is
1254
   * fairly arbitrary.
1255
   *
1256
   * Both searches use pointer comparisons against @property_name.
1257
   * If this function is called with a non-static @property_name,
1258
   * it will fall through to the g_param_spec_pool_lookup() case.
1259
   * That’s OK; this is an opportunistic optimisation which relies
1260
   * on the fact that *most* (but not all) property lookups use
1261
   * static property names.
1262
   */
1263
3.88k
  if (n_pspecs < 10)
1264
3.88k
    {
1265
3.88k
      for (gsize i = 0; i < n_pspecs; i++)
1266
0
        {
1267
0
          if (pspecs[i].name == property_name)
1268
0
            return pspecs[i].pspec;
1269
0
        }
1270
3.88k
    }
1271
0
  else
1272
0
    {
1273
0
      gssize lower = 0;
1274
0
      gssize upper = (int)class->n_pspecs - 1;
1275
0
      gssize mid;
1276
1277
0
      while (lower <= upper)
1278
0
        {
1279
0
          mid = (lower + upper) / 2;
1280
1281
0
          if (property_name < pspecs[mid].name)
1282
0
            upper = mid - 1;
1283
0
          else if (property_name > pspecs[mid].name)
1284
0
            lower = mid + 1;
1285
0
          else
1286
0
            return pspecs[mid].pspec;
1287
0
        }
1288
0
    }
1289
1290
3.88k
  return g_param_spec_pool_lookup (g_atomic_pointer_get (&pspec_pool),
1291
3.88k
                                   property_name,
1292
3.88k
                                   ((GTypeClass *)class)->g_type,
1293
3.88k
                                   TRUE);
1294
3.88k
}
1295
1296
/**
1297
 * g_object_class_install_properties:
1298
 * @oclass: a #GObjectClass
1299
 * @n_pspecs: the length of the #GParamSpecs array
1300
 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
1301
 *   defining the new properties
1302
 *
1303
 * Installs new properties from an array of #GParamSpecs.
1304
 *
1305
 * All properties should be installed during the class initializer.  It
1306
 * is possible to install properties after that, but doing so is not
1307
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
1308
 * use of properties on the same type on other threads.
1309
 *
1310
 * The property id of each property is the index of each #GParamSpec in
1311
 * the @pspecs array.
1312
 *
1313
 * The property id of 0 is treated specially by #GObject and it should not
1314
 * be used to store a #GParamSpec.
1315
 *
1316
 * This function should be used if you plan to use a static array of
1317
 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
1318
 * class initialization:
1319
 *
1320
 * |[<!-- language="C" --> 
1321
 * typedef enum {
1322
 *   PROP_FOO = 1,
1323
 *   PROP_BAR,
1324
 *   N_PROPERTIES
1325
 * } MyObjectProperty;
1326
 *
1327
 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
1328
 *
1329
 * static void
1330
 * my_object_class_init (MyObjectClass *klass)
1331
 * {
1332
 *   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1333
 *
1334
 *   obj_properties[PROP_FOO] =
1335
 *     g_param_spec_int ("foo", NULL, NULL,
1336
 *                       -1, G_MAXINT,
1337
 *                       0,
1338
 *                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1339
 *
1340
 *   obj_properties[PROP_BAR] =
1341
 *     g_param_spec_string ("bar", NULL, NULL,
1342
 *                          NULL,
1343
 *                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1344
 *
1345
 *   gobject_class->set_property = my_object_set_property;
1346
 *   gobject_class->get_property = my_object_get_property;
1347
 *   g_object_class_install_properties (gobject_class,
1348
 *                                      G_N_ELEMENTS (obj_properties),
1349
 *                                      obj_properties);
1350
 * }
1351
 * ]|
1352
 *
1353
 * allows calling g_object_notify_by_pspec() to notify of property changes:
1354
 *
1355
 * |[<!-- language="C" --> 
1356
 * void
1357
 * my_object_set_foo (MyObject *self, gint foo)
1358
 * {
1359
 *   if (self->foo != foo)
1360
 *     {
1361
 *       self->foo = foo;
1362
 *       g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
1363
 *     }
1364
 *  }
1365
 * ]|
1366
 *
1367
 * Since: 2.26
1368
 */
1369
void
1370
g_object_class_install_properties (GObjectClass  *oclass,
1371
                                   guint          n_pspecs,
1372
                                   GParamSpec   **pspecs)
1373
0
{
1374
0
  GType oclass_type, parent_type;
1375
0
  guint i;
1376
1377
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
1378
0
  g_return_if_fail (n_pspecs > 1);
1379
0
  g_return_if_fail (pspecs[0] == NULL);
1380
1381
0
  if (CLASS_HAS_DERIVED_CLASS (oclass))
1382
0
    g_error ("Attempt to add properties to %s after it was derived",
1383
0
             G_OBJECT_CLASS_NAME (oclass));
1384
1385
0
  oclass_type = G_OBJECT_CLASS_TYPE (oclass);
1386
0
  parent_type = g_type_parent (oclass_type);
1387
1388
  /* we skip the first element of the array as it would have a 0 prop_id */
1389
0
  for (i = 1; i < n_pspecs; i++)
1390
0
    {
1391
0
      GParamSpec *pspec = pspecs[i];
1392
1393
0
      if (!validate_and_install_class_property (oclass,
1394
0
                                                oclass_type,
1395
0
                                                parent_type,
1396
0
                                                i,
1397
0
                                                pspec))
1398
0
        {
1399
0
          break;
1400
0
        }
1401
0
    }
1402
1403
  /* Save a copy of the pspec array inside the class struct. This
1404
   * makes it faster to look up pspecs for the class in future when
1405
   * acting on those properties.
1406
   *
1407
   * If a pspec is not in this cache array, calling code will fall
1408
   * back to using g_param_spec_pool_lookup(), so a pspec not being
1409
   * in this array is a (potential) performance problem but not a
1410
   * correctness problem. */
1411
0
  if (oclass->pspecs == NULL)
1412
0
    {
1413
0
      PspecEntry *entries;
1414
1415
0
      entries = g_new (PspecEntry, n_pspecs - 1);
1416
1417
0
      for (i = 1; i < n_pspecs; i++)
1418
0
        {
1419
0
          entries[i - 1].name = pspecs[i]->name;
1420
0
          entries[i - 1].pspec = pspecs[i];
1421
0
        }
1422
1423
0
      qsort (entries, n_pspecs - 1, sizeof (PspecEntry), compare_pspec_entry);
1424
1425
0
      oclass->pspecs = entries;
1426
0
      oclass->n_pspecs = n_pspecs - 1;
1427
0
    }
1428
0
}
1429
1430
/**
1431
 * g_object_interface_install_property:
1432
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
1433
 *    interface, or the default
1434
 *  vtable for the interface.
1435
 * @pspec: the #GParamSpec for the new property
1436
 *
1437
 * Add a property to an interface; this is only useful for interfaces
1438
 * that are added to GObject-derived types. Adding a property to an
1439
 * interface forces all objects classes with that interface to have a
1440
 * compatible property. The compatible property could be a newly
1441
 * created #GParamSpec, but normally
1442
 * g_object_class_override_property() will be used so that the object
1443
 * class only needs to provide an implementation and inherits the
1444
 * property description, default value, bounds, and so forth from the
1445
 * interface property.
1446
 *
1447
 * This function is meant to be called from the interface's default
1448
 * vtable initialization function (the @class_init member of
1449
 * #GTypeInfo.) It must not be called after after @class_init has
1450
 * been called for any object types implementing this interface.
1451
 *
1452
 * If @pspec is a floating reference, it will be consumed.
1453
 *
1454
 * Since: 2.4
1455
 */
1456
void
1457
g_object_interface_install_property (gpointer      g_iface,
1458
             GParamSpec   *pspec)
1459
0
{
1460
0
  GTypeInterface *iface_class = g_iface;
1461
  
1462
0
  g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
1463
0
  g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
1464
1465
0
  if (!validate_pspec_to_install (pspec))
1466
0
    {
1467
0
      g_param_spec_ref_sink (pspec);
1468
0
      g_param_spec_unref (pspec);
1469
0
      return;
1470
0
    }
1471
1472
0
  (void) install_property_internal (iface_class->g_type, 0, pspec);
1473
0
}
1474
1475
/* Inlined version of g_param_spec_get_redirect_target(), for speed */
1476
static inline void
1477
param_spec_follow_override (GParamSpec **pspec)
1478
10.0k
{
1479
10.0k
  if (((GTypeInstance *) (*pspec))->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
1480
0
    *pspec = ((GParamSpecOverride *) (*pspec))->overridden;
1481
10.0k
}
1482
1483
/**
1484
 * g_object_class_find_property:
1485
 * @oclass: a #GObjectClass
1486
 * @property_name: the name of the property to look up
1487
 *
1488
 * Looks up the #GParamSpec for a property of a class.
1489
 *
1490
 * Returns: (transfer none): the #GParamSpec for the property, or
1491
 *          %NULL if the class doesn't have a property of that name
1492
 */
1493
GParamSpec*
1494
g_object_class_find_property (GObjectClass *class,
1495
            const gchar  *property_name)
1496
0
{
1497
0
  GParamSpec *pspec;
1498
1499
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
1500
0
  g_return_val_if_fail (property_name != NULL, NULL);
1501
1502
0
  pspec = find_pspec (class, property_name);
1503
1504
0
  if (pspec)
1505
0
    param_spec_follow_override (&pspec);
1506
1507
0
  return pspec;
1508
0
}
1509
1510
/**
1511
 * g_object_interface_find_property:
1512
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
1513
 *  interface, or the default vtable for the interface
1514
 * @property_name: name of a property to look up.
1515
 *
1516
 * Find the #GParamSpec with the given name for an
1517
 * interface. Generally, the interface vtable passed in as @g_iface
1518
 * will be the default vtable from g_type_default_interface_ref(), or,
1519
 * if you know the interface has already been loaded,
1520
 * g_type_default_interface_peek().
1521
 *
1522
 * Since: 2.4
1523
 *
1524
 * Returns: (transfer none): the #GParamSpec for the property of the
1525
 *          interface with the name @property_name, or %NULL if no
1526
 *          such property exists.
1527
 */
1528
GParamSpec*
1529
g_object_interface_find_property (gpointer      g_iface,
1530
          const gchar  *property_name)
1531
0
{
1532
0
  GTypeInterface *iface_class = g_iface;
1533
0
  GParamSpecPool *param_spec_pool;
1534
  
1535
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
1536
0
  g_return_val_if_fail (property_name != NULL, NULL);
1537
1538
0
  param_spec_pool = g_object_maybe_init_pspec_pool ();
1539
1540
0
  return g_param_spec_pool_lookup (param_spec_pool,
1541
0
           property_name,
1542
0
           iface_class->g_type,
1543
0
           FALSE);
1544
0
}
1545
1546
/**
1547
 * g_object_class_override_property:
1548
 * @oclass: a #GObjectClass
1549
 * @property_id: the new property ID
1550
 * @name: the name of a property registered in a parent class or
1551
 *  in an interface of this class.
1552
 *
1553
 * Registers @property_id as referring to a property with the name
1554
 * @name in a parent class or in an interface implemented by @oclass.
1555
 * This allows this class to "override" a property implementation in
1556
 * a parent class or to provide the implementation of a property from
1557
 * an interface.
1558
 *
1559
 * Internally, overriding is implemented by creating a property of type
1560
 * #GParamSpecOverride; generally operations that query the properties of
1561
 * the object class, such as g_object_class_find_property() or
1562
 * g_object_class_list_properties() will return the overridden
1563
 * property. However, in one case, the @construct_properties argument of
1564
 * the @constructor virtual function, the #GParamSpecOverride is passed
1565
 * instead, so that the @param_id field of the #GParamSpec will be
1566
 * correct.  For virtually all uses, this makes no difference. If you
1567
 * need to get the overridden property, you can call
1568
 * g_param_spec_get_redirect_target().
1569
 *
1570
 * Since: 2.4
1571
 */
1572
void
1573
g_object_class_override_property (GObjectClass *oclass,
1574
          guint         property_id,
1575
          const gchar  *name)
1576
0
{
1577
0
  GParamSpecPool *param_spec_pool;
1578
0
  GParamSpec *overridden = NULL;
1579
0
  GParamSpec *new;
1580
0
  GType parent_type;
1581
  
1582
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
1583
0
  g_return_if_fail (property_id > 0);
1584
0
  g_return_if_fail (name != NULL);
1585
1586
0
  param_spec_pool = g_atomic_pointer_get (&pspec_pool);
1587
1588
  /* Find the overridden property; first check parent types
1589
   */
1590
0
  parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
1591
0
  if (parent_type != G_TYPE_NONE)
1592
0
    overridden = g_param_spec_pool_lookup (param_spec_pool,
1593
0
             name,
1594
0
             parent_type,
1595
0
             TRUE);
1596
0
  if (!overridden)
1597
0
    {
1598
0
      GType *ifaces;
1599
0
      guint n_ifaces;
1600
      
1601
      /* Now check interfaces
1602
       */
1603
0
      ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
1604
0
      while (n_ifaces-- && !overridden)
1605
0
  {
1606
0
    overridden = g_param_spec_pool_lookup (param_spec_pool,
1607
0
             name,
1608
0
             ifaces[n_ifaces],
1609
0
             FALSE);
1610
0
  }
1611
      
1612
0
      g_free (ifaces);
1613
0
    }
1614
1615
0
  if (!overridden)
1616
0
    {
1617
0
      g_critical ("%s: Can't find property to override for '%s::%s'",
1618
0
      G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
1619
0
      return;
1620
0
    }
1621
1622
0
  new = g_param_spec_override (name, overridden);
1623
0
  g_object_class_install_property (oclass, property_id, new);
1624
0
}
1625
1626
/**
1627
 * g_object_class_list_properties:
1628
 * @oclass: a #GObjectClass
1629
 * @n_properties: (out): return location for the length of the returned array
1630
 *
1631
 * Get an array of #GParamSpec* for all properties of a class.
1632
 *
1633
 * Returns: (array length=n_properties) (transfer container): an array of
1634
 *          #GParamSpec* which should be freed after use
1635
 */
1636
GParamSpec** /* free result */
1637
g_object_class_list_properties (GObjectClass *class,
1638
        guint        *n_properties_p)
1639
0
{
1640
0
  GParamSpec **pspecs;
1641
0
  guint n;
1642
1643
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
1644
1645
0
  pspecs = g_param_spec_pool_list (g_atomic_pointer_get (&pspec_pool),
1646
0
           G_OBJECT_CLASS_TYPE (class),
1647
0
           &n);
1648
0
  if (n_properties_p)
1649
0
    *n_properties_p = n;
1650
1651
0
  return pspecs;
1652
0
}
1653
1654
/**
1655
 * g_object_interface_list_properties:
1656
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
1657
 *  interface, or the default vtable for the interface
1658
 * @n_properties_p: (out): location to store number of properties returned.
1659
 *
1660
 * Lists the properties of an interface.Generally, the interface
1661
 * vtable passed in as @g_iface will be the default vtable from
1662
 * g_type_default_interface_ref(), or, if you know the interface has
1663
 * already been loaded, g_type_default_interface_peek().
1664
 *
1665
 * Since: 2.4
1666
 *
1667
 * Returns: (array length=n_properties_p) (transfer container): a
1668
 *   pointer to an array of pointers to #GParamSpec
1669
 *   structures. The paramspecs are owned by GLib, but the
1670
 *   array should be freed with g_free() when you are done with
1671
 *   it.
1672
 */
1673
GParamSpec**
1674
g_object_interface_list_properties (gpointer      g_iface,
1675
            guint        *n_properties_p)
1676
0
{
1677
0
  GTypeInterface *iface_class = g_iface;
1678
0
  GParamSpecPool *param_spec_pool;
1679
0
  GParamSpec **pspecs;
1680
0
  guint n;
1681
1682
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
1683
1684
0
  param_spec_pool = g_object_maybe_init_pspec_pool ();
1685
1686
0
  pspecs = g_param_spec_pool_list (param_spec_pool,
1687
0
           iface_class->g_type,
1688
0
           &n);
1689
0
  if (n_properties_p)
1690
0
    *n_properties_p = n;
1691
1692
0
  return pspecs;
1693
0
}
1694
1695
static inline guint
1696
object_get_optional_flags (GObject *object)
1697
28.7k
{
1698
28.7k
  return (guint) g_atomic_int_get ((gint *) object_get_optional_flags_p (object));
1699
28.7k
}
1700
1701
static inline void
1702
object_set_optional_flags (GObject *object,
1703
                          guint flags)
1704
6.48k
{
1705
6.48k
  g_atomic_int_or ((gint *) object_get_optional_flags_p (object), (int) flags);
1706
6.48k
}
1707
1708
static inline void
1709
object_unset_optional_flags (GObject *object,
1710
                               guint flags)
1711
6.48k
{
1712
6.48k
  g_atomic_int_and ((gint *) object_get_optional_flags_p (object), (int) ~flags);
1713
6.48k
}
1714
1715
gboolean
1716
_g_object_has_signal_handler (GObject *object)
1717
0
{
1718
0
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_SIGNAL_HANDLER) != 0;
1719
0
}
1720
1721
static inline gboolean
1722
_g_object_has_notify_handler (GObject *object)
1723
5.42k
{
1724
5.42k
  return CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)) ||
1725
5.42k
         (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0;
1726
5.42k
}
1727
1728
void
1729
_g_object_set_has_signal_handler (GObject *object,
1730
                                  guint    signal_id)
1731
0
{
1732
0
  guint flags = OPTIONAL_FLAG_HAS_SIGNAL_HANDLER;
1733
0
  if (signal_id == gobject_signals[NOTIFY])
1734
0
    flags |= OPTIONAL_FLAG_HAS_NOTIFY_HANDLER;
1735
0
  object_set_optional_flags (object, flags);
1736
0
}
1737
1738
static inline gboolean
1739
object_in_construction (GObject *object)
1740
6.48k
{
1741
6.48k
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0;
1742
6.48k
}
1743
1744
static inline void
1745
set_object_in_construction (GObject *object)
1746
6.48k
{
1747
6.48k
  object_set_optional_flags (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1748
6.48k
}
1749
1750
static inline void
1751
unset_object_in_construction (GObject *object)
1752
6.48k
{
1753
6.48k
  object_unset_optional_flags (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1754
6.48k
}
1755
1756
static void
1757
g_object_init (GObject    *object,
1758
         GObjectClass *class)
1759
6.48k
{
1760
6.48k
  object->ref_count = 1;
1761
6.48k
  object->qdata = NULL;
1762
1763
6.48k
  if (CLASS_HAS_PROPS (class) && CLASS_NEEDS_NOTIFY (class))
1764
0
    {
1765
      /* freeze object's notification queue, g_object_new_internal() preserves pairedness */
1766
0
      g_object_notify_queue_freeze (object, TRUE);
1767
0
    }
1768
1769
  /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
1770
6.48k
  set_object_in_construction (object);
1771
1772
6.48k
  GOBJECT_IF_DEBUG (OBJECTS,
1773
6.48k
    {
1774
6.48k
      G_LOCK (debug_objects);
1775
6.48k
      debug_objects_count++;
1776
6.48k
      g_hash_table_add (debug_objects_ht, object);
1777
6.48k
      G_UNLOCK (debug_objects);
1778
6.48k
    });
1779
6.48k
}
1780
1781
static void
1782
g_object_do_set_property (GObject      *object,
1783
        guint         property_id,
1784
        const GValue *value,
1785
        GParamSpec   *pspec)
1786
0
{
1787
0
  switch (property_id)
1788
0
    {
1789
0
    default:
1790
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1791
0
      break;
1792
0
    }
1793
0
}
1794
1795
static void
1796
g_object_do_get_property (GObject     *object,
1797
        guint        property_id,
1798
        GValue      *value,
1799
        GParamSpec  *pspec)
1800
0
{
1801
0
  switch (property_id)
1802
0
    {
1803
0
    default:
1804
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1805
0
      break;
1806
0
    }
1807
0
}
1808
1809
static void
1810
g_object_real_dispose (GObject *object)
1811
6.48k
{
1812
6.48k
  g_signal_handlers_destroy (object);
1813
1814
  /* GWeakNotify and GClosure can call into user code */
1815
6.48k
  g_object_weak_release_all (object, FALSE);
1816
6.48k
  closure_array_destroy_all (object);
1817
6.48k
}
1818
1819
static gboolean
1820
g_diagnostic_is_enabled (void)
1821
6.48k
{
1822
6.48k
  static const char *g_enable_diagnostic = NULL;
1823
1824
6.48k
  if (g_once_init_enter_pointer (&g_enable_diagnostic))
1825
9
    {
1826
9
      const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1827
1828
9
      if (value == NULL)
1829
9
        value = "0";
1830
1831
9
      g_once_init_leave_pointer (&g_enable_diagnostic, value);
1832
9
    }
1833
1834
6.48k
  return g_enable_diagnostic[0] == '1';
1835
6.48k
}
1836
1837
#ifdef G_ENABLE_DEBUG
1838
static gboolean
1839
floating_check (GObject *object)
1840
6.48k
{
1841
6.48k
  if (g_diagnostic_is_enabled ())
1842
0
    return g_object_is_floating (object);
1843
1844
6.48k
  return FALSE;
1845
6.48k
}
1846
#endif
1847
1848
static void
1849
g_object_finalize (GObject *object)
1850
6.48k
{
1851
6.48k
#ifdef G_ENABLE_DEBUG
1852
6.48k
  if (object_in_construction (object))
1853
0
    {
1854
0
      g_critical ("object %s %p finalized while still in-construction",
1855
0
                  G_OBJECT_TYPE_NAME (object), object);
1856
0
    }
1857
1858
6.48k
 if (floating_check (object))
1859
0
   {
1860
0
      g_critical ("A floating object %s %p was finalized. This means that someone\n"
1861
0
                  "called g_object_unref() on an object that had only a floating\n"
1862
0
                  "reference; the initial floating reference is not owned by anyone\n"
1863
0
                  "and must be removed with g_object_ref_sink().",
1864
0
                  G_OBJECT_TYPE_NAME (object), object);
1865
0
   }
1866
6.48k
#endif
1867
1868
6.48k
  g_datalist_clear (&object->qdata);
1869
  
1870
6.48k
  GOBJECT_IF_DEBUG (OBJECTS,
1871
6.48k
    {
1872
6.48k
      G_LOCK (debug_objects);
1873
6.48k
      g_assert (g_hash_table_contains (debug_objects_ht, object));
1874
6.48k
      g_hash_table_remove (debug_objects_ht, object);
1875
6.48k
      debug_objects_count--;
1876
6.48k
      G_UNLOCK (debug_objects);
1877
6.48k
    });
1878
6.48k
}
1879
1880
static void
1881
g_object_dispatch_properties_changed (GObject     *object,
1882
              guint        n_pspecs,
1883
              GParamSpec **pspecs)
1884
0
{
1885
0
  guint i;
1886
1887
0
  for (i = 0; i < n_pspecs; i++)
1888
0
    g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1889
0
}
1890
1891
/**
1892
 * g_object_run_dispose:
1893
 * @object: a #GObject
1894
 *
1895
 * Releases all references to other objects. This can be used to break
1896
 * reference cycles.
1897
 *
1898
 * This function should only be called from object system implementations.
1899
 */
1900
void
1901
g_object_run_dispose (GObject *object)
1902
0
{
1903
0
  WeakRefData *wrdata;
1904
1905
0
  g_return_if_fail (G_IS_OBJECT (object));
1906
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0);
1907
1908
0
  g_object_ref (object);
1909
1910
0
  TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1911
0
  G_OBJECT_GET_CLASS (object)->dispose (object);
1912
0
  TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1913
1914
0
  if ((object_get_optional_flags (object) & OPTIONAL_FLAG_EVER_HAD_WEAK_REF))
1915
0
    {
1916
0
      wrdata = weak_ref_data_get_surely (object);
1917
0
      weak_ref_data_lock (wrdata);
1918
0
      weak_ref_data_clear_list (wrdata, object);
1919
0
      weak_ref_data_unlock (wrdata);
1920
0
    }
1921
1922
0
  g_object_unref (object);
1923
0
}
1924
1925
/**
1926
 * g_object_freeze_notify:
1927
 * @object: a #GObject
1928
 *
1929
 * Increases the freeze count on @object. If the freeze count is
1930
 * non-zero, the emission of "notify" signals on @object is
1931
 * stopped. The signals are queued until the freeze count is decreased
1932
 * to zero. Duplicate notifications are squashed so that at most one
1933
 * #GObject::notify signal is emitted for each property modified while the
1934
 * object is frozen.
1935
 *
1936
 * This is necessary for accessors that modify multiple properties to prevent
1937
 * premature notification while the object is still being modified.
1938
 */
1939
void
1940
g_object_freeze_notify (GObject *object)
1941
0
{
1942
0
  g_return_if_fail (G_IS_OBJECT (object));
1943
1944
0
#ifndef G_DISABLE_CHECKS
1945
0
  if (G_UNLIKELY (g_atomic_int_get (&object->ref_count) <= 0))
1946
0
    {
1947
0
      g_critical ("Attempting to freeze the notification queue for object %s[%p]; "
1948
0
                  "Property notification does not work during instance finalization.",
1949
0
                  G_OBJECT_TYPE_NAME (object),
1950
0
                  object);
1951
0
      return;
1952
0
    }
1953
0
#endif
1954
1955
0
  g_object_notify_queue_freeze (object, TRUE);
1956
0
}
1957
1958
static inline void
1959
g_object_notify_by_spec_internal (GObject    *object,
1960
                                  GParamSpec *pspec)
1961
3.87k
{
1962
3.87k
  guint object_flags;
1963
3.87k
  gboolean needs_notify;
1964
3.87k
  gboolean in_init;
1965
1966
3.87k
  if (G_UNLIKELY (~pspec->flags & G_PARAM_READABLE))
1967
0
    return;
1968
1969
3.87k
  param_spec_follow_override (&pspec);
1970
1971
  /* get all flags we need with a single atomic read */
1972
3.87k
  object_flags = object_get_optional_flags (object);
1973
3.87k
  needs_notify = ((object_flags & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0) ||
1974
3.87k
                  CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object));
1975
3.87k
  in_init = (object_flags & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0;
1976
1977
3.87k
  if (pspec != NULL && needs_notify)
1978
0
    {
1979
0
      if (!g_object_notify_queue_add (object, pspec, in_init))
1980
0
        {
1981
          /*
1982
           * Coverity doesn’t understand the paired ref/unref here and seems to
1983
           * ignore the ref, thus reports every call to g_object_notify() as
1984
           * causing a double-free. That’s incorrect, but I can’t get a model
1985
           * file to work for avoiding the false positives, so instead comment
1986
           * out the ref/unref when doing static analysis.
1987
           */
1988
0
#ifndef __COVERITY__
1989
0
          g_object_ref (object);
1990
0
#endif
1991
1992
          /* not frozen, so just dispatch the notification directly */
1993
0
          G_OBJECT_GET_CLASS (object)
1994
0
              ->dispatch_properties_changed (object, 1, &pspec);
1995
1996
0
#ifndef __COVERITY__
1997
0
          g_object_unref (object);
1998
0
#endif
1999
0
        }
2000
0
    }
2001
3.87k
}
2002
2003
/**
2004
 * g_object_notify:
2005
 * @object: a #GObject
2006
 * @property_name: the name of a property installed on the class of @object.
2007
 *
2008
 * Emits a "notify" signal for the property @property_name on @object.
2009
 *
2010
 * When possible, eg. when signaling a property change from within the class
2011
 * that registered the property, you should use g_object_notify_by_pspec()
2012
 * instead.
2013
 *
2014
 * Note that emission of the notify signal may be blocked with
2015
 * g_object_freeze_notify(). In this case, the signal emissions are queued
2016
 * and will be emitted (in reverse order) when g_object_thaw_notify() is
2017
 * called.
2018
 */
2019
void
2020
g_object_notify (GObject     *object,
2021
     const gchar *property_name)
2022
3.87k
{
2023
3.87k
  GParamSpec *pspec;
2024
  
2025
3.87k
  g_return_if_fail (G_IS_OBJECT (object));
2026
3.87k
  g_return_if_fail (property_name != NULL);
2027
  
2028
  /* We don't need to get the redirect target
2029
   * (by, e.g. calling g_object_class_find_property())
2030
   * because g_object_notify_queue_add() does that
2031
   */
2032
3.87k
  pspec = g_param_spec_pool_lookup (g_atomic_pointer_get (&pspec_pool),
2033
3.87k
            property_name,
2034
3.87k
            G_OBJECT_TYPE (object),
2035
3.87k
            TRUE);
2036
2037
3.87k
  if (!pspec)
2038
0
    g_critical ("%s: object class '%s' has no property named '%s'",
2039
3.87k
          G_STRFUNC,
2040
3.87k
          G_OBJECT_TYPE_NAME (object),
2041
3.87k
          property_name);
2042
3.87k
  else
2043
3.87k
    g_object_notify_by_spec_internal (object, pspec);
2044
3.87k
}
2045
2046
/**
2047
 * g_object_notify_by_pspec:
2048
 * @object: a #GObject
2049
 * @pspec: the #GParamSpec of a property installed on the class of @object.
2050
 *
2051
 * Emits a "notify" signal for the property specified by @pspec on @object.
2052
 *
2053
 * This function omits the property name lookup, hence it is faster than
2054
 * g_object_notify().
2055
 *
2056
 * One way to avoid using g_object_notify() from within the
2057
 * class that registered the properties, and using g_object_notify_by_pspec()
2058
 * instead, is to store the GParamSpec used with
2059
 * g_object_class_install_property() inside a static array, e.g.:
2060
 *
2061
 *|[<!-- language="C" --> 
2062
 *   typedef enum
2063
 *   {
2064
 *     PROP_FOO = 1,
2065
 *     PROP_LAST
2066
 *   } MyObjectProperty;
2067
 *
2068
 *   static GParamSpec *properties[PROP_LAST];
2069
 *
2070
 *   static void
2071
 *   my_object_class_init (MyObjectClass *klass)
2072
 *   {
2073
 *     properties[PROP_FOO] = g_param_spec_int ("foo", NULL, NULL,
2074
 *                                              0, 100,
2075
 *                                              50,
2076
 *                                              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2077
 *     g_object_class_install_property (gobject_class,
2078
 *                                      PROP_FOO,
2079
 *                                      properties[PROP_FOO]);
2080
 *   }
2081
 * ]|
2082
 *
2083
 * and then notify a change on the "foo" property with:
2084
 *
2085
 * |[<!-- language="C" --> 
2086
 *   g_object_notify_by_pspec (self, properties[PROP_FOO]);
2087
 * ]|
2088
 *
2089
 * Since: 2.26
2090
 */
2091
void
2092
g_object_notify_by_pspec (GObject    *object,
2093
        GParamSpec *pspec)
2094
0
{
2095
2096
0
  g_return_if_fail (G_IS_OBJECT (object));
2097
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
2098
2099
0
  g_object_notify_by_spec_internal (object, pspec);
2100
0
}
2101
2102
/**
2103
 * g_object_thaw_notify:
2104
 * @object: a #GObject
2105
 *
2106
 * Reverts the effect of a previous call to
2107
 * g_object_freeze_notify(). The freeze count is decreased on @object
2108
 * and when it reaches zero, queued "notify" signals are emitted.
2109
 *
2110
 * Duplicate notifications for each property are squashed so that at most one
2111
 * #GObject::notify signal is emitted for each property, in the reverse order
2112
 * in which they have been queued.
2113
 *
2114
 * It is an error to call this function when the freeze count is zero.
2115
 */
2116
void
2117
g_object_thaw_notify (GObject *object)
2118
0
{
2119
0
  g_return_if_fail (G_IS_OBJECT (object));
2120
2121
0
#ifndef G_DISABLE_CHECKS
2122
0
  if (G_UNLIKELY (g_atomic_int_get (&object->ref_count) <= 0))
2123
0
    {
2124
0
      g_critical ("Attempting to thaw the notification queue for object %s[%p]; "
2125
0
                  "Property notification does not work during instance finalization.",
2126
0
                  G_OBJECT_TYPE_NAME (object),
2127
0
                  object);
2128
0
      return;
2129
0
    }
2130
0
#endif
2131
2132
0
  g_object_notify_queue_thaw (object, TRUE);
2133
0
}
2134
2135
static void
2136
maybe_issue_property_deprecation_warning (const GParamSpec *pspec)
2137
0
{
2138
0
  static GHashTable *already_warned_table;
2139
0
  static GMutex already_warned_lock;
2140
0
  gboolean already;
2141
2142
0
  if (!g_diagnostic_is_enabled ())
2143
0
    return;
2144
2145
  /* We hash only on property names: this means that we could end up in
2146
   * a situation where we fail to emit a warning about a pair of
2147
   * same-named deprecated properties used on two separate types.
2148
   * That's pretty unlikely to occur, and even if it does, you'll still
2149
   * have seen the warning for the first one...
2150
   *
2151
   * Doing it this way lets us hash directly on the (interned) property
2152
   * name pointers.
2153
   */
2154
0
  g_mutex_lock (&already_warned_lock);
2155
2156
0
  if (already_warned_table == NULL)
2157
0
    already_warned_table = g_hash_table_new (NULL, NULL);
2158
2159
0
  already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
2160
0
  if (!already)
2161
0
    g_hash_table_add (already_warned_table, (gpointer) pspec->name);
2162
2163
0
  g_mutex_unlock (&already_warned_lock);
2164
2165
0
  if (!already)
2166
0
    g_warning ("The property %s:%s is deprecated and shouldn't be used "
2167
0
               "anymore. It will be removed in a future version.",
2168
0
               g_type_name (pspec->owner_type), pspec->name);
2169
0
}
2170
2171
static inline void
2172
consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
2173
3.88k
{
2174
3.88k
  if (G_UNLIKELY (pspec->flags & G_PARAM_DEPRECATED))
2175
0
    maybe_issue_property_deprecation_warning (pspec);
2176
3.88k
}
2177
2178
static inline void
2179
object_get_property (GObject     *object,
2180
         GParamSpec  *pspec,
2181
         GValue      *value)
2182
0
{
2183
0
  GTypeInstance *inst = (GTypeInstance *) object;
2184
0
  GObjectClass *class;
2185
0
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
2186
2187
0
  if (G_LIKELY (inst->g_class->g_type == pspec->owner_type))
2188
0
    class = (GObjectClass *) inst->g_class;
2189
0
  else
2190
0
    class = g_type_class_peek (pspec->owner_type);
2191
2192
0
  g_assert (class != NULL);
2193
2194
0
  param_spec_follow_override (&pspec);
2195
2196
0
  consider_issuing_property_deprecation_warning (pspec);
2197
2198
0
  class->get_property (object, param_id, value, pspec);
2199
0
}
2200
2201
static inline void
2202
object_set_property (GObject             *object,
2203
         GParamSpec          *pspec,
2204
         const GValue        *value,
2205
         gboolean             nqueue_is_frozen,
2206
         gboolean             user_specified)
2207
6.17k
{
2208
6.17k
  GTypeInstance *inst = (GTypeInstance *) object;
2209
6.17k
  GObjectClass *class;
2210
6.17k
  GParamSpecClass *pclass;
2211
6.17k
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
2212
2213
6.17k
  if (G_LIKELY (inst->g_class->g_type == pspec->owner_type))
2214
2.97k
    class = (GObjectClass *) inst->g_class;
2215
3.20k
  else
2216
3.20k
    class = g_type_class_peek (pspec->owner_type);
2217
2218
6.17k
  g_assert (class != NULL);
2219
2220
6.17k
  param_spec_follow_override (&pspec);
2221
2222
6.17k
  if (user_specified)
2223
3.88k
    consider_issuing_property_deprecation_warning (pspec);
2224
2225
6.17k
  pclass = G_PARAM_SPEC_GET_CLASS (pspec);
2226
6.17k
  if (g_value_type_compatible (G_VALUE_TYPE (value), pspec->value_type) &&
2227
6.17k
      (pclass->value_validate == NULL ||
2228
6.17k
       (pclass->value_is_valid != NULL && pclass->value_is_valid (pspec, value))))
2229
6.08k
    {
2230
6.08k
      class->set_property (object, param_id, value, pspec);
2231
6.08k
    }
2232
87
  else
2233
87
    {
2234
      /* provide a copy to work from, convert (if necessary) and validate */
2235
87
      GValue tmp_value = G_VALUE_INIT;
2236
2237
87
      g_value_init (&tmp_value, pspec->value_type);
2238
2239
87
      if (!g_value_transform (value, &tmp_value))
2240
0
        g_critical ("unable to set property '%s' of type '%s' from value of type '%s'",
2241
87
                    pspec->name,
2242
87
                    g_type_name (pspec->value_type),
2243
87
                    G_VALUE_TYPE_NAME (value));
2244
87
      else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
2245
87
        {
2246
87
          gchar *contents = g_strdup_value_contents (value);
2247
2248
87
          g_critical ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
2249
87
                      contents,
2250
87
                      G_VALUE_TYPE_NAME (value),
2251
87
                      pspec->name,
2252
87
                      g_type_name (pspec->value_type));
2253
87
          g_free (contents);
2254
87
        }
2255
0
      else
2256
0
        {
2257
0
          class->set_property (object, param_id, &tmp_value, pspec);
2258
0
        }
2259
2260
87
      g_value_unset (&tmp_value);
2261
87
    }
2262
2263
6.17k
  if ((pspec->flags & (G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READABLE)) == G_PARAM_READABLE &&
2264
6.17k
      nqueue_is_frozen)
2265
0
    g_object_notify_queue_add (object, pspec, FALSE);
2266
6.17k
}
2267
2268
static void
2269
object_interface_check_properties (gpointer check_data,
2270
           gpointer g_iface)
2271
14
{
2272
14
  GTypeInterface *iface_class = g_iface;
2273
14
  GObjectClass *class;
2274
14
  GParamSpecPool *param_spec_pool;
2275
14
  GType iface_type = iface_class->g_type;
2276
14
  GParamSpec **pspecs;
2277
14
  guint n;
2278
2279
14
  class = g_type_class_ref (iface_class->g_instance_type);
2280
2281
14
  if (class == NULL)
2282
0
    return;
2283
2284
14
  if (!G_IS_OBJECT_CLASS (class))
2285
0
    goto out;
2286
2287
14
  param_spec_pool = g_atomic_pointer_get (&pspec_pool);
2288
14
  pspecs = g_param_spec_pool_list (param_spec_pool, iface_type, &n);
2289
2290
14
  while (n--)
2291
0
    {
2292
0
      GParamSpec *class_pspec = g_param_spec_pool_lookup (param_spec_pool,
2293
0
                pspecs[n]->name,
2294
0
                G_OBJECT_CLASS_TYPE (class),
2295
0
                TRUE);
2296
2297
0
      if (!class_pspec)
2298
0
  {
2299
0
    g_critical ("Object class %s doesn't implement property "
2300
0
          "'%s' from interface '%s'",
2301
0
          g_type_name (G_OBJECT_CLASS_TYPE (class)),
2302
0
          pspecs[n]->name,
2303
0
          g_type_name (iface_type));
2304
2305
0
    continue;
2306
0
  }
2307
2308
      /* We do a number of checks on the properties of an interface to
2309
       * make sure that all classes implementing the interface are
2310
       * overriding the properties correctly.
2311
       *
2312
       * We do the checks in order of importance so that we can give
2313
       * more useful error messages first.
2314
       *
2315
       * First, we check that the implementation doesn't remove the
2316
       * basic functionality (readability, writability) advertised by
2317
       * the interface.  Next, we check that it doesn't introduce
2318
       * additional restrictions (such as construct-only).  Finally, we
2319
       * make sure the types are compatible.
2320
       */
2321
2322
0
#define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
2323
      /* If the property on the interface is readable then the
2324
       * implementation must be readable.  If the interface is writable
2325
       * then the implementation must be writable.
2326
       */
2327
0
      if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
2328
0
        {
2329
0
          g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
2330
0
                      "property on interface '%s'\n", pspecs[n]->name,
2331
0
                      g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
2332
0
          continue;
2333
0
        }
2334
2335
      /* If the property on the interface is writable then we need to
2336
       * make sure the implementation doesn't introduce new restrictions
2337
       * on that writability (ie: construct-only).
2338
       *
2339
       * If the interface was not writable to begin with then we don't
2340
       * really have any problems here because "writable at construct
2341
       * time only" is still more permissive than "read only".
2342
       */
2343
0
      if (pspecs[n]->flags & G_PARAM_WRITABLE)
2344
0
        {
2345
0
          if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
2346
0
            {
2347
0
              g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
2348
0
                          "writability compared with the property on interface '%s'\n", pspecs[n]->name,
2349
0
                          g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
2350
0
              continue;
2351
0
            }
2352
0
        }
2353
0
#undef SUBSET
2354
2355
      /* If the property on the interface is readable then we are
2356
       * effectively advertising that reading the property will return a
2357
       * value of a specific type.  All implementations of the interface
2358
       * need to return items of this type -- but may be more
2359
       * restrictive.  For example, it is legal to have:
2360
       *
2361
       *   GtkWidget *get_item();
2362
       *
2363
       * that is implemented by a function that always returns a
2364
       * GtkEntry.  In short: readability implies that the
2365
       * implementation  value type must be equal or more restrictive.
2366
       *
2367
       * Similarly, if the property on the interface is writable then
2368
       * must be able to accept the property being set to any value of
2369
       * that type, including subclasses.  In this case, we may also be
2370
       * less restrictive.  For example, it is legal to have:
2371
       *
2372
       *   set_item (GtkEntry *);
2373
       *
2374
       * that is implemented by a function that will actually work with
2375
       * any GtkWidget.  In short: writability implies that the
2376
       * implementation value type must be equal or less restrictive.
2377
       *
2378
       * In the case that the property is both readable and writable
2379
       * then the only way that both of the above can be satisfied is
2380
       * with a type that is exactly equal.
2381
       */
2382
0
      switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
2383
0
        {
2384
0
        case G_PARAM_READABLE | G_PARAM_WRITABLE:
2385
          /* class pspec value type must have exact equality with interface */
2386
0
          if (pspecs[n]->value_type != class_pspec->value_type)
2387
0
            g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
2388
0
                        "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
2389
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
2390
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
2391
0
          break;
2392
2393
0
        case G_PARAM_READABLE:
2394
          /* class pspec value type equal or more restrictive than interface */
2395
0
          if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
2396
0
            g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
2397
0
                        "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
2398
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
2399
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
2400
0
          break;
2401
2402
0
        case G_PARAM_WRITABLE:
2403
          /* class pspec value type equal or less restrictive than interface */
2404
0
          if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
2405
0
            g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
2406
0
                        "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
2407
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
2408
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
2409
0
          break;
2410
2411
0
        default:
2412
0
          g_assert_not_reached ();
2413
0
        }
2414
0
    }
2415
2416
14
  g_free (pspecs);
2417
2418
14
 out:
2419
14
  g_type_class_unref (class);
2420
14
}
2421
2422
GType
2423
g_object_get_type (void)
2424
0
{
2425
0
    return G_TYPE_OBJECT;
2426
0
}
2427
2428
/**
2429
 * g_object_new: (skip)
2430
 * @object_type: the type id of the #GObject subtype to instantiate
2431
 * @first_property_name: the name of the first property
2432
 * @...: the value of the first property, followed optionally by more
2433
 *   name/value pairs, followed by %NULL
2434
 *
2435
 * Creates a new instance of a #GObject subtype and sets its properties.
2436
 *
2437
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2438
 * which are not explicitly specified are set to their default values. Any
2439
 * private data for the object is guaranteed to be initialized with zeros, as
2440
 * per g_type_create_instance().
2441
 *
2442
 * Note that in C, small integer types in variable argument lists are promoted
2443
 * up to `gint` or `guint` as appropriate, and read back accordingly. `gint` is
2444
 * 32 bits on every platform on which GLib is currently supported. This means that
2445
 * you can use C expressions of type `gint` with g_object_new() and properties of
2446
 * type `gint` or `guint` or smaller. Specifically, you can use integer literals
2447
 * with these property types.
2448
 *
2449
 * When using property types of `gint64` or `guint64`, you must ensure that the
2450
 * value that you provide is 64 bit. This means that you should use a cast or
2451
 * make use of the %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
2452
 *
2453
 * Similarly, `gfloat` is promoted to `gdouble`, so you must ensure that the value
2454
 * you provide is a `gdouble`, even for a property of type `gfloat`.
2455
 *
2456
 * Since GLib 2.72, all #GObjects are guaranteed to be aligned to at least the
2457
 * alignment of the largest basic GLib type (typically this is `guint64` or
2458
 * `gdouble`). If you need larger alignment for an element in a #GObject, you
2459
 * should allocate it on the heap (aligned), or arrange for your #GObject to be
2460
 * appropriately padded.
2461
 *
2462
 * Returns: (transfer full) (type GObject.Object): a new instance of
2463
 *   @object_type
2464
 */
2465
gpointer
2466
g_object_new (GType    object_type,
2467
        const gchar *first_property_name,
2468
        ...)
2469
6.34k
{
2470
6.34k
  GObject *object;
2471
6.34k
  va_list var_args;
2472
  
2473
  /* short circuit for calls supplying no properties */
2474
6.34k
  if (!first_property_name)
2475
4.33k
    return g_object_new_with_properties (object_type, 0, NULL, NULL);
2476
2477
2.01k
  va_start (var_args, first_property_name);
2478
2.01k
  object = g_object_new_valist (object_type, first_property_name, var_args);
2479
2.01k
  va_end (var_args);
2480
  
2481
2.01k
  return object;
2482
6.34k
}
2483
2484
/* Check alignment. (See https://gitlab.gnome.org/GNOME/glib/-/issues/1231.)
2485
 * This should never fail, since g_type_create_instance() uses g_slice_alloc0().
2486
 * The GSlice allocator always aligns to the next power of 2 greater than the
2487
 * allocation size. The allocation size for a GObject is
2488
 *   sizeof(GTypeInstance) + sizeof(guint) + sizeof(GData*)
2489
 * which is 12B on 32-bit platforms, and larger on 64-bit systems. In both
2490
 * cases, that’s larger than the 8B needed for a guint64 or gdouble.
2491
 *
2492
 * If GSlice falls back to malloc(), it’s documented to return something
2493
 * suitably aligned for any basic type. */
2494
static inline gboolean
2495
g_object_is_aligned (GObject *object)
2496
6.48k
{
2497
6.48k
  return ((((guintptr) (void *) object) %
2498
6.48k
             MAX (G_ALIGNOF (gdouble),
2499
6.48k
                  MAX (G_ALIGNOF (guint64),
2500
6.48k
                       MAX (G_ALIGNOF (gint),
2501
6.48k
                            G_ALIGNOF (glong))))) == 0);
2502
6.48k
}
2503
2504
static gpointer
2505
g_object_new_with_custom_constructor (GObjectClass          *class,
2506
                                      GObjectConstructParam *params,
2507
                                      guint                  n_params)
2508
0
{
2509
0
  gboolean nqueue_is_frozen = FALSE;
2510
0
  gboolean newly_constructed;
2511
0
  GObjectConstructParam *cparams;
2512
0
  gboolean free_cparams = FALSE;
2513
0
  GObject *object;
2514
0
  GValue *cvalues;
2515
0
  gint cvals_used;
2516
0
  GSList *node;
2517
0
  guint i;
2518
2519
  /* If we have ->constructed() then we have to do a lot more work.
2520
   * It's possible that this is a singleton and it's also possible
2521
   * that the user's constructor() will attempt to modify the values
2522
   * that we pass in, so we'll need to allocate copies of them.
2523
   * It's also possible that the user may attempt to call
2524
   * g_object_set() from inside of their constructor, so we need to
2525
   * add ourselves to a list of objects for which that is allowed
2526
   * while their constructor() is running.
2527
   */
2528
2529
  /* Create the array of GObjectConstructParams for constructor(),
2530
   * The 1024 here is an arbitrary, high limit that no sane code
2531
   * will ever hit, just to avoid the possibility of stack overflow.
2532
   */
2533
0
  if (G_LIKELY (class->n_construct_properties < 1024))
2534
0
    {
2535
0
      cparams = g_newa0 (GObjectConstructParam, class->n_construct_properties);
2536
0
      cvalues = g_newa0 (GValue, class->n_construct_properties);
2537
0
    }
2538
0
  else
2539
0
    {
2540
0
      cparams = g_new0 (GObjectConstructParam, class->n_construct_properties);
2541
0
      cvalues = g_new0 (GValue, class->n_construct_properties);
2542
0
      free_cparams = TRUE;
2543
0
    }
2544
0
  cvals_used = 0;
2545
0
  i = 0;
2546
2547
  /* As above, we may find the value in the passed-in params list.
2548
   *
2549
   * If we have the value passed in then we can use the GValue from
2550
   * it directly because it is safe to modify.  If we use the
2551
   * default value from the class, we had better not pass that in
2552
   * and risk it being modified, so we create a new one.
2553
   * */
2554
0
  for (node = class->construct_properties; node; node = node->next)
2555
0
    {
2556
0
      GParamSpec *pspec;
2557
0
      GValue *value;
2558
0
      guint j;
2559
2560
0
      pspec = node->data;
2561
0
      value = NULL; /* to silence gcc... */
2562
2563
0
      for (j = 0; j < n_params; j++)
2564
0
        if (params[j].pspec == pspec)
2565
0
          {
2566
0
            consider_issuing_property_deprecation_warning (pspec);
2567
0
            value = params[j].value;
2568
0
            break;
2569
0
          }
2570
2571
0
      if (value == NULL)
2572
0
        {
2573
0
          value = &cvalues[cvals_used++];
2574
0
          g_value_init (value, pspec->value_type);
2575
0
          g_param_value_set_default (pspec, value);
2576
0
        }
2577
2578
0
      cparams[i].pspec = pspec;
2579
0
      cparams[i].value = value;
2580
0
      i++;
2581
0
    }
2582
2583
  /* construct object from construction parameters */
2584
0
  object = class->constructor (class->g_type_class.g_type, class->n_construct_properties, cparams);
2585
  /* free construction values */
2586
0
  while (cvals_used--)
2587
0
    g_value_unset (&cvalues[cvals_used]);
2588
2589
0
  if (free_cparams)
2590
0
    {
2591
0
      g_free (cparams);
2592
0
      g_free (cvalues);
2593
0
    }
2594
2595
  /* There is code in the wild that relies on being able to return NULL
2596
   * from its custom constructor.  This was never a supported operation,
2597
   * but since the code is already out there...
2598
   */
2599
0
  if (object == NULL)
2600
0
    {
2601
0
      g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
2602
0
                  "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
2603
0
      return NULL;
2604
0
    }
2605
2606
0
  if (!g_object_is_aligned (object))
2607
0
    {
2608
0
      g_critical ("Custom constructor for class %s returned a non-aligned "
2609
0
                  "GObject (which is invalid since GLib 2.72). Assuming any "
2610
0
                  "code using this object doesn’t require it to be aligned. "
2611
0
                  "Please fix your constructor to align to the largest GLib "
2612
0
                  "basic type (typically gdouble or guint64).",
2613
0
                  G_OBJECT_CLASS_NAME (class));
2614
0
    }
2615
2616
  /* g_object_init() will have marked the object as being in-construction.
2617
   * Check if the returned object still is so marked, or if this is an
2618
   * already-existing singleton (in which case we should not do 'constructed').
2619
   */
2620
0
  newly_constructed = object_in_construction (object);
2621
0
  if (newly_constructed)
2622
0
    unset_object_in_construction (object);
2623
2624
0
  if (CLASS_HAS_PROPS (class))
2625
0
    {
2626
0
      if ((newly_constructed && _g_object_has_notify_handler (object)) ||
2627
0
          _g_object_has_notify_handler (object))
2628
0
        {
2629
          /* This may or may not have been setup in g_object_init().
2630
           * If it hasn't, we do it now.
2631
           */
2632
0
          g_object_notify_queue_freeze (object, FALSE);
2633
0
          nqueue_is_frozen = TRUE;
2634
0
        }
2635
0
    }
2636
2637
  /* run 'constructed' handler if there is a custom one */
2638
0
  if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
2639
0
    class->constructed (object);
2640
2641
  /* set remaining properties */
2642
0
  for (i = 0; i < n_params; i++)
2643
0
    if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2644
0
      object_set_property (object, params[i].pspec, params[i].value, nqueue_is_frozen, TRUE);
2645
2646
0
  if (nqueue_is_frozen)
2647
0
    g_object_notify_queue_thaw (object, FALSE);
2648
2649
0
  return object;
2650
0
}
2651
2652
static gpointer
2653
g_object_new_internal (GObjectClass          *class,
2654
                       GObjectConstructParam *params,
2655
                       guint                  n_params)
2656
6.48k
{
2657
6.48k
  gboolean nqueue_is_frozen = FALSE;
2658
6.48k
  GObject *object;
2659
6.48k
  guint i;
2660
2661
6.48k
  if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
2662
0
    return g_object_new_with_custom_constructor (class, params, n_params);
2663
2664
6.48k
  object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
2665
2666
6.48k
  g_assert (g_object_is_aligned (object));
2667
2668
6.48k
  unset_object_in_construction (object);
2669
2670
6.48k
  if (CLASS_HAS_PROPS (class))
2671
5.42k
    {
2672
5.42k
      GSList *node;
2673
2674
5.42k
      if (_g_object_has_notify_handler (object))
2675
0
        {
2676
          /* This may or may not have been setup in g_object_init().
2677
           * If it hasn't, we do it now.
2678
           */
2679
0
          g_object_notify_queue_freeze (object, FALSE);
2680
0
          nqueue_is_frozen = TRUE;
2681
0
        }
2682
2683
      /* We will set exactly n_construct_properties construct
2684
       * properties, but they may come from either the class default
2685
       * values or the passed-in parameter list.
2686
       */
2687
11.3k
      for (node = class->construct_properties; node; node = node->next)
2688
5.89k
        {
2689
5.89k
          const GValue *value;
2690
5.89k
          GParamSpec *pspec;
2691
5.89k
          guint j;
2692
5.89k
          gboolean user_specified = FALSE;
2693
2694
5.89k
          pspec = node->data;
2695
5.89k
          value = NULL; /* to silence gcc... */
2696
2697
10.8k
          for (j = 0; j < n_params; j++)
2698
8.51k
            if (params[j].pspec == pspec)
2699
3.60k
              {
2700
3.60k
                value = params[j].value;
2701
3.60k
                user_specified = TRUE;
2702
3.60k
                break;
2703
3.60k
              }
2704
2705
5.89k
          if (value == NULL)
2706
2.29k
            value = g_param_spec_get_default_value (pspec);
2707
2708
5.89k
          object_set_property (object, pspec, value, nqueue_is_frozen, user_specified);
2709
5.89k
        }
2710
5.42k
    }
2711
2712
  /* run 'constructed' handler if there is a custom one */
2713
6.48k
  if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
2714
0
    class->constructed (object);
2715
2716
  /* Set remaining properties.  The construct properties will
2717
   * already have been taken, so set only the non-construct ones.
2718
   */
2719
10.3k
  for (i = 0; i < n_params; i++)
2720
3.88k
    if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2721
280
      object_set_property (object, params[i].pspec, params[i].value, nqueue_is_frozen, TRUE);
2722
2723
6.48k
  if (nqueue_is_frozen)
2724
0
    g_object_notify_queue_thaw (object, FALSE);
2725
2726
6.48k
  return object;
2727
6.48k
}
2728
2729
2730
static inline gboolean
2731
g_object_new_is_valid_property (GType                  object_type,
2732
                                GParamSpec            *pspec,
2733
                                const char            *name,
2734
                                GObjectConstructParam *params,
2735
                                guint                  n_params)
2736
3.88k
{
2737
3.88k
  guint i;
2738
2739
3.88k
  if (G_UNLIKELY (pspec == NULL))
2740
0
    {
2741
0
      g_critical ("%s: object class '%s' has no property named '%s'",
2742
0
                  G_STRFUNC, g_type_name (object_type), name);
2743
0
      return FALSE;
2744
0
    }
2745
2746
3.88k
  if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
2747
0
    {
2748
0
      g_critical ("%s: property '%s' of object class '%s' is not writable",
2749
0
                  G_STRFUNC, pspec->name, g_type_name (object_type));
2750
0
      return FALSE;
2751
0
    }
2752
2753
3.88k
  if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2754
3.60k
    {
2755
6.06k
      for (i = 0; i < n_params; i++)
2756
2.46k
        if (params[i].pspec == pspec)
2757
0
          break;
2758
3.60k
      if (G_UNLIKELY (i != n_params))
2759
0
        {
2760
0
          g_critical ("%s: property '%s' for type '%s' cannot be set twice",
2761
0
                      G_STRFUNC, name, g_type_name (object_type));
2762
0
          return FALSE;
2763
0
        }
2764
3.60k
    }
2765
3.88k
  return TRUE;
2766
3.88k
}
2767
2768
2769
/**
2770
 * g_object_new_with_properties: (skip)
2771
 * @object_type: the object type to instantiate
2772
 * @n_properties: the number of properties
2773
 * @names: (array length=n_properties): the names of each property to be set
2774
 * @values: (array length=n_properties): the values of each property to be set
2775
 *
2776
 * Creates a new instance of a #GObject subtype and sets its properties using
2777
 * the provided arrays. Both arrays must have exactly @n_properties elements,
2778
 * and the names and values correspond by index.
2779
 *
2780
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2781
 * which are not explicitly specified are set to their default values.
2782
 *
2783
 * Returns: (type GObject.Object) (transfer full): a new instance of
2784
 * @object_type
2785
 *
2786
 * Since: 2.54
2787
 */
2788
GObject *
2789
g_object_new_with_properties (GType          object_type,
2790
                              guint          n_properties,
2791
                              const char    *names[],
2792
                              const GValue   values[])
2793
4.33k
{
2794
4.33k
  GObjectClass *class, *unref_class = NULL;
2795
4.33k
  GObject *object;
2796
2797
4.33k
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2798
2799
  /* Try to avoid thrashing the ref_count if we don't need to (since
2800
   * it's a locked operation).
2801
   */
2802
4.33k
  class = g_type_class_peek_static (object_type);
2803
2804
4.33k
  if (class == NULL)
2805
4
    class = unref_class = g_type_class_ref (object_type);
2806
2807
4.33k
  if (n_properties > 0)
2808
0
    {
2809
0
      guint i, count = 0;
2810
0
      GObjectConstructParam *params;
2811
2812
0
      params = g_newa (GObjectConstructParam, n_properties);
2813
0
      for (i = 0; i < n_properties; i++)
2814
0
        {
2815
0
          GParamSpec *pspec = find_pspec (class, names[i]);
2816
2817
0
          if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
2818
0
            continue;
2819
0
          params[count].pspec = pspec;
2820
0
          params[count].value = (GValue *) &values[i];
2821
0
          count++;
2822
0
        }
2823
0
      object = g_object_new_internal (class, params, count);
2824
0
    }
2825
4.33k
  else
2826
4.33k
    object = g_object_new_internal (class, NULL, 0);
2827
2828
4.33k
  if (unref_class != NULL)
2829
4
    g_type_class_unref (unref_class);
2830
2831
4.33k
  return object;
2832
4.33k
}
2833
2834
/**
2835
 * g_object_newv:
2836
 * @object_type: the type id of the #GObject subtype to instantiate
2837
 * @n_parameters: the length of the @parameters array
2838
 * @parameters: (array length=n_parameters): an array of #GParameter
2839
 *
2840
 * Creates a new instance of a #GObject subtype and sets its properties.
2841
 *
2842
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2843
 * which are not explicitly specified are set to their default values.
2844
 *
2845
 * Returns: (type GObject.Object) (transfer full): a new instance of
2846
 * @object_type
2847
 *
2848
 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
2849
 * deprecated. See #GParameter for more information.
2850
 */
2851
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2852
gpointer
2853
g_object_newv (GType       object_type,
2854
               guint       n_parameters,
2855
               GParameter *parameters)
2856
0
{
2857
0
  GObjectClass *class, *unref_class = NULL;
2858
0
  GObject *object;
2859
2860
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2861
0
  g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
2862
2863
  /* Try to avoid thrashing the ref_count if we don't need to (since
2864
   * it's a locked operation).
2865
   */
2866
0
  class = g_type_class_peek_static (object_type);
2867
2868
0
  if (!class)
2869
0
    class = unref_class = g_type_class_ref (object_type);
2870
2871
0
  if (n_parameters)
2872
0
    {
2873
0
      GObjectConstructParam *cparams;
2874
0
      guint i, j;
2875
2876
0
      cparams = g_newa (GObjectConstructParam, n_parameters);
2877
0
      j = 0;
2878
2879
0
      for (i = 0; i < n_parameters; i++)
2880
0
        {
2881
0
          GParamSpec *pspec = find_pspec (class, parameters[i].name);
2882
2883
0
          if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2884
0
            continue;
2885
2886
0
          cparams[j].pspec = pspec;
2887
0
          cparams[j].value = &parameters[i].value;
2888
0
          j++;
2889
0
        }
2890
2891
0
      object = g_object_new_internal (class, cparams, j);
2892
0
    }
2893
0
  else
2894
    /* Fast case: no properties passed in. */
2895
0
    object = g_object_new_internal (class, NULL, 0);
2896
2897
0
  if (unref_class)
2898
0
    g_type_class_unref (unref_class);
2899
2900
0
  return object;
2901
0
}
2902
G_GNUC_END_IGNORE_DEPRECATIONS
2903
2904
/**
2905
 * g_object_new_valist: (skip)
2906
 * @object_type: the type id of the #GObject subtype to instantiate
2907
 * @first_property_name: the name of the first property
2908
 * @var_args: the value of the first property, followed optionally by more
2909
 *  name/value pairs, followed by %NULL
2910
 *
2911
 * Creates a new instance of a #GObject subtype and sets its properties.
2912
 *
2913
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2914
 * which are not explicitly specified are set to their default values.
2915
 *
2916
 * Returns: a new instance of @object_type
2917
 */
2918
GObject*
2919
g_object_new_valist (GType        object_type,
2920
                     const gchar *first_property_name,
2921
                     va_list      var_args)
2922
2.15k
{
2923
2.15k
  GObjectClass *class, *unref_class = NULL;
2924
2.15k
  GObject *object;
2925
2926
2.15k
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2927
2928
  /* Try to avoid thrashing the ref_count if we don't need to (since
2929
   * it's a locked operation).
2930
   */
2931
2.15k
  class = g_type_class_peek_static (object_type);
2932
2933
2.15k
  if (!class)
2934
10
    class = unref_class = g_type_class_ref (object_type);
2935
2936
2.15k
  if (first_property_name)
2937
2.15k
    {
2938
2.15k
      GObjectConstructParam params_stack[16];
2939
2.15k
      GValue values_stack[G_N_ELEMENTS (params_stack)];
2940
2.15k
      GTypeValueTable *vtabs_stack[G_N_ELEMENTS (params_stack)];
2941
2.15k
      const gchar *name;
2942
2.15k
      GObjectConstructParam *params = params_stack;
2943
2.15k
      GValue *values = values_stack;
2944
2.15k
      GTypeValueTable **vtabs = vtabs_stack;
2945
2.15k
      guint n_params = 0;
2946
2.15k
      guint n_params_alloc = G_N_ELEMENTS (params_stack);
2947
2948
2.15k
      name = first_property_name;
2949
2950
2.15k
      do
2951
3.88k
        {
2952
3.88k
          gchar *error = NULL;
2953
3.88k
          GParamSpec *pspec = find_pspec (class, name);
2954
2955
3.88k
          if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2956
0
            break;
2957
2958
3.88k
          if (G_UNLIKELY (n_params == n_params_alloc))
2959
0
            {
2960
0
              guint i;
2961
2962
0
              if (n_params_alloc == G_N_ELEMENTS (params_stack))
2963
0
                {
2964
0
                  n_params_alloc = G_N_ELEMENTS (params_stack) * 2u;
2965
0
                  params = g_new (GObjectConstructParam, n_params_alloc);
2966
0
                  values = g_new (GValue, n_params_alloc);
2967
0
                  vtabs = g_new (GTypeValueTable *, n_params_alloc);
2968
0
                  memcpy (params, params_stack, sizeof (GObjectConstructParam) * n_params);
2969
0
                  memcpy (values, values_stack, sizeof (GValue) * n_params);
2970
0
                  memcpy (vtabs, vtabs_stack, sizeof (GTypeValueTable *) * n_params);
2971
0
                }
2972
0
              else
2973
0
                {
2974
0
                  n_params_alloc *= 2u;
2975
0
                  params = g_realloc (params, sizeof (GObjectConstructParam) * n_params_alloc);
2976
0
                  values = g_realloc (values, sizeof (GValue) * n_params_alloc);
2977
0
                  vtabs = g_realloc (vtabs, sizeof (GTypeValueTable *) * n_params_alloc);
2978
0
                }
2979
2980
0
              for (i = 0; i < n_params; i++)
2981
0
                params[i].value = &values[i];
2982
0
            }
2983
2984
3.88k
          params[n_params].pspec = pspec;
2985
3.88k
          params[n_params].value = &values[n_params];
2986
3.88k
          memset (&values[n_params], 0, sizeof (GValue));
2987
2988
3.88k
          G_VALUE_COLLECT_INIT2 (&values[n_params], vtabs[n_params], pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
2989
2990
3.88k
          if (error)
2991
0
            {
2992
0
              g_critical ("%s: %s", G_STRFUNC, error);
2993
0
              g_value_unset (&values[n_params]);
2994
0
              g_free (error);
2995
0
              break;
2996
0
            }
2997
2998
3.88k
          n_params++;
2999
3.88k
        }
3000
3.88k
      while ((name = va_arg (var_args, const gchar *)));
3001
3002
2.15k
      object = g_object_new_internal (class, params, n_params);
3003
3004
6.03k
      while (n_params--)
3005
3.88k
        {
3006
          /* We open-code g_value_unset() here to avoid the
3007
           * cost of looking up the GTypeValueTable again.
3008
           */
3009
3.88k
          if (vtabs[n_params]->value_free)
3010
1.92k
            vtabs[n_params]->value_free (params[n_params].value);
3011
3.88k
        }
3012
3013
2.15k
      if (G_UNLIKELY (n_params_alloc != G_N_ELEMENTS (params_stack)))
3014
0
        {
3015
0
          g_free (params);
3016
0
          g_free (values);
3017
0
          g_free (vtabs);
3018
0
        }
3019
2.15k
    }
3020
0
  else
3021
    /* Fast case: no properties passed in. */
3022
0
    object = g_object_new_internal (class, NULL, 0);
3023
3024
2.15k
  if (unref_class)
3025
10
    g_type_class_unref (unref_class);
3026
3027
2.15k
  return object;
3028
2.15k
}
3029
3030
static GObject*
3031
g_object_constructor (GType                  type,
3032
          guint                  n_construct_properties,
3033
          GObjectConstructParam *construct_params)
3034
0
{
3035
0
  GObject *object;
3036
3037
  /* create object */
3038
0
  object = (GObject*) g_type_create_instance (type);
3039
  
3040
  /* set construction parameters */
3041
0
  if (n_construct_properties)
3042
0
    {
3043
0
      g_object_notify_queue_freeze (object, TRUE);
3044
      
3045
      /* set construct properties */
3046
0
      while (n_construct_properties--)
3047
0
  {
3048
0
    GValue *value = construct_params->value;
3049
0
    GParamSpec *pspec = construct_params->pspec;
3050
3051
0
    construct_params++;
3052
0
    object_set_property (object, pspec, value, TRUE, FALSE);
3053
0
  }
3054
3055
0
      g_object_notify_queue_thaw (object, FALSE);
3056
      /* the notification queue is still frozen from g_object_init(), so
3057
       * we don't need to handle it here, g_object_newv() takes
3058
       * care of that
3059
       */
3060
0
    }
3061
3062
0
  return object;
3063
0
}
3064
3065
static void
3066
g_object_constructed (GObject *object)
3067
0
{
3068
  /* empty default impl to allow unconditional upchaining */
3069
0
}
3070
3071
static inline gboolean
3072
g_object_set_is_valid_property (GObject         *object,
3073
                                GParamSpec      *pspec,
3074
                                const char      *property_name)
3075
0
{
3076
0
  if (G_UNLIKELY (pspec == NULL))
3077
0
    {
3078
0
      g_critical ("%s: object class '%s' has no property named '%s'",
3079
0
                  G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
3080
0
      return FALSE;
3081
0
    }
3082
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
3083
0
    {
3084
0
      g_critical ("%s: property '%s' of object class '%s' is not writable",
3085
0
                  G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
3086
0
      return FALSE;
3087
0
    }
3088
0
  if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
3089
0
    {
3090
0
      g_critical ("%s: construct property \"%s\" for object '%s' can't be set after construction",
3091
0
                  G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
3092
0
      return FALSE;
3093
0
    }
3094
0
  return TRUE;
3095
0
}
3096
3097
/**
3098
 * g_object_setv: (skip)
3099
 * @object: a #GObject
3100
 * @n_properties: the number of properties
3101
 * @names: (array length=n_properties): the names of each property to be set
3102
 * @values: (array length=n_properties): the values of each property to be set
3103
 *
3104
 * Sets @n_properties properties for an @object.
3105
 * Properties to be set will be taken from @values. All properties must be
3106
 * valid. Warnings will be emitted and undefined behaviour may result if invalid
3107
 * properties are passed in.
3108
 *
3109
 * Since: 2.54
3110
 */
3111
void
3112
g_object_setv (GObject       *object,
3113
               guint          n_properties,
3114
               const gchar   *names[],
3115
               const GValue   values[])
3116
0
{
3117
0
  guint i;
3118
0
  gboolean nqueue_is_frozen = FALSE;
3119
0
  GParamSpec *pspec;
3120
0
  GObjectClass *class;
3121
3122
0
  g_return_if_fail (G_IS_OBJECT (object));
3123
3124
0
  if (n_properties == 0)
3125
0
    return;
3126
3127
0
  g_object_ref (object);
3128
3129
0
  class = G_OBJECT_GET_CLASS (object);
3130
3131
0
  if (_g_object_has_notify_handler (object))
3132
0
    {
3133
0
      g_object_notify_queue_freeze (object, TRUE);
3134
0
      nqueue_is_frozen = TRUE;
3135
0
    }
3136
3137
0
  for (i = 0; i < n_properties; i++)
3138
0
    {
3139
0
      pspec = find_pspec (class, names[i]);
3140
3141
0
      if (!g_object_set_is_valid_property (object, pspec, names[i]))
3142
0
        break;
3143
3144
0
      object_set_property (object, pspec, &values[i], nqueue_is_frozen, TRUE);
3145
0
    }
3146
3147
0
  if (nqueue_is_frozen)
3148
0
    g_object_notify_queue_thaw (object, FALSE);
3149
3150
0
  g_object_unref (object);
3151
0
}
3152
3153
/**
3154
 * g_object_set_valist: (skip)
3155
 * @object: a #GObject
3156
 * @first_property_name: name of the first property to set
3157
 * @var_args: value for the first property, followed optionally by more
3158
 *  name/value pairs, followed by %NULL
3159
 *
3160
 * Sets properties on an object.
3161
 */
3162
void
3163
g_object_set_valist (GObject   *object,
3164
         const gchar *first_property_name,
3165
         va_list    var_args)
3166
0
{
3167
0
  gboolean nqueue_is_frozen = FALSE;
3168
0
  const gchar *name;
3169
0
  GObjectClass *class;
3170
  
3171
0
  g_return_if_fail (G_IS_OBJECT (object));
3172
3173
0
  g_object_ref (object);
3174
3175
0
  if (_g_object_has_notify_handler (object))
3176
0
    {
3177
0
      g_object_notify_queue_freeze (object, TRUE);
3178
0
      nqueue_is_frozen = TRUE;
3179
0
    }
3180
3181
0
  class = G_OBJECT_GET_CLASS (object);
3182
3183
0
  name = first_property_name;
3184
0
  while (name)
3185
0
    {
3186
0
      GValue value = G_VALUE_INIT;
3187
0
      GParamSpec *pspec;
3188
0
      gchar *error = NULL;
3189
0
      GTypeValueTable *vtab;
3190
      
3191
0
      pspec = find_pspec (class, name);
3192
3193
0
      if (!g_object_set_is_valid_property (object, pspec, name))
3194
0
        break;
3195
3196
0
      G_VALUE_COLLECT_INIT2 (&value, vtab, pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
3197
0
      if (error)
3198
0
  {
3199
0
    g_critical ("%s: %s", G_STRFUNC, error);
3200
0
    g_free (error);
3201
0
          g_value_unset (&value);
3202
0
    break;
3203
0
  }
3204
3205
0
      object_set_property (object, pspec, &value, nqueue_is_frozen, TRUE);
3206
3207
      /* We open-code g_value_unset() here to avoid the
3208
       * cost of looking up the GTypeValueTable again.
3209
       */
3210
0
      if (vtab->value_free)
3211
0
        vtab->value_free (&value);
3212
3213
0
      name = va_arg (var_args, gchar*);
3214
0
    }
3215
3216
0
  if (nqueue_is_frozen)
3217
0
    g_object_notify_queue_thaw (object, FALSE);
3218
3219
0
  g_object_unref (object);
3220
0
}
3221
3222
static inline gboolean
3223
g_object_get_is_valid_property (GObject          *object,
3224
                                GParamSpec       *pspec,
3225
                                const char       *property_name)
3226
0
{
3227
0
  if (G_UNLIKELY (pspec == NULL))
3228
0
    {
3229
0
      g_critical ("%s: object class '%s' has no property named '%s'",
3230
0
                  G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
3231
0
      return FALSE;
3232
0
    }
3233
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
3234
0
    {
3235
0
      g_critical ("%s: property '%s' of object class '%s' is not readable",
3236
0
                  G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
3237
0
      return FALSE;
3238
0
    }
3239
0
  return TRUE;
3240
0
}
3241
3242
/**
3243
 * g_object_getv:
3244
 * @object: a #GObject
3245
 * @n_properties: the number of properties
3246
 * @names: (array length=n_properties): the names of each property to get
3247
 * @values: (array length=n_properties): the values of each property to get
3248
 *
3249
 * Gets @n_properties properties for an @object.
3250
 * Obtained properties will be set to @values. All properties must be valid.
3251
 * Warnings will be emitted and undefined behaviour may result if invalid
3252
 * properties are passed in.
3253
 *
3254
 * Since: 2.54
3255
 */
3256
void
3257
g_object_getv (GObject      *object,
3258
               guint         n_properties,
3259
               const gchar  *names[],
3260
               GValue        values[])
3261
0
{
3262
0
  guint i;
3263
0
  GParamSpec *pspec;
3264
0
  GObjectClass *class;
3265
3266
0
  g_return_if_fail (G_IS_OBJECT (object));
3267
3268
0
  if (n_properties == 0)
3269
0
    return;
3270
3271
0
  g_object_ref (object);
3272
3273
0
  class = G_OBJECT_GET_CLASS (object);
3274
3275
0
  memset (values, 0, n_properties * sizeof (GValue));
3276
3277
0
  for (i = 0; i < n_properties; i++)
3278
0
    {
3279
0
      pspec = find_pspec (class, names[i]);
3280
3281
0
      if (!g_object_get_is_valid_property (object, pspec, names[i]))
3282
0
        break;
3283
0
      g_value_init (&values[i], pspec->value_type);
3284
0
      object_get_property (object, pspec, &values[i]);
3285
0
    }
3286
0
  g_object_unref (object);
3287
0
}
3288
3289
/**
3290
 * g_object_get_valist: (skip)
3291
 * @object: a #GObject
3292
 * @first_property_name: name of the first property to get
3293
 * @var_args: return location for the first property, followed optionally by more
3294
 *  name/return location pairs, followed by %NULL
3295
 *
3296
 * Gets properties of an object.
3297
 *
3298
 * In general, a copy is made of the property contents and the caller
3299
 * is responsible for freeing the memory in the appropriate manner for
3300
 * the type, for instance by calling g_free() or g_object_unref().
3301
 *
3302
 * See g_object_get().
3303
 */
3304
void
3305
g_object_get_valist (GObject   *object,
3306
         const gchar *first_property_name,
3307
         va_list    var_args)
3308
0
{
3309
0
  const gchar *name;
3310
0
  GObjectClass *class;
3311
  
3312
0
  g_return_if_fail (G_IS_OBJECT (object));
3313
  
3314
0
  g_object_ref (object);
3315
3316
0
  class = G_OBJECT_GET_CLASS (object);
3317
3318
0
  name = first_property_name;
3319
3320
0
  while (name)
3321
0
    {
3322
0
      GValue value = G_VALUE_INIT;
3323
0
      GParamSpec *pspec;
3324
0
      gchar *error;
3325
3326
0
      pspec = find_pspec (class, name);
3327
3328
0
      if (!g_object_get_is_valid_property (object, pspec, name))
3329
0
        break;
3330
      
3331
0
      g_value_init (&value, pspec->value_type);
3332
      
3333
0
      object_get_property (object, pspec, &value);
3334
      
3335
0
      G_VALUE_LCOPY (&value, var_args, 0, &error);
3336
0
      if (error)
3337
0
  {
3338
0
    g_critical ("%s: %s", G_STRFUNC, error);
3339
0
    g_free (error);
3340
0
    g_value_unset (&value);
3341
0
    break;
3342
0
  }
3343
      
3344
0
      g_value_unset (&value);
3345
      
3346
0
      name = va_arg (var_args, gchar*);
3347
0
    }
3348
  
3349
0
  g_object_unref (object);
3350
0
}
3351
3352
/**
3353
 * g_object_set: (skip)
3354
 * @object: (type GObject.Object): a #GObject
3355
 * @first_property_name: name of the first property to set
3356
 * @...: value for the first property, followed optionally by more
3357
 *  name/value pairs, followed by %NULL
3358
 *
3359
 * Sets properties on an object.
3360
 *
3361
 * The same caveats about passing integer literals as varargs apply as with
3362
 * g_object_new(). In particular, any integer literals set as the values for
3363
 * properties of type #gint64 or #guint64 must be 64 bits wide, using the
3364
 * %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
3365
 *
3366
 * Note that the "notify" signals are queued and only emitted (in
3367
 * reverse order) after all properties have been set. See
3368
 * g_object_freeze_notify().
3369
 */
3370
void
3371
g_object_set (gpointer     _object,
3372
        const gchar *first_property_name,
3373
        ...)
3374
0
{
3375
0
  GObject *object = _object;
3376
0
  va_list var_args;
3377
  
3378
0
  g_return_if_fail (G_IS_OBJECT (object));
3379
  
3380
0
  va_start (var_args, first_property_name);
3381
0
  g_object_set_valist (object, first_property_name, var_args);
3382
0
  va_end (var_args);
3383
0
}
3384
3385
/**
3386
 * g_object_get: (skip)
3387
 * @object: (type GObject.Object): a #GObject
3388
 * @first_property_name: name of the first property to get
3389
 * @...: return location for the first property, followed optionally by more
3390
 *  name/return location pairs, followed by %NULL
3391
 *
3392
 * Gets properties of an object.
3393
 *
3394
 * In general, a copy is made of the property contents and the caller
3395
 * is responsible for freeing the memory in the appropriate manner for
3396
 * the type, for instance by calling g_free() or g_object_unref().
3397
 *
3398
 * Here is an example of using g_object_get() to get the contents
3399
 * of three properties: an integer, a string and an object:
3400
 * |[<!-- language="C" --> 
3401
 *  gint intval;
3402
 *  guint64 uint64val;
3403
 *  gchar *strval;
3404
 *  GObject *objval;
3405
 *
3406
 *  g_object_get (my_object,
3407
 *                "int-property", &intval,
3408
 *                "uint64-property", &uint64val,
3409
 *                "str-property", &strval,
3410
 *                "obj-property", &objval,
3411
 *                NULL);
3412
 *
3413
 *  // Do something with intval, uint64val, strval, objval
3414
 *
3415
 *  g_free (strval);
3416
 *  g_object_unref (objval);
3417
 * ]|
3418
 */
3419
void
3420
g_object_get (gpointer     _object,
3421
        const gchar *first_property_name,
3422
        ...)
3423
0
{
3424
0
  GObject *object = _object;
3425
0
  va_list var_args;
3426
  
3427
0
  g_return_if_fail (G_IS_OBJECT (object));
3428
  
3429
0
  va_start (var_args, first_property_name);
3430
0
  g_object_get_valist (object, first_property_name, var_args);
3431
0
  va_end (var_args);
3432
0
}
3433
3434
/**
3435
 * g_object_set_property:
3436
 * @object: a #GObject
3437
 * @property_name: the name of the property to set
3438
 * @value: the value
3439
 *
3440
 * Sets a property on an object.
3441
 */
3442
void
3443
g_object_set_property (GObject      *object,
3444
           const gchar  *property_name,
3445
           const GValue *value)
3446
0
{
3447
0
  g_object_setv (object, 1, &property_name, value);
3448
0
}
3449
3450
/**
3451
 * g_object_get_property:
3452
 * @object: a #GObject
3453
 * @property_name: the name of the property to get
3454
 * @value: return location for the property value
3455
 *
3456
 * Gets a property of an object.
3457
 *
3458
 * The @value can be:
3459
 *
3460
 *  - an empty #GValue initialized by %G_VALUE_INIT, which will be
3461
 *    automatically initialized with the expected type of the property
3462
 *    (since GLib 2.60)
3463
 *  - a #GValue initialized with the expected type of the property
3464
 *  - a #GValue initialized with a type to which the expected type
3465
 *    of the property can be transformed
3466
 *
3467
 * In general, a copy is made of the property contents and the caller is
3468
 * responsible for freeing the memory by calling g_value_unset().
3469
 *
3470
 * Note that g_object_get_property() is really intended for language
3471
 * bindings, g_object_get() is much more convenient for C programming.
3472
 */
3473
void
3474
g_object_get_property (GObject     *object,
3475
           const gchar *property_name,
3476
           GValue    *value)
3477
0
{
3478
0
  GParamSpec *pspec;
3479
  
3480
0
  g_return_if_fail (G_IS_OBJECT (object));
3481
0
  g_return_if_fail (property_name != NULL);
3482
0
  g_return_if_fail (value != NULL);
3483
  
3484
0
  g_object_ref (object);
3485
  
3486
0
  pspec = find_pspec (G_OBJECT_GET_CLASS (object), property_name);
3487
3488
0
  if (g_object_get_is_valid_property (object, pspec, property_name))
3489
0
    {
3490
0
      GValue *prop_value, tmp_value = G_VALUE_INIT;
3491
      
3492
0
      if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
3493
0
        {
3494
          /* zero-initialized value */
3495
0
          g_value_init (value, pspec->value_type);
3496
0
          prop_value = value;
3497
0
        }
3498
0
      else if (G_VALUE_TYPE (value) == pspec->value_type)
3499
0
        {
3500
          /* auto-conversion of the callers value type */
3501
0
          g_value_reset (value);
3502
0
          prop_value = value;
3503
0
        }
3504
0
      else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
3505
0
        {
3506
0
          g_critical ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
3507
0
                      G_STRFUNC, pspec->name,
3508
0
                      g_type_name (pspec->value_type),
3509
0
                      G_VALUE_TYPE_NAME (value));
3510
0
          g_object_unref (object);
3511
0
          return;
3512
0
        }
3513
0
      else
3514
0
        {
3515
0
          g_value_init (&tmp_value, pspec->value_type);
3516
0
          prop_value = &tmp_value;
3517
0
        }
3518
0
      object_get_property (object, pspec, prop_value);
3519
0
      if (prop_value != value)
3520
0
        {
3521
0
          g_value_transform (prop_value, value);
3522
0
          g_value_unset (&tmp_value);
3523
0
        }
3524
0
    }
3525
  
3526
0
  g_object_unref (object);
3527
0
}
3528
3529
/**
3530
 * g_object_connect: (skip)
3531
 * @object: (type GObject.Object): a #GObject
3532
 * @signal_spec: the spec for the first signal
3533
 * @...: [type@GObject.Callback] for the first signal, followed by data for the
3534
 *   first signal, followed optionally by more signal
3535
 *   spec/callback/data triples, followed by `NULL`
3536
 *
3537
 * A convenience function to connect multiple signals at once.
3538
 *
3539
 * The signal specs expected by this function have the form
3540
 * `modifier::signal_name`, where `modifier` can be one of the
3541
 * following:
3542
 *
3543
 * - `signal`: equivalent to `g_signal_connect_data (..., NULL, G_CONNECT_DEFAULT)`
3544
 * - `object-signal`, `object_signal`: equivalent to `g_signal_connect_object (..., G_CONNECT_DEFAULT)`
3545
 * - `swapped-signal`, `swapped_signal`: equivalent to `g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)`
3546
 * - `swapped_object_signal`, `swapped-object-signal`: equivalent to `g_signal_connect_object (..., G_CONNECT_SWAPPED)`
3547
 * - `signal_after`, `signal-after`: equivalent to `g_signal_connect_data (..., NULL, G_CONNECT_AFTER)`
3548
 * - `object_signal_after`, `object-signal-after`: equivalent to `g_signal_connect_object (..., G_CONNECT_AFTER)`
3549
 * - `swapped_signal_after`, `swapped-signal-after`: equivalent to `g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)`
3550
 * - `swapped_object_signal_after`, `swapped-object-signal-after`: equivalent to `g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)`
3551
 *
3552
 * ```c
3553
 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
3554
 *                                                  "type", GTK_WINDOW_POPUP,
3555
 *                                                  "child", menu,
3556
 *                                                  NULL),
3557
 *                                    "signal::event", gtk_menu_window_event, menu,
3558
 *                                    "signal::size_request", gtk_menu_window_size_request, menu,
3559
 *                                    "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
3560
 *                                    NULL);
3561
 * ```
3562
 *
3563
 * Returns: (transfer none) (type GObject.Object): the object
3564
 */
3565
gpointer
3566
g_object_connect (gpointer     _object,
3567
      const gchar *signal_spec,
3568
      ...)
3569
0
{
3570
0
  GObject *object = _object;
3571
0
  va_list var_args;
3572
3573
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3574
0
  g_return_val_if_fail (object->ref_count > 0, object);
3575
3576
0
  va_start (var_args, signal_spec);
3577
0
  while (signal_spec)
3578
0
    {
3579
0
      GCallback callback = va_arg (var_args, GCallback);
3580
0
      gpointer data = va_arg (var_args, gpointer);
3581
3582
0
      if (strncmp (signal_spec, "signal::", 8) == 0)
3583
0
  g_signal_connect_data (object, signal_spec + 8,
3584
0
             callback, data, NULL,
3585
0
             G_CONNECT_DEFAULT);
3586
0
      else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
3587
0
               strncmp (signal_spec, "object-signal::", 15) == 0)
3588
0
  g_signal_connect_object (object, signal_spec + 15,
3589
0
         callback, data,
3590
0
         G_CONNECT_DEFAULT);
3591
0
      else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
3592
0
               strncmp (signal_spec, "swapped-signal::", 16) == 0)
3593
0
  g_signal_connect_data (object, signal_spec + 16,
3594
0
             callback, data, NULL,
3595
0
             G_CONNECT_SWAPPED);
3596
0
      else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
3597
0
               strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
3598
0
  g_signal_connect_object (object, signal_spec + 23,
3599
0
         callback, data,
3600
0
         G_CONNECT_SWAPPED);
3601
0
      else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
3602
0
               strncmp (signal_spec, "signal-after::", 14) == 0)
3603
0
  g_signal_connect_data (object, signal_spec + 14,
3604
0
             callback, data, NULL,
3605
0
             G_CONNECT_AFTER);
3606
0
      else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
3607
0
               strncmp (signal_spec, "object-signal-after::", 21) == 0)
3608
0
  g_signal_connect_object (object, signal_spec + 21,
3609
0
         callback, data,
3610
0
         G_CONNECT_AFTER);
3611
0
      else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
3612
0
               strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
3613
0
  g_signal_connect_data (object, signal_spec + 22,
3614
0
             callback, data, NULL,
3615
0
             G_CONNECT_SWAPPED | G_CONNECT_AFTER);
3616
0
      else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
3617
0
               strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
3618
0
  g_signal_connect_object (object, signal_spec + 29,
3619
0
         callback, data,
3620
0
         G_CONNECT_SWAPPED | G_CONNECT_AFTER);
3621
0
      else
3622
0
  {
3623
0
    g_critical ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
3624
0
    break;
3625
0
  }
3626
0
      signal_spec = va_arg (var_args, gchar*);
3627
0
    }
3628
0
  va_end (var_args);
3629
3630
0
  return object;
3631
0
}
3632
3633
/**
3634
 * g_object_disconnect: (skip)
3635
 * @object: (type GObject.Object): a #GObject
3636
 * @signal_spec: the spec for the first signal
3637
 * @...: #GCallback for the first signal, followed by data for the first signal,
3638
 *  followed optionally by more signal spec/callback/data triples,
3639
 *  followed by %NULL
3640
 *
3641
 * A convenience function to disconnect multiple signals at once.
3642
 *
3643
 * The signal specs expected by this function have the form
3644
 * "any_signal", which means to disconnect any signal with matching
3645
 * callback and data, or "any_signal::signal_name", which only
3646
 * disconnects the signal named "signal_name".
3647
 */
3648
void
3649
g_object_disconnect (gpointer     _object,
3650
         const gchar *signal_spec,
3651
         ...)
3652
0
{
3653
0
  GObject *object = _object;
3654
0
  va_list var_args;
3655
3656
0
  g_return_if_fail (G_IS_OBJECT (object));
3657
0
  g_return_if_fail (object->ref_count > 0);
3658
3659
0
  va_start (var_args, signal_spec);
3660
0
  while (signal_spec)
3661
0
    {
3662
0
      GCallback callback = va_arg (var_args, GCallback);
3663
0
      gpointer data = va_arg (var_args, gpointer);
3664
0
      guint sid = 0, detail = 0, mask = 0;
3665
3666
0
      if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
3667
0
          strncmp (signal_spec, "any-signal::", 12) == 0)
3668
0
  {
3669
0
    signal_spec += 12;
3670
0
    mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
3671
0
  }
3672
0
      else if (strcmp (signal_spec, "any_signal") == 0 ||
3673
0
               strcmp (signal_spec, "any-signal") == 0)
3674
0
  {
3675
0
    signal_spec += 10;
3676
0
    mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
3677
0
  }
3678
0
      else
3679
0
  {
3680
0
    g_critical ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
3681
0
    break;
3682
0
  }
3683
3684
0
      if ((mask & G_SIGNAL_MATCH_ID) &&
3685
0
    !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
3686
0
  g_critical ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
3687
0
      else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
3688
0
                  sid, detail,
3689
0
                  NULL, (gpointer)callback, data))
3690
0
  g_critical ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
3691
0
      signal_spec = va_arg (var_args, gchar*);
3692
0
    }
3693
0
  va_end (var_args);
3694
0
}
3695
3696
typedef struct
3697
{
3698
  GWeakNotify notify;
3699
  gpointer data;
3700
} WeakRefTuple;
3701
3702
struct _WeakRefReleaseAllState;
3703
3704
typedef struct _WeakRefReleaseAllState
3705
{
3706
  guint remaining_to_notify;
3707
  struct _WeakRefReleaseAllState *release_all_next;
3708
} WeakRefReleaseAllState;
3709
3710
typedef struct
3711
{
3712
  guint n_weak_refs;
3713
  guint alloc_size;
3714
  WeakRefReleaseAllState *release_all_states;
3715
  WeakRefTuple weak_refs[1]; /* flexible array */
3716
} WeakRefStack;
3717
3718
0
#define WEAK_REF_STACK_ALLOC_SIZE(alloc_size) (G_STRUCT_OFFSET (WeakRefStack, weak_refs) + sizeof (WeakRefTuple) * (alloc_size))
3719
3720
G_GNUC_UNUSED G_ALWAYS_INLINE static inline gboolean
3721
_weak_ref_release_all_state_contains (WeakRefReleaseAllState *release_all_state, WeakRefReleaseAllState *needle)
3722
0
{
3723
0
  for (; release_all_state; release_all_state = release_all_state->release_all_next)
3724
0
    {
3725
0
      if (release_all_state == needle)
3726
0
        return TRUE;
3727
0
    }
3728
0
  return FALSE;
3729
0
}
3730
3731
G_ALWAYS_INLINE static inline void
3732
_weak_ref_stack_free (WeakRefStack *wstack)
3733
0
{
3734
0
#ifdef G_ENABLE_DEBUG
3735
0
  g_assert (!wstack->release_all_states);
3736
0
#endif
3737
0
  g_free (wstack);
3738
0
}
3739
3740
G_ALWAYS_INLINE static inline void
3741
_weak_ref_stack_update_release_all_state (WeakRefStack *wstack, guint idx)
3742
0
{
3743
0
  WeakRefReleaseAllState **previous_ptr;
3744
0
  WeakRefReleaseAllState *release_all_state;
3745
3746
0
#ifdef G_ENABLE_DEBUG
3747
0
  g_assert (idx < wstack->n_weak_refs);
3748
0
#endif
3749
3750
0
  previous_ptr = &wstack->release_all_states;
3751
3752
0
  while (G_UNLIKELY ((release_all_state = *previous_ptr)))
3753
0
    {
3754
0
      if (idx >= release_all_state->remaining_to_notify)
3755
0
        {
3756
0
#ifdef G_ENABLE_DEBUG
3757
0
          g_assert (release_all_state->remaining_to_notify <= wstack->n_weak_refs);
3758
0
#endif
3759
          /* We removed an index higher than the "remaining_to_notify" count. */
3760
0
          goto next;
3761
0
        }
3762
3763
      /* Lower the "remaining_to_notify" bar of the entries we consider, as we
3764
       * just removed an entry at index @idx (below that bar). */
3765
0
      release_all_state->remaining_to_notify--;
3766
3767
0
      if (release_all_state->remaining_to_notify > 0)
3768
0
        goto next;
3769
3770
      /* Remove the entry from the linked list. No need to reset
3771
       * release_all_state->release_all_next pointer to NULL as it has no
3772
       * purpose when not being linked. */
3773
0
      *previous_ptr = release_all_state->release_all_next;
3774
0
      continue;
3775
3776
0
    next:
3777
0
      previous_ptr = &release_all_state->release_all_next;
3778
0
    }
3779
0
}
3780
3781
static gpointer
3782
g_object_weak_ref_cb (gpointer *data,
3783
                      GDestroyNotify *destroy_notify,
3784
                      gpointer user_data)
3785
0
{
3786
0
  WeakRefTuple *tuple = user_data;
3787
0
  WeakRefStack *wstack = *data;
3788
0
  guint i;
3789
3790
0
  if (!wstack)
3791
0
    {
3792
0
      wstack = g_malloc (WEAK_REF_STACK_ALLOC_SIZE (1));
3793
0
      wstack->alloc_size = 1;
3794
0
      wstack->n_weak_refs = 1;
3795
0
      wstack->release_all_states = NULL;
3796
0
      i = 0;
3797
3798
0
      *data = wstack;
3799
      /* We don't set a @destroy_notify. Shortly before finalize(), we call
3800
       * g_object_weak_release_all(), which frees the WeakRefStack. At that
3801
       * point the ref-count is already at zero and g_object_weak_ref() will
3802
       * assert against being called. This means, we expect that there is
3803
       * never anything to destroy. */
3804
0
#ifdef G_ENABLE_DEBUG
3805
0
      *destroy_notify = g_destroy_notify_assert_not_reached;
3806
0
#endif
3807
0
    }
3808
0
  else
3809
0
    {
3810
0
      i = wstack->n_weak_refs++;
3811
3812
0
      if (G_UNLIKELY (wstack->n_weak_refs > wstack->alloc_size))
3813
0
        {
3814
0
          if (G_UNLIKELY (wstack->alloc_size >= (G_MAXUINT / 2u + 1u)))
3815
0
            g_error ("g_object_weak_ref(): cannot register more than 2^31 references");
3816
0
          wstack->alloc_size = wstack->alloc_size * 2u;
3817
3818
0
          wstack = g_realloc (wstack, WEAK_REF_STACK_ALLOC_SIZE (wstack->alloc_size));
3819
0
          *data = wstack;
3820
0
        }
3821
0
    }
3822
3823
0
  wstack->weak_refs[i] = *tuple;
3824
3825
0
  return NULL;
3826
0
}
3827
3828
/**
3829
 * g_object_weak_ref: (skip)
3830
 * @object: #GObject to reference weakly
3831
 * @notify: callback to invoke before the object is freed
3832
 * @data: extra data to pass to notify
3833
 *
3834
 * Adds a weak reference callback to an object. Weak references are
3835
 * used for notification when an object is disposed. They are called
3836
 * "weak references" because they allow you to safely hold a pointer
3837
 * to an object without calling g_object_ref() (g_object_ref() adds a
3838
 * strong reference, that is, forces the object to stay alive).
3839
 *
3840
 * Note that the weak references created by this method are not
3841
 * thread-safe: they cannot safely be used in one thread if the
3842
 * object's last g_object_unref() might happen in another thread.
3843
 * Use #GWeakRef if thread-safety is required.
3844
 */
3845
void
3846
g_object_weak_ref (GObject    *object,
3847
       GWeakNotify notify,
3848
       gpointer    data)
3849
0
{
3850
0
  g_return_if_fail (G_IS_OBJECT (object));
3851
0
  g_return_if_fail (notify != NULL);
3852
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3853
3854
0
  _g_datalist_id_update_atomic (&object->qdata,
3855
0
                                quark_weak_notifies,
3856
0
                                g_object_weak_ref_cb,
3857
0
                                &((WeakRefTuple){
3858
0
                                    .notify = notify,
3859
0
                                    .data = data,
3860
0
                                }));
3861
0
}
3862
3863
static gpointer
3864
g_object_weak_unref_cb (gpointer *data,
3865
                        GDestroyNotify *destroy_notify,
3866
                        gpointer user_data)
3867
0
{
3868
0
  WeakRefTuple *tuple = user_data;
3869
0
  WeakRefStack *wstack = *data;
3870
0
  gboolean found_one = FALSE;
3871
0
  guint i;
3872
3873
0
  if (wstack)
3874
0
    {
3875
0
      for (i = 0; i < wstack->n_weak_refs; i++)
3876
0
        {
3877
0
          if (wstack->weak_refs[i].notify != tuple->notify ||
3878
0
              wstack->weak_refs[i].data != tuple->data)
3879
0
            continue;
3880
3881
0
          _weak_ref_stack_update_release_all_state (wstack, i);
3882
3883
0
          wstack->n_weak_refs -= 1;
3884
0
          if (wstack->n_weak_refs == 0)
3885
0
            {
3886
0
              _weak_ref_stack_free (wstack);
3887
0
              *data = NULL;
3888
0
            }
3889
0
          else
3890
0
            {
3891
0
              if (i != wstack->n_weak_refs)
3892
0
                {
3893
0
                  memmove (&wstack->weak_refs[i],
3894
0
                           &wstack->weak_refs[i + 1],
3895
0
                           sizeof (wstack->weak_refs[i]) * (wstack->n_weak_refs - i));
3896
0
                }
3897
3898
0
              if (G_UNLIKELY (wstack->n_weak_refs <= wstack->alloc_size / 4u))
3899
0
                {
3900
0
                  wstack->alloc_size = wstack->alloc_size / 2u;
3901
0
                  wstack = g_realloc (wstack, WEAK_REF_STACK_ALLOC_SIZE (wstack->alloc_size));
3902
0
                  *data = wstack;
3903
0
                }
3904
0
            }
3905
3906
0
          found_one = TRUE;
3907
0
          break;
3908
0
        }
3909
0
    }
3910
3911
0
  if (!found_one)
3912
0
    g_critical ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, tuple->notify, tuple->data);
3913
3914
0
  return NULL;
3915
0
}
3916
3917
/**
3918
 * g_object_weak_unref: (skip)
3919
 * @object: #GObject to remove a weak reference from
3920
 * @notify: callback to search for
3921
 * @data: data to search for
3922
 *
3923
 * Removes a weak reference callback to an object.
3924
 */
3925
void
3926
g_object_weak_unref (GObject    *object,
3927
         GWeakNotify notify,
3928
         gpointer    data)
3929
0
{
3930
0
  g_return_if_fail (G_IS_OBJECT (object));
3931
0
  g_return_if_fail (notify != NULL);
3932
3933
0
  _g_datalist_id_update_atomic (&object->qdata,
3934
0
                                quark_weak_notifies,
3935
0
                                g_object_weak_unref_cb,
3936
0
                                &((WeakRefTuple){
3937
0
                                    .notify = notify,
3938
0
                                    .data = data,
3939
0
                                }));
3940
0
}
3941
3942
typedef struct
3943
{
3944
  WeakRefReleaseAllState *const release_all_state;
3945
  WeakRefTuple tuple;
3946
  gboolean release_all_done;
3947
} WeakRefReleaseAllData;
3948
3949
static gpointer
3950
g_object_weak_release_all_cb (gpointer *data,
3951
                              GDestroyNotify *destroy_notify,
3952
                              gpointer user_data)
3953
12.9k
{
3954
12.9k
  WeakRefStack *wstack = *data;
3955
12.9k
  WeakRefReleaseAllData *wdata = user_data;
3956
12.9k
  WeakRefReleaseAllState *release_all_state = wdata->release_all_state;
3957
3958
12.9k
  if (!wstack)
3959
12.9k
    return NULL;
3960
3961
0
#ifdef G_ENABLE_DEBUG
3962
12.9k
  g_assert (wstack->n_weak_refs > 0);
3963
0
#endif
3964
3965
0
  if (release_all_state)
3966
0
    {
3967
0
      if (release_all_state->remaining_to_notify == G_MAXUINT)
3968
0
        {
3969
0
          if (wstack->n_weak_refs == 1u)
3970
0
            {
3971
              /* We only pop the single entry. */
3972
0
              wdata->release_all_done = TRUE;
3973
0
              release_all_state = NULL;
3974
0
            }
3975
0
          else
3976
0
            {
3977
0
              release_all_state->remaining_to_notify = wstack->n_weak_refs;
3978
3979
              /* Prepend to linked list. */
3980
0
              release_all_state->release_all_next = wstack->release_all_states;
3981
0
              wstack->release_all_states = release_all_state;
3982
0
            }
3983
0
        }
3984
0
      else
3985
0
        {
3986
0
          if (release_all_state->remaining_to_notify == 0u)
3987
0
            {
3988
0
#ifdef G_ENABLE_DEBUG
3989
0
              g_assert (!_weak_ref_release_all_state_contains (wstack->release_all_states, release_all_state));
3990
0
#endif
3991
0
              return NULL;
3992
0
            }
3993
0
#ifdef G_ENABLE_DEBUG
3994
0
          g_assert (release_all_state->remaining_to_notify <= wstack->n_weak_refs);
3995
0
          g_assert (_weak_ref_release_all_state_contains (wstack->release_all_states, release_all_state));
3996
0
#endif
3997
0
        }
3998
0
    }
3999
4000
0
  _weak_ref_stack_update_release_all_state (wstack, 0);
4001
4002
0
  if (release_all_state && release_all_state->remaining_to_notify == 0)
4003
0
    wdata->release_all_done = TRUE;
4004
4005
0
  wstack->n_weak_refs--;
4006
4007
  /* Emit the notifications in FIFO order. */
4008
0
  wdata->tuple = wstack->weak_refs[0];
4009
4010
0
  if (wstack->n_weak_refs == 0)
4011
0
    {
4012
0
      _weak_ref_stack_free (wstack);
4013
0
      *data = NULL;
4014
4015
      /* Also set release_all_done.
4016
       *
4017
       * If g_object_weak_release_all() was called during dispose (with
4018
       * release_all FALSE), we anyway have an upper limit of how many
4019
       * notifications we want to pop. We only pop the notifications that were
4020
       * registered when the loop initially starts. In that case, we surely
4021
       * don't want the caller to call back.
4022
       *
4023
       * g_object_weak_release_all() is also being called before finalize. At
4024
       * that point, the ref count is already at zero, and g_object_weak_ref()
4025
       * asserts against being called. So nobody can register a new weak ref
4026
       * anymore.
4027
       *
4028
       * In both cases, we don't require the calling loop to call back. This
4029
       * saves an additional GData lookup. */
4030
0
      wdata->release_all_done = TRUE;
4031
0
    }
4032
0
  else
4033
0
    {
4034
0
      memmove (&wstack->weak_refs[0],
4035
0
               &wstack->weak_refs[1],
4036
0
               sizeof (wstack->weak_refs[0]) * wstack->n_weak_refs);
4037
4038
      /* Don't bother to shrink the buffer. Most likely the object gets
4039
       * destroyed soon after. */
4040
0
    }
4041
4042
0
  return wdata;
4043
0
}
4044
4045
static void
4046
g_object_weak_release_all (GObject *object, gboolean release_all)
4047
12.9k
{
4048
12.9k
  WeakRefReleaseAllState release_all_state = {
4049
12.9k
    .remaining_to_notify = G_MAXUINT,
4050
12.9k
  };
4051
12.9k
  WeakRefReleaseAllData wdata = {
4052
12.9k
    .release_all_state = release_all ? NULL : &release_all_state,
4053
12.9k
    .release_all_done = FALSE,
4054
12.9k
  };
4055
4056
12.9k
  while (TRUE)
4057
12.9k
    {
4058
12.9k
      if (!_g_datalist_id_update_atomic (&object->qdata,
4059
12.9k
                                         quark_weak_notifies,
4060
12.9k
                                         g_object_weak_release_all_cb,
4061
12.9k
                                         &wdata))
4062
12.9k
        break;
4063
4064
0
      wdata.tuple.notify (wdata.tuple.data, object);
4065
4066
0
      if (wdata.release_all_done)
4067
0
        break;
4068
0
    }
4069
12.9k
}
4070
4071
/**
4072
 * g_object_add_weak_pointer: (skip)
4073
 * @object: The object that should be weak referenced.
4074
 * @weak_pointer_location: (inout) (not optional): The memory address
4075
 *    of a pointer.
4076
 *
4077
 * Adds a weak reference from weak_pointer to @object to indicate that
4078
 * the pointer located at @weak_pointer_location is only valid during
4079
 * the lifetime of @object. When the @object is finalized,
4080
 * @weak_pointer will be set to %NULL.
4081
 *
4082
 * Note that as with g_object_weak_ref(), the weak references created by
4083
 * this method are not thread-safe: they cannot safely be used in one
4084
 * thread if the object's last g_object_unref() might happen in another
4085
 * thread. Use #GWeakRef if thread-safety is required.
4086
 */
4087
void
4088
g_object_add_weak_pointer (GObject  *object, 
4089
                           gpointer *weak_pointer_location)
4090
0
{
4091
0
  g_return_if_fail (G_IS_OBJECT (object));
4092
0
  g_return_if_fail (weak_pointer_location != NULL);
4093
4094
0
  g_object_weak_ref (object, 
4095
0
                     (GWeakNotify) g_nullify_pointer, 
4096
0
                     weak_pointer_location);
4097
0
}
4098
4099
/**
4100
 * g_object_remove_weak_pointer: (skip)
4101
 * @object: The object that is weak referenced.
4102
 * @weak_pointer_location: (inout) (not optional): The memory address
4103
 *    of a pointer.
4104
 *
4105
 * Removes a weak reference from @object that was previously added
4106
 * using g_object_add_weak_pointer(). The @weak_pointer_location has
4107
 * to match the one used with g_object_add_weak_pointer().
4108
 */
4109
void
4110
g_object_remove_weak_pointer (GObject  *object, 
4111
                              gpointer *weak_pointer_location)
4112
0
{
4113
0
  g_return_if_fail (G_IS_OBJECT (object));
4114
0
  g_return_if_fail (weak_pointer_location != NULL);
4115
4116
0
  g_object_weak_unref (object, 
4117
0
                       (GWeakNotify) g_nullify_pointer, 
4118
0
                       weak_pointer_location);
4119
0
}
4120
4121
static guint
4122
object_floating_flag_handler (GObject        *object,
4123
                              gint            job)
4124
0
{
4125
0
  switch (job)
4126
0
    {
4127
0
      gpointer oldvalue;
4128
0
    case +1:    /* force floating if possible */
4129
0
      oldvalue = g_atomic_pointer_get (&object->qdata);
4130
0
      while (!g_atomic_pointer_compare_and_exchange_full (
4131
0
        (void**) &object->qdata, oldvalue,
4132
0
        (void *) ((guintptr) oldvalue | OBJECT_FLOATING_FLAG),
4133
0
        &oldvalue))
4134
0
        ;
4135
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
4136
0
    case -1:    /* sink if possible */
4137
0
      oldvalue = g_atomic_pointer_get (&object->qdata);
4138
0
      while (!g_atomic_pointer_compare_and_exchange_full (
4139
0
        (void**) &object->qdata, oldvalue,
4140
0
        (void *) ((guintptr) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG),
4141
0
        &oldvalue))
4142
0
        ;
4143
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
4144
0
    default:    /* check floating */
4145
0
      return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
4146
0
    }
4147
0
}
4148
4149
/**
4150
 * g_object_is_floating:
4151
 * @object: (type GObject.Object): a #GObject
4152
 *
4153
 * Checks whether @object has a [floating][floating-ref] reference.
4154
 *
4155
 * Since: 2.10
4156
 *
4157
 * Returns: %TRUE if @object has a floating reference
4158
 */
4159
gboolean
4160
g_object_is_floating (gpointer _object)
4161
0
{
4162
0
  GObject *object = _object;
4163
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
4164
0
  return (floating_flag_handler (object, 0) != 0);
4165
0
}
4166
4167
/**
4168
 * g_object_ref_sink:
4169
 * @object: (type GObject.Object): a #GObject
4170
 *
4171
 * Increase the reference count of @object, and possibly remove the
4172
 * [floating][floating-ref] reference, if @object has a floating reference.
4173
 *
4174
 * In other words, if the object is floating, then this call "assumes
4175
 * ownership" of the floating reference, converting it to a normal
4176
 * reference by clearing the floating flag while leaving the reference
4177
 * count unchanged.  If the object is not floating, then this call
4178
 * adds a new normal reference increasing the reference count by one.
4179
 *
4180
 * Since GLib 2.56, the type of @object will be propagated to the return type
4181
 * under the same conditions as for g_object_ref().
4182
 *
4183
 * Since: 2.10
4184
 *
4185
 * Returns: (type GObject.Object) (transfer none): @object
4186
 */
4187
gpointer
4188
(g_object_ref_sink) (gpointer _object)
4189
0
{
4190
0
  GObject *object = _object;
4191
0
  gboolean was_floating;
4192
0
  g_return_val_if_fail (G_IS_OBJECT (object), object);
4193
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object);
4194
0
  g_object_ref (object);
4195
0
  was_floating = (floating_flag_handler (object, -1) != 0);
4196
0
  if (was_floating)
4197
0
    g_object_unref (object);
4198
0
  return object;
4199
0
}
4200
4201
/**
4202
 * g_object_take_ref: (skip)
4203
 * @object: (type GObject.Object): a #GObject
4204
 *
4205
 * If @object is floating, sink it.  Otherwise, do nothing.
4206
 *
4207
 * In other words, this function will convert a floating reference (if
4208
 * present) into a full reference.
4209
 *
4210
 * Typically you want to use g_object_ref_sink() in order to
4211
 * automatically do the correct thing with respect to floating or
4212
 * non-floating references, but there is one specific scenario where
4213
 * this function is helpful.
4214
 *
4215
 * The situation where this function is helpful is when creating an API
4216
 * that allows the user to provide a callback function that returns a
4217
 * GObject. We certainly want to allow the user the flexibility to
4218
 * return a non-floating reference from this callback (for the case
4219
 * where the object that is being returned already exists).
4220
 *
4221
 * At the same time, the API style of some popular GObject-based
4222
 * libraries (such as Gtk) make it likely that for newly-created GObject
4223
 * instances, the user can be saved some typing if they are allowed to
4224
 * return a floating reference.
4225
 *
4226
 * Using this function on the return value of the user's callback allows
4227
 * the user to do whichever is more convenient for them. The caller will
4228
 * always receives exactly one full reference to the value: either the
4229
 * one that was returned in the first place, or a floating reference
4230
 * that has been converted to a full reference.
4231
 *
4232
 * This function has an odd interaction when combined with
4233
 * g_object_ref_sink() running at the same time in another thread on
4234
 * the same #GObject instance. If g_object_ref_sink() runs first then
4235
 * the result will be that the floating reference is converted to a hard
4236
 * reference. If g_object_take_ref() runs first then the result will be
4237
 * that the floating reference is converted to a hard reference and an
4238
 * additional reference on top of that one is added. It is best to avoid
4239
 * this situation.
4240
 *
4241
 * Since: 2.70
4242
 *
4243
 * Returns: (type GObject.Object) (transfer full): @object
4244
 */
4245
gpointer
4246
g_object_take_ref (gpointer _object)
4247
0
{
4248
0
  GObject *object = _object;
4249
0
  g_return_val_if_fail (G_IS_OBJECT (object), object);
4250
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object);
4251
4252
0
  floating_flag_handler (object, -1);
4253
4254
0
  return object;
4255
0
}
4256
4257
/**
4258
 * g_object_force_floating:
4259
 * @object: a #GObject
4260
 *
4261
 * This function is intended for #GObject implementations to re-enforce
4262
 * a [floating][floating-ref] object reference. Doing this is seldom
4263
 * required: all #GInitiallyUnowneds are created with a floating reference
4264
 * which usually just needs to be sunken by calling g_object_ref_sink().
4265
 *
4266
 * Since: 2.10
4267
 */
4268
void
4269
g_object_force_floating (GObject *object)
4270
0
{
4271
0
  g_return_if_fail (G_IS_OBJECT (object));
4272
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
4273
4274
0
  floating_flag_handler (object, +1);
4275
0
}
4276
4277
typedef struct
4278
{
4279
  GToggleNotify notify;
4280
  gpointer data;
4281
} ToggleRefTuple;
4282
4283
typedef struct
4284
{
4285
  GObject *object;
4286
  ToggleRefTuple tuple;
4287
} ToggleRefCallbackData;
4288
4289
typedef struct
4290
{
4291
  guint n_toggle_refs;
4292
  ToggleRefTuple toggle_refs[1]; /* flexible array */
4293
} ToggleRefStack;
4294
4295
static gpointer
4296
toggle_refs_check_and_ref_cb (gpointer *data,
4297
                              GDestroyNotify *destroy_notify,
4298
                              gpointer user_data)
4299
0
{
4300
0
  GToggleNotify *toggle_notify = ((gpointer *) user_data)[0];
4301
0
  gpointer *toggle_data = ((gpointer *) user_data)[1];
4302
0
  ToggleRefStack *tstack = *data;
4303
4304
0
  if (G_UNLIKELY (tstack->n_toggle_refs != 1))
4305
0
    {
4306
      /* We only reach this line after we checked that the ref-count was 1
4307
       * and that OBJECT_HAS_TOGGLE_REF(). We expect that there is exactly
4308
       * one toggle reference registered. */
4309
0
      g_critical ("Unexpected number of toggle-refs. g_object_add_toggle_ref() must be paired with g_object_remove_toggle_ref()");
4310
0
      *toggle_notify = NULL;
4311
0
      return NULL;
4312
0
    }
4313
4314
0
  *toggle_notify = tstack->toggle_refs[0].notify;
4315
0
  *toggle_data = tstack->toggle_refs[0].data;
4316
0
  return NULL;
4317
0
}
4318
4319
G_ALWAYS_INLINE static inline gboolean
4320
toggle_refs_check_and_ref_or_deref (GObject *object,
4321
                                    gboolean is_ref,
4322
                                    gint *old_ref,
4323
                                    GToggleNotify *toggle_notify,
4324
                                    gpointer *toggle_data)
4325
2.89k
{
4326
2.89k
  const gint ref_curr = is_ref ? 1 : 2;
4327
2.89k
  const gint ref_next = is_ref ? 2 : 1;
4328
2.89k
  gboolean success;
4329
4330
2.89k
#if G_ENABLE_DEBUG
4331
2.89k
  g_assert (ref_curr == *old_ref);
4332
2.89k
#endif
4333
4334
2.89k
  *toggle_notify = NULL;
4335
2.89k
  *toggle_data = NULL;
4336
4337
  /* This is called from g_object_ref()/g_object_unref() and a hot path.
4338
   *
4339
   * We hack the GData open and take the g_datalist_lock() outside. Then we
4340
   * perform checks, that most likely will tell us that there is not toggle
4341
   * notifications. Only if we have a toggle notification, we call
4342
   * _g_datalist_id_update_atomic_full(). */
4343
4344
2.89k
  g_datalist_lock (&object->qdata);
4345
4346
  /* @old_ref is mainly an (out) parameter. On failure to compare-and-exchange,
4347
   * we MUST return the new value which the caller will use for retry.*/
4348
4349
2.89k
  success = g_atomic_int_compare_and_exchange_full ((int *) &object->ref_count,
4350
2.89k
                                                    ref_curr,
4351
2.89k
                                                    ref_next,
4352
2.89k
                                                    old_ref);
4353
4354
  /* Note that if we are called during g_object_unref (@is_ref set to FALSE),
4355
   * then we drop the ref count from 2 to 1 and give up our reference. We thus
4356
   * no longer hold a strong reference and another thread may race against
4357
   * destroying the object.
4358
   *
4359
   * After this point with is_ref=FALSE and success=TRUE, @object must no
4360
   * longer be accessed.
4361
   *
4362
   * The exception is here. While we still hold the lock, we know that @object
4363
   * could not be destroyed, because g_object_unref() also needs to acquire the
4364
   * same lock before finalizing @object. Thus, we know object cannot yet be
4365
   * destroyed and we can access it until the unlock below. */
4366
4367
2.89k
  if (G_UNLIKELY (!success))
4368
0
    {
4369
0
      g_datalist_unlock (&object->qdata);
4370
0
      return FALSE;
4371
0
    }
4372
4373
2.89k
  if (G_LIKELY (!OBJECT_HAS_TOGGLE_REF (object)))
4374
2.89k
    {
4375
2.89k
      g_datalist_unlock (&object->qdata);
4376
2.89k
      return TRUE;
4377
2.89k
    }
4378
4379
  /* slow-path. We have a toggle reference. Call into g_datalist_id_update_atomic().
4380
   *
4381
   * Note that _g_datalist_id_update_atomic_full() will release the lock! */
4382
0
  _g_datalist_id_update_atomic_full (&object->qdata,
4383
0
                                     quark_toggle_refs,
4384
0
                                     TRUE,
4385
0
                                     toggle_refs_check_and_ref_cb,
4386
0
                                     (gpointer[2]){ toggle_notify, toggle_data });
4387
4388
0
  return TRUE;
4389
2.89k
}
4390
4391
static gpointer
4392
toggle_refs_ref_cb (gpointer *data,
4393
                    GDestroyNotify *destroy_notify,
4394
                    gpointer user_data)
4395
0
{
4396
0
  ToggleRefCallbackData *trdata = user_data;
4397
0
  ToggleRefStack *tstack = *data;
4398
0
  guint i;
4399
4400
0
  if (!tstack)
4401
0
    {
4402
0
      tstack = g_new (ToggleRefStack, 1);
4403
0
      tstack->n_toggle_refs = 1;
4404
0
      i = 0;
4405
4406
0
      g_datalist_set_flags (&trdata->object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
4407
4408
0
      *destroy_notify = g_free;
4409
0
    }
4410
0
  else
4411
0
    {
4412
0
      i = tstack->n_toggle_refs++;
4413
0
      tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
4414
0
    }
4415
4416
0
  *data = tstack;
4417
4418
0
  tstack->toggle_refs[i] = trdata->tuple;
4419
4420
0
  return NULL;
4421
0
}
4422
4423
/**
4424
 * g_object_add_toggle_ref: (skip)
4425
 * @object: a #GObject
4426
 * @notify: a function to call when this reference is the
4427
 *  last reference to the object, or is no longer
4428
 *  the last reference.
4429
 * @data: data to pass to @notify
4430
 *
4431
 * Increases the reference count of the object by one and sets a
4432
 * callback to be called when all other references to the object are
4433
 * dropped, or when this is already the last reference to the object
4434
 * and another reference is established.
4435
 *
4436
 * This functionality is intended for binding @object to a proxy
4437
 * object managed by another memory manager. This is done with two
4438
 * paired references: the strong reference added by
4439
 * g_object_add_toggle_ref() and a reverse reference to the proxy
4440
 * object which is either a strong reference or weak reference.
4441
 *
4442
 * The setup is that when there are no other references to @object,
4443
 * only a weak reference is held in the reverse direction from @object
4444
 * to the proxy object, but when there are other references held to
4445
 * @object, a strong reference is held. The @notify callback is called
4446
 * when the reference from @object to the proxy object should be
4447
 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
4448
 * (@is_last_ref false).
4449
 *
4450
 * Since a (normal) reference must be held to the object before
4451
 * calling g_object_add_toggle_ref(), the initial state of the reverse
4452
 * link is always strong.
4453
 *
4454
 * Multiple toggle references may be added to the same gobject,
4455
 * however if there are multiple toggle references to an object, none
4456
 * of them will ever be notified until all but one are removed.  For
4457
 * this reason, you should only ever use a toggle reference if there
4458
 * is important state in the proxy object.
4459
 *
4460
 * Note that if you unref the object on another thread, then @notify might
4461
 * still be invoked after g_object_remove_toggle_ref(), and the object argument
4462
 * might be a dangling pointer. If the object is destroyed on other threads,
4463
 * you must take care of that yourself.
4464
 *
4465
 * A g_object_add_toggle_ref() must be released with g_object_remove_toggle_ref().
4466
 *
4467
 * Since: 2.8
4468
 */
4469
void
4470
g_object_add_toggle_ref (GObject       *object,
4471
       GToggleNotify  notify,
4472
       gpointer       data)
4473
0
{
4474
0
  g_return_if_fail (G_IS_OBJECT (object));
4475
0
  g_return_if_fail (notify != NULL);
4476
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
4477
4478
0
  g_object_ref (object);
4479
4480
0
  _g_datalist_id_update_atomic (&object->qdata,
4481
0
                                quark_toggle_refs,
4482
0
                                toggle_refs_ref_cb,
4483
0
                                &((ToggleRefCallbackData){
4484
0
                                    .object = object,
4485
0
                                    .tuple = {
4486
0
                                        .notify = notify,
4487
0
                                        .data = data,
4488
0
                                    },
4489
0
                                }));
4490
0
}
4491
4492
static gpointer
4493
toggle_refs_unref_cb (gpointer *data,
4494
                      GDestroyNotify *destroy_notify,
4495
                      gpointer user_data)
4496
0
{
4497
0
  ToggleRefCallbackData *trdata = user_data;
4498
0
  ToggleRefStack *tstack = *data;
4499
0
  gboolean found_one = FALSE;
4500
0
  guint i;
4501
4502
0
  if (tstack)
4503
0
    {
4504
0
      for (i = 0; i < tstack->n_toggle_refs; i++)
4505
0
        {
4506
0
          if (tstack->toggle_refs[i].notify == trdata->tuple.notify &&
4507
0
              (tstack->toggle_refs[i].data == trdata->tuple.data || trdata->tuple.data == NULL))
4508
0
            {
4509
0
              found_one = TRUE;
4510
0
              break;
4511
0
            }
4512
0
        }
4513
0
    }
4514
4515
0
  if (G_LIKELY (found_one))
4516
0
    {
4517
4518
0
      tstack->n_toggle_refs -= 1;
4519
0
      if (tstack->n_toggle_refs == 0)
4520
0
        {
4521
0
          g_datalist_unset_flags (&trdata->object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
4522
0
          g_free (tstack);
4523
0
          *data = NULL;
4524
0
          *destroy_notify = NULL;
4525
0
        }
4526
0
      else if (i != tstack->n_toggle_refs)
4527
0
        tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
4528
0
    }
4529
4530
0
  return GINT_TO_POINTER (found_one);
4531
0
}
4532
4533
/**
4534
 * g_object_remove_toggle_ref: (skip)
4535
 * @object: a #GObject
4536
 * @notify: a function to call when this reference is the
4537
 *  last reference to the object, or is no longer
4538
 *  the last reference.
4539
 * @data: (nullable): data to pass to @notify, or %NULL to
4540
 *  match any toggle refs with the @notify argument.
4541
 *
4542
 * Removes a reference added with g_object_add_toggle_ref(). The
4543
 * reference count of the object is decreased by one.
4544
 *
4545
 * Note that if you unref the object on another thread, then @notify might
4546
 * still be invoked after g_object_remove_toggle_ref(), and the object argument
4547
 * might be a dangling pointer. If the object is destroyed on other threads,
4548
 * you must take care of that yourself.
4549
 *
4550
 * Since: 2.8
4551
 */
4552
void
4553
g_object_remove_toggle_ref (GObject       *object,
4554
          GToggleNotify  notify,
4555
          gpointer       data)
4556
0
{
4557
0
  gboolean found_one;
4558
0
  gpointer result;
4559
4560
0
  g_return_if_fail (G_IS_OBJECT (object));
4561
0
  g_return_if_fail (notify != NULL);
4562
4563
0
  result = _g_datalist_id_update_atomic (&object->qdata,
4564
0
                                         quark_toggle_refs,
4565
0
                                         toggle_refs_unref_cb,
4566
0
                                         &((ToggleRefCallbackData){
4567
0
                                             .object = object,
4568
0
                                             .tuple = {
4569
0
                                                 .notify = notify,
4570
0
                                                 .data = data,
4571
0
                                             },
4572
0
                                         }));
4573
4574
0
  found_one = GPOINTER_TO_INT (result);
4575
4576
0
  if (!found_one)
4577
0
    {
4578
0
      g_critical ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
4579
0
      return;
4580
0
    }
4581
4582
0
  g_object_unref (object);
4583
0
}
4584
4585
/* Internal implementation of g_object_ref() which doesn't call out to user code.
4586
 * @out_toggle_notify and @out_toggle_data *must* be provided, and if non-`NULL`
4587
 * values are returned, then the caller *must* call that toggle notify function
4588
 * as soon as it is safe to do so. It may call (or be) user-provided code so should
4589
 * only be called once all locks are released. */
4590
static gpointer
4591
object_ref (GObject *object,
4592
            GToggleNotify *out_toggle_notify,
4593
            gpointer *out_toggle_data)
4594
2.89k
{
4595
2.89k
  GToggleNotify toggle_notify;
4596
2.89k
  gpointer toggle_data;
4597
2.89k
  gint old_ref;
4598
4599
2.89k
  old_ref = g_atomic_int_get (&object->ref_count);
4600
4601
2.89k
retry:
4602
2.89k
  toggle_notify = NULL;
4603
2.89k
  toggle_data = NULL;
4604
2.89k
  if (old_ref > 1 && old_ref < G_MAXINT)
4605
1.44k
    {
4606
      /* Fast-path. We have apparently more than 1 references already. No
4607
       * special handling for toggle references, just increment the ref count. */
4608
1.44k
      if (!g_atomic_int_compare_and_exchange_full ((int *) &object->ref_count,
4609
1.44k
                                                   old_ref, old_ref + 1, &old_ref))
4610
0
        goto retry;
4611
1.44k
    }
4612
1.44k
  else if (old_ref == 1)
4613
1.44k
    {
4614
      /* With ref count 1, check whether we need to emit a toggle notification. */
4615
1.44k
      if (!toggle_refs_check_and_ref_or_deref (object, TRUE, &old_ref, &toggle_notify, &toggle_data))
4616
0
        goto retry;
4617
1.44k
    }
4618
0
  else
4619
0
    {
4620
0
      gboolean object_already_finalized = TRUE;
4621
4622
0
      *out_toggle_notify = NULL;
4623
0
      *out_toggle_data = NULL;
4624
0
      g_return_val_if_fail (!object_already_finalized, NULL);
4625
0
      return NULL;
4626
0
    }
4627
4628
2.89k
  TRACE (GOBJECT_OBJECT_REF (object, G_TYPE_FROM_INSTANCE (object), old_ref));
4629
4630
2.89k
  *out_toggle_notify = toggle_notify;
4631
2.89k
  *out_toggle_data = toggle_data;
4632
2.89k
  return object;
4633
2.89k
}
4634
4635
/**
4636
 * g_object_ref:
4637
 * @object: (type GObject.Object): a #GObject
4638
 *
4639
 * Increases the reference count of @object.
4640
 *
4641
 * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type
4642
 * of @object will be propagated to the return type (using the GCC typeof()
4643
 * extension), so any casting the caller needs to do on the return type must be
4644
 * explicit.
4645
 *
4646
 * Returns: (type GObject.Object) (transfer none): the same @object
4647
 */
4648
gpointer
4649
(g_object_ref) (gpointer _object)
4650
2.89k
{
4651
2.89k
  GObject *object = _object;
4652
2.89k
  GToggleNotify toggle_notify;
4653
2.89k
  gpointer toggle_data;
4654
4655
2.89k
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4656
4657
2.89k
  object = object_ref (object, &toggle_notify, &toggle_data);
4658
4659
2.89k
  if (toggle_notify)
4660
0
    toggle_notify (toggle_data, object, FALSE);
4661
4662
2.89k
  return object;
4663
2.89k
}
4664
4665
static gboolean
4666
_object_unref_clear_weak_locations (GObject *object, gint *p_old_ref, gboolean do_unref)
4667
12.9k
{
4668
12.9k
  WeakRefData *wrdata;
4669
12.9k
  gboolean success;
4670
4671
  /* Fast path, for objects that never had a GWeakRef registered. */
4672
12.9k
  if (!(object_get_optional_flags (object) & OPTIONAL_FLAG_EVER_HAD_WEAK_REF))
4673
12.9k
    {
4674
      /* The caller previously just checked atomically that the ref-count was
4675
       * one.
4676
       *
4677
       * At this point still, @object never ever had a GWeakRef registered.
4678
       * That means, nobody else holds a strong reference and also nobody else
4679
       * can hold a weak reference, to race against obtaining another
4680
       * reference. We are good to proceed. */
4681
12.9k
      if (do_unref)
4682
6.48k
        {
4683
6.48k
          if (!g_atomic_int_compare_and_exchange ((gint *) &object->ref_count, 1, 0))
4684
0
            {
4685
0
#if G_ENABLE_DEBUG
4686
0
              g_assert_not_reached ();
4687
0
#endif
4688
0
            }
4689
6.48k
        }
4690
12.9k
      return TRUE;
4691
12.9k
    }
4692
4693
  /* Slow path. We must obtain a lock on the @wrdata, to atomically release
4694
   * weak references and check that the ref count is as expected. */
4695
4696
0
  wrdata = weak_ref_data_get_surely (object);
4697
4698
0
  weak_ref_data_lock (wrdata);
4699
4700
0
  if (do_unref)
4701
0
    {
4702
0
      success = g_atomic_int_compare_and_exchange_full ((gint *) &object->ref_count,
4703
0
                                                        1, 0,
4704
0
                                                        p_old_ref);
4705
0
    }
4706
0
  else
4707
0
    {
4708
0
      *p_old_ref = g_atomic_int_get ((gint *) &object->ref_count);
4709
0
      success = (*p_old_ref == 1);
4710
0
    }
4711
4712
0
  if (success)
4713
0
    weak_ref_data_clear_list (wrdata, object);
4714
4715
0
  weak_ref_data_unlock (wrdata);
4716
4717
0
  return success;
4718
12.9k
}
4719
4720
/**
4721
 * g_object_unref:
4722
 * @object: (type GObject.Object): a #GObject
4723
 *
4724
 * Decreases the reference count of @object. When its reference count
4725
 * drops to 0, the object is finalized (i.e. its memory is freed).
4726
 *
4727
 * If the pointer to the #GObject may be reused in future (for example, if it is
4728
 * an instance variable of another object), it is recommended to clear the
4729
 * pointer to %NULL rather than retain a dangling pointer to a potentially
4730
 * invalid #GObject instance. Use g_clear_object() for this.
4731
 */
4732
void
4733
g_object_unref (gpointer _object)
4734
9.38k
{
4735
9.38k
  GObject *object = _object;
4736
9.38k
  gint old_ref;
4737
9.38k
  GToggleNotify toggle_notify;
4738
9.38k
  gpointer toggle_data;
4739
9.38k
  gboolean nqueue_is_frozen;
4740
9.38k
  GType obj_gtype;
4741
4742
9.38k
  g_return_if_fail (G_IS_OBJECT (object));
4743
4744
  /* obj_gtype will be needed for TRACE(GOBJECT_OBJECT_UNREF()) later. Note
4745
   * that we issue the TRACE() after decrementing the ref-counter. If at that
4746
   * point the reference counter does not reach zero, somebody else can race
4747
   * and destroy the object.
4748
   *
4749
   * This means, TRACE() can be called with a dangling object pointer. This
4750
   * could only be avoided, by emitting the TRACE before doing the actual
4751
   * unref, but at that point we wouldn't know the correct "old_ref" value.
4752
   * Maybe this should change.
4753
   *
4754
   * Anyway. At that later point we can also no longer safely get the GType for
4755
   * the TRACE(). Do it now.
4756
   */
4757
9.38k
  obj_gtype = G_TYPE_FROM_INSTANCE (object);
4758
9.38k
  (void) obj_gtype;
4759
4760
9.38k
  old_ref = g_atomic_int_get (&object->ref_count);
4761
4762
9.38k
retry_beginning:
4763
4764
9.38k
  if (old_ref > 2)
4765
1.44k
    {
4766
      /* We have many references. If we can decrement the ref counter, we are done. */
4767
1.44k
      if (!g_atomic_int_compare_and_exchange_full ((int *) &object->ref_count,
4768
1.44k
                                                   old_ref, old_ref - 1, &old_ref))
4769
0
        goto retry_beginning;
4770
4771
      /* Beware: object might be a dangling pointer. */
4772
1.44k
      TRACE (GOBJECT_OBJECT_UNREF (object, obj_gtype, old_ref));
4773
1.44k
      return;
4774
1.44k
    }
4775
4776
7.93k
  if (old_ref == 2)
4777
1.44k
    {
4778
      /* We are about to return the second-to-last reference. In that case we
4779
       * might need to notify a toggle reference.
4780
       *
4781
       * Note that a g_object_add_toggle_ref() MUST always be released
4782
       * via g_object_remove_toggle_ref(). Thus, if we are here with
4783
       * an old_ref of 2, then at most one of the references can be
4784
       * a toggle reference.
4785
       *
4786
       * We need to take a lock, to avoid races. */
4787
4788
1.44k
      if (!toggle_refs_check_and_ref_or_deref (object, FALSE, &old_ref, &toggle_notify, &toggle_data))
4789
0
        goto retry_beginning;
4790
4791
      /* Beware: object might be a dangling pointer. */
4792
1.44k
      TRACE (GOBJECT_OBJECT_UNREF (object, obj_gtype, old_ref));
4793
1.44k
      if (toggle_notify)
4794
0
        toggle_notify (toggle_data, object, TRUE);
4795
1.44k
      return;
4796
1.44k
    }
4797
4798
6.48k
  if (G_UNLIKELY (old_ref != 1))
4799
0
    {
4800
0
      gboolean object_already_finalized = TRUE;
4801
4802
0
      g_return_if_fail (!object_already_finalized);
4803
0
      return;
4804
0
    }
4805
4806
  /* We only have one reference left. Proceed to (maybe) clear weak locations. */
4807
6.48k
  if (!_object_unref_clear_weak_locations (object, &old_ref, FALSE))
4808
0
    goto retry_beginning;
4809
4810
  /* At this point, we checked with an atomic read that we only hold only one
4811
   * reference. Weak locations are cleared (and toggle references are not to
4812
   * be considered in this case). Proceed with dispose().
4813
   *
4814
   * First, freeze the notification queue, so we don't accidentally emit
4815
   * notifications during dispose() and finalize().
4816
   *
4817
   * The notification queue stays frozen unless the instance acquires a
4818
   * reference during dispose(), in which case we thaw it and dispatch all the
4819
   * notifications. If the instance gets through to finalize(), the
4820
   * notification queue gets automatically drained when g_object_finalize() is
4821
   * reached and the qdata is cleared.
4822
   *
4823
   * Important: Note that g_object_notify_queue_freeze() takes an object lock.
4824
   * That happens to be the same lock that is also taken by
4825
   * toggle_refs_check_and_ref_or_deref(), that is very important. See also the
4826
   * code comment in toggle_refs_check_and_ref_or_deref().
4827
   */
4828
6.48k
  g_object_notify_queue_freeze (object, TRUE);
4829
6.48k
  nqueue_is_frozen = TRUE;
4830
4831
6.48k
  TRACE (GOBJECT_OBJECT_DISPOSE (object, G_TYPE_FROM_INSTANCE (object), 1));
4832
6.48k
  G_OBJECT_GET_CLASS (object)->dispose (object);
4833
6.48k
  TRACE (GOBJECT_OBJECT_DISPOSE_END (object, G_TYPE_FROM_INSTANCE (object), 1));
4834
4835
  /* Must re-fetch old-ref. _object_unref_clear_weak_locations() relies on
4836
   * that.  */
4837
6.48k
  old_ref = g_atomic_int_get (&object->ref_count);
4838
4839
6.48k
retry_decrement:
4840
  /* Here, old_ref is 1 if we just come from dispose(). If the object was resurrected,
4841
   * we can hit `goto retry_decrement` and be here with a larger old_ref. */
4842
4843
6.48k
  if (old_ref > 1 && nqueue_is_frozen)
4844
0
    {
4845
      /* If the object was resurrected, we need to unfreeze the notify
4846
       * queue. */
4847
0
      g_object_notify_queue_thaw (object, FALSE);
4848
0
      nqueue_is_frozen = FALSE;
4849
4850
      /* Note at this point, @old_ref might be wrong.
4851
       *
4852
       * Also note that _object_unref_clear_weak_locations() requires that we
4853
       * atomically checked that @old_ref is 1. However, as @old_ref is larger
4854
       * than 1, that will not be called. Instead, all other code paths below,
4855
       * handle the possibility of a bogus @old_ref.
4856
       *
4857
       * No need to re-fetch. */
4858
0
    }
4859
4860
6.48k
  if (old_ref > 2)
4861
0
    {
4862
0
      if (!g_atomic_int_compare_and_exchange_full ((int *) &object->ref_count,
4863
0
                                                   old_ref, old_ref - 1,
4864
0
                                                   &old_ref))
4865
0
        goto retry_decrement;
4866
4867
      /* Beware: object might be a dangling pointer. */
4868
0
      TRACE (GOBJECT_OBJECT_UNREF (object, obj_gtype, old_ref));
4869
0
      return;
4870
0
    }
4871
4872
6.48k
  if (old_ref == 2)
4873
0
    {
4874
      /* If the object was resurrected and the current ref-count is 2, then we
4875
       * are about to drop the ref-count to 1. We may need to emit a toggle
4876
       * notification. Take a lock and check for that.
4877
       *
4878
       * In that case, we need a lock to get the toggle notification. */
4879
0
      if (!toggle_refs_check_and_ref_or_deref (object, FALSE, &old_ref, &toggle_notify, &toggle_data))
4880
0
        goto retry_decrement;
4881
4882
      /* Beware: object might be a dangling pointer. */
4883
0
      TRACE (GOBJECT_OBJECT_UNREF (object, obj_gtype, old_ref));
4884
0
      if (toggle_notify)
4885
0
        toggle_notify (toggle_data, object, TRUE);
4886
0
      return;
4887
0
    }
4888
4889
  /* old_ref is (atomically!) checked to be 1, we are about to drop the
4890
   * reference count to zero in _object_unref_clear_weak_locations(). */
4891
6.48k
  if (!_object_unref_clear_weak_locations (object, &old_ref, TRUE))
4892
0
    goto retry_decrement;
4893
4894
6.48k
  TRACE (GOBJECT_OBJECT_UNREF (object, obj_gtype, old_ref));
4895
4896
  /* The object is almost gone. Finalize. */
4897
4898
6.48k
  closure_array_destroy_all (object);
4899
6.48k
  g_signal_handlers_destroy (object);
4900
6.48k
  g_object_weak_release_all (object, TRUE);
4901
4902
6.48k
  TRACE (GOBJECT_OBJECT_FINALIZE (object, G_TYPE_FROM_INSTANCE (object)));
4903
6.48k
  G_OBJECT_GET_CLASS (object)->finalize (object);
4904
6.48k
  TRACE (GOBJECT_OBJECT_FINALIZE_END (object, G_TYPE_FROM_INSTANCE (object)));
4905
4906
6.48k
  GOBJECT_IF_DEBUG (OBJECTS,
4907
6.48k
                    {
4908
6.48k
                      gboolean was_present;
4909
4910
                      /* catch objects not chaining finalize handlers */
4911
6.48k
                      G_LOCK (debug_objects);
4912
6.48k
                      was_present = g_hash_table_remove (debug_objects_ht, object);
4913
6.48k
                      G_UNLOCK (debug_objects);
4914
4915
6.48k
                      if (was_present)
4916
6.48k
                        g_critical ("Object %p of type %s not finalized correctly.",
4917
6.48k
                                    object, G_OBJECT_TYPE_NAME (object));
4918
6.48k
                    });
4919
6.48k
  g_type_free_instance ((GTypeInstance *) object);
4920
6.48k
}
4921
4922
/**
4923
 * g_clear_object: (skip)
4924
 * @object_ptr: a pointer to a #GObject reference
4925
 *
4926
 * Clears a reference to a #GObject.
4927
 *
4928
 * @object_ptr must not be %NULL.
4929
 *
4930
 * If the reference is %NULL then this function does nothing.
4931
 * Otherwise, the reference count of the object is decreased and the
4932
 * pointer is set to %NULL.
4933
 *
4934
 * A macro is also included that allows this function to be used without
4935
 * pointer casts.
4936
 *
4937
 * Since: 2.28
4938
 **/
4939
#undef g_clear_object
4940
void
4941
g_clear_object (GObject **object_ptr)
4942
1.44k
{
4943
1.44k
  g_clear_pointer (object_ptr, g_object_unref);
4944
1.44k
}
4945
4946
/**
4947
 * g_object_get_qdata:
4948
 * @object: The GObject to get a stored user data pointer from
4949
 * @quark: A #GQuark, naming the user data pointer
4950
 * 
4951
 * This function gets back user data pointers stored via
4952
 * g_object_set_qdata().
4953
 * 
4954
 * Returns: (transfer none) (nullable): The user data pointer set, or %NULL
4955
 */
4956
gpointer
4957
g_object_get_qdata (GObject *object,
4958
        GQuark   quark)
4959
0
{
4960
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4961
  
4962
0
  return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
4963
0
}
4964
4965
/**
4966
 * g_object_set_qdata: (skip)
4967
 * @object: The GObject to set store a user data pointer
4968
 * @quark: A #GQuark, naming the user data pointer
4969
 * @data: (nullable): An opaque user data pointer
4970
 *
4971
 * This sets an opaque, named pointer on an object.
4972
 * The name is specified through a #GQuark (retrieved e.g. via
4973
 * g_quark_from_static_string()), and the pointer
4974
 * can be gotten back from the @object with g_object_get_qdata()
4975
 * until the @object is finalized.
4976
 * Setting a previously set user data pointer, overrides (frees)
4977
 * the old pointer set, using #NULL as pointer essentially
4978
 * removes the data stored.
4979
 */
4980
void
4981
g_object_set_qdata (GObject *object,
4982
        GQuark   quark,
4983
        gpointer data)
4984
0
{
4985
0
  g_return_if_fail (G_IS_OBJECT (object));
4986
0
  g_return_if_fail (quark > 0);
4987
4988
0
  g_datalist_id_set_data (&object->qdata, quark, data);
4989
0
}
4990
4991
/**
4992
 * g_object_dup_qdata: (skip)
4993
 * @object: the #GObject to store user data on
4994
 * @quark: a #GQuark, naming the user data pointer
4995
 * @dup_func: (nullable): function to dup the value
4996
 * @user_data: (nullable): passed as user_data to @dup_func
4997
 *
4998
 * This is a variant of g_object_get_qdata() which returns
4999
 * a 'duplicate' of the value. @dup_func defines the
5000
 * meaning of 'duplicate' in this context, it could e.g.
5001
 * take a reference on a ref-counted object.
5002
 *
5003
 * If the @quark is not set on the object then @dup_func
5004
 * will be called with a %NULL argument.
5005
 *
5006
 * Note that @dup_func is called while user data of @object
5007
 * is locked.
5008
 *
5009
 * This function can be useful to avoid races when multiple
5010
 * threads are using object data on the same key on the same
5011
 * object.
5012
 *
5013
 * Returns: the result of calling @dup_func on the value
5014
 *     associated with @quark on @object, or %NULL if not set.
5015
 *     If @dup_func is %NULL, the value is returned
5016
 *     unmodified.
5017
 *
5018
 * Since: 2.34
5019
 */
5020
gpointer
5021
g_object_dup_qdata (GObject        *object,
5022
                    GQuark          quark,
5023
                    GDuplicateFunc   dup_func,
5024
                    gpointer         user_data)
5025
0
{
5026
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5027
0
  g_return_val_if_fail (quark > 0, NULL);
5028
5029
0
  return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
5030
0
}
5031
5032
/**
5033
 * g_object_replace_qdata: (skip)
5034
 * @object: the #GObject to store user data on
5035
 * @quark: a #GQuark, naming the user data pointer
5036
 * @oldval: (nullable): the old value to compare against
5037
 * @newval: (nullable): the new value
5038
 * @destroy: (nullable): a destroy notify for the new value
5039
 * @old_destroy: (out) (optional): destroy notify for the existing value
5040
 *
5041
 * Compares the user data for the key @quark on @object with
5042
 * @oldval, and if they are the same, replaces @oldval with
5043
 * @newval.
5044
 *
5045
 * This is like a typical atomic compare-and-exchange
5046
 * operation, for user data on an object.
5047
 *
5048
 * If the previous value was replaced then ownership of the
5049
 * old value (@oldval) is passed to the caller, including
5050
 * the registered destroy notify for it (passed out in @old_destroy).
5051
 * It’s up to the caller to free this as needed, which may
5052
 * or may not include using @old_destroy as sometimes replacement
5053
 * should not destroy the object in the normal way.
5054
 *
5055
 * Returns: %TRUE if the existing value for @quark was replaced
5056
 *  by @newval, %FALSE otherwise.
5057
 *
5058
 * Since: 2.34
5059
 */
5060
gboolean
5061
g_object_replace_qdata (GObject        *object,
5062
                        GQuark          quark,
5063
                        gpointer        oldval,
5064
                        gpointer        newval,
5065
                        GDestroyNotify  destroy,
5066
                        GDestroyNotify *old_destroy)
5067
0
{
5068
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
5069
0
  g_return_val_if_fail (quark > 0, FALSE);
5070
5071
0
  return g_datalist_id_replace_data (&object->qdata, quark,
5072
0
                                     oldval, newval, destroy,
5073
0
                                     old_destroy);
5074
0
}
5075
5076
/**
5077
 * g_object_set_qdata_full: (skip)
5078
 * @object: The GObject to set store a user data pointer
5079
 * @quark: A #GQuark, naming the user data pointer
5080
 * @data: (nullable): An opaque user data pointer
5081
 * @destroy: (nullable): Function to invoke with @data as argument, when @data
5082
 *           needs to be freed
5083
 *
5084
 * This function works like g_object_set_qdata(), but in addition,
5085
 * a void (*destroy) (gpointer) function may be specified which is
5086
 * called with @data as argument when the @object is finalized, or
5087
 * the data is being overwritten by a call to g_object_set_qdata()
5088
 * with the same @quark.
5089
 */
5090
void
5091
g_object_set_qdata_full (GObject       *object,
5092
       GQuark   quark,
5093
       gpointer data,
5094
       GDestroyNotify destroy)
5095
0
{
5096
0
  g_return_if_fail (G_IS_OBJECT (object));
5097
0
  g_return_if_fail (quark > 0);
5098
  
5099
0
  g_datalist_id_set_data_full (&object->qdata, quark, data,
5100
0
             data ? destroy : (GDestroyNotify) NULL);
5101
0
}
5102
5103
/**
5104
 * g_object_steal_qdata:
5105
 * @object: The GObject to get a stored user data pointer from
5106
 * @quark: A #GQuark, naming the user data pointer
5107
 *
5108
 * This function gets back user data pointers stored via
5109
 * g_object_set_qdata() and removes the @data from object
5110
 * without invoking its destroy() function (if any was
5111
 * set).
5112
 * Usually, calling this function is only required to update
5113
 * user data pointers with a destroy notifier, for example:
5114
 * |[<!-- language="C" --> 
5115
 * void
5116
 * object_add_to_user_list (GObject     *object,
5117
 *                          const gchar *new_string)
5118
 * {
5119
 *   // the quark, naming the object data
5120
 *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
5121
 *   // retrieve the old string list
5122
 *   GList *list = g_object_steal_qdata (object, quark_string_list);
5123
 *
5124
 *   // prepend new string
5125
 *   list = g_list_prepend (list, g_strdup (new_string));
5126
 *   // this changed 'list', so we need to set it again
5127
 *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
5128
 * }
5129
 * static void
5130
 * free_string_list (gpointer data)
5131
 * {
5132
 *   GList *node, *list = data;
5133
 *
5134
 *   for (node = list; node; node = node->next)
5135
 *     g_free (node->data);
5136
 *   g_list_free (list);
5137
 * }
5138
 * ]|
5139
 * Using g_object_get_qdata() in the above example, instead of
5140
 * g_object_steal_qdata() would have left the destroy function set,
5141
 * and thus the partial string list would have been freed upon
5142
 * g_object_set_qdata_full().
5143
 *
5144
 * Returns: (transfer full) (nullable): The user data pointer set, or %NULL
5145
 */
5146
gpointer
5147
g_object_steal_qdata (GObject *object,
5148
          GQuark   quark)
5149
0
{
5150
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5151
0
  g_return_val_if_fail (quark > 0, NULL);
5152
  
5153
0
  return g_datalist_id_remove_no_notify (&object->qdata, quark);
5154
0
}
5155
5156
/**
5157
 * g_object_get_data:
5158
 * @object: #GObject containing the associations
5159
 * @key: name of the key for that association
5160
 * 
5161
 * Gets a named field from the objects table of associations (see g_object_set_data()).
5162
 * 
5163
 * Returns: (transfer none) (nullable): the data if found,
5164
 *          or %NULL if no such data exists.
5165
 */
5166
gpointer
5167
g_object_get_data (GObject     *object,
5168
                   const gchar *key)
5169
0
{
5170
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5171
0
  g_return_val_if_fail (key != NULL, NULL);
5172
5173
0
  return g_datalist_get_data (&object->qdata, key);
5174
0
}
5175
5176
/**
5177
 * g_object_set_data:
5178
 * @object: #GObject containing the associations.
5179
 * @key: name of the key
5180
 * @data: (nullable): data to associate with that key
5181
 *
5182
 * Each object carries around a table of associations from
5183
 * strings to pointers.  This function lets you set an association.
5184
 *
5185
 * If the object already had an association with that name,
5186
 * the old association will be destroyed.
5187
 *
5188
 * Internally, the @key is converted to a #GQuark using g_quark_from_string().
5189
 * This means a copy of @key is kept permanently (even after @object has been
5190
 * finalized) — so it is recommended to only use a small, bounded set of values
5191
 * for @key in your program, to avoid the #GQuark storage growing unbounded.
5192
 */
5193
void
5194
g_object_set_data (GObject     *object,
5195
                   const gchar *key,
5196
                   gpointer     data)
5197
0
{
5198
0
  g_return_if_fail (G_IS_OBJECT (object));
5199
0
  g_return_if_fail (key != NULL);
5200
5201
0
  g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
5202
0
}
5203
5204
/**
5205
 * g_object_dup_data: (skip)
5206
 * @object: the #GObject to store user data on
5207
 * @key: a string, naming the user data pointer
5208
 * @dup_func: (nullable): function to dup the value
5209
 * @user_data: (nullable): passed as user_data to @dup_func
5210
 *
5211
 * This is a variant of g_object_get_data() which returns
5212
 * a 'duplicate' of the value. @dup_func defines the
5213
 * meaning of 'duplicate' in this context, it could e.g.
5214
 * take a reference on a ref-counted object.
5215
 *
5216
 * If the @key is not set on the object then @dup_func
5217
 * will be called with a %NULL argument.
5218
 *
5219
 * Note that @dup_func is called while user data of @object
5220
 * is locked.
5221
 *
5222
 * This function can be useful to avoid races when multiple
5223
 * threads are using object data on the same key on the same
5224
 * object.
5225
 *
5226
 * Returns: the result of calling @dup_func on the value
5227
 *     associated with @key on @object, or %NULL if not set.
5228
 *     If @dup_func is %NULL, the value is returned
5229
 *     unmodified.
5230
 *
5231
 * Since: 2.34
5232
 */
5233
gpointer
5234
g_object_dup_data (GObject        *object,
5235
                   const gchar    *key,
5236
                   GDuplicateFunc   dup_func,
5237
                   gpointer         user_data)
5238
0
{
5239
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5240
0
  g_return_val_if_fail (key != NULL, NULL);
5241
5242
0
  return g_datalist_id_dup_data (&object->qdata,
5243
0
                                 g_quark_from_string (key),
5244
0
                                 dup_func, user_data);
5245
0
}
5246
5247
/**
5248
 * g_object_replace_data: (skip)
5249
 * @object: the #GObject to store user data on
5250
 * @key: a string, naming the user data pointer
5251
 * @oldval: (nullable): the old value to compare against
5252
 * @newval: (nullable): the new value
5253
 * @destroy: (nullable): a destroy notify for the new value
5254
 * @old_destroy: (out) (optional): destroy notify for the existing value
5255
 *
5256
 * Compares the user data for the key @key on @object with
5257
 * @oldval, and if they are the same, replaces @oldval with
5258
 * @newval.
5259
 *
5260
 * This is like a typical atomic compare-and-exchange
5261
 * operation, for user data on an object.
5262
 *
5263
 * If the previous value was replaced then ownership of the
5264
 * old value (@oldval) is passed to the caller, including
5265
 * the registered destroy notify for it (passed out in @old_destroy).
5266
 * It’s up to the caller to free this as needed, which may
5267
 * or may not include using @old_destroy as sometimes replacement
5268
 * should not destroy the object in the normal way.
5269
 *
5270
 * See g_object_set_data() for guidance on using a small, bounded set of values
5271
 * for @key.
5272
 *
5273
 * Returns: %TRUE if the existing value for @key was replaced
5274
 *  by @newval, %FALSE otherwise.
5275
 *
5276
 * Since: 2.34
5277
 */
5278
gboolean
5279
g_object_replace_data (GObject        *object,
5280
                       const gchar    *key,
5281
                       gpointer        oldval,
5282
                       gpointer        newval,
5283
                       GDestroyNotify  destroy,
5284
                       GDestroyNotify *old_destroy)
5285
0
{
5286
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
5287
0
  g_return_val_if_fail (key != NULL, FALSE);
5288
5289
0
  return g_datalist_id_replace_data (&object->qdata,
5290
0
                                     g_quark_from_string (key),
5291
0
                                     oldval, newval, destroy,
5292
0
                                     old_destroy);
5293
0
}
5294
5295
/**
5296
 * g_object_set_data_full: (skip)
5297
 * @object: #GObject containing the associations
5298
 * @key: name of the key
5299
 * @data: (nullable): data to associate with that key
5300
 * @destroy: (nullable): function to call when the association is destroyed
5301
 *
5302
 * Like g_object_set_data() except it adds notification
5303
 * for when the association is destroyed, either by setting it
5304
 * to a different value or when the object is destroyed.
5305
 *
5306
 * Note that the @destroy callback is not called if @data is %NULL.
5307
 */
5308
void
5309
g_object_set_data_full (GObject       *object,
5310
                        const gchar   *key,
5311
                        gpointer       data,
5312
                        GDestroyNotify destroy)
5313
0
{
5314
0
  g_return_if_fail (G_IS_OBJECT (object));
5315
0
  g_return_if_fail (key != NULL);
5316
5317
0
  g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
5318
0
             data ? destroy : (GDestroyNotify) NULL);
5319
0
}
5320
5321
/**
5322
 * g_object_steal_data:
5323
 * @object: #GObject containing the associations
5324
 * @key: name of the key
5325
 *
5326
 * Remove a specified datum from the object's data associations,
5327
 * without invoking the association's destroy handler.
5328
 *
5329
 * Returns: (transfer full) (nullable): the data if found, or %NULL
5330
 *          if no such data exists.
5331
 */
5332
gpointer
5333
g_object_steal_data (GObject     *object,
5334
                     const gchar *key)
5335
0
{
5336
0
  GQuark quark;
5337
5338
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5339
0
  g_return_val_if_fail (key != NULL, NULL);
5340
5341
0
  quark = g_quark_try_string (key);
5342
5343
0
  return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
5344
0
}
5345
5346
static void
5347
g_value_object_init (GValue *value)
5348
0
{
5349
0
  value->data[0].v_pointer = NULL;
5350
0
}
5351
5352
static void
5353
g_value_object_free_value (GValue *value)
5354
1.44k
{
5355
1.44k
  g_clear_object ((GObject**) &value->data[0].v_pointer);
5356
1.44k
}
5357
5358
static void
5359
g_value_object_copy_value (const GValue *src_value,
5360
         GValue *dest_value)
5361
0
{
5362
0
  g_set_object ((GObject**) &dest_value->data[0].v_pointer,
5363
0
                src_value->data[0].v_pointer);
5364
0
}
5365
5366
static void
5367
g_value_object_transform_value (const GValue *src_value,
5368
        GValue       *dest_value)
5369
0
{
5370
0
  if (src_value->data[0].v_pointer && g_type_is_a (G_OBJECT_TYPE (src_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
5371
0
    dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
5372
0
  else
5373
0
    dest_value->data[0].v_pointer = NULL;
5374
0
}
5375
5376
static gpointer
5377
g_value_object_peek_pointer (const GValue *value)
5378
0
{
5379
0
  return value->data[0].v_pointer;
5380
0
}
5381
5382
static gchar*
5383
g_value_object_collect_value (GValue    *value,
5384
            guint        n_collect_values,
5385
            GTypeCValue *collect_values,
5386
            guint        collect_flags)
5387
1.44k
{
5388
1.44k
  if (collect_values[0].v_pointer)
5389
1.44k
    {
5390
1.44k
      GObject *object = collect_values[0].v_pointer;
5391
      
5392
1.44k
      if (object->g_type_instance.g_class == NULL)
5393
0
  return g_strconcat ("invalid unclassed object pointer for value type '",
5394
0
          G_VALUE_TYPE_NAME (value),
5395
0
          "'",
5396
0
          NULL);
5397
1.44k
      else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
5398
0
  return g_strconcat ("invalid object type '",
5399
0
          G_OBJECT_TYPE_NAME (object),
5400
0
          "' for value type '",
5401
0
          G_VALUE_TYPE_NAME (value),
5402
0
          "'",
5403
0
          NULL);
5404
      /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
5405
1.44k
      value->data[0].v_pointer = g_object_ref (object);
5406
1.44k
    }
5407
0
  else
5408
0
    value->data[0].v_pointer = NULL;
5409
  
5410
1.44k
  return NULL;
5411
1.44k
}
5412
5413
static gchar*
5414
g_value_object_lcopy_value (const GValue *value,
5415
          guint        n_collect_values,
5416
          GTypeCValue *collect_values,
5417
          guint        collect_flags)
5418
0
{
5419
0
  GObject **object_p = collect_values[0].v_pointer;
5420
5421
0
  g_return_val_if_fail (object_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
5422
5423
0
  if (!value->data[0].v_pointer)
5424
0
    *object_p = NULL;
5425
0
  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
5426
0
    *object_p = value->data[0].v_pointer;
5427
0
  else
5428
0
    *object_p = g_object_ref (value->data[0].v_pointer);
5429
  
5430
0
  return NULL;
5431
0
}
5432
5433
/**
5434
 * g_value_set_object:
5435
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
5436
 * @v_object: (type GObject.Object) (nullable): object value to be set
5437
 *
5438
 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
5439
 *
5440
 * g_value_set_object() increases the reference count of @v_object
5441
 * (the #GValue holds a reference to @v_object).  If you do not wish
5442
 * to increase the reference count of the object (i.e. you wish to
5443
 * pass your current reference to the #GValue because you no longer
5444
 * need it), use g_value_take_object() instead.
5445
 *
5446
 * It is important that your #GValue holds a reference to @v_object (either its
5447
 * own, or one it has taken) to ensure that the object won't be destroyed while
5448
 * the #GValue still exists).
5449
 */
5450
void
5451
g_value_set_object (GValue   *value,
5452
        gpointer  v_object)
5453
0
{
5454
0
  GObject *old;
5455
5456
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
5457
5458
0
  if G_UNLIKELY (value->data[0].v_pointer == v_object)
5459
0
    return;
5460
5461
0
  old = g_steal_pointer (&value->data[0].v_pointer);
5462
5463
0
  if (v_object)
5464
0
    {
5465
0
      g_return_if_fail (G_IS_OBJECT (v_object));
5466
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
5467
5468
0
      value->data[0].v_pointer = g_object_ref (v_object);
5469
0
    }
5470
5471
0
  g_clear_object (&old);
5472
0
}
5473
5474
/**
5475
 * g_value_set_object_take_ownership: (skip)
5476
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
5477
 * @v_object: (nullable): object value to be set
5478
 *
5479
 * This is an internal function introduced mainly for C marshallers.
5480
 *
5481
 * Deprecated: 2.4: Use g_value_take_object() instead.
5482
 */
5483
void
5484
g_value_set_object_take_ownership (GValue  *value,
5485
           gpointer v_object)
5486
0
{
5487
0
  g_value_take_object (value, v_object);
5488
0
}
5489
5490
/**
5491
 * g_value_take_object: (skip)
5492
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
5493
 * @v_object: (nullable): object value to be set
5494
 *
5495
 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
5496
 * and takes over the ownership of the caller’s reference to @v_object;
5497
 * the caller doesn’t have to unref it any more (i.e. the reference
5498
 * count of the object is not increased).
5499
 *
5500
 * If you want the #GValue to hold its own reference to @v_object, use
5501
 * g_value_set_object() instead.
5502
 *
5503
 * Since: 2.4
5504
 */
5505
void
5506
g_value_take_object (GValue  *value,
5507
         gpointer v_object)
5508
0
{
5509
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
5510
5511
0
  g_clear_object ((GObject **) &value->data[0].v_pointer);
5512
5513
0
  if (v_object)
5514
0
    {
5515
0
      g_return_if_fail (G_IS_OBJECT (v_object));
5516
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
5517
5518
0
      value->data[0].v_pointer = g_steal_pointer (&v_object);
5519
0
    }
5520
0
}
5521
5522
/**
5523
 * g_value_get_object:
5524
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
5525
 * 
5526
 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
5527
 * 
5528
 * Returns: (type GObject.Object) (transfer none) (nullable): object contents of @value
5529
 */
5530
gpointer
5531
g_value_get_object (const GValue *value)
5532
240
{
5533
240
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
5534
  
5535
240
  return value->data[0].v_pointer;
5536
240
}
5537
5538
/**
5539
 * g_value_dup_object:
5540
 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
5541
 *
5542
 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
5543
 * its reference count. If the contents of the #GValue are %NULL, then
5544
 * %NULL will be returned.
5545
 *
5546
 * Returns: (type GObject.Object) (transfer full) (nullable): object content of @value,
5547
 *          should be unreferenced when no longer needed.
5548
 */
5549
gpointer
5550
g_value_dup_object (const GValue *value)
5551
1.20k
{
5552
1.20k
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
5553
  
5554
1.20k
  return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
5555
1.20k
}
5556
5557
/**
5558
 * g_signal_connect_object: (skip)
5559
 * @instance: (type GObject.TypeInstance): the instance to connect to.
5560
 * @detailed_signal: a string of the form "signal-name::detail".
5561
 * @c_handler: the #GCallback to connect.
5562
 * @gobject: (type GObject.Object) (nullable): the object to pass as data
5563
 *    to @c_handler.
5564
 * @connect_flags: a combination of #GConnectFlags.
5565
 *
5566
 * This is similar to g_signal_connect_data(), but uses a closure which
5567
 * ensures that the @gobject stays alive during the call to @c_handler
5568
 * by temporarily adding a reference count to @gobject.
5569
 *
5570
 * When the @gobject is destroyed the signal handler will be automatically
5571
 * disconnected.  Note that this is not currently threadsafe (ie:
5572
 * emitting a signal while @gobject is being destroyed in another thread
5573
 * is not safe).
5574
 *
5575
 * This function cannot fail. If the given signal name doesn’t exist,
5576
 * a critical warning is emitted. No validation is performed on the
5577
 * "detail" string when specified in @detailed_signal, other than a
5578
 * non-empty check.
5579
 *
5580
 * Refer to the [signals documentation](signals.html) for more
5581
 * details.
5582
 *
5583
 * Returns: the handler id.
5584
 */
5585
gulong
5586
g_signal_connect_object (gpointer      instance,
5587
       const gchar  *detailed_signal,
5588
       GCallback     c_handler,
5589
       gpointer      gobject,
5590
       GConnectFlags connect_flags)
5591
0
{
5592
0
  g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
5593
0
  g_return_val_if_fail (detailed_signal != NULL, 0);
5594
0
  g_return_val_if_fail (c_handler != NULL, 0);
5595
5596
0
  if (gobject)
5597
0
    {
5598
0
      GClosure *closure;
5599
5600
0
      g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
5601
5602
0
      closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
5603
5604
0
      return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
5605
0
    }
5606
0
  else
5607
0
    return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
5608
0
}
5609
5610
typedef struct {
5611
  GObject  *object;
5612
  guint     n_closures;
5613
  GClosure *closures[1]; /* flexible array */
5614
} CArray;
5615
5616
static gpointer
5617
object_remove_closure_cb (gpointer *data,
5618
                          GDestroyNotify *destroy_notify,
5619
                          gpointer user_data)
5620
0
{
5621
0
  GClosure *closure = user_data;
5622
0
  CArray *carray = *data;
5623
0
  guint i;
5624
5625
0
  for (i = 0; i < carray->n_closures; i++)
5626
0
    {
5627
0
      if (carray->closures[i] == closure)
5628
0
        {
5629
0
          carray->n_closures--;
5630
0
          if (carray->n_closures == 0)
5631
0
            {
5632
0
              g_free (carray);
5633
0
              *data = NULL;
5634
0
            }
5635
0
          else if (i < carray->n_closures)
5636
0
            carray->closures[i] = carray->closures[carray->n_closures];
5637
0
          return NULL;
5638
0
        }
5639
0
    }
5640
5641
0
  g_return_val_if_reached (NULL);
5642
0
}
5643
5644
static void
5645
object_remove_closure (gpointer data,
5646
                       GClosure *closure)
5647
0
{
5648
0
  GObject *object = data;
5649
5650
0
  _g_datalist_id_update_atomic (&object->qdata,
5651
0
                                quark_closure_array,
5652
0
                                object_remove_closure_cb,
5653
0
                                closure);
5654
0
}
5655
5656
static gpointer
5657
closure_array_destroy_all_cb (gpointer *data,
5658
                              GDestroyNotify *destroy_notify,
5659
                              gpointer user_data)
5660
12.9k
{
5661
12.9k
  CArray *carray = *data;
5662
12.9k
  GClosure *closure;
5663
5664
12.9k
  if (!carray)
5665
12.9k
    return NULL;
5666
5667
0
  closure = carray->closures[--carray->n_closures];
5668
5669
0
  if (carray->n_closures == 0)
5670
0
    {
5671
0
      g_free (carray);
5672
0
      *data = NULL;
5673
0
    }
5674
5675
0
  return closure;
5676
12.9k
}
5677
5678
static void
5679
closure_array_destroy_all (GObject *object)
5680
12.9k
{
5681
12.9k
  GClosure *closure;
5682
5683
  /* We invalidate closures in a loop. As this emits external callbacks, a callee
5684
   * could register another closure, which the loop would invalidate too.
5685
   *
5686
   * This is an intentional choice. Maybe it would be instead better to only
5687
   * only release the closures that were registered when the loop started. That
5688
   * would be possible, but is not done that way. */
5689
12.9k
  while ((closure = _g_datalist_id_update_atomic (&object->qdata,
5690
12.9k
                                                  quark_closure_array,
5691
12.9k
                                                  closure_array_destroy_all_cb,
5692
12.9k
                                                  NULL)))
5693
0
    {
5694
0
      g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
5695
0
      g_closure_invalidate (closure);
5696
0
    }
5697
12.9k
}
5698
5699
static gpointer
5700
g_object_watch_closure_cb (gpointer *data,
5701
                           GDestroyNotify *destroy_notify,
5702
                           gpointer user_data)
5703
0
{
5704
0
  GObject *object = ((gpointer *) user_data)[0];
5705
0
  GClosure *closure = ((gpointer *) user_data)[1];
5706
0
  CArray *carray = *data;
5707
0
  guint i;
5708
5709
0
  if (!carray)
5710
0
    {
5711
0
      carray = g_new (CArray, 1);
5712
0
      carray->object = object;
5713
0
      carray->n_closures = 1;
5714
0
      i = 0;
5715
5716
0
#if G_ENABLE_DEBUG
5717
      /* We never expect there is anything to destroy. We require
5718
       * these entries to be released via closure_array_destroy_all(). */
5719
0
      *destroy_notify = g_destroy_notify_assert_not_reached;
5720
0
#endif
5721
0
    }
5722
0
  else
5723
0
    {
5724
0
      i = carray->n_closures++;
5725
0
      carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
5726
0
    }
5727
5728
0
  *data = carray;
5729
5730
0
  carray->closures[i] = closure;
5731
5732
0
  return NULL;
5733
0
}
5734
5735
/**
5736
 * g_object_watch_closure:
5737
 * @object: #GObject restricting lifetime of @closure
5738
 * @closure: #GClosure to watch
5739
 *
5740
 * This function essentially limits the life time of the @closure to
5741
 * the life time of the object. That is, when the object is finalized,
5742
 * the @closure is invalidated by calling g_closure_invalidate() on
5743
 * it, in order to prevent invocations of the closure with a finalized
5744
 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
5745
 * added as marshal guards to the @closure, to ensure that an extra
5746
 * reference count is held on @object during invocation of the
5747
 * @closure.  Usually, this function will be called on closures that
5748
 * use this @object as closure data.
5749
 */
5750
void
5751
g_object_watch_closure (GObject  *object,
5752
      GClosure *closure)
5753
0
{
5754
0
  g_return_if_fail (G_IS_OBJECT (object));
5755
0
  g_return_if_fail (closure != NULL);
5756
0
  g_return_if_fail (closure->is_invalid == FALSE);
5757
0
  g_return_if_fail (closure->in_marshal == FALSE);
5758
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0); /* this doesn't work on finalizing objects */
5759
5760
0
  g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
5761
0
  g_closure_add_marshal_guards (closure,
5762
0
                                object, (GClosureNotify) g_object_ref,
5763
0
                                object, (GClosureNotify) g_object_unref);
5764
5765
0
  _g_datalist_id_update_atomic (&object->qdata,
5766
0
                                quark_closure_array,
5767
0
                                g_object_watch_closure_cb,
5768
0
                                ((gpointer[]){ object, closure }));
5769
0
}
5770
5771
/**
5772
 * g_closure_new_object:
5773
 * @sizeof_closure: the size of the structure to allocate, must be at least
5774
 *  `sizeof (GClosure)`
5775
 * @object: a #GObject pointer to store in the @data field of the newly
5776
 *  allocated #GClosure
5777
 *
5778
 * A variant of g_closure_new_simple() which stores @object in the
5779
 * @data field of the closure and calls g_object_watch_closure() on
5780
 * @object and the created closure. This function is mainly useful
5781
 * when implementing new types of closures.
5782
 *
5783
 * Returns: (transfer floating): a newly allocated #GClosure
5784
 */
5785
GClosure *
5786
g_closure_new_object (guint    sizeof_closure,
5787
          GObject *object)
5788
0
{
5789
0
  GClosure *closure;
5790
5791
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5792
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
5793
5794
0
  closure = g_closure_new_simple (sizeof_closure, object);
5795
0
  g_object_watch_closure (object, closure);
5796
5797
0
  return closure;
5798
0
}
5799
5800
/**
5801
 * g_cclosure_new_object: (skip)
5802
 * @callback_func: the function to invoke
5803
 * @object: a #GObject pointer to pass to @callback_func
5804
 *
5805
 * A variant of g_cclosure_new() which uses @object as @user_data and
5806
 * calls g_object_watch_closure() on @object and the created
5807
 * closure. This function is useful when you have a callback closely
5808
 * associated with a #GObject, and want the callback to no longer run
5809
 * after the object is is freed.
5810
 *
5811
 * Returns: (transfer floating): a new #GCClosure
5812
 */
5813
GClosure *
5814
g_cclosure_new_object (GCallback callback_func,
5815
           GObject  *object)
5816
0
{
5817
0
  GClosure *closure;
5818
5819
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5820
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
5821
0
  g_return_val_if_fail (callback_func != NULL, NULL);
5822
5823
0
  closure = g_cclosure_new (callback_func, object, NULL);
5824
0
  g_object_watch_closure (object, closure);
5825
5826
0
  return closure;
5827
0
}
5828
5829
/**
5830
 * g_cclosure_new_object_swap: (skip)
5831
 * @callback_func: the function to invoke
5832
 * @object: a #GObject pointer to pass to @callback_func
5833
 *
5834
 * A variant of g_cclosure_new_swap() which uses @object as @user_data
5835
 * and calls g_object_watch_closure() on @object and the created
5836
 * closure. This function is useful when you have a callback closely
5837
 * associated with a #GObject, and want the callback to no longer run
5838
 * after the object is is freed.
5839
 *
5840
 * Returns: (transfer floating): a new #GCClosure
5841
 */
5842
GClosure *
5843
g_cclosure_new_object_swap (GCallback callback_func,
5844
          GObject  *object)
5845
0
{
5846
0
  GClosure *closure;
5847
5848
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
5849
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
5850
0
  g_return_val_if_fail (callback_func != NULL, NULL);
5851
5852
0
  closure = g_cclosure_new_swap (callback_func, object, NULL);
5853
0
  g_object_watch_closure (object, closure);
5854
5855
0
  return closure;
5856
0
}
5857
5858
gsize
5859
g_object_compat_control (gsize           what,
5860
                         gpointer        data)
5861
0
{
5862
0
  switch (what)
5863
0
    {
5864
0
      gpointer *pp;
5865
0
    case 1:     /* floating base type */
5866
0
      return (gsize) G_TYPE_INITIALLY_UNOWNED;
5867
0
    case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
5868
0
      floating_flag_handler = (guint(*)(GObject*,gint)) data;
5869
0
      return 1;
5870
0
    case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
5871
0
      pp = data;
5872
0
      *pp = floating_flag_handler;
5873
0
      return 1;
5874
0
    default:
5875
0
      return 0;
5876
0
    }
5877
0
}
5878
5879
G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
5880
5881
static void
5882
g_initially_unowned_init (GInitiallyUnowned *object)
5883
0
{
5884
0
  g_object_force_floating (object);
5885
0
}
5886
5887
static void
5888
g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
5889
0
{
5890
0
}
5891
5892
/**
5893
 * GWeakRef:
5894
 *
5895
 * A structure containing a weak reference to a #GObject.
5896
 *
5897
 * A `GWeakRef` can either be empty (i.e. point to %NULL), or point to an
5898
 * object for as long as at least one "strong" reference to that object
5899
 * exists. Before the object's #GObjectClass.dispose method is called,
5900
 * every #GWeakRef associated with becomes empty (i.e. points to %NULL).
5901
 *
5902
 * Like #GValue, #GWeakRef can be statically allocated, stack- or
5903
 * heap-allocated, or embedded in larger structures.
5904
 *
5905
 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
5906
 * reference is thread-safe: converting a weak pointer to a reference is
5907
 * atomic with respect to invalidation of weak pointers to destroyed
5908
 * objects.
5909
 *
5910
 * If the object's #GObjectClass.dispose method results in additional
5911
 * references to the object being held (‘re-referencing’), any #GWeakRefs taken
5912
 * before it was disposed will continue to point to %NULL.  Any #GWeakRefs taken
5913
 * during disposal and after re-referencing, or after disposal has returned due
5914
 * to the re-referencing, will continue to point to the object until its refcount
5915
 * goes back to zero, at which point they too will be invalidated.
5916
 *
5917
 * It is invalid to take a #GWeakRef on an object during #GObjectClass.dispose
5918
 * without first having or creating a strong reference to the object.
5919
 */
5920
5921
0
#define WEAK_REF_LOCK_BIT 0
5922
5923
static GObject *
5924
_weak_ref_clean_pointer (gpointer ptr)
5925
0
{
5926
  /* Drop the lockbit WEAK_REF_LOCK_BIT from @ptr (if set). */
5927
0
  return g_pointer_bit_lock_mask_ptr (ptr, WEAK_REF_LOCK_BIT, FALSE, 0, NULL);
5928
0
}
5929
5930
static void
5931
_weak_ref_lock (GWeakRef *weak_ref, GObject **out_object)
5932
0
{
5933
  /* Note that while holding a _weak_ref_lock() on the @weak_ref, we MUST not acquire a
5934
   * weak_ref_data_lock() on the @wrdata. The other way around! */
5935
5936
0
  if (out_object)
5937
0
    {
5938
0
      guintptr ptr;
5939
5940
0
      g_pointer_bit_lock_and_get (&weak_ref->priv.p, WEAK_REF_LOCK_BIT, &ptr);
5941
0
      *out_object = _weak_ref_clean_pointer ((gpointer) ptr);
5942
0
    }
5943
0
  else
5944
0
    g_pointer_bit_lock (&weak_ref->priv.p, WEAK_REF_LOCK_BIT);
5945
0
}
5946
5947
static void
5948
_weak_ref_unlock (GWeakRef *weak_ref)
5949
0
{
5950
0
  g_pointer_bit_unlock (&weak_ref->priv.p, WEAK_REF_LOCK_BIT);
5951
0
}
5952
5953
static void
5954
_weak_ref_unlock_and_set (GWeakRef *weak_ref, GObject *object)
5955
0
{
5956
0
  g_pointer_bit_unlock_and_set (&weak_ref->priv.p, WEAK_REF_LOCK_BIT, object, 0);
5957
0
}
5958
5959
static void
5960
weak_ref_data_clear_list (WeakRefData *wrdata, GObject *object)
5961
0
{
5962
0
  while (wrdata->len > 0u)
5963
0
    {
5964
0
      GWeakRef *weak_ref;
5965
0
      gpointer ptr;
5966
5967
      /* pass "allow_shrink=FALSE", so we don't reallocate needlessly. We
5968
       * anyway are about to clear the entire list. */
5969
0
      weak_ref = weak_ref_data_list_remove (wrdata, wrdata->len - 1u, FALSE);
5970
5971
      /* Fast-path. Most likely @weak_ref is currently not locked, so we can
5972
       * just atomically set the pointer to NULL. */
5973
0
      ptr = g_atomic_pointer_get (&weak_ref->priv.p);
5974
0
#if G_ENABLE_DEBUG
5975
0
      g_assert (G_IS_OBJECT (_weak_ref_clean_pointer (ptr)));
5976
0
      g_assert (!object || object == _weak_ref_clean_pointer (ptr));
5977
0
#endif
5978
0
      if (G_LIKELY (ptr == _weak_ref_clean_pointer (ptr)))
5979
0
        {
5980
          /* The pointer is unlocked. Try an atomic compare-and-exchange... */
5981
0
          if (g_atomic_pointer_compare_and_exchange (&weak_ref->priv.p, ptr, NULL))
5982
0
            {
5983
              /* Done. Go to the next. */
5984
0
              continue;
5985
0
            }
5986
0
        }
5987
5988
      /* The @weak_ref is locked. Acquire the lock to set the pointer to NULL. */
5989
0
      _weak_ref_lock (weak_ref, NULL);
5990
0
      _weak_ref_unlock_and_set (weak_ref, NULL);
5991
0
    }
5992
0
}
5993
5994
static void
5995
_weak_ref_set (GWeakRef *weak_ref,
5996
               GObject *new_object,
5997
               gboolean called_by_init)
5998
0
{
5999
0
  WeakRefData *old_wrdata;
6000
0
  WeakRefData *new_wrdata;
6001
0
  GObject *old_object;
6002
6003
0
  new_wrdata = weak_ref_data_get_or_create (new_object);
6004
6005
0
#if G_ENABLE_DEBUG
6006
0
  g_assert (!new_object || object_get_optional_flags (new_object) & OPTIONAL_FLAG_EVER_HAD_WEAK_REF);
6007
0
#endif
6008
6009
0
  if (called_by_init)
6010
0
    {
6011
      /* The caller is g_weak_ref_init(). We know that the weak_ref should be
6012
       * NULL. We thus set @old_wrdata to NULL without checking.
6013
       *
6014
       * Also important, the caller ensured that @new_object is not NULL. So we
6015
       * are expected to set @weak_ref from NULL to a non-NULL @new_object. */
6016
0
      old_wrdata = NULL;
6017
0
#if G_ENABLE_DEBUG
6018
0
      g_assert (new_object);
6019
0
#endif
6020
0
    }
6021
0
  else
6022
0
    {
6023
      /* We must get a wrdata object @old_wrdata for the current @old_object. */
6024
0
      _weak_ref_lock (weak_ref, &old_object);
6025
6026
0
      if (old_object == new_object)
6027
0
        {
6028
          /* Already set. We are done. */
6029
0
          _weak_ref_unlock (weak_ref);
6030
0
          return;
6031
0
        }
6032
6033
0
      old_wrdata = old_object
6034
0
                       ? weak_ref_data_ref (weak_ref_data_get (old_object))
6035
0
                       : NULL;
6036
0
      _weak_ref_unlock (weak_ref);
6037
0
    }
6038
6039
  /* We need a lock on @old_wrdata, @new_wrdata and @weak_ref. We need to take
6040
   * these locks in a certain order to avoid deadlock. We sort them by pointer
6041
   * value.
6042
   *
6043
   * Note that @old_wrdata or @new_wrdata may be NULL, which is handled
6044
   * correctly.
6045
   *
6046
   * Note that @old_wrdata and @new_wrdata are never identical at this point.
6047
   */
6048
0
  if (new_wrdata && old_wrdata && (((guintptr) (gpointer) old_wrdata) < ((guintptr) ((gpointer) new_wrdata))))
6049
0
    {
6050
0
      weak_ref_data_lock (old_wrdata);
6051
0
      weak_ref_data_lock (new_wrdata);
6052
0
    }
6053
0
  else
6054
0
    {
6055
0
      weak_ref_data_lock (new_wrdata);
6056
0
      weak_ref_data_lock (old_wrdata);
6057
0
    }
6058
0
  _weak_ref_lock (weak_ref, &old_object);
6059
6060
0
  if (!weak_ref_data_has (old_object, old_wrdata, NULL))
6061
0
    {
6062
      /* A race. @old_object no longer has the expected @old_wrdata after
6063
       * getting all the locks. */
6064
0
      if (old_object)
6065
0
        {
6066
          /* We lost the race and find a different object set. It's fine, our
6067
           * action was lost in the race and we are done. No need to retry. */
6068
0
          weak_ref_data_unlock (old_wrdata);
6069
0
          weak_ref_data_unlock (new_wrdata);
6070
0
          _weak_ref_unlock (weak_ref);
6071
0
          weak_ref_data_unref (old_wrdata);
6072
0
          return;
6073
0
        }
6074
6075
      /* @old_object is NULL after a race. We didn't expect that, but it's
6076
       * fine. Proceed to set @new_object... */
6077
0
    }
6078
6079
0
  if (old_object)
6080
0
    {
6081
0
      gint32 idx;
6082
6083
0
      idx = weak_ref_data_list_find (old_wrdata, weak_ref);
6084
0
      if (idx < 0)
6085
0
        g_critical ("unexpected missing GWeakRef data");
6086
0
      else
6087
0
        weak_ref_data_list_remove (old_wrdata, idx, TRUE);
6088
0
    }
6089
6090
0
  weak_ref_data_unlock (old_wrdata);
6091
6092
0
  if (new_object)
6093
0
    {
6094
0
#if G_ENABLE_DEBUG
6095
0
      g_assert (new_wrdata != NULL);
6096
0
      g_assert (weak_ref_data_list_find (new_wrdata, weak_ref) < 0);
6097
0
#endif
6098
0
      if (g_atomic_int_get (&new_object->ref_count) < 1)
6099
0
        {
6100
0
          g_critical ("calling g_weak_ref_set() with already destroyed object");
6101
0
          new_object = NULL;
6102
0
        }
6103
0
      else
6104
0
        {
6105
0
          if (!weak_ref_data_list_add (new_wrdata, weak_ref))
6106
0
            {
6107
0
              g_critical ("Too many GWeakRef registered");
6108
0
              new_object = NULL;
6109
0
            }
6110
0
        }
6111
0
    }
6112
6113
0
  _weak_ref_unlock_and_set (weak_ref, new_object);
6114
0
  weak_ref_data_unlock (new_wrdata);
6115
6116
0
  weak_ref_data_unref (old_wrdata);
6117
0
}
6118
6119
/**
6120
 * g_weak_ref_init: (skip)
6121
 * @weak_ref: uninitialized or empty location for a weak reference
6122
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
6123
 *
6124
 * Initialise a non-statically-allocated #GWeakRef.
6125
 *
6126
 * This function also calls g_weak_ref_set() with @object on the
6127
 * freshly-initialised weak reference.
6128
 *
6129
 * This function should always be matched with a call to
6130
 * g_weak_ref_clear().  It is not necessary to use this function for a
6131
 * #GWeakRef in static storage because it will already be
6132
 * properly initialised.  Just use g_weak_ref_set() directly.
6133
 *
6134
 * Since: 2.32
6135
 */
6136
void
6137
g_weak_ref_init (GWeakRef *weak_ref,
6138
                 gpointer object)
6139
0
{
6140
0
  g_return_if_fail (weak_ref);
6141
0
  g_return_if_fail (object == NULL || G_IS_OBJECT (object));
6142
6143
0
  g_atomic_pointer_set (&weak_ref->priv.p, NULL);
6144
0
  if (object)
6145
0
    {
6146
      /* We give a hint that the weak_ref is currently NULL. Unlike
6147
       * g_weak_ref_set(), we then don't need the extra lock just to
6148
       * find out that we have no object. */
6149
0
      _weak_ref_set (weak_ref, object, TRUE);
6150
0
    }
6151
0
}
6152
6153
/**
6154
 * g_weak_ref_clear: (skip)
6155
 * @weak_ref: location of a weak reference, which
6156
 *  may be empty
6157
 *
6158
 * Frees resources associated with a non-statically-allocated #GWeakRef.
6159
 * After this call, the #GWeakRef is left in an undefined state.
6160
 *
6161
 * You should only call this on a #GWeakRef that previously had
6162
 * g_weak_ref_init() called on it.
6163
 *
6164
 * Since: 2.32
6165
 */
6166
void
6167
g_weak_ref_clear (GWeakRef *weak_ref)
6168
0
{
6169
0
  g_weak_ref_set (weak_ref, NULL);
6170
6171
  /* be unkind */
6172
0
  weak_ref->priv.p = (void *) 0xccccccccu;
6173
0
}
6174
6175
/**
6176
 * g_weak_ref_get: (skip)
6177
 * @weak_ref: location of a weak reference to a #GObject
6178
 *
6179
 * If @weak_ref is not empty, atomically acquire a strong
6180
 * reference to the object it points to, and return that reference.
6181
 *
6182
 * This function is needed because of the potential race between taking
6183
 * the pointer value and g_object_ref() on it, if the object was losing
6184
 * its last reference at the same time in a different thread.
6185
 *
6186
 * The caller should release the resulting reference in the usual way,
6187
 * by using g_object_unref().
6188
 *
6189
 * Returns: (transfer full) (type GObject.Object): the object pointed to
6190
 *     by @weak_ref, or %NULL if it was empty
6191
 *
6192
 * Since: 2.32
6193
 */
6194
gpointer
6195
g_weak_ref_get (GWeakRef *weak_ref)
6196
0
{
6197
0
  WeakRefData *wrdata;
6198
0
  WeakRefData *new_wrdata;
6199
0
  GToggleNotify toggle_notify = NULL;
6200
0
  gpointer toggle_data = NULL;
6201
0
  GObject *object;
6202
6203
0
  g_return_val_if_fail (weak_ref, NULL);
6204
6205
  /* We cannot take the strong reference on @object yet. Otherwise,
6206
   * _object_unref_clear_weak_locations() might have just taken the lock on
6207
   * @wrdata, see that the ref-count is 1 and plan to proceed clearing weak
6208
   * locations. If we then take a strong reference here, the object becomes
6209
   * alive and well, but _object_unref_clear_weak_locations() would proceed and
6210
   * clear the @weak_ref.
6211
   *
6212
   * We avoid that, by can only taking the strong reference when having a lock
6213
   * on @wrdata, so we are in sync with _object_unref_clear_weak_locations().
6214
   *
6215
   * But first we must get a reference to the @wrdata.
6216
   */
6217
0
  _weak_ref_lock (weak_ref, &object);
6218
0
  wrdata = object
6219
0
               ? weak_ref_data_ref (weak_ref_data_get (object))
6220
0
               : NULL;
6221
0
  _weak_ref_unlock (weak_ref);
6222
6223
0
  if (!wrdata)
6224
0
    {
6225
      /* There is no @wrdata and no object. We are done. */
6226
0
      return NULL;
6227
0
    }
6228
6229
0
retry:
6230
6231
  /* Now proceed to get the strong reference. This time with acquiring a lock
6232
   * on the per-object @wrdata and on @weak_ref.
6233
   *
6234
   * As the order in which locks are taken is important, we previously had to
6235
   * get a _weak_ref_lock(), to obtain the @wrdata. Now we have to lock on the
6236
   * @wrdata first, and the @weak_ref again. */
6237
0
  weak_ref_data_lock (wrdata);
6238
0
  _weak_ref_lock (weak_ref, &object);
6239
6240
0
  if (!object)
6241
0
    {
6242
      /* Object is gone in the meantime. That is fine. */
6243
0
      new_wrdata = NULL;
6244
0
    }
6245
0
  else
6246
0
    {
6247
      /* Check that @object still refers to the same object as before. We do
6248
       * that by comparing the @wrdata object. A GObject keeps its (unique!)
6249
       * wrdata instance until the end, and since @wrdata is still alive,
6250
       * @object is the same as before, if-and-only-if its @wrdata is the same.
6251
       */
6252
0
      if (weak_ref_data_has (object, wrdata, &new_wrdata))
6253
0
        {
6254
          /* We are (still) good. Take a strong ref while holding the necessary locks. */
6255
0
          object = object_ref (object, &toggle_notify, &toggle_data);
6256
0
        }
6257
0
      else
6258
0
        {
6259
          /* The @object changed and has no longer the same @wrdata. In this
6260
           * case, we need to start over.
6261
           *
6262
           * Note that @new_wrdata references the wrdata of the now current
6263
           * @object. We will use that during the retry. */
6264
0
        }
6265
0
    }
6266
6267
0
  _weak_ref_unlock (weak_ref);
6268
0
  weak_ref_data_unlock (wrdata);
6269
0
  weak_ref_data_unref (wrdata);
6270
6271
0
  if (new_wrdata)
6272
0
    {
6273
      /* There was a race. The object changed. Retry, with @new_wrdata. */
6274
0
      wrdata = new_wrdata;
6275
0
      goto retry;
6276
0
    }
6277
6278
0
  if (toggle_notify)
6279
0
    toggle_notify (toggle_data, object, FALSE);
6280
6281
0
  return object;
6282
0
}
6283
6284
/**
6285
 * g_weak_ref_set: (skip)
6286
 * @weak_ref: location for a weak reference
6287
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
6288
 *
6289
 * Change the object to which @weak_ref points, or set it to
6290
 * %NULL.
6291
 *
6292
 * You must own a strong reference on @object while calling this
6293
 * function.
6294
 *
6295
 * Since: 2.32
6296
 */
6297
void
6298
g_weak_ref_set (GWeakRef *weak_ref,
6299
                gpointer object)
6300
0
{
6301
0
  g_return_if_fail (weak_ref != NULL);
6302
0
  g_return_if_fail (object == NULL || G_IS_OBJECT (object));
6303
6304
0
  _weak_ref_set (weak_ref, object, FALSE);
6305
0
}