Coverage Report

Created: 2025-07-01 07:09

/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
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General
15
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/*
19
 * MT safe with regards to reference counting.
20
 */
21
22
#include "config.h"
23
24
#include <string.h>
25
#include <signal.h>
26
27
#include "gobject.h"
28
#include "gtype-private.h"
29
#include "gvaluecollector.h"
30
#include "gsignal.h"
31
#include "gparamspecs.h"
32
#include "gvaluetypes.h"
33
#include "gobject_trace.h"
34
#include "gconstructor.h"
35
36
/**
37
 * SECTION:objects
38
 * @title: GObject
39
 * @short_description: The base object type
40
 * @see_also: #GParamSpecObject, g_param_spec_object()
41
 *
42
 * GObject is the fundamental type providing the common attributes and
43
 * methods for all object types in GTK+, Pango and other libraries
44
 * based on GObject.  The GObject class provides methods for object
45
 * construction and destruction, property access methods, and signal
46
 * support.  Signals are described in detail [here][gobject-Signals].
47
 *
48
 * For a tutorial on implementing a new GObject class, see [How to define and
49
 * implement a new GObject][howto-gobject]. For a list of naming conventions for
50
 * GObjects and their methods, see the [GType conventions][gtype-conventions].
51
 * For the high-level concepts behind GObject, read [Instantiatable classed types:
52
 * Objects][gtype-instantiatable-classed].
53
 *
54
 * ## Floating references # {#floating-ref}
55
 *
56
 * **Note**: Floating references are a C convenience API and should not be
57
 * used in modern GObject code. Language bindings in particular find the
58
 * concept highly problematic, as floating references are not identifiable
59
 * through annotations, and neither are deviations from the floating reference
60
 * behavior, like types that inherit from #GInitiallyUnowned and still return
61
 * a full reference from g_object_new().
62
 *
63
 * GInitiallyUnowned is derived from GObject. The only difference between
64
 * the two is that the initial reference of a GInitiallyUnowned is flagged
65
 * as a "floating" reference. This means that it is not specifically
66
 * claimed to be "owned" by any code portion. The main motivation for
67
 * providing floating references is C convenience. In particular, it
68
 * allows code to be written as:
69
 * |[<!-- language="C" --> 
70
 * container = create_container ();
71
 * container_add_child (container, create_child());
72
 * ]|
73
 * If container_add_child() calls g_object_ref_sink() on the passed-in child,
74
 * no reference of the newly created child is leaked. Without floating
75
 * references, container_add_child() can only g_object_ref() the new child,
76
 * so to implement this code without reference leaks, it would have to be
77
 * written as:
78
 * |[<!-- language="C" --> 
79
 * Child *child;
80
 * container = create_container ();
81
 * child = create_child ();
82
 * container_add_child (container, child);
83
 * g_object_unref (child);
84
 * ]|
85
 * The floating reference can be converted into an ordinary reference by
86
 * calling g_object_ref_sink(). For already sunken objects (objects that
87
 * don't have a floating reference anymore), g_object_ref_sink() is equivalent
88
 * to g_object_ref() and returns a new reference.
89
 *
90
 * Since floating references are useful almost exclusively for C convenience,
91
 * language bindings that provide automated reference and memory ownership
92
 * maintenance (such as smart pointers or garbage collection) should not
93
 * expose floating references in their API. The best practice for handling
94
 * types that have initially floating references is to immediately sink those
95
 * references after g_object_new() returns, by checking if the #GType
96
 * inherits from #GInitiallyUnowned. For instance:
97
 *
98
 * |[<!-- language="C" -->
99
 * GObject *res = g_object_new_with_properties (gtype,
100
 *                                              n_props,
101
 *                                              prop_names,
102
 *                                              prop_values);
103
 *
104
 * // or: if (g_type_is_a (gtype, G_TYPE_INITIALLY_UNOWNED))
105
 * if (G_IS_INITIALLY_UNOWNED (res))
106
 *   g_object_ref_sink (res);
107
 *
108
 * return res;
109
 * ]|
110
 *
111
 * Some object implementations may need to save an objects floating state
112
 * across certain code portions (an example is #GtkMenu), to achieve this,
113
 * the following sequence can be used:
114
 *
115
 * |[<!-- language="C" --> 
116
 * // save floating state
117
 * gboolean was_floating = g_object_is_floating (object);
118
 * g_object_ref_sink (object);
119
 * // protected code portion
120
 *
121
 * ...
122
 *
123
 * // restore floating state
124
 * if (was_floating)
125
 *   g_object_force_floating (object);
126
 * else
127
 *   g_object_unref (object); // release previously acquired reference
128
 * ]|
129
 */
130
131
132
/* --- macros --- */
133
9.88M
#define PARAM_SPEC_PARAM_ID(pspec)    ((pspec)->param_id)
134
36
#define PARAM_SPEC_SET_PARAM_ID(pspec, id)  ((pspec)->param_id = (id))
135
136
10.0M
#define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
137
#define OBJECT_HAS_TOGGLE_REF(object) \
138
10.0M
    ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
139
0
#define OBJECT_FLOATING_FLAG 0x2
140
141
28.0M
#define CLASS_HAS_PROPS_FLAG 0x1
142
#define CLASS_HAS_PROPS(class) \
143
28.0M
    ((class)->flags & CLASS_HAS_PROPS_FLAG)
144
#define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
145
14.0M
    ((class)->constructor != g_object_constructor)
146
#define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
147
14.0M
    ((class)->constructed != g_object_constructed)
148
149
528
#define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
150
#define CLASS_HAS_DERIVED_CLASS(class) \
151
36
    ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
152
153
/* --- signals --- */
154
enum {
155
  NOTIFY,
156
  LAST_SIGNAL
157
};
158
159
160
/* --- properties --- */
161
enum {
162
  PROP_NONE
163
};
164
165
14.0M
#define OPTIONAL_FLAG_IN_CONSTRUCTION 1<<0
166
9.88M
#define OPTIONAL_FLAG_HAS_SIGNAL_HANDLER 1<<1 /* Set if object ever had a signal handler */
167
168
#if SIZEOF_INT == 4 && GLIB_SIZEOF_VOID_P == 8
169
#define HAVE_OPTIONAL_FLAGS
170
#endif
171
172
typedef struct
173
{
174
  GTypeInstance  g_type_instance;
175
176
  /*< private >*/
177
  guint          ref_count;  /* (atomic) */
178
#ifdef HAVE_OPTIONAL_FLAGS
179
  guint          optional_flags;  /* (atomic) */
180
#endif
181
  GData         *qdata;
182
} GObjectReal;
183
184
G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal));
185
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count));
186
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, qdata) == G_STRUCT_OFFSET(GObjectReal, qdata));
187
188
189
/* --- prototypes --- */
190
static void g_object_base_class_init    (GObjectClass *class);
191
static void g_object_base_class_finalize    (GObjectClass *class);
192
static void g_object_do_class_init      (GObjectClass *class);
193
static void g_object_init       (GObject  *object,
194
               GObjectClass *class);
195
static GObject* g_object_constructor      (GType                  type,
196
               guint                  n_construct_properties,
197
               GObjectConstructParam *construct_params);
198
static void     g_object_constructed                    (GObject        *object);
199
static void g_object_real_dispose     (GObject  *object);
200
static void g_object_finalize     (GObject  *object);
201
static void g_object_do_set_property    (GObject        *object,
202
               guint           property_id,
203
               const GValue   *value,
204
               GParamSpec     *pspec);
205
static void g_object_do_get_property    (GObject        *object,
206
               guint           property_id,
207
               GValue         *value,
208
               GParamSpec     *pspec);
209
static void g_value_object_init     (GValue   *value);
210
static void g_value_object_free_value   (GValue   *value);
211
static void g_value_object_copy_value   (const GValue *src_value,
212
               GValue   *dest_value);
213
static void g_value_object_transform_value    (const GValue *src_value,
214
               GValue   *dest_value);
215
static gpointer g_value_object_peek_pointer             (const GValue   *value);
216
static gchar* g_value_object_collect_value    (GValue   *value,
217
               guint           n_collect_values,
218
               GTypeCValue    *collect_values,
219
               guint           collect_flags);
220
static gchar* g_value_object_lcopy_value    (const GValue *value,
221
               guint           n_collect_values,
222
               GTypeCValue    *collect_values,
223
               guint           collect_flags);
224
static void g_object_dispatch_properties_changed  (GObject  *object,
225
               guint     n_pspecs,
226
               GParamSpec    **pspecs);
227
static guint               object_floating_flag_handler (GObject        *object,
228
                                                         gint            job);
229
230
static void object_interface_check_properties           (gpointer        check_data,
231
               gpointer        g_iface);
232
233
/* --- typedefs --- */
234
typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
235
236
struct _GObjectNotifyQueue
237
{
238
  GSList  *pspecs;
239
  guint16  n_pspecs;
240
  guint16  freeze_count;
241
};
242
243
/* --- variables --- */
244
G_LOCK_DEFINE_STATIC (closure_array_mutex);
245
G_LOCK_DEFINE_STATIC (weak_refs_mutex);
246
G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
247
static GQuark             quark_closure_array = 0;
248
static GQuark             quark_weak_refs = 0;
249
static GQuark             quark_toggle_refs = 0;
250
static GQuark               quark_notify_queue;
251
static GQuark               quark_in_construction;
252
static GParamSpecPool      *pspec_pool = NULL;
253
static gulong             gobject_signals[LAST_SIGNAL] = { 0, };
254
static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
255
/* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
256
static GQuark             quark_weak_locations = 0;
257
static GRWLock              weak_locations_lock;
258
259
G_LOCK_DEFINE_STATIC(notify_lock);
260
261
/* --- functions --- */
262
static void
263
g_object_notify_queue_free (gpointer data)
264
9.88M
{
265
9.88M
  GObjectNotifyQueue *nqueue = data;
266
267
9.88M
  g_slist_free (nqueue->pspecs);
268
9.88M
  g_slice_free (GObjectNotifyQueue, nqueue);
269
9.88M
}
270
271
static GObjectNotifyQueue*
272
g_object_notify_queue_freeze (GObject  *object,
273
                              gboolean  conditional)
274
9.88M
{
275
9.88M
  GObjectNotifyQueue *nqueue;
276
277
9.88M
  G_LOCK(notify_lock);
278
9.88M
  nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
279
9.88M
  if (!nqueue)
280
9.88M
    {
281
9.88M
      if (conditional)
282
0
        {
283
0
          G_UNLOCK(notify_lock);
284
0
          return NULL;
285
0
        }
286
287
9.88M
      nqueue = g_slice_new0 (GObjectNotifyQueue);
288
9.88M
      g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
289
9.88M
                                   nqueue, g_object_notify_queue_free);
290
9.88M
    }
291
292
9.88M
  if (nqueue->freeze_count >= 65535)
293
0
    g_critical("Free queue for %s (%p) is larger than 65535,"
294
9.88M
               " called g_object_freeze_notify() too often."
295
9.88M
               " Forgot to call g_object_thaw_notify() or infinite loop",
296
9.88M
               G_OBJECT_TYPE_NAME (object), object);
297
9.88M
  else
298
9.88M
    nqueue->freeze_count++;
299
9.88M
  G_UNLOCK(notify_lock);
300
301
9.88M
  return nqueue;
302
9.88M
}
303
304
static void
305
g_object_notify_queue_thaw (GObject            *object,
306
                            GObjectNotifyQueue *nqueue)
307
9.88M
{
308
9.88M
  GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
309
9.88M
  GSList *slist;
310
9.88M
  guint n_pspecs = 0;
311
312
9.88M
  g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
313
314
9.88M
  G_LOCK(notify_lock);
315
316
  /* Just make sure we never get into some nasty race condition */
317
9.88M
  if (G_UNLIKELY(nqueue->freeze_count == 0)) {
318
0
    G_UNLOCK(notify_lock);
319
0
    g_warning ("%s: property-changed notification for %s(%p) is not frozen",
320
0
               G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
321
0
    return;
322
0
  }
323
324
9.88M
  nqueue->freeze_count--;
325
9.88M
  if (nqueue->freeze_count) {
326
0
    G_UNLOCK(notify_lock);
327
0
    return;
328
0
  }
329
330
9.88M
  pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
331
332
19.7M
  for (slist = nqueue->pspecs; slist; slist = slist->next)
333
9.88M
    {
334
9.88M
      pspecs[n_pspecs++] = slist->data;
335
9.88M
    }
336
9.88M
  g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
337
338
9.88M
  G_UNLOCK(notify_lock);
339
340
9.88M
  if (n_pspecs)
341
9.88M
    G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
342
9.88M
  g_free (free_me);
343
9.88M
}
344
345
static void
346
g_object_notify_queue_add (GObject            *object,
347
                           GObjectNotifyQueue *nqueue,
348
                           GParamSpec         *pspec)
349
9.88M
{
350
9.88M
  G_LOCK(notify_lock);
351
352
9.88M
  g_assert (nqueue->n_pspecs < 65535);
353
354
9.88M
  if (g_slist_find (nqueue->pspecs, pspec) == NULL)
355
9.88M
    {
356
9.88M
      nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
357
9.88M
      nqueue->n_pspecs++;
358
9.88M
    }
359
360
9.88M
  G_UNLOCK(notify_lock);
361
9.88M
}
362
363
#ifdef  G_ENABLE_DEBUG
364
G_LOCK_DEFINE_STATIC     (debug_objects);
365
static guint     debug_objects_count = 0;
366
static GHashTable *debug_objects_ht = NULL;
367
368
static void
369
debug_objects_foreach (gpointer key,
370
           gpointer value,
371
           gpointer user_data)
372
0
{
373
0
  GObject *object = value;
374
375
0
  g_message ("[%p] stale %s\tref_count=%u",
376
0
       object,
377
0
       G_OBJECT_TYPE_NAME (object),
378
0
       object->ref_count);
379
0
}
380
381
#ifdef G_HAS_CONSTRUCTORS
382
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
383
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
384
#endif
385
G_DEFINE_DESTRUCTOR(debug_objects_atexit)
386
#endif /* G_HAS_CONSTRUCTORS */
387
388
static void
389
debug_objects_atexit (void)
390
0
{
391
0
  GOBJECT_IF_DEBUG (OBJECTS,
392
0
    {
393
0
      G_LOCK (debug_objects);
394
0
      g_message ("stale GObjects: %u", debug_objects_count);
395
0
      g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
396
0
      G_UNLOCK (debug_objects);
397
0
    });
398
0
}
399
#endif  /* G_ENABLE_DEBUG */
400
401
void
402
_g_object_type_init (void)
403
75
{
404
75
  static gboolean initialized = FALSE;
405
75
  static const GTypeFundamentalInfo finfo = {
406
75
    G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
407
75
  };
408
75
  GTypeInfo info = {
409
75
    sizeof (GObjectClass),
410
75
    (GBaseInitFunc) g_object_base_class_init,
411
75
    (GBaseFinalizeFunc) g_object_base_class_finalize,
412
75
    (GClassInitFunc) g_object_do_class_init,
413
75
    NULL  /* class_destroy */,
414
75
    NULL  /* class_data */,
415
75
    sizeof (GObject),
416
75
    0   /* n_preallocs */,
417
75
    (GInstanceInitFunc) g_object_init,
418
75
    NULL, /* value_table */
419
75
  };
420
75
  static const GTypeValueTable value_table = {
421
75
    g_value_object_init,    /* value_init */
422
75
    g_value_object_free_value,    /* value_free */
423
75
    g_value_object_copy_value,    /* value_copy */
424
75
    g_value_object_peek_pointer,  /* value_peek_pointer */
425
75
    "p",        /* collect_format */
426
75
    g_value_object_collect_value, /* collect_value */
427
75
    "p",        /* lcopy_format */
428
75
    g_value_object_lcopy_value,   /* lcopy_value */
429
75
  };
430
75
  GType type G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
431
  
432
75
  g_return_if_fail (initialized == FALSE);
433
75
  initialized = TRUE;
434
  
435
  /* G_TYPE_OBJECT
436
   */
437
75
  info.value_table = &value_table;
438
75
  type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
439
75
  g_assert (type == G_TYPE_OBJECT);
440
75
  g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
441
442
75
#if G_ENABLE_DEBUG
443
  /* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS
444
   * conditional in between, as the C spec leaves conditionals inside macro
445
   * expansions as undefined behavior. Only GCC and Clang are known to work
446
   * but compilation breaks on MSVC.
447
   *
448
   * See: https://bugzilla.gnome.org/show_bug.cgi?id=769504
449
   */
450
75
  if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \
451
0
    {
452
0
      debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
453
# ifndef G_HAS_CONSTRUCTORS
454
      g_atexit (debug_objects_atexit);
455
# endif /* G_HAS_CONSTRUCTORS */
456
0
    }
457
75
#endif /* G_ENABLE_DEBUG */
458
75
}
459
460
static void
461
g_object_base_class_init (GObjectClass *class)
462
264
{
463
264
  GObjectClass *pclass = g_type_class_peek_parent (class);
464
465
  /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
466
264
  class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
467
468
264
  if (pclass)
469
228
    pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
470
471
  /* reset instance specific fields and methods that don't get inherited */
472
264
  class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
473
264
  class->get_property = NULL;
474
264
  class->set_property = NULL;
475
264
}
476
477
static void
478
g_object_base_class_finalize (GObjectClass *class)
479
0
{
480
0
  GList *list, *node;
481
  
482
0
  _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
483
484
0
  g_slist_free (class->construct_properties);
485
0
  class->construct_properties = NULL;
486
0
  list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
487
0
  for (node = list; node; node = node->next)
488
0
    {
489
0
      GParamSpec *pspec = node->data;
490
      
491
0
      g_param_spec_pool_remove (pspec_pool, pspec);
492
0
      PARAM_SPEC_SET_PARAM_ID (pspec, 0);
493
0
      g_param_spec_unref (pspec);
494
0
    }
495
0
  g_list_free (list);
496
0
}
497
498
static void
499
g_object_do_class_init (GObjectClass *class)
500
36
{
501
  /* read the comment about typedef struct CArray; on why not to change this quark */
502
36
  quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
503
504
36
  quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
505
36
  quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
506
36
  quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
507
36
  quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
508
36
  quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
509
36
  pspec_pool = g_param_spec_pool_new (TRUE);
510
511
36
  class->constructor = g_object_constructor;
512
36
  class->constructed = g_object_constructed;
513
36
  class->set_property = g_object_do_set_property;
514
36
  class->get_property = g_object_do_get_property;
515
36
  class->dispose = g_object_real_dispose;
516
36
  class->finalize = g_object_finalize;
517
36
  class->dispatch_properties_changed = g_object_dispatch_properties_changed;
518
36
  class->notify = NULL;
519
520
  /**
521
   * GObject::notify:
522
   * @gobject: the object which received the signal.
523
   * @pspec: the #GParamSpec of the property which changed.
524
   *
525
   * The notify signal is emitted on an object when one of its properties has
526
   * its value set through g_object_set_property(), g_object_set(), et al.
527
   *
528
   * Note that getting this signal doesn’t itself guarantee that the value of
529
   * the property has actually changed. When it is emitted is determined by the
530
   * derived GObject class. If the implementor did not create the property with
531
   * %G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results
532
   * in ::notify being emitted, even if the new value is the same as the old.
533
   * If they did pass %G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only
534
   * when they explicitly call g_object_notify() or g_object_notify_by_pspec(),
535
   * and common practice is to do that only when the value has actually changed.
536
   *
537
   * This signal is typically used to obtain change notification for a
538
   * single property, by specifying the property name as a detail in the
539
   * g_signal_connect() call, like this:
540
   * |[<!-- language="C" --> 
541
   * g_signal_connect (text_view->buffer, "notify::paste-target-list",
542
   *                   G_CALLBACK (gtk_text_view_target_list_notify),
543
   *                   text_view)
544
   * ]|
545
   * It is important to note that you must use
546
   * [canonical parameter names][canonical-parameter-names] as
547
   * detail strings for the notify signal.
548
   */
549
36
  gobject_signals[NOTIFY] =
550
36
    g_signal_new (g_intern_static_string ("notify"),
551
36
      G_TYPE_FROM_CLASS (class),
552
36
      G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
553
36
      G_STRUCT_OFFSET (GObjectClass, notify),
554
36
      NULL, NULL,
555
36
      NULL,
556
36
      G_TYPE_NONE,
557
36
      1, G_TYPE_PARAM);
558
559
  /* Install a check function that we'll use to verify that classes that
560
   * implement an interface implement all properties for that interface
561
   */
562
36
  g_type_add_interface_check (NULL, object_interface_check_properties);
563
36
}
564
565
static inline gboolean
566
install_property_internal (GType       g_type,
567
         guint       property_id,
568
         GParamSpec *pspec)
569
36
{
570
36
  if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
571
0
    {
572
0
      g_warning ("When installing property: type '%s' already has a property named '%s'",
573
0
     g_type_name (g_type),
574
0
     pspec->name);
575
0
      return FALSE;
576
0
    }
577
578
36
  g_param_spec_ref_sink (pspec);
579
36
  PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
580
36
  g_param_spec_pool_insert (pspec_pool, pspec, g_type);
581
36
  return TRUE;
582
36
}
583
584
static gboolean
585
validate_pspec_to_install (GParamSpec *pspec)
586
36
{
587
36
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
588
36
  g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */
589
590
36
  g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE);
591
592
36
  if (pspec->flags & G_PARAM_CONSTRUCT)
593
36
    g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE);
594
595
36
  if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
596
36
    g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE);
597
598
36
  return TRUE;
599
36
}
600
601
static gboolean
602
validate_and_install_class_property (GObjectClass *class,
603
                                     GType         oclass_type,
604
                                     GType         parent_type,
605
                                     guint         property_id,
606
                                     GParamSpec   *pspec)
607
36
{
608
36
  if (!validate_pspec_to_install (pspec))
609
0
    return FALSE;
610
611
36
  if (pspec->flags & G_PARAM_WRITABLE)
612
36
    g_return_val_if_fail (class->set_property != NULL, FALSE);
613
36
  if (pspec->flags & G_PARAM_READABLE)
614
36
    g_return_val_if_fail (class->get_property != NULL, FALSE);
615
616
36
  class->flags |= CLASS_HAS_PROPS_FLAG;
617
36
  if (install_property_internal (oclass_type, property_id, pspec))
618
36
    {
619
36
      if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
620
36
        class->construct_properties = g_slist_append (class->construct_properties, pspec);
621
622
      /* for property overrides of construct properties, we have to get rid
623
       * of the overridden inherited construct property
624
       */
625
36
      pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
626
36
      if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
627
0
        class->construct_properties = g_slist_remove (class->construct_properties, pspec);
628
629
36
      return TRUE;
630
36
    }
631
0
  else
632
0
    return FALSE;
633
36
}
634
635
/**
636
 * g_object_class_install_property:
637
 * @oclass: a #GObjectClass
638
 * @property_id: the id for the new property
639
 * @pspec: the #GParamSpec for the new property
640
 *
641
 * Installs a new property.
642
 *
643
 * All properties should be installed during the class initializer.  It
644
 * is possible to install properties after that, but doing so is not
645
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
646
 * use of properties on the same type on other threads.
647
 *
648
 * Note that it is possible to redefine a property in a derived class,
649
 * by installing a property with the same name. This can be useful at times,
650
 * e.g. to change the range of allowed values or the default value.
651
 */
652
void
653
g_object_class_install_property (GObjectClass *class,
654
         guint         property_id,
655
         GParamSpec   *pspec)
656
36
{
657
36
  GType oclass_type, parent_type;
658
659
36
  g_return_if_fail (G_IS_OBJECT_CLASS (class));
660
36
  g_return_if_fail (property_id > 0);
661
662
36
  oclass_type = G_OBJECT_CLASS_TYPE (class);
663
36
  parent_type = g_type_parent (oclass_type);
664
665
36
  if (CLASS_HAS_DERIVED_CLASS (class))
666
36
    g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
667
668
36
  (void) validate_and_install_class_property (class,
669
36
                                              oclass_type,
670
36
                                              parent_type,
671
36
                                              property_id,
672
36
                                              pspec);
673
36
}
674
675
/**
676
 * g_object_class_install_properties:
677
 * @oclass: a #GObjectClass
678
 * @n_pspecs: the length of the #GParamSpecs array
679
 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
680
 *   defining the new properties
681
 *
682
 * Installs new properties from an array of #GParamSpecs.
683
 *
684
 * All properties should be installed during the class initializer.  It
685
 * is possible to install properties after that, but doing so is not
686
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
687
 * use of properties on the same type on other threads.
688
 *
689
 * The property id of each property is the index of each #GParamSpec in
690
 * the @pspecs array.
691
 *
692
 * The property id of 0 is treated specially by #GObject and it should not
693
 * be used to store a #GParamSpec.
694
 *
695
 * This function should be used if you plan to use a static array of
696
 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
697
 * class initialization:
698
 *
699
 * |[<!-- language="C" --> 
700
 * enum {
701
 *   PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
702
 * };
703
 *
704
 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
705
 *
706
 * static void
707
 * my_object_class_init (MyObjectClass *klass)
708
 * {
709
 *   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
710
 *
711
 *   obj_properties[PROP_FOO] =
712
 *     g_param_spec_int ("foo", "Foo", "Foo",
713
 *                       -1, G_MAXINT,
714
 *                       0,
715
 *                       G_PARAM_READWRITE);
716
 *
717
 *   obj_properties[PROP_BAR] =
718
 *     g_param_spec_string ("bar", "Bar", "Bar",
719
 *                          NULL,
720
 *                          G_PARAM_READWRITE);
721
 *
722
 *   gobject_class->set_property = my_object_set_property;
723
 *   gobject_class->get_property = my_object_get_property;
724
 *   g_object_class_install_properties (gobject_class,
725
 *                                      N_PROPERTIES,
726
 *                                      obj_properties);
727
 * }
728
 * ]|
729
 *
730
 * allows calling g_object_notify_by_pspec() to notify of property changes:
731
 *
732
 * |[<!-- language="C" --> 
733
 * void
734
 * my_object_set_foo (MyObject *self, gint foo)
735
 * {
736
 *   if (self->foo != foo)
737
 *     {
738
 *       self->foo = foo;
739
 *       g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
740
 *     }
741
 *  }
742
 * ]|
743
 *
744
 * Since: 2.26
745
 */
746
void
747
g_object_class_install_properties (GObjectClass  *oclass,
748
                                   guint          n_pspecs,
749
                                   GParamSpec   **pspecs)
750
0
{
751
0
  GType oclass_type, parent_type;
752
0
  guint i;
753
754
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
755
0
  g_return_if_fail (n_pspecs > 1);
756
0
  g_return_if_fail (pspecs[0] == NULL);
757
758
0
  if (CLASS_HAS_DERIVED_CLASS (oclass))
759
0
    g_error ("Attempt to add properties to %s after it was derived",
760
0
             G_OBJECT_CLASS_NAME (oclass));
761
762
0
  oclass_type = G_OBJECT_CLASS_TYPE (oclass);
763
0
  parent_type = g_type_parent (oclass_type);
764
765
  /* we skip the first element of the array as it would have a 0 prop_id */
766
0
  for (i = 1; i < n_pspecs; i++)
767
0
    {
768
0
      GParamSpec *pspec = pspecs[i];
769
770
0
      if (!validate_and_install_class_property (oclass,
771
0
                                                oclass_type,
772
0
                                                parent_type,
773
0
                                                i,
774
0
                                                pspec))
775
0
        {
776
0
          break;
777
0
        }
778
0
    }
779
0
}
780
781
/**
782
 * g_object_interface_install_property:
783
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
784
 *    interface, or the default
785
 *  vtable for the interface.
786
 * @pspec: the #GParamSpec for the new property
787
 *
788
 * Add a property to an interface; this is only useful for interfaces
789
 * that are added to GObject-derived types. Adding a property to an
790
 * interface forces all objects classes with that interface to have a
791
 * compatible property. The compatible property could be a newly
792
 * created #GParamSpec, but normally
793
 * g_object_class_override_property() will be used so that the object
794
 * class only needs to provide an implementation and inherits the
795
 * property description, default value, bounds, and so forth from the
796
 * interface property.
797
 *
798
 * This function is meant to be called from the interface's default
799
 * vtable initialization function (the @class_init member of
800
 * #GTypeInfo.) It must not be called after after @class_init has
801
 * been called for any object types implementing this interface.
802
 *
803
 * If @pspec is a floating reference, it will be consumed.
804
 *
805
 * Since: 2.4
806
 */
807
void
808
g_object_interface_install_property (gpointer      g_iface,
809
             GParamSpec   *pspec)
810
0
{
811
0
  GTypeInterface *iface_class = g_iface;
812
  
813
0
  g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
814
0
  g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
815
816
0
  if (!validate_pspec_to_install (pspec))
817
0
    return;
818
819
0
  (void) install_property_internal (iface_class->g_type, 0, pspec);
820
0
}
821
822
/**
823
 * g_object_class_find_property:
824
 * @oclass: a #GObjectClass
825
 * @property_name: the name of the property to look up
826
 *
827
 * Looks up the #GParamSpec for a property of a class.
828
 *
829
 * Returns: (transfer none): the #GParamSpec for the property, or
830
 *          %NULL if the class doesn't have a property of that name
831
 */
832
GParamSpec*
833
g_object_class_find_property (GObjectClass *class,
834
            const gchar  *property_name)
835
0
{
836
0
  GParamSpec *pspec;
837
0
  GParamSpec *redirect;
838
  
839
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
840
0
  g_return_val_if_fail (property_name != NULL, NULL);
841
  
842
0
  pspec = g_param_spec_pool_lookup (pspec_pool,
843
0
            property_name,
844
0
            G_OBJECT_CLASS_TYPE (class),
845
0
            TRUE);
846
0
  if (pspec)
847
0
    {
848
0
      redirect = g_param_spec_get_redirect_target (pspec);
849
0
      if (redirect)
850
0
  return redirect;
851
0
      else
852
0
  return pspec;
853
0
    }
854
0
  else
855
0
    return NULL;
856
0
}
857
858
/**
859
 * g_object_interface_find_property:
860
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
861
 *  interface, or the default vtable for the interface
862
 * @property_name: name of a property to look up.
863
 *
864
 * Find the #GParamSpec with the given name for an
865
 * interface. Generally, the interface vtable passed in as @g_iface
866
 * will be the default vtable from g_type_default_interface_ref(), or,
867
 * if you know the interface has already been loaded,
868
 * g_type_default_interface_peek().
869
 *
870
 * Since: 2.4
871
 *
872
 * Returns: (transfer none): the #GParamSpec for the property of the
873
 *          interface with the name @property_name, or %NULL if no
874
 *          such property exists.
875
 */
876
GParamSpec*
877
g_object_interface_find_property (gpointer      g_iface,
878
          const gchar  *property_name)
879
0
{
880
0
  GTypeInterface *iface_class = g_iface;
881
  
882
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
883
0
  g_return_val_if_fail (property_name != NULL, NULL);
884
  
885
0
  return g_param_spec_pool_lookup (pspec_pool,
886
0
           property_name,
887
0
           iface_class->g_type,
888
0
           FALSE);
889
0
}
890
891
/**
892
 * g_object_class_override_property:
893
 * @oclass: a #GObjectClass
894
 * @property_id: the new property ID
895
 * @name: the name of a property registered in a parent class or
896
 *  in an interface of this class.
897
 *
898
 * Registers @property_id as referring to a property with the name
899
 * @name in a parent class or in an interface implemented by @oclass.
900
 * This allows this class to "override" a property implementation in
901
 * a parent class or to provide the implementation of a property from
902
 * an interface.
903
 *
904
 * Internally, overriding is implemented by creating a property of type
905
 * #GParamSpecOverride; generally operations that query the properties of
906
 * the object class, such as g_object_class_find_property() or
907
 * g_object_class_list_properties() will return the overridden
908
 * property. However, in one case, the @construct_properties argument of
909
 * the @constructor virtual function, the #GParamSpecOverride is passed
910
 * instead, so that the @param_id field of the #GParamSpec will be
911
 * correct.  For virtually all uses, this makes no difference. If you
912
 * need to get the overridden property, you can call
913
 * g_param_spec_get_redirect_target().
914
 *
915
 * Since: 2.4
916
 */
917
void
918
g_object_class_override_property (GObjectClass *oclass,
919
          guint         property_id,
920
          const gchar  *name)
921
0
{
922
0
  GParamSpec *overridden = NULL;
923
0
  GParamSpec *new;
924
0
  GType parent_type;
925
  
926
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
927
0
  g_return_if_fail (property_id > 0);
928
0
  g_return_if_fail (name != NULL);
929
930
  /* Find the overridden property; first check parent types
931
   */
932
0
  parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
933
0
  if (parent_type != G_TYPE_NONE)
934
0
    overridden = g_param_spec_pool_lookup (pspec_pool,
935
0
             name,
936
0
             parent_type,
937
0
             TRUE);
938
0
  if (!overridden)
939
0
    {
940
0
      GType *ifaces;
941
0
      guint n_ifaces;
942
      
943
      /* Now check interfaces
944
       */
945
0
      ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
946
0
      while (n_ifaces-- && !overridden)
947
0
  {
948
0
    overridden = g_param_spec_pool_lookup (pspec_pool,
949
0
             name,
950
0
             ifaces[n_ifaces],
951
0
             FALSE);
952
0
  }
953
      
954
0
      g_free (ifaces);
955
0
    }
956
957
0
  if (!overridden)
958
0
    {
959
0
      g_warning ("%s: Can't find property to override for '%s::%s'",
960
0
     G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
961
0
      return;
962
0
    }
963
964
0
  new = g_param_spec_override (name, overridden);
965
0
  g_object_class_install_property (oclass, property_id, new);
966
0
}
967
968
/**
969
 * g_object_class_list_properties:
970
 * @oclass: a #GObjectClass
971
 * @n_properties: (out): return location for the length of the returned array
972
 *
973
 * Get an array of #GParamSpec* for all properties of a class.
974
 *
975
 * Returns: (array length=n_properties) (transfer container): an array of
976
 *          #GParamSpec* which should be freed after use
977
 */
978
GParamSpec** /* free result */
979
g_object_class_list_properties (GObjectClass *class,
980
        guint        *n_properties_p)
981
0
{
982
0
  GParamSpec **pspecs;
983
0
  guint n;
984
985
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
986
987
0
  pspecs = g_param_spec_pool_list (pspec_pool,
988
0
           G_OBJECT_CLASS_TYPE (class),
989
0
           &n);
990
0
  if (n_properties_p)
991
0
    *n_properties_p = n;
992
993
0
  return pspecs;
994
0
}
995
996
/**
997
 * g_object_interface_list_properties:
998
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
999
 *  interface, or the default vtable for the interface
1000
 * @n_properties_p: (out): location to store number of properties returned.
1001
 *
1002
 * Lists the properties of an interface.Generally, the interface
1003
 * vtable passed in as @g_iface will be the default vtable from
1004
 * g_type_default_interface_ref(), or, if you know the interface has
1005
 * already been loaded, g_type_default_interface_peek().
1006
 *
1007
 * Since: 2.4
1008
 *
1009
 * Returns: (array length=n_properties_p) (transfer container): a
1010
 *          pointer to an array of pointers to #GParamSpec
1011
 *          structures. The paramspecs are owned by GLib, but the
1012
 *          array should be freed with g_free() when you are done with
1013
 *          it.
1014
 */
1015
GParamSpec**
1016
g_object_interface_list_properties (gpointer      g_iface,
1017
            guint        *n_properties_p)
1018
0
{
1019
0
  GTypeInterface *iface_class = g_iface;
1020
0
  GParamSpec **pspecs;
1021
0
  guint n;
1022
1023
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
1024
1025
0
  pspecs = g_param_spec_pool_list (pspec_pool,
1026
0
           iface_class->g_type,
1027
0
           &n);
1028
0
  if (n_properties_p)
1029
0
    *n_properties_p = n;
1030
1031
0
  return pspecs;
1032
0
}
1033
1034
static inline guint
1035
object_get_optional_flags (GObject *object)
1036
23.9M
{
1037
23.9M
#ifdef HAVE_OPTIONAL_FLAGS
1038
23.9M
  GObjectReal *real = (GObjectReal *)object;
1039
23.9M
  return (guint)g_atomic_int_get (&real->optional_flags);
1040
#else
1041
  return 0;
1042
#endif
1043
23.9M
}
1044
1045
static inline void
1046
object_set_optional_flags (GObject *object,
1047
                          guint flags)
1048
0
{
1049
0
#ifdef HAVE_OPTIONAL_FLAGS
1050
0
  GObjectReal *real = (GObjectReal *)object;
1051
0
  g_atomic_int_or (&real->optional_flags, flags);
1052
0
#endif
1053
0
}
1054
1055
static inline void
1056
object_unset_optional_flags (GObject *object,
1057
                            guint flags)
1058
0
{
1059
0
#ifdef HAVE_OPTIONAL_FLAGS
1060
0
  GObjectReal *real = (GObjectReal *)object;
1061
0
  g_atomic_int_and (&real->optional_flags, ~flags);
1062
0
#endif
1063
0
}
1064
1065
gboolean
1066
_g_object_has_signal_handler  (GObject *object)
1067
9.88M
{
1068
9.88M
#ifdef HAVE_OPTIONAL_FLAGS
1069
9.88M
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_SIGNAL_HANDLER) != 0;
1070
#else
1071
  return TRUE;
1072
#endif
1073
9.88M
}
1074
1075
void
1076
_g_object_set_has_signal_handler (GObject     *object)
1077
0
{
1078
0
#ifdef HAVE_OPTIONAL_FLAGS
1079
0
  object_set_optional_flags (object, OPTIONAL_FLAG_HAS_SIGNAL_HANDLER);
1080
0
#endif
1081
0
}
1082
1083
static inline gboolean
1084
object_in_construction (GObject *object)
1085
14.0M
{
1086
14.0M
#ifdef HAVE_OPTIONAL_FLAGS
1087
14.0M
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0;
1088
#else
1089
  return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
1090
#endif
1091
14.0M
}
1092
1093
static inline void
1094
set_object_in_construction (GObject *object)
1095
0
{
1096
0
#ifdef HAVE_OPTIONAL_FLAGS
1097
0
  object_set_optional_flags (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1098
#else
1099
  g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
1100
#endif
1101
0
}
1102
1103
static inline void
1104
unset_object_in_construction (GObject *object)
1105
0
{
1106
0
#ifdef HAVE_OPTIONAL_FLAGS
1107
0
  object_unset_optional_flags (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1108
#else
1109
  g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1110
#endif
1111
0
}
1112
1113
static void
1114
g_object_init (GObject    *object,
1115
         GObjectClass *class)
1116
14.0M
{
1117
14.0M
  object->ref_count = 1;
1118
14.0M
  object->qdata = NULL;
1119
1120
14.0M
  if (CLASS_HAS_PROPS (class))
1121
9.88M
    {
1122
      /* freeze object's notification queue, g_object_newv() preserves pairedness */
1123
9.88M
      g_object_notify_queue_freeze (object, FALSE);
1124
9.88M
    }
1125
1126
14.0M
  if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1127
0
    {
1128
      /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
1129
0
      set_object_in_construction (object);
1130
0
    }
1131
1132
14.0M
  GOBJECT_IF_DEBUG (OBJECTS,
1133
14.0M
    {
1134
14.0M
      G_LOCK (debug_objects);
1135
14.0M
      debug_objects_count++;
1136
14.0M
      g_hash_table_add (debug_objects_ht, object);
1137
14.0M
      G_UNLOCK (debug_objects);
1138
14.0M
    });
1139
14.0M
}
1140
1141
static void
1142
g_object_do_set_property (GObject      *object,
1143
        guint         property_id,
1144
        const GValue *value,
1145
        GParamSpec   *pspec)
1146
0
{
1147
0
  switch (property_id)
1148
0
    {
1149
0
    default:
1150
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1151
0
      break;
1152
0
    }
1153
0
}
1154
1155
static void
1156
g_object_do_get_property (GObject     *object,
1157
        guint        property_id,
1158
        GValue      *value,
1159
        GParamSpec  *pspec)
1160
0
{
1161
0
  switch (property_id)
1162
0
    {
1163
0
    default:
1164
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1165
0
      break;
1166
0
    }
1167
0
}
1168
1169
static void
1170
g_object_real_dispose (GObject *object)
1171
14.0M
{
1172
14.0M
  g_signal_handlers_destroy (object);
1173
14.0M
  g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1174
14.0M
  g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1175
14.0M
}
1176
1177
static void
1178
g_object_finalize (GObject *object)
1179
14.0M
{
1180
14.0M
  if (object_in_construction (object))
1181
0
    {
1182
0
      g_critical ("object %s %p finalized while still in-construction",
1183
0
                  G_OBJECT_TYPE_NAME (object), object);
1184
0
    }
1185
1186
14.0M
  g_datalist_clear (&object->qdata);
1187
  
1188
14.0M
  GOBJECT_IF_DEBUG (OBJECTS,
1189
14.0M
    {
1190
14.0M
      G_LOCK (debug_objects);
1191
14.0M
      g_assert (g_hash_table_contains (debug_objects_ht, object));
1192
14.0M
      g_hash_table_remove (debug_objects_ht, object);
1193
14.0M
      debug_objects_count--;
1194
14.0M
      G_UNLOCK (debug_objects);
1195
14.0M
    });
1196
14.0M
}
1197
1198
static void
1199
g_object_dispatch_properties_changed (GObject     *object,
1200
              guint        n_pspecs,
1201
              GParamSpec **pspecs)
1202
9.88M
{
1203
9.88M
  guint i;
1204
1205
19.7M
  for (i = 0; i < n_pspecs; i++)
1206
9.88M
    g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1207
9.88M
}
1208
1209
/**
1210
 * g_object_run_dispose:
1211
 * @object: a #GObject
1212
 *
1213
 * Releases all references to other objects. This can be used to break
1214
 * reference cycles.
1215
 *
1216
 * This function should only be called from object system implementations.
1217
 */
1218
void
1219
g_object_run_dispose (GObject *object)
1220
0
{
1221
0
  g_return_if_fail (G_IS_OBJECT (object));
1222
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0);
1223
1224
0
  g_object_ref (object);
1225
0
  TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1226
0
  G_OBJECT_GET_CLASS (object)->dispose (object);
1227
0
  TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1228
0
  g_object_unref (object);
1229
0
}
1230
1231
/**
1232
 * g_object_freeze_notify:
1233
 * @object: a #GObject
1234
 *
1235
 * Increases the freeze count on @object. If the freeze count is
1236
 * non-zero, the emission of "notify" signals on @object is
1237
 * stopped. The signals are queued until the freeze count is decreased
1238
 * to zero. Duplicate notifications are squashed so that at most one
1239
 * #GObject::notify signal is emitted for each property modified while the
1240
 * object is frozen.
1241
 *
1242
 * This is necessary for accessors that modify multiple properties to prevent
1243
 * premature notification while the object is still being modified.
1244
 */
1245
void
1246
g_object_freeze_notify (GObject *object)
1247
0
{
1248
0
  g_return_if_fail (G_IS_OBJECT (object));
1249
1250
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1251
0
    return;
1252
1253
0
  g_object_ref (object);
1254
0
  g_object_notify_queue_freeze (object, FALSE);
1255
0
  g_object_unref (object);
1256
0
}
1257
1258
static GParamSpec *
1259
get_notify_pspec (GParamSpec *pspec)
1260
9.88M
{
1261
9.88M
  GParamSpec *redirected;
1262
1263
  /* we don't notify on non-READABLE parameters */
1264
9.88M
  if (~pspec->flags & G_PARAM_READABLE)
1265
0
    return NULL;
1266
1267
  /* if the paramspec is redirected, notify on the target */
1268
9.88M
  redirected = g_param_spec_get_redirect_target (pspec);
1269
9.88M
  if (redirected != NULL)
1270
0
    return redirected;
1271
1272
  /* else, notify normally */
1273
9.88M
  return pspec;
1274
9.88M
}
1275
1276
static inline void
1277
g_object_notify_by_spec_internal (GObject    *object,
1278
          GParamSpec *pspec)
1279
0
{
1280
0
  GParamSpec *notify_pspec;
1281
1282
0
  notify_pspec = get_notify_pspec (pspec);
1283
1284
0
  if (notify_pspec != NULL)
1285
0
    {
1286
0
      GObjectNotifyQueue *nqueue;
1287
1288
      /* conditional freeze: only increase freeze count if already frozen */
1289
0
      nqueue = g_object_notify_queue_freeze (object, TRUE);
1290
1291
0
      if (nqueue != NULL)
1292
0
        {
1293
          /* we're frozen, so add to the queue and release our freeze */
1294
0
          g_object_notify_queue_add (object, nqueue, notify_pspec);
1295
0
          g_object_notify_queue_thaw (object, nqueue);
1296
0
        }
1297
0
      else
1298
        /* not frozen, so just dispatch the notification directly */
1299
0
        G_OBJECT_GET_CLASS (object)
1300
0
          ->dispatch_properties_changed (object, 1, &notify_pspec);
1301
0
    }
1302
0
}
1303
1304
/**
1305
 * g_object_notify:
1306
 * @object: a #GObject
1307
 * @property_name: the name of a property installed on the class of @object.
1308
 *
1309
 * Emits a "notify" signal for the property @property_name on @object.
1310
 *
1311
 * When possible, eg. when signaling a property change from within the class
1312
 * that registered the property, you should use g_object_notify_by_pspec()
1313
 * instead.
1314
 *
1315
 * Note that emission of the notify signal may be blocked with
1316
 * g_object_freeze_notify(). In this case, the signal emissions are queued
1317
 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1318
 * called.
1319
 */
1320
void
1321
g_object_notify (GObject     *object,
1322
     const gchar *property_name)
1323
0
{
1324
0
  GParamSpec *pspec;
1325
  
1326
0
  g_return_if_fail (G_IS_OBJECT (object));
1327
0
  g_return_if_fail (property_name != NULL);
1328
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1329
0
    return;
1330
  
1331
0
  g_object_ref (object);
1332
  /* We don't need to get the redirect target
1333
   * (by, e.g. calling g_object_class_find_property())
1334
   * because g_object_notify_queue_add() does that
1335
   */
1336
0
  pspec = g_param_spec_pool_lookup (pspec_pool,
1337
0
            property_name,
1338
0
            G_OBJECT_TYPE (object),
1339
0
            TRUE);
1340
1341
0
  if (!pspec)
1342
0
    g_warning ("%s: object class '%s' has no property named '%s'",
1343
0
         G_STRFUNC,
1344
0
         G_OBJECT_TYPE_NAME (object),
1345
0
         property_name);
1346
0
  else
1347
0
    g_object_notify_by_spec_internal (object, pspec);
1348
0
  g_object_unref (object);
1349
0
}
1350
1351
/**
1352
 * g_object_notify_by_pspec:
1353
 * @object: a #GObject
1354
 * @pspec: the #GParamSpec of a property installed on the class of @object.
1355
 *
1356
 * Emits a "notify" signal for the property specified by @pspec on @object.
1357
 *
1358
 * This function omits the property name lookup, hence it is faster than
1359
 * g_object_notify().
1360
 *
1361
 * One way to avoid using g_object_notify() from within the
1362
 * class that registered the properties, and using g_object_notify_by_pspec()
1363
 * instead, is to store the GParamSpec used with
1364
 * g_object_class_install_property() inside a static array, e.g.:
1365
 *
1366
 *|[<!-- language="C" --> 
1367
 *   enum
1368
 *   {
1369
 *     PROP_0,
1370
 *     PROP_FOO,
1371
 *     PROP_LAST
1372
 *   };
1373
 *
1374
 *   static GParamSpec *properties[PROP_LAST];
1375
 *
1376
 *   static void
1377
 *   my_object_class_init (MyObjectClass *klass)
1378
 *   {
1379
 *     properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1380
 *                                              0, 100,
1381
 *                                              50,
1382
 *                                              G_PARAM_READWRITE);
1383
 *     g_object_class_install_property (gobject_class,
1384
 *                                      PROP_FOO,
1385
 *                                      properties[PROP_FOO]);
1386
 *   }
1387
 * ]|
1388
 *
1389
 * and then notify a change on the "foo" property with:
1390
 *
1391
 * |[<!-- language="C" --> 
1392
 *   g_object_notify_by_pspec (self, properties[PROP_FOO]);
1393
 * ]|
1394
 *
1395
 * Since: 2.26
1396
 */
1397
void
1398
g_object_notify_by_pspec (GObject    *object,
1399
        GParamSpec *pspec)
1400
0
{
1401
1402
0
  g_return_if_fail (G_IS_OBJECT (object));
1403
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1404
1405
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1406
0
    return;
1407
1408
0
  g_object_ref (object);
1409
0
  g_object_notify_by_spec_internal (object, pspec);
1410
0
  g_object_unref (object);
1411
0
}
1412
1413
/**
1414
 * g_object_thaw_notify:
1415
 * @object: a #GObject
1416
 *
1417
 * Reverts the effect of a previous call to
1418
 * g_object_freeze_notify(). The freeze count is decreased on @object
1419
 * and when it reaches zero, queued "notify" signals are emitted.
1420
 *
1421
 * Duplicate notifications for each property are squashed so that at most one
1422
 * #GObject::notify signal is emitted for each property, in the reverse order
1423
 * in which they have been queued.
1424
 *
1425
 * It is an error to call this function when the freeze count is zero.
1426
 */
1427
void
1428
g_object_thaw_notify (GObject *object)
1429
0
{
1430
0
  GObjectNotifyQueue *nqueue;
1431
  
1432
0
  g_return_if_fail (G_IS_OBJECT (object));
1433
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1434
0
    return;
1435
  
1436
0
  g_object_ref (object);
1437
1438
  /* FIXME: Freezing is the only way to get at the notify queue.
1439
   * So we freeze once and then thaw twice.
1440
   */
1441
0
  nqueue = g_object_notify_queue_freeze (object, FALSE);
1442
0
  g_object_notify_queue_thaw (object, nqueue);
1443
0
  g_object_notify_queue_thaw (object, nqueue);
1444
1445
0
  g_object_unref (object);
1446
0
}
1447
1448
static void
1449
consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
1450
0
{
1451
0
  static GHashTable *already_warned_table;
1452
0
  static const gchar *enable_diagnostic;
1453
0
  static GMutex already_warned_lock;
1454
0
  gboolean already;
1455
1456
0
  if (!(pspec->flags & G_PARAM_DEPRECATED))
1457
0
    return;
1458
1459
0
  if (g_once_init_enter (&enable_diagnostic))
1460
0
    {
1461
0
      const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1462
1463
0
      if (!value)
1464
0
        value = "0";
1465
1466
0
      g_once_init_leave (&enable_diagnostic, value);
1467
0
    }
1468
1469
0
  if (enable_diagnostic[0] == '0')
1470
0
    return;
1471
1472
  /* We hash only on property names: this means that we could end up in
1473
   * a situation where we fail to emit a warning about a pair of
1474
   * same-named deprecated properties used on two separate types.
1475
   * That's pretty unlikely to occur, and even if it does, you'll still
1476
   * have seen the warning for the first one...
1477
   *
1478
   * Doing it this way lets us hash directly on the (interned) property
1479
   * name pointers.
1480
   */
1481
0
  g_mutex_lock (&already_warned_lock);
1482
1483
0
  if (already_warned_table == NULL)
1484
0
    already_warned_table = g_hash_table_new (NULL, NULL);
1485
1486
0
  already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
1487
0
  if (!already)
1488
0
    g_hash_table_add (already_warned_table, (gpointer) pspec->name);
1489
1490
0
  g_mutex_unlock (&already_warned_lock);
1491
1492
0
  if (!already)
1493
0
    g_warning ("The property %s:%s is deprecated and shouldn't be used "
1494
0
               "anymore. It will be removed in a future version.",
1495
0
               g_type_name (pspec->owner_type), pspec->name);
1496
0
}
1497
1498
static inline void
1499
object_get_property (GObject     *object,
1500
         GParamSpec  *pspec,
1501
         GValue      *value)
1502
0
{
1503
0
  GObjectClass *class = g_type_class_peek (pspec->owner_type);
1504
0
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1505
0
  GParamSpec *redirect;
1506
1507
0
  if (class == NULL)
1508
0
    {
1509
0
      g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1510
0
                 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1511
0
      return;
1512
0
    }
1513
1514
0
  redirect = g_param_spec_get_redirect_target (pspec);
1515
0
  if (redirect)
1516
0
    pspec = redirect;
1517
1518
0
  consider_issuing_property_deprecation_warning (pspec);
1519
1520
0
  class->get_property (object, param_id, value, pspec);
1521
0
}
1522
1523
static inline void
1524
object_set_property (GObject             *object,
1525
         GParamSpec          *pspec,
1526
         const GValue        *value,
1527
         GObjectNotifyQueue  *nqueue)
1528
9.88M
{
1529
9.88M
  GValue tmp_value = G_VALUE_INIT;
1530
9.88M
  GObjectClass *class = g_type_class_peek (pspec->owner_type);
1531
9.88M
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1532
9.88M
  GParamSpec *redirect;
1533
1534
9.88M
  if (class == NULL)
1535
0
    {
1536
0
      g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1537
0
                 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1538
0
      return;
1539
0
    }
1540
1541
9.88M
  redirect = g_param_spec_get_redirect_target (pspec);
1542
9.88M
  if (redirect)
1543
0
    pspec = redirect;
1544
1545
  /* provide a copy to work from, convert (if necessary) and validate */
1546
9.88M
  g_value_init (&tmp_value, pspec->value_type);
1547
9.88M
  if (!g_value_transform (value, &tmp_value))
1548
0
    g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1549
9.88M
         pspec->name,
1550
9.88M
         g_type_name (pspec->value_type),
1551
9.88M
         G_VALUE_TYPE_NAME (value));
1552
9.88M
  else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1553
0
    {
1554
0
      gchar *contents = g_strdup_value_contents (value);
1555
1556
0
      g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1557
0
     contents,
1558
0
     G_VALUE_TYPE_NAME (value),
1559
0
     pspec->name,
1560
0
     g_type_name (pspec->value_type));
1561
0
      g_free (contents);
1562
0
    }
1563
9.88M
  else
1564
9.88M
    {
1565
9.88M
      class->set_property (object, param_id, &tmp_value, pspec);
1566
1567
9.88M
      if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY)
1568
9.88M
        {
1569
9.88M
          GParamSpec *notify_pspec;
1570
1571
9.88M
          notify_pspec = get_notify_pspec (pspec);
1572
1573
9.88M
          if (notify_pspec != NULL)
1574
9.88M
            g_object_notify_queue_add (object, nqueue, notify_pspec);
1575
9.88M
        }
1576
9.88M
    }
1577
9.88M
  g_value_unset (&tmp_value);
1578
9.88M
}
1579
1580
static void
1581
object_interface_check_properties (gpointer check_data,
1582
           gpointer g_iface)
1583
129
{
1584
129
  GTypeInterface *iface_class = g_iface;
1585
129
  GObjectClass *class;
1586
129
  GType iface_type = iface_class->g_type;
1587
129
  GParamSpec **pspecs;
1588
129
  guint n;
1589
1590
129
  class = g_type_class_ref (iface_class->g_instance_type);
1591
1592
129
  if (class == NULL)
1593
0
    return;
1594
1595
129
  if (!G_IS_OBJECT_CLASS (class))
1596
0
    goto out;
1597
1598
129
  pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1599
1600
129
  while (n--)
1601
0
    {
1602
0
      GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1603
0
                pspecs[n]->name,
1604
0
                G_OBJECT_CLASS_TYPE (class),
1605
0
                TRUE);
1606
1607
0
      if (!class_pspec)
1608
0
  {
1609
0
    g_critical ("Object class %s doesn't implement property "
1610
0
          "'%s' from interface '%s'",
1611
0
          g_type_name (G_OBJECT_CLASS_TYPE (class)),
1612
0
          pspecs[n]->name,
1613
0
          g_type_name (iface_type));
1614
1615
0
    continue;
1616
0
  }
1617
1618
      /* We do a number of checks on the properties of an interface to
1619
       * make sure that all classes implementing the interface are
1620
       * overriding the properties correctly.
1621
       *
1622
       * We do the checks in order of importance so that we can give
1623
       * more useful error messages first.
1624
       *
1625
       * First, we check that the implementation doesn't remove the
1626
       * basic functionality (readability, writability) advertised by
1627
       * the interface.  Next, we check that it doesn't introduce
1628
       * additional restrictions (such as construct-only).  Finally, we
1629
       * make sure the types are compatible.
1630
       */
1631
1632
0
#define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1633
      /* If the property on the interface is readable then the
1634
       * implementation must be readable.  If the interface is writable
1635
       * then the implementation must be writable.
1636
       */
1637
0
      if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1638
0
        {
1639
0
          g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1640
0
                      "property on interface '%s'\n", pspecs[n]->name,
1641
0
                      g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1642
0
          continue;
1643
0
        }
1644
1645
      /* If the property on the interface is writable then we need to
1646
       * make sure the implementation doesn't introduce new restrictions
1647
       * on that writability (ie: construct-only).
1648
       *
1649
       * If the interface was not writable to begin with then we don't
1650
       * really have any problems here because "writable at construct
1651
       * time only" is still more permissive than "read only".
1652
       */
1653
0
      if (pspecs[n]->flags & G_PARAM_WRITABLE)
1654
0
        {
1655
0
          if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1656
0
            {
1657
0
              g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1658
0
                          "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1659
0
                          g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1660
0
              continue;
1661
0
            }
1662
0
        }
1663
0
#undef SUBSET
1664
1665
      /* If the property on the interface is readable then we are
1666
       * effectively advertising that reading the property will return a
1667
       * value of a specific type.  All implementations of the interface
1668
       * need to return items of this type -- but may be more
1669
       * restrictive.  For example, it is legal to have:
1670
       *
1671
       *   GtkWidget *get_item();
1672
       *
1673
       * that is implemented by a function that always returns a
1674
       * GtkEntry.  In short: readability implies that the
1675
       * implementation  value type must be equal or more restrictive.
1676
       *
1677
       * Similarly, if the property on the interface is writable then
1678
       * must be able to accept the property being set to any value of
1679
       * that type, including subclasses.  In this case, we may also be
1680
       * less restrictive.  For example, it is legal to have:
1681
       *
1682
       *   set_item (GtkEntry *);
1683
       *
1684
       * that is implemented by a function that will actually work with
1685
       * any GtkWidget.  In short: writability implies that the
1686
       * implementation value type must be equal or less restrictive.
1687
       *
1688
       * In the case that the property is both readable and writable
1689
       * then the only way that both of the above can be satisfied is
1690
       * with a type that is exactly equal.
1691
       */
1692
0
      switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1693
0
        {
1694
0
        case G_PARAM_READABLE | G_PARAM_WRITABLE:
1695
          /* class pspec value type must have exact equality with interface */
1696
0
          if (pspecs[n]->value_type != class_pspec->value_type)
1697
0
            g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1698
0
                        "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1699
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1700
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1701
0
          break;
1702
1703
0
        case G_PARAM_READABLE:
1704
          /* class pspec value type equal or more restrictive than interface */
1705
0
          if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1706
0
            g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1707
0
                        "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1708
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1709
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1710
0
          break;
1711
1712
0
        case G_PARAM_WRITABLE:
1713
          /* class pspec value type equal or less restrictive than interface */
1714
0
          if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1715
0
            g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1716
0
                        "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1717
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1718
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1719
0
          break;
1720
1721
0
        default:
1722
0
          g_assert_not_reached ();
1723
0
        }
1724
0
    }
1725
1726
129
  g_free (pspecs);
1727
1728
129
 out:
1729
129
  g_type_class_unref (class);
1730
129
}
1731
1732
GType
1733
g_object_get_type (void)
1734
0
{
1735
0
    return G_TYPE_OBJECT;
1736
0
}
1737
1738
/**
1739
 * g_object_new: (skip)
1740
 * @object_type: the type id of the #GObject subtype to instantiate
1741
 * @first_property_name: the name of the first property
1742
 * @...: the value of the first property, followed optionally by more
1743
 *  name/value pairs, followed by %NULL
1744
 *
1745
 * Creates a new instance of a #GObject subtype and sets its properties.
1746
 *
1747
 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1748
 * which are not explicitly specified are set to their default values. Any
1749
 * private data for the object is guaranteed to be initialized with zeros, as
1750
 * per g_type_create_instance().
1751
 *
1752
 * Note that in C, small integer types in variable argument lists are promoted
1753
 * up to #gint or #guint as appropriate, and read back accordingly. #gint is 32
1754
 * bits on every platform on which GLib is currently supported. This means that
1755
 * you can use C expressions of type #gint with g_object_new() and properties of
1756
 * type #gint or #guint or smaller. Specifically, you can use integer literals
1757
 * with these property types.
1758
 *
1759
 * When using property types of #gint64 or #guint64, you must ensure that the
1760
 * value that you provide is 64 bit. This means that you should use a cast or
1761
 * make use of the %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
1762
 *
1763
 * Similarly, #gfloat is promoted to #gdouble, so you must ensure that the value
1764
 * you provide is a #gdouble, even for a property of type #gfloat.
1765
 *
1766
 * Returns: (transfer full) (type GObject.Object): a new instance of
1767
 *   @object_type
1768
 */
1769
gpointer
1770
g_object_new (GType    object_type,
1771
        const gchar *first_property_name,
1772
        ...)
1773
14.0M
{
1774
14.0M
  GObject *object;
1775
14.0M
  va_list var_args;
1776
  
1777
  /* short circuit for calls supplying no properties */
1778
14.0M
  if (!first_property_name)
1779
14.0M
    return g_object_new_with_properties (object_type, 0, NULL, NULL);
1780
1781
0
  va_start (var_args, first_property_name);
1782
0
  object = g_object_new_valist (object_type, first_property_name, var_args);
1783
0
  va_end (var_args);
1784
  
1785
0
  return object;
1786
14.0M
}
1787
1788
static gpointer
1789
g_object_new_with_custom_constructor (GObjectClass          *class,
1790
                                      GObjectConstructParam *params,
1791
                                      guint                  n_params)
1792
0
{
1793
0
  GObjectNotifyQueue *nqueue = NULL;
1794
0
  gboolean newly_constructed;
1795
0
  GObjectConstructParam *cparams;
1796
0
  GObject *object;
1797
0
  GValue *cvalues;
1798
0
  gint n_cparams;
1799
0
  gint cvals_used;
1800
0
  GSList *node;
1801
0
  guint i;
1802
1803
  /* If we have ->constructed() then we have to do a lot more work.
1804
   * It's possible that this is a singleton and it's also possible
1805
   * that the user's constructor() will attempt to modify the values
1806
   * that we pass in, so we'll need to allocate copies of them.
1807
   * It's also possible that the user may attempt to call
1808
   * g_object_set() from inside of their constructor, so we need to
1809
   * add ourselves to a list of objects for which that is allowed
1810
   * while their constructor() is running.
1811
   */
1812
1813
  /* Create the array of GObjectConstructParams for constructor() */
1814
0
  n_cparams = g_slist_length (class->construct_properties);
1815
0
  cparams = g_new (GObjectConstructParam, n_cparams);
1816
0
  cvalues = g_new0 (GValue, n_cparams);
1817
0
  cvals_used = 0;
1818
0
  i = 0;
1819
1820
  /* As above, we may find the value in the passed-in params list.
1821
   *
1822
   * If we have the value passed in then we can use the GValue from
1823
   * it directly because it is safe to modify.  If we use the
1824
   * default value from the class, we had better not pass that in
1825
   * and risk it being modified, so we create a new one.
1826
   * */
1827
0
  for (node = class->construct_properties; node; node = node->next)
1828
0
    {
1829
0
      GParamSpec *pspec;
1830
0
      GValue *value;
1831
0
      guint j;
1832
1833
0
      pspec = node->data;
1834
0
      value = NULL; /* to silence gcc... */
1835
1836
0
      for (j = 0; j < n_params; j++)
1837
0
        if (params[j].pspec == pspec)
1838
0
          {
1839
0
            consider_issuing_property_deprecation_warning (pspec);
1840
0
            value = params[j].value;
1841
0
            break;
1842
0
          }
1843
1844
0
      if (value == NULL)
1845
0
        {
1846
0
          value = &cvalues[cvals_used++];
1847
0
          g_value_init (value, pspec->value_type);
1848
0
          g_param_value_set_default (pspec, value);
1849
0
        }
1850
1851
0
      cparams[i].pspec = pspec;
1852
0
      cparams[i].value = value;
1853
0
      i++;
1854
0
    }
1855
1856
  /* construct object from construction parameters */
1857
0
  object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1858
  /* free construction values */
1859
0
  g_free (cparams);
1860
0
  while (cvals_used--)
1861
0
    g_value_unset (&cvalues[cvals_used]);
1862
0
  g_free (cvalues);
1863
1864
  /* There is code in the wild that relies on being able to return NULL
1865
   * from its custom constructor.  This was never a supported operation,
1866
   * but since the code is already out there...
1867
   */
1868
0
  if (object == NULL)
1869
0
    {
1870
0
      g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1871
0
                  "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1872
0
      return NULL;
1873
0
    }
1874
1875
  /* g_object_init() will have marked the object as being in-construction.
1876
   * Check if the returned object still is so marked, or if this is an
1877
   * already-existing singleton (in which case we should not do 'constructed').
1878
   */
1879
0
  newly_constructed = object_in_construction (object);
1880
0
  if (newly_constructed)
1881
0
    unset_object_in_construction (object);
1882
1883
0
  if (CLASS_HAS_PROPS (class))
1884
0
    {
1885
      /* If this object was newly_constructed then g_object_init()
1886
       * froze the queue.  We need to freeze it here in order to get
1887
       * the handle so that we can thaw it below (otherwise it will
1888
       * be frozen forever).
1889
       *
1890
       * We also want to do a freeze if we have any params to set,
1891
       * even on a non-newly_constructed object.
1892
       *
1893
       * It's possible that we have the case of non-newly created
1894
       * singleton and all of the passed-in params were construct
1895
       * properties so n_params > 0 but we will actually set no
1896
       * properties.  This is a pretty lame case to optimise, so
1897
       * just ignore it and freeze anyway.
1898
       */
1899
0
      if (newly_constructed || n_params)
1900
0
        nqueue = g_object_notify_queue_freeze (object, FALSE);
1901
1902
      /* Remember: if it was newly_constructed then g_object_init()
1903
       * already did a freeze, so we now have two.  Release one.
1904
       */
1905
0
      if (newly_constructed)
1906
0
        g_object_notify_queue_thaw (object, nqueue);
1907
0
    }
1908
1909
  /* run 'constructed' handler if there is a custom one */
1910
0
  if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1911
0
    class->constructed (object);
1912
1913
  /* set remaining properties */
1914
0
  for (i = 0; i < n_params; i++)
1915
0
    if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1916
0
      {
1917
0
        consider_issuing_property_deprecation_warning (params[i].pspec);
1918
0
        object_set_property (object, params[i].pspec, params[i].value, nqueue);
1919
0
      }
1920
1921
  /* If nqueue is non-NULL then we are frozen.  Thaw it. */
1922
0
  if (nqueue)
1923
0
    g_object_notify_queue_thaw (object, nqueue);
1924
1925
0
  return object;
1926
0
}
1927
1928
static gpointer
1929
g_object_new_internal (GObjectClass          *class,
1930
                       GObjectConstructParam *params,
1931
                       guint                  n_params)
1932
14.0M
{
1933
14.0M
  GObjectNotifyQueue *nqueue = NULL;
1934
14.0M
  GObject *object;
1935
1936
14.0M
  if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1937
0
    return g_object_new_with_custom_constructor (class, params, n_params);
1938
1939
14.0M
  object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1940
1941
14.0M
  if (CLASS_HAS_PROPS (class))
1942
9.88M
    {
1943
9.88M
      GSList *node;
1944
1945
      /* This will have been setup in g_object_init() */
1946
9.88M
      nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1947
9.88M
      g_assert (nqueue != NULL);
1948
1949
      /* We will set exactly n_construct_properties construct
1950
       * properties, but they may come from either the class default
1951
       * values or the passed-in parameter list.
1952
       */
1953
19.7M
      for (node = class->construct_properties; node; node = node->next)
1954
9.88M
        {
1955
9.88M
          const GValue *value;
1956
9.88M
          GParamSpec *pspec;
1957
9.88M
          guint j;
1958
1959
9.88M
          pspec = node->data;
1960
9.88M
          value = NULL; /* to silence gcc... */
1961
1962
9.88M
          for (j = 0; j < n_params; j++)
1963
0
            if (params[j].pspec == pspec)
1964
0
              {
1965
0
                consider_issuing_property_deprecation_warning (pspec);
1966
0
                value = params[j].value;
1967
0
                break;
1968
0
              }
1969
1970
9.88M
          if (value == NULL)
1971
9.88M
            value = g_param_spec_get_default_value (pspec);
1972
1973
9.88M
          object_set_property (object, pspec, value, nqueue);
1974
9.88M
        }
1975
9.88M
    }
1976
1977
  /* run 'constructed' handler if there is a custom one */
1978
14.0M
  if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1979
0
    class->constructed (object);
1980
1981
14.0M
  if (nqueue)
1982
9.88M
    {
1983
9.88M
      guint i;
1984
1985
      /* Set remaining properties.  The construct properties will
1986
       * already have been taken, so set only the non-construct
1987
       * ones.
1988
       */
1989
9.88M
      for (i = 0; i < n_params; i++)
1990
0
        if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1991
0
          {
1992
0
            consider_issuing_property_deprecation_warning (params[i].pspec);
1993
0
            object_set_property (object, params[i].pspec, params[i].value, nqueue);
1994
0
          }
1995
1996
9.88M
      g_object_notify_queue_thaw (object, nqueue);
1997
9.88M
    }
1998
1999
14.0M
  return object;
2000
14.0M
}
2001
2002
2003
static inline gboolean
2004
g_object_new_is_valid_property (GType                  object_type,
2005
                                GParamSpec            *pspec,
2006
                                const char            *name,
2007
                                GObjectConstructParam *params,
2008
                                guint                  n_params)
2009
0
{
2010
0
  guint i;
2011
2012
0
  if (G_UNLIKELY (pspec == NULL))
2013
0
    {
2014
0
      g_critical ("%s: object class '%s' has no property named '%s'",
2015
0
                  G_STRFUNC, g_type_name (object_type), name);
2016
0
      return FALSE;
2017
0
    }
2018
2019
0
  if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
2020
0
    {
2021
0
      g_critical ("%s: property '%s' of object class '%s' is not writable",
2022
0
                  G_STRFUNC, pspec->name, g_type_name (object_type));
2023
0
      return FALSE;
2024
0
    }
2025
2026
0
  if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2027
0
    {
2028
0
      for (i = 0; i < n_params; i++)
2029
0
        if (params[i].pspec == pspec)
2030
0
          break;
2031
0
      if (G_UNLIKELY (i != n_params))
2032
0
        {
2033
0
          g_critical ("%s: property '%s' for type '%s' cannot be set twice",
2034
0
                      G_STRFUNC, name, g_type_name (object_type));
2035
0
          return FALSE;
2036
0
        }
2037
0
    }
2038
0
  return TRUE;
2039
0
}
2040
2041
2042
/**
2043
 * g_object_new_with_properties: (skip)
2044
 * @object_type: the object type to instantiate
2045
 * @n_properties: the number of properties
2046
 * @names: (array length=n_properties): the names of each property to be set
2047
 * @values: (array length=n_properties): the values of each property to be set
2048
 *
2049
 * Creates a new instance of a #GObject subtype and sets its properties using
2050
 * the provided arrays. Both arrays must have exactly @n_properties elements,
2051
 * and the names and values correspond by index.
2052
 *
2053
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2054
 * which are not explicitly specified are set to their default values.
2055
 *
2056
 * Returns: (type GObject.Object) (transfer full): a new instance of
2057
 * @object_type
2058
 *
2059
 * Since: 2.54
2060
 */
2061
GObject *
2062
g_object_new_with_properties (GType          object_type,
2063
                              guint          n_properties,
2064
                              const char    *names[],
2065
                              const GValue   values[])
2066
14.0M
{
2067
14.0M
  GObjectClass *class, *unref_class = NULL;
2068
14.0M
  GObject *object;
2069
2070
14.0M
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2071
2072
  /* Try to avoid thrashing the ref_count if we don't need to (since
2073
   * it's a locked operation).
2074
   */
2075
14.0M
  class = g_type_class_peek_static (object_type);
2076
2077
14.0M
  if (class == NULL)
2078
151
    class = unref_class = g_type_class_ref (object_type);
2079
2080
14.0M
  if (n_properties > 0)
2081
0
    {
2082
0
      guint i, count = 0;
2083
0
      GObjectConstructParam *params;
2084
2085
0
      params = g_newa (GObjectConstructParam, n_properties);
2086
0
      for (i = 0; i < n_properties; i++)
2087
0
        {
2088
0
          GParamSpec *pspec;
2089
0
          pspec = g_param_spec_pool_lookup (pspec_pool, names[i], object_type, TRUE);
2090
0
          if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
2091
0
            continue;
2092
0
          params[count].pspec = pspec;
2093
2094
          /* Init GValue */
2095
0
          params[count].value = g_newa (GValue, 1);
2096
0
          memset (params[count].value, 0, sizeof (GValue));
2097
0
          g_value_init (params[count].value, G_VALUE_TYPE (&values[i]));
2098
2099
0
          g_value_copy (&values[i], params[count].value);
2100
0
          count++;
2101
0
        }
2102
0
      object = g_object_new_internal (class, params, count);
2103
2104
0
      while (count--)
2105
0
        g_value_unset (params[count].value);
2106
0
    }
2107
14.0M
  else
2108
14.0M
    object = g_object_new_internal (class, NULL, 0);
2109
2110
14.0M
  if (unref_class != NULL)
2111
151
    g_type_class_unref (unref_class);
2112
2113
14.0M
  return object;
2114
14.0M
}
2115
2116
/**
2117
 * g_object_newv:
2118
 * @object_type: the type id of the #GObject subtype to instantiate
2119
 * @n_parameters: the length of the @parameters array
2120
 * @parameters: (array length=n_parameters): an array of #GParameter
2121
 *
2122
 * Creates a new instance of a #GObject subtype and sets its properties.
2123
 *
2124
 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
2125
 * which are not explicitly specified are set to their default values.
2126
 *
2127
 * Returns: (type GObject.Object) (transfer full): a new instance of
2128
 * @object_type
2129
 *
2130
 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
2131
 * deprecated. See #GParameter for more information.
2132
 */
2133
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2134
gpointer
2135
g_object_newv (GType       object_type,
2136
               guint       n_parameters,
2137
               GParameter *parameters)
2138
0
{
2139
0
  GObjectClass *class, *unref_class = NULL;
2140
0
  GObject *object;
2141
2142
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2143
0
  g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
2144
2145
  /* Try to avoid thrashing the ref_count if we don't need to (since
2146
   * it's a locked operation).
2147
   */
2148
0
  class = g_type_class_peek_static (object_type);
2149
2150
0
  if (!class)
2151
0
    class = unref_class = g_type_class_ref (object_type);
2152
2153
0
  if (n_parameters)
2154
0
    {
2155
0
      GObjectConstructParam *cparams;
2156
0
      guint i, j;
2157
2158
0
      cparams = g_newa (GObjectConstructParam, n_parameters);
2159
0
      j = 0;
2160
2161
0
      for (i = 0; i < n_parameters; i++)
2162
0
        {
2163
0
          GParamSpec *pspec;
2164
2165
0
          pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
2166
0
          if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2167
0
            continue;
2168
2169
0
          cparams[j].pspec = pspec;
2170
0
          cparams[j].value = &parameters[i].value;
2171
0
          j++;
2172
0
        }
2173
2174
0
      object = g_object_new_internal (class, cparams, j);
2175
0
    }
2176
0
  else
2177
    /* Fast case: no properties passed in. */
2178
0
    object = g_object_new_internal (class, NULL, 0);
2179
2180
0
  if (unref_class)
2181
0
    g_type_class_unref (unref_class);
2182
2183
0
  return object;
2184
0
}
2185
G_GNUC_END_IGNORE_DEPRECATIONS
2186
2187
/**
2188
 * g_object_new_valist: (skip)
2189
 * @object_type: the type id of the #GObject subtype to instantiate
2190
 * @first_property_name: the name of the first property
2191
 * @var_args: the value of the first property, followed optionally by more
2192
 *  name/value pairs, followed by %NULL
2193
 *
2194
 * Creates a new instance of a #GObject subtype and sets its properties.
2195
 *
2196
 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
2197
 * which are not explicitly specified are set to their default values.
2198
 *
2199
 * Returns: a new instance of @object_type
2200
 */
2201
GObject*
2202
g_object_new_valist (GType        object_type,
2203
                     const gchar *first_property_name,
2204
                     va_list      var_args)
2205
0
{
2206
0
  GObjectClass *class, *unref_class = NULL;
2207
0
  GObject *object;
2208
2209
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2210
2211
  /* Try to avoid thrashing the ref_count if we don't need to (since
2212
   * it's a locked operation).
2213
   */
2214
0
  class = g_type_class_peek_static (object_type);
2215
2216
0
  if (!class)
2217
0
    class = unref_class = g_type_class_ref (object_type);
2218
2219
0
  if (first_property_name)
2220
0
    {
2221
0
      GObjectConstructParam params_stack[16];
2222
0
      GValue values_stack[G_N_ELEMENTS (params_stack)];
2223
0
      const gchar *name;
2224
0
      GObjectConstructParam *params = params_stack;
2225
0
      GValue *values = values_stack;
2226
0
      guint n_params = 0;
2227
0
      guint n_params_alloc = G_N_ELEMENTS (params_stack);
2228
2229
0
      name = first_property_name;
2230
2231
0
      do
2232
0
        {
2233
0
          gchar *error = NULL;
2234
0
          GParamSpec *pspec;
2235
2236
0
          pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
2237
2238
0
          if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2239
0
            break;
2240
2241
0
          if (G_UNLIKELY (n_params == n_params_alloc))
2242
0
            {
2243
0
              guint i;
2244
2245
0
              if (n_params_alloc == G_N_ELEMENTS (params_stack))
2246
0
                {
2247
0
                  n_params_alloc = G_N_ELEMENTS (params_stack) * 2u;
2248
0
                  params = g_new (GObjectConstructParam, n_params_alloc);
2249
0
                  values = g_new (GValue, n_params_alloc);
2250
0
                  memcpy (params, params_stack, sizeof (GObjectConstructParam) * n_params);
2251
0
                  memcpy (values, values_stack, sizeof (GValue) * n_params);
2252
0
                }
2253
0
              else
2254
0
                {
2255
0
                  n_params_alloc *= 2u;
2256
0
                  params = g_realloc (params, sizeof (GObjectConstructParam) * n_params_alloc);
2257
0
                  values = g_realloc (values, sizeof (GValue) * n_params_alloc);
2258
0
                }
2259
2260
0
              for (i = 0; i < n_params; i++)
2261
0
                params[i].value = &values[i];
2262
0
            }
2263
2264
0
          params[n_params].pspec = pspec;
2265
0
          params[n_params].value = &values[n_params];
2266
0
          memset (&values[n_params], 0, sizeof (GValue));
2267
2268
0
          G_VALUE_COLLECT_INIT (&values[n_params], pspec->value_type, var_args, 0, &error);
2269
2270
0
          if (error)
2271
0
            {
2272
0
              g_critical ("%s: %s", G_STRFUNC, error);
2273
0
              g_value_unset (&values[n_params]);
2274
0
              g_free (error);
2275
0
              break;
2276
0
            }
2277
2278
0
          n_params++;
2279
0
        }
2280
0
      while ((name = va_arg (var_args, const gchar *)));
2281
2282
0
      object = g_object_new_internal (class, params, n_params);
2283
2284
0
      while (n_params--)
2285
0
        g_value_unset (params[n_params].value);
2286
2287
0
      if (G_UNLIKELY (n_params_alloc != G_N_ELEMENTS (params_stack)))
2288
0
        {
2289
0
          g_free (params);
2290
0
          g_free (values);
2291
0
        }
2292
0
    }
2293
0
  else
2294
    /* Fast case: no properties passed in. */
2295
0
    object = g_object_new_internal (class, NULL, 0);
2296
2297
0
  if (unref_class)
2298
0
    g_type_class_unref (unref_class);
2299
2300
0
  return object;
2301
0
}
2302
2303
static GObject*
2304
g_object_constructor (GType                  type,
2305
          guint                  n_construct_properties,
2306
          GObjectConstructParam *construct_params)
2307
0
{
2308
0
  GObject *object;
2309
2310
  /* create object */
2311
0
  object = (GObject*) g_type_create_instance (type);
2312
  
2313
  /* set construction parameters */
2314
0
  if (n_construct_properties)
2315
0
    {
2316
0
      GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2317
      
2318
      /* set construct properties */
2319
0
      while (n_construct_properties--)
2320
0
  {
2321
0
    GValue *value = construct_params->value;
2322
0
    GParamSpec *pspec = construct_params->pspec;
2323
2324
0
    construct_params++;
2325
0
    object_set_property (object, pspec, value, nqueue);
2326
0
  }
2327
0
      g_object_notify_queue_thaw (object, nqueue);
2328
      /* the notification queue is still frozen from g_object_init(), so
2329
       * we don't need to handle it here, g_object_newv() takes
2330
       * care of that
2331
       */
2332
0
    }
2333
2334
0
  return object;
2335
0
}
2336
2337
static void
2338
g_object_constructed (GObject *object)
2339
0
{
2340
  /* empty default impl to allow unconditional upchaining */
2341
0
}
2342
2343
static inline gboolean
2344
g_object_set_is_valid_property (GObject         *object,
2345
                                GParamSpec      *pspec,
2346
                                const char      *property_name)
2347
0
{
2348
0
  if (G_UNLIKELY (pspec == NULL))
2349
0
    {
2350
0
      g_warning ("%s: object class '%s' has no property named '%s'",
2351
0
                 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2352
0
      return FALSE;
2353
0
    }
2354
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
2355
0
    {
2356
0
      g_warning ("%s: property '%s' of object class '%s' is not writable",
2357
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2358
0
      return FALSE;
2359
0
    }
2360
0
  if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
2361
0
    {
2362
0
      g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2363
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2364
0
      return FALSE;
2365
0
    }
2366
0
  return TRUE;
2367
0
}
2368
2369
/**
2370
 * g_object_setv: (skip)
2371
 * @object: a #GObject
2372
 * @n_properties: the number of properties
2373
 * @names: (array length=n_properties): the names of each property to be set
2374
 * @values: (array length=n_properties): the values of each property to be set
2375
 *
2376
 * Sets @n_properties properties for an @object.
2377
 * Properties to be set will be taken from @values. All properties must be
2378
 * valid. Warnings will be emitted and undefined behaviour may result if invalid
2379
 * properties are passed in.
2380
 *
2381
 * Since: 2.54
2382
 */
2383
void
2384
g_object_setv (GObject       *object,
2385
               guint          n_properties,
2386
               const gchar   *names[],
2387
               const GValue   values[])
2388
0
{
2389
0
  guint i;
2390
0
  GObjectNotifyQueue *nqueue;
2391
0
  GParamSpec *pspec;
2392
0
  GType obj_type;
2393
2394
0
  g_return_if_fail (G_IS_OBJECT (object));
2395
2396
0
  if (n_properties == 0)
2397
0
    return;
2398
2399
0
  g_object_ref (object);
2400
0
  obj_type = G_OBJECT_TYPE (object);
2401
0
  nqueue = g_object_notify_queue_freeze (object, FALSE);
2402
0
  for (i = 0; i < n_properties; i++)
2403
0
    {
2404
0
      pspec = g_param_spec_pool_lookup (pspec_pool, names[i], obj_type, TRUE);
2405
2406
0
      if (!g_object_set_is_valid_property (object, pspec, names[i]))
2407
0
        break;
2408
2409
0
      consider_issuing_property_deprecation_warning (pspec);
2410
0
      object_set_property (object, pspec, &values[i], nqueue);
2411
0
    }
2412
2413
0
  g_object_notify_queue_thaw (object, nqueue);
2414
0
  g_object_unref (object);
2415
0
}
2416
2417
/**
2418
 * g_object_set_valist: (skip)
2419
 * @object: a #GObject
2420
 * @first_property_name: name of the first property to set
2421
 * @var_args: value for the first property, followed optionally by more
2422
 *  name/value pairs, followed by %NULL
2423
 *
2424
 * Sets properties on an object.
2425
 */
2426
void
2427
g_object_set_valist (GObject   *object,
2428
         const gchar *first_property_name,
2429
         va_list    var_args)
2430
0
{
2431
0
  GObjectNotifyQueue *nqueue;
2432
0
  const gchar *name;
2433
  
2434
0
  g_return_if_fail (G_IS_OBJECT (object));
2435
  
2436
0
  g_object_ref (object);
2437
0
  nqueue = g_object_notify_queue_freeze (object, FALSE);
2438
  
2439
0
  name = first_property_name;
2440
0
  while (name)
2441
0
    {
2442
0
      GValue value = G_VALUE_INIT;
2443
0
      GParamSpec *pspec;
2444
0
      gchar *error = NULL;
2445
      
2446
0
      pspec = g_param_spec_pool_lookup (pspec_pool,
2447
0
          name,
2448
0
          G_OBJECT_TYPE (object),
2449
0
          TRUE);
2450
2451
0
      if (!g_object_set_is_valid_property (object, pspec, name))
2452
0
        break;
2453
2454
0
      G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2455
0
          0, &error);
2456
0
      if (error)
2457
0
  {
2458
0
    g_warning ("%s: %s", G_STRFUNC, error);
2459
0
    g_free (error);
2460
0
          g_value_unset (&value);
2461
0
    break;
2462
0
  }
2463
2464
0
      consider_issuing_property_deprecation_warning (pspec);
2465
0
      object_set_property (object, pspec, &value, nqueue);
2466
0
      g_value_unset (&value);
2467
2468
0
      name = va_arg (var_args, gchar*);
2469
0
    }
2470
2471
0
  g_object_notify_queue_thaw (object, nqueue);
2472
0
  g_object_unref (object);
2473
0
}
2474
2475
static inline gboolean
2476
g_object_get_is_valid_property (GObject          *object,
2477
                                GParamSpec       *pspec,
2478
                                const char       *property_name)
2479
0
{
2480
0
  if (G_UNLIKELY (pspec == NULL))
2481
0
    {
2482
0
      g_warning ("%s: object class '%s' has no property named '%s'",
2483
0
                 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2484
0
      return FALSE;
2485
0
    }
2486
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
2487
0
    {
2488
0
      g_warning ("%s: property '%s' of object class '%s' is not readable",
2489
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2490
0
      return FALSE;
2491
0
    }
2492
0
  return TRUE;
2493
0
}
2494
2495
/**
2496
 * g_object_getv:
2497
 * @object: a #GObject
2498
 * @n_properties: the number of properties
2499
 * @names: (array length=n_properties): the names of each property to get
2500
 * @values: (array length=n_properties): the values of each property to get
2501
 *
2502
 * Gets @n_properties properties for an @object.
2503
 * Obtained properties will be set to @values. All properties must be valid.
2504
 * Warnings will be emitted and undefined behaviour may result if invalid
2505
 * properties are passed in.
2506
 *
2507
 * Since: 2.54
2508
 */
2509
void
2510
g_object_getv (GObject      *object,
2511
               guint         n_properties,
2512
               const gchar  *names[],
2513
               GValue        values[])
2514
0
{
2515
0
  guint i;
2516
0
  GParamSpec *pspec;
2517
0
  GType obj_type;
2518
2519
0
  g_return_if_fail (G_IS_OBJECT (object));
2520
2521
0
  if (n_properties == 0)
2522
0
    return;
2523
2524
0
  g_object_ref (object);
2525
2526
0
  obj_type = G_OBJECT_TYPE (object);
2527
0
  for (i = 0; i < n_properties; i++)
2528
0
    {
2529
0
      pspec = g_param_spec_pool_lookup (pspec_pool,
2530
0
                names[i],
2531
0
                obj_type,
2532
0
                TRUE);
2533
0
      if (!g_object_get_is_valid_property (object, pspec, names[i]))
2534
0
        break;
2535
2536
0
      memset (&values[i], 0, sizeof (GValue));
2537
0
      g_value_init (&values[i], pspec->value_type);
2538
0
      object_get_property (object, pspec, &values[i]);
2539
0
    }
2540
0
  g_object_unref (object);
2541
0
}
2542
2543
/**
2544
 * g_object_get_valist: (skip)
2545
 * @object: a #GObject
2546
 * @first_property_name: name of the first property to get
2547
 * @var_args: return location for the first property, followed optionally by more
2548
 *  name/return location pairs, followed by %NULL
2549
 *
2550
 * Gets properties of an object.
2551
 *
2552
 * In general, a copy is made of the property contents and the caller
2553
 * is responsible for freeing the memory in the appropriate manner for
2554
 * the type, for instance by calling g_free() or g_object_unref().
2555
 *
2556
 * See g_object_get().
2557
 */
2558
void
2559
g_object_get_valist (GObject   *object,
2560
         const gchar *first_property_name,
2561
         va_list    var_args)
2562
0
{
2563
0
  const gchar *name;
2564
  
2565
0
  g_return_if_fail (G_IS_OBJECT (object));
2566
  
2567
0
  g_object_ref (object);
2568
  
2569
0
  name = first_property_name;
2570
  
2571
0
  while (name)
2572
0
    {
2573
0
      GValue value = G_VALUE_INIT;
2574
0
      GParamSpec *pspec;
2575
0
      gchar *error;
2576
      
2577
0
      pspec = g_param_spec_pool_lookup (pspec_pool,
2578
0
          name,
2579
0
          G_OBJECT_TYPE (object),
2580
0
          TRUE);
2581
2582
0
      if (!g_object_get_is_valid_property (object, pspec, name))
2583
0
        break;
2584
      
2585
0
      g_value_init (&value, pspec->value_type);
2586
      
2587
0
      object_get_property (object, pspec, &value);
2588
      
2589
0
      G_VALUE_LCOPY (&value, var_args, 0, &error);
2590
0
      if (error)
2591
0
  {
2592
0
    g_warning ("%s: %s", G_STRFUNC, error);
2593
0
    g_free (error);
2594
0
    g_value_unset (&value);
2595
0
    break;
2596
0
  }
2597
      
2598
0
      g_value_unset (&value);
2599
      
2600
0
      name = va_arg (var_args, gchar*);
2601
0
    }
2602
  
2603
0
  g_object_unref (object);
2604
0
}
2605
2606
/**
2607
 * g_object_set: (skip)
2608
 * @object: (type GObject.Object): a #GObject
2609
 * @first_property_name: name of the first property to set
2610
 * @...: value for the first property, followed optionally by more
2611
 *  name/value pairs, followed by %NULL
2612
 *
2613
 * Sets properties on an object.
2614
 *
2615
 * The same caveats about passing integer literals as varargs apply as with
2616
 * g_object_new(). In particular, any integer literals set as the values for
2617
 * properties of type #gint64 or #guint64 must be 64 bits wide, using the
2618
 * %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
2619
 *
2620
 * Note that the "notify" signals are queued and only emitted (in
2621
 * reverse order) after all properties have been set. See
2622
 * g_object_freeze_notify().
2623
 */
2624
void
2625
g_object_set (gpointer     _object,
2626
        const gchar *first_property_name,
2627
        ...)
2628
0
{
2629
0
  GObject *object = _object;
2630
0
  va_list var_args;
2631
  
2632
0
  g_return_if_fail (G_IS_OBJECT (object));
2633
  
2634
0
  va_start (var_args, first_property_name);
2635
0
  g_object_set_valist (object, first_property_name, var_args);
2636
0
  va_end (var_args);
2637
0
}
2638
2639
/**
2640
 * g_object_get: (skip)
2641
 * @object: (type GObject.Object): a #GObject
2642
 * @first_property_name: name of the first property to get
2643
 * @...: return location for the first property, followed optionally by more
2644
 *  name/return location pairs, followed by %NULL
2645
 *
2646
 * Gets properties of an object.
2647
 *
2648
 * In general, a copy is made of the property contents and the caller
2649
 * is responsible for freeing the memory in the appropriate manner for
2650
 * the type, for instance by calling g_free() or g_object_unref().
2651
 *
2652
 * Here is an example of using g_object_get() to get the contents
2653
 * of three properties: an integer, a string and an object:
2654
 * |[<!-- language="C" --> 
2655
 *  gint intval;
2656
 *  guint64 uint64val;
2657
 *  gchar *strval;
2658
 *  GObject *objval;
2659
 *
2660
 *  g_object_get (my_object,
2661
 *                "int-property", &intval,
2662
 *                "uint64-property", &uint64val,
2663
 *                "str-property", &strval,
2664
 *                "obj-property", &objval,
2665
 *                NULL);
2666
 *
2667
 *  // Do something with intval, uint64val, strval, objval
2668
 *
2669
 *  g_free (strval);
2670
 *  g_object_unref (objval);
2671
 * ]|
2672
 */
2673
void
2674
g_object_get (gpointer     _object,
2675
        const gchar *first_property_name,
2676
        ...)
2677
0
{
2678
0
  GObject *object = _object;
2679
0
  va_list var_args;
2680
  
2681
0
  g_return_if_fail (G_IS_OBJECT (object));
2682
  
2683
0
  va_start (var_args, first_property_name);
2684
0
  g_object_get_valist (object, first_property_name, var_args);
2685
0
  va_end (var_args);
2686
0
}
2687
2688
/**
2689
 * g_object_set_property:
2690
 * @object: a #GObject
2691
 * @property_name: the name of the property to set
2692
 * @value: the value
2693
 *
2694
 * Sets a property on an object.
2695
 */
2696
void
2697
g_object_set_property (GObject      *object,
2698
           const gchar  *property_name,
2699
           const GValue *value)
2700
0
{
2701
0
  g_object_setv (object, 1, &property_name, value);
2702
0
}
2703
2704
/**
2705
 * g_object_get_property:
2706
 * @object: a #GObject
2707
 * @property_name: the name of the property to get
2708
 * @value: return location for the property value
2709
 *
2710
 * Gets a property of an object.
2711
 *
2712
 * The @value can be:
2713
 *
2714
 *  - an empty #GValue initialized by %G_VALUE_INIT, which will be
2715
 *    automatically initialized with the expected type of the property
2716
 *    (since GLib 2.60)
2717
 *  - a #GValue initialized with the expected type of the property
2718
 *  - a #GValue initialized with a type to which the expected type
2719
 *    of the property can be transformed
2720
 *
2721
 * In general, a copy is made of the property contents and the caller is
2722
 * responsible for freeing the memory by calling g_value_unset().
2723
 *
2724
 * Note that g_object_get_property() is really intended for language
2725
 * bindings, g_object_get() is much more convenient for C programming.
2726
 */
2727
void
2728
g_object_get_property (GObject     *object,
2729
           const gchar *property_name,
2730
           GValue    *value)
2731
0
{
2732
0
  GParamSpec *pspec;
2733
  
2734
0
  g_return_if_fail (G_IS_OBJECT (object));
2735
0
  g_return_if_fail (property_name != NULL);
2736
0
  g_return_if_fail (value != NULL);
2737
  
2738
0
  g_object_ref (object);
2739
  
2740
0
  pspec = g_param_spec_pool_lookup (pspec_pool,
2741
0
            property_name,
2742
0
            G_OBJECT_TYPE (object),
2743
0
            TRUE);
2744
2745
0
  if (g_object_get_is_valid_property (object, pspec, property_name))
2746
0
    {
2747
0
      GValue *prop_value, tmp_value = G_VALUE_INIT;
2748
      
2749
0
      if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
2750
0
        {
2751
          /* zero-initialized value */
2752
0
          g_value_init (value, pspec->value_type);
2753
0
          prop_value = value;
2754
0
        }
2755
0
      else if (G_VALUE_TYPE (value) == pspec->value_type)
2756
0
        {
2757
          /* auto-conversion of the callers value type */
2758
0
          g_value_reset (value);
2759
0
          prop_value = value;
2760
0
        }
2761
0
      else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2762
0
        {
2763
0
          g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2764
0
                     G_STRFUNC, pspec->name,
2765
0
                     g_type_name (pspec->value_type),
2766
0
                     G_VALUE_TYPE_NAME (value));
2767
0
          g_object_unref (object);
2768
0
          return;
2769
0
        }
2770
0
      else
2771
0
        {
2772
0
          g_value_init (&tmp_value, pspec->value_type);
2773
0
          prop_value = &tmp_value;
2774
0
        }
2775
0
      object_get_property (object, pspec, prop_value);
2776
0
      if (prop_value != value)
2777
0
        {
2778
0
          g_value_transform (prop_value, value);
2779
0
          g_value_unset (&tmp_value);
2780
0
        }
2781
0
    }
2782
  
2783
0
  g_object_unref (object);
2784
0
}
2785
2786
/**
2787
 * g_object_connect: (skip)
2788
 * @object: (type GObject.Object): a #GObject
2789
 * @signal_spec: the spec for the first signal
2790
 * @...: #GCallback for the first signal, followed by data for the
2791
 *       first signal, followed optionally by more signal
2792
 *       spec/callback/data triples, followed by %NULL
2793
 *
2794
 * A convenience function to connect multiple signals at once.
2795
 *
2796
 * The signal specs expected by this function have the form
2797
 * "modifier::signal_name", where modifier can be one of the following:
2798
 * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2799
 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2800
 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2801
 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2802
 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2803
 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2804
 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2805
 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2806
 *
2807
 * |[<!-- language="C" --> 
2808
 *   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2809
 *               "type", GTK_WINDOW_POPUP,
2810
 *               "child", menu,
2811
 *               NULL),
2812
 *             "signal::event", gtk_menu_window_event, menu,
2813
 *             "signal::size_request", gtk_menu_window_size_request, menu,
2814
 *             "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2815
 *             NULL);
2816
 * ]|
2817
 *
2818
 * Returns: (transfer none) (type GObject.Object): @object
2819
 */
2820
gpointer
2821
g_object_connect (gpointer     _object,
2822
      const gchar *signal_spec,
2823
      ...)
2824
0
{
2825
0
  GObject *object = _object;
2826
0
  va_list var_args;
2827
2828
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2829
0
  g_return_val_if_fail (object->ref_count > 0, object);
2830
2831
0
  va_start (var_args, signal_spec);
2832
0
  while (signal_spec)
2833
0
    {
2834
0
      GCallback callback = va_arg (var_args, GCallback);
2835
0
      gpointer data = va_arg (var_args, gpointer);
2836
2837
0
      if (strncmp (signal_spec, "signal::", 8) == 0)
2838
0
  g_signal_connect_data (object, signal_spec + 8,
2839
0
             callback, data, NULL,
2840
0
             0);
2841
0
      else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2842
0
               strncmp (signal_spec, "object-signal::", 15) == 0)
2843
0
  g_signal_connect_object (object, signal_spec + 15,
2844
0
         callback, data,
2845
0
         0);
2846
0
      else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2847
0
               strncmp (signal_spec, "swapped-signal::", 16) == 0)
2848
0
  g_signal_connect_data (object, signal_spec + 16,
2849
0
             callback, data, NULL,
2850
0
             G_CONNECT_SWAPPED);
2851
0
      else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2852
0
               strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2853
0
  g_signal_connect_object (object, signal_spec + 23,
2854
0
         callback, data,
2855
0
         G_CONNECT_SWAPPED);
2856
0
      else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2857
0
               strncmp (signal_spec, "signal-after::", 14) == 0)
2858
0
  g_signal_connect_data (object, signal_spec + 14,
2859
0
             callback, data, NULL,
2860
0
             G_CONNECT_AFTER);
2861
0
      else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2862
0
               strncmp (signal_spec, "object-signal-after::", 21) == 0)
2863
0
  g_signal_connect_object (object, signal_spec + 21,
2864
0
         callback, data,
2865
0
         G_CONNECT_AFTER);
2866
0
      else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2867
0
               strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2868
0
  g_signal_connect_data (object, signal_spec + 22,
2869
0
             callback, data, NULL,
2870
0
             G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2871
0
      else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2872
0
               strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2873
0
  g_signal_connect_object (object, signal_spec + 29,
2874
0
         callback, data,
2875
0
         G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2876
0
      else
2877
0
  {
2878
0
    g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2879
0
    break;
2880
0
  }
2881
0
      signal_spec = va_arg (var_args, gchar*);
2882
0
    }
2883
0
  va_end (var_args);
2884
2885
0
  return object;
2886
0
}
2887
2888
/**
2889
 * g_object_disconnect: (skip)
2890
 * @object: (type GObject.Object): a #GObject
2891
 * @signal_spec: the spec for the first signal
2892
 * @...: #GCallback for the first signal, followed by data for the first signal,
2893
 *  followed optionally by more signal spec/callback/data triples,
2894
 *  followed by %NULL
2895
 *
2896
 * A convenience function to disconnect multiple signals at once.
2897
 *
2898
 * The signal specs expected by this function have the form
2899
 * "any_signal", which means to disconnect any signal with matching
2900
 * callback and data, or "any_signal::signal_name", which only
2901
 * disconnects the signal named "signal_name".
2902
 */
2903
void
2904
g_object_disconnect (gpointer     _object,
2905
         const gchar *signal_spec,
2906
         ...)
2907
0
{
2908
0
  GObject *object = _object;
2909
0
  va_list var_args;
2910
2911
0
  g_return_if_fail (G_IS_OBJECT (object));
2912
0
  g_return_if_fail (object->ref_count > 0);
2913
2914
0
  va_start (var_args, signal_spec);
2915
0
  while (signal_spec)
2916
0
    {
2917
0
      GCallback callback = va_arg (var_args, GCallback);
2918
0
      gpointer data = va_arg (var_args, gpointer);
2919
0
      guint sid = 0, detail = 0, mask = 0;
2920
2921
0
      if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2922
0
          strncmp (signal_spec, "any-signal::", 12) == 0)
2923
0
  {
2924
0
    signal_spec += 12;
2925
0
    mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2926
0
  }
2927
0
      else if (strcmp (signal_spec, "any_signal") == 0 ||
2928
0
               strcmp (signal_spec, "any-signal") == 0)
2929
0
  {
2930
0
    signal_spec += 10;
2931
0
    mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2932
0
  }
2933
0
      else
2934
0
  {
2935
0
    g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2936
0
    break;
2937
0
  }
2938
2939
0
      if ((mask & G_SIGNAL_MATCH_ID) &&
2940
0
    !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2941
0
  g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2942
0
      else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2943
0
                  sid, detail,
2944
0
                  NULL, (gpointer)callback, data))
2945
0
  g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2946
0
      signal_spec = va_arg (var_args, gchar*);
2947
0
    }
2948
0
  va_end (var_args);
2949
0
}
2950
2951
typedef struct {
2952
  GObject *object;
2953
  guint n_weak_refs;
2954
  struct {
2955
    GWeakNotify notify;
2956
    gpointer    data;
2957
  } weak_refs[1];  /* flexible array */
2958
} WeakRefStack;
2959
2960
static void
2961
weak_refs_notify (gpointer data)
2962
76.6k
{
2963
76.6k
  WeakRefStack *wstack = data;
2964
76.6k
  guint i;
2965
2966
761k
  for (i = 0; i < wstack->n_weak_refs; i++)
2967
684k
    wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2968
76.6k
  g_free (wstack);
2969
76.6k
}
2970
2971
/**
2972
 * g_object_weak_ref: (skip)
2973
 * @object: #GObject to reference weakly
2974
 * @notify: callback to invoke before the object is freed
2975
 * @data: extra data to pass to notify
2976
 *
2977
 * Adds a weak reference callback to an object. Weak references are
2978
 * used for notification when an object is disposed. They are called
2979
 * "weak references" because they allow you to safely hold a pointer
2980
 * to an object without calling g_object_ref() (g_object_ref() adds a
2981
 * strong reference, that is, forces the object to stay alive).
2982
 *
2983
 * Note that the weak references created by this method are not
2984
 * thread-safe: they cannot safely be used in one thread if the
2985
 * object's last g_object_unref() might happen in another thread.
2986
 * Use #GWeakRef if thread-safety is required.
2987
 */
2988
void
2989
g_object_weak_ref (GObject    *object,
2990
       GWeakNotify notify,
2991
       gpointer    data)
2992
847k
{
2993
847k
  WeakRefStack *wstack;
2994
847k
  guint i;
2995
  
2996
847k
  g_return_if_fail (G_IS_OBJECT (object));
2997
847k
  g_return_if_fail (notify != NULL);
2998
847k
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
2999
3000
847k
  G_LOCK (weak_refs_mutex);
3001
847k
  wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
3002
847k
  if (wstack)
3003
771k
    {
3004
771k
      i = wstack->n_weak_refs++;
3005
771k
      wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
3006
771k
    }
3007
76.6k
  else
3008
76.6k
    {
3009
76.6k
      wstack = g_renew (WeakRefStack, NULL, 1);
3010
76.6k
      wstack->object = object;
3011
76.6k
      wstack->n_weak_refs = 1;
3012
76.6k
      i = 0;
3013
76.6k
    }
3014
847k
  wstack->weak_refs[i].notify = notify;
3015
847k
  wstack->weak_refs[i].data = data;
3016
847k
  g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
3017
847k
  G_UNLOCK (weak_refs_mutex);
3018
847k
}
3019
3020
/**
3021
 * g_object_weak_unref: (skip)
3022
 * @object: #GObject to remove a weak reference from
3023
 * @notify: callback to search for
3024
 * @data: data to search for
3025
 *
3026
 * Removes a weak reference callback to an object.
3027
 */
3028
void
3029
g_object_weak_unref (GObject    *object,
3030
         GWeakNotify notify,
3031
         gpointer    data)
3032
162k
{
3033
162k
  WeakRefStack *wstack;
3034
162k
  gboolean found_one = FALSE;
3035
3036
162k
  g_return_if_fail (G_IS_OBJECT (object));
3037
162k
  g_return_if_fail (notify != NULL);
3038
3039
162k
  G_LOCK (weak_refs_mutex);
3040
162k
  wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
3041
162k
  if (wstack)
3042
162k
    {
3043
162k
      guint i;
3044
3045
17.3M
      for (i = 0; i < wstack->n_weak_refs; i++)
3046
17.3M
  if (wstack->weak_refs[i].notify == notify &&
3047
17.3M
      wstack->weak_refs[i].data == data)
3048
162k
    {
3049
162k
      found_one = TRUE;
3050
162k
      wstack->n_weak_refs -= 1;
3051
162k
      if (i != wstack->n_weak_refs)
3052
89.5k
        wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
3053
3054
162k
      break;
3055
162k
    }
3056
162k
    }
3057
162k
  G_UNLOCK (weak_refs_mutex);
3058
162k
  if (!found_one)
3059
0
    g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
3060
162k
}
3061
3062
/**
3063
 * g_object_add_weak_pointer: (skip)
3064
 * @object: The object that should be weak referenced.
3065
 * @weak_pointer_location: (inout) (not optional): The memory address
3066
 *    of a pointer.
3067
 *
3068
 * Adds a weak reference from weak_pointer to @object to indicate that
3069
 * the pointer located at @weak_pointer_location is only valid during
3070
 * the lifetime of @object. When the @object is finalized,
3071
 * @weak_pointer will be set to %NULL.
3072
 *
3073
 * Note that as with g_object_weak_ref(), the weak references created by
3074
 * this method are not thread-safe: they cannot safely be used in one
3075
 * thread if the object's last g_object_unref() might happen in another
3076
 * thread. Use #GWeakRef if thread-safety is required.
3077
 */
3078
void
3079
g_object_add_weak_pointer (GObject  *object, 
3080
                           gpointer *weak_pointer_location)
3081
847k
{
3082
847k
  g_return_if_fail (G_IS_OBJECT (object));
3083
847k
  g_return_if_fail (weak_pointer_location != NULL);
3084
3085
847k
  g_object_weak_ref (object, 
3086
847k
                     (GWeakNotify) g_nullify_pointer, 
3087
847k
                     weak_pointer_location);
3088
847k
}
3089
3090
/**
3091
 * g_object_remove_weak_pointer: (skip)
3092
 * @object: The object that is weak referenced.
3093
 * @weak_pointer_location: (inout) (not optional): The memory address
3094
 *    of a pointer.
3095
 *
3096
 * Removes a weak reference from @object that was previously added
3097
 * using g_object_add_weak_pointer(). The @weak_pointer_location has
3098
 * to match the one used with g_object_add_weak_pointer().
3099
 */
3100
void
3101
g_object_remove_weak_pointer (GObject  *object, 
3102
                              gpointer *weak_pointer_location)
3103
162k
{
3104
162k
  g_return_if_fail (G_IS_OBJECT (object));
3105
162k
  g_return_if_fail (weak_pointer_location != NULL);
3106
3107
162k
  g_object_weak_unref (object, 
3108
162k
                       (GWeakNotify) g_nullify_pointer, 
3109
162k
                       weak_pointer_location);
3110
162k
}
3111
3112
static guint
3113
object_floating_flag_handler (GObject        *object,
3114
                              gint            job)
3115
0
{
3116
0
  switch (job)
3117
0
    {
3118
0
      gpointer oldvalue;
3119
0
    case +1:    /* force floating if possible */
3120
0
      do
3121
0
        oldvalue = g_atomic_pointer_get (&object->qdata);
3122
0
      while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
3123
0
                                                     (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
3124
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
3125
0
    case -1:    /* sink if possible */
3126
0
      do
3127
0
        oldvalue = g_atomic_pointer_get (&object->qdata);
3128
0
      while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
3129
0
                                                     (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
3130
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
3131
0
    default:    /* check floating */
3132
0
      return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
3133
0
    }
3134
0
}
3135
3136
/**
3137
 * g_object_is_floating:
3138
 * @object: (type GObject.Object): a #GObject
3139
 *
3140
 * Checks whether @object has a [floating][floating-ref] reference.
3141
 *
3142
 * Since: 2.10
3143
 *
3144
 * Returns: %TRUE if @object has a floating reference
3145
 */
3146
gboolean
3147
g_object_is_floating (gpointer _object)
3148
0
{
3149
0
  GObject *object = _object;
3150
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3151
0
  return floating_flag_handler (object, 0);
3152
0
}
3153
3154
/**
3155
 * g_object_ref_sink:
3156
 * @object: (type GObject.Object): a #GObject
3157
 *
3158
 * Increase the reference count of @object, and possibly remove the
3159
 * [floating][floating-ref] reference, if @object has a floating reference.
3160
 *
3161
 * In other words, if the object is floating, then this call "assumes
3162
 * ownership" of the floating reference, converting it to a normal
3163
 * reference by clearing the floating flag while leaving the reference
3164
 * count unchanged.  If the object is not floating, then this call
3165
 * adds a new normal reference increasing the reference count by one.
3166
 *
3167
 * Since GLib 2.56, the type of @object will be propagated to the return type
3168
 * under the same conditions as for g_object_ref().
3169
 *
3170
 * Since: 2.10
3171
 *
3172
 * Returns: (type GObject.Object) (transfer none): @object
3173
 */
3174
gpointer
3175
(g_object_ref_sink) (gpointer _object)
3176
0
{
3177
0
  GObject *object = _object;
3178
0
  gboolean was_floating;
3179
0
  g_return_val_if_fail (G_IS_OBJECT (object), object);
3180
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object);
3181
0
  g_object_ref (object);
3182
0
  was_floating = floating_flag_handler (object, -1);
3183
0
  if (was_floating)
3184
0
    g_object_unref (object);
3185
0
  return object;
3186
0
}
3187
3188
/**
3189
 * g_object_force_floating:
3190
 * @object: a #GObject
3191
 *
3192
 * This function is intended for #GObject implementations to re-enforce
3193
 * a [floating][floating-ref] object reference. Doing this is seldom
3194
 * required: all #GInitiallyUnowneds are created with a floating reference
3195
 * which usually just needs to be sunken by calling g_object_ref_sink().
3196
 *
3197
 * Since: 2.10
3198
 */
3199
void
3200
g_object_force_floating (GObject *object)
3201
0
{
3202
0
  g_return_if_fail (G_IS_OBJECT (object));
3203
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3204
3205
0
  floating_flag_handler (object, +1);
3206
0
}
3207
3208
typedef struct {
3209
  GObject *object;
3210
  guint n_toggle_refs;
3211
  struct {
3212
    GToggleNotify notify;
3213
    gpointer    data;
3214
  } toggle_refs[1];  /* flexible array */
3215
} ToggleRefStack;
3216
3217
static void
3218
toggle_refs_notify (GObject *object,
3219
        gboolean is_last_ref)
3220
0
{
3221
0
  ToggleRefStack tstack, *tstackptr;
3222
3223
0
  G_LOCK (toggle_refs_mutex);
3224
0
  tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3225
0
  tstack = *tstackptr;
3226
0
  G_UNLOCK (toggle_refs_mutex);
3227
3228
  /* Reentrancy here is not as tricky as it seems, because a toggle reference
3229
   * will only be notified when there is exactly one of them.
3230
   */
3231
0
  g_assert (tstack.n_toggle_refs == 1);
3232
0
  tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
3233
0
}
3234
3235
/**
3236
 * g_object_add_toggle_ref: (skip)
3237
 * @object: a #GObject
3238
 * @notify: a function to call when this reference is the
3239
 *  last reference to the object, or is no longer
3240
 *  the last reference.
3241
 * @data: data to pass to @notify
3242
 *
3243
 * Increases the reference count of the object by one and sets a
3244
 * callback to be called when all other references to the object are
3245
 * dropped, or when this is already the last reference to the object
3246
 * and another reference is established.
3247
 *
3248
 * This functionality is intended for binding @object to a proxy
3249
 * object managed by another memory manager. This is done with two
3250
 * paired references: the strong reference added by
3251
 * g_object_add_toggle_ref() and a reverse reference to the proxy
3252
 * object which is either a strong reference or weak reference.
3253
 *
3254
 * The setup is that when there are no other references to @object,
3255
 * only a weak reference is held in the reverse direction from @object
3256
 * to the proxy object, but when there are other references held to
3257
 * @object, a strong reference is held. The @notify callback is called
3258
 * when the reference from @object to the proxy object should be
3259
 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
3260
 * (@is_last_ref false).
3261
 *
3262
 * Since a (normal) reference must be held to the object before
3263
 * calling g_object_add_toggle_ref(), the initial state of the reverse
3264
 * link is always strong.
3265
 *
3266
 * Multiple toggle references may be added to the same gobject,
3267
 * however if there are multiple toggle references to an object, none
3268
 * of them will ever be notified until all but one are removed.  For
3269
 * this reason, you should only ever use a toggle reference if there
3270
 * is important state in the proxy object.
3271
 *
3272
 * Since: 2.8
3273
 */
3274
void
3275
g_object_add_toggle_ref (GObject       *object,
3276
       GToggleNotify  notify,
3277
       gpointer       data)
3278
0
{
3279
0
  ToggleRefStack *tstack;
3280
0
  guint i;
3281
  
3282
0
  g_return_if_fail (G_IS_OBJECT (object));
3283
0
  g_return_if_fail (notify != NULL);
3284
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3285
3286
0
  g_object_ref (object);
3287
3288
0
  G_LOCK (toggle_refs_mutex);
3289
0
  tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
3290
0
  if (tstack)
3291
0
    {
3292
0
      i = tstack->n_toggle_refs++;
3293
      /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
3294
       * in tstate->toggle_refs */
3295
0
      tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
3296
0
    }
3297
0
  else
3298
0
    {
3299
0
      tstack = g_renew (ToggleRefStack, NULL, 1);
3300
0
      tstack->object = object;
3301
0
      tstack->n_toggle_refs = 1;
3302
0
      i = 0;
3303
0
    }
3304
3305
  /* Set a flag for fast lookup after adding the first toggle reference */
3306
0
  if (tstack->n_toggle_refs == 1)
3307
0
    g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3308
  
3309
0
  tstack->toggle_refs[i].notify = notify;
3310
0
  tstack->toggle_refs[i].data = data;
3311
0
  g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
3312
0
             (GDestroyNotify)g_free);
3313
0
  G_UNLOCK (toggle_refs_mutex);
3314
0
}
3315
3316
/**
3317
 * g_object_remove_toggle_ref: (skip)
3318
 * @object: a #GObject
3319
 * @notify: a function to call when this reference is the
3320
 *  last reference to the object, or is no longer
3321
 *  the last reference.
3322
 * @data: data to pass to @notify
3323
 *
3324
 * Removes a reference added with g_object_add_toggle_ref(). The
3325
 * reference count of the object is decreased by one.
3326
 *
3327
 * Since: 2.8
3328
 */
3329
void
3330
g_object_remove_toggle_ref (GObject       *object,
3331
          GToggleNotify  notify,
3332
          gpointer       data)
3333
0
{
3334
0
  ToggleRefStack *tstack;
3335
0
  gboolean found_one = FALSE;
3336
3337
0
  g_return_if_fail (G_IS_OBJECT (object));
3338
0
  g_return_if_fail (notify != NULL);
3339
3340
0
  G_LOCK (toggle_refs_mutex);
3341
0
  tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3342
0
  if (tstack)
3343
0
    {
3344
0
      guint i;
3345
3346
0
      for (i = 0; i < tstack->n_toggle_refs; i++)
3347
0
  if (tstack->toggle_refs[i].notify == notify &&
3348
0
      tstack->toggle_refs[i].data == data)
3349
0
    {
3350
0
      found_one = TRUE;
3351
0
      tstack->n_toggle_refs -= 1;
3352
0
      if (i != tstack->n_toggle_refs)
3353
0
        tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3354
3355
0
      if (tstack->n_toggle_refs == 0)
3356
0
        g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3357
3358
0
      break;
3359
0
    }
3360
0
    }
3361
0
  G_UNLOCK (toggle_refs_mutex);
3362
3363
0
  if (found_one)
3364
0
    g_object_unref (object);
3365
0
  else
3366
0
    g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3367
0
}
3368
3369
/**
3370
 * g_object_ref:
3371
 * @object: (type GObject.Object): a #GObject
3372
 *
3373
 * Increases the reference count of @object.
3374
 *
3375
 * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type
3376
 * of @object will be propagated to the return type (using the GCC typeof()
3377
 * extension), so any casting the caller needs to do on the return type must be
3378
 * explicit.
3379
 *
3380
 * Returns: (type GObject.Object) (transfer none): the same @object
3381
 */
3382
gpointer
3383
(g_object_ref) (gpointer _object)
3384
5.93M
{
3385
5.93M
  GObject *object = _object;
3386
5.93M
  gint old_val;
3387
5.93M
  gboolean object_already_finalized;
3388
3389
5.93M
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3390
  
3391
5.93M
  old_val = g_atomic_int_add (&object->ref_count, 1);
3392
5.93M
  object_already_finalized = (old_val <= 0);
3393
5.93M
  g_return_val_if_fail (!object_already_finalized, NULL);
3394
3395
5.93M
  if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3396
0
    toggle_refs_notify (object, FALSE);
3397
3398
5.93M
  TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3399
3400
5.93M
  return object;
3401
5.93M
}
3402
3403
/**
3404
 * g_object_unref:
3405
 * @object: (type GObject.Object): a #GObject
3406
 *
3407
 * Decreases the reference count of @object. When its reference count
3408
 * drops to 0, the object is finalized (i.e. its memory is freed).
3409
 *
3410
 * If the pointer to the #GObject may be reused in future (for example, if it is
3411
 * an instance variable of another object), it is recommended to clear the
3412
 * pointer to %NULL rather than retain a dangling pointer to a potentially
3413
 * invalid #GObject instance. Use g_clear_object() for this.
3414
 */
3415
void
3416
g_object_unref (gpointer _object)
3417
19.9M
{
3418
19.9M
  GObject *object = _object;
3419
19.9M
  gint old_ref;
3420
  
3421
19.9M
  g_return_if_fail (G_IS_OBJECT (object));
3422
  
3423
  /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3424
19.9M
 retry_atomic_decrement1:
3425
19.9M
  old_ref = g_atomic_int_get (&object->ref_count);
3426
19.9M
  if (old_ref > 1)
3427
5.93M
    {
3428
      /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3429
5.93M
      gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3430
3431
5.93M
      if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3432
0
  goto retry_atomic_decrement1;
3433
3434
5.93M
      TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3435
3436
      /* if we went from 2->1 we need to notify toggle refs if any */
3437
5.93M
      if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3438
0
  toggle_refs_notify (object, TRUE);
3439
5.93M
    }
3440
14.0M
  else
3441
14.0M
    {
3442
14.0M
      GSList **weak_locations;
3443
3444
      /* The only way that this object can live at this point is if
3445
       * there are outstanding weak references already established
3446
       * before we got here.
3447
       *
3448
       * If there were not already weak references then no more can be
3449
       * established at this time, because the other thread would have
3450
       * to hold a strong ref in order to call
3451
       * g_object_add_weak_pointer() and then we wouldn't be here.
3452
       */
3453
14.0M
      weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3454
3455
14.0M
      if (weak_locations != NULL)
3456
0
        {
3457
0
          g_rw_lock_writer_lock (&weak_locations_lock);
3458
3459
          /* It is possible that one of the weak references beat us to
3460
           * the lock. Make sure the refcount is still what we expected
3461
           * it to be.
3462
           */
3463
0
          old_ref = g_atomic_int_get (&object->ref_count);
3464
0
          if (old_ref != 1)
3465
0
            {
3466
0
              g_rw_lock_writer_unlock (&weak_locations_lock);
3467
0
              goto retry_atomic_decrement1;
3468
0
            }
3469
3470
          /* We got the lock first, so the object will definitely die
3471
           * now. Clear out all the weak references.
3472
           */
3473
0
          while (*weak_locations)
3474
0
            {
3475
0
              GWeakRef *weak_ref_location = (*weak_locations)->data;
3476
3477
0
              weak_ref_location->priv.p = NULL;
3478
0
              *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3479
0
            }
3480
3481
0
          g_rw_lock_writer_unlock (&weak_locations_lock);
3482
0
        }
3483
3484
      /* we are about to remove the last reference */
3485
14.0M
      TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3486
14.0M
      G_OBJECT_GET_CLASS (object)->dispose (object);
3487
14.0M
      TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3488
3489
      /* may have been re-referenced meanwhile */
3490
14.0M
    retry_atomic_decrement2:
3491
14.0M
      old_ref = g_atomic_int_get ((int *)&object->ref_count);
3492
14.0M
      if (old_ref > 1)
3493
0
        {
3494
          /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3495
0
          gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3496
3497
0
          if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3498
0
      goto retry_atomic_decrement2;
3499
3500
0
    TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3501
3502
          /* if we went from 2->1 we need to notify toggle refs if any */
3503
0
          if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3504
0
      toggle_refs_notify (object, TRUE);
3505
3506
0
    return;
3507
0
  }
3508
3509
      /* we are still in the process of taking away the last ref */
3510
14.0M
      g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3511
14.0M
      g_signal_handlers_destroy (object);
3512
14.0M
      g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3513
      
3514
      /* decrement the last reference */
3515
14.0M
      old_ref = g_atomic_int_add (&object->ref_count, -1);
3516
14.0M
      g_return_if_fail (old_ref > 0);
3517
3518
14.0M
      TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3519
3520
      /* may have been re-referenced meanwhile */
3521
14.0M
      if (G_LIKELY (old_ref == 1))
3522
14.0M
  {
3523
14.0M
    TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3524
14.0M
          G_OBJECT_GET_CLASS (object)->finalize (object);
3525
3526
14.0M
    TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3527
3528
14.0M
          GOBJECT_IF_DEBUG (OBJECTS,
3529
14.0M
      {
3530
14.0M
              gboolean was_present;
3531
3532
              /* catch objects not chaining finalize handlers */
3533
14.0M
              G_LOCK (debug_objects);
3534
14.0M
              was_present = g_hash_table_remove (debug_objects_ht, object);
3535
14.0M
              G_UNLOCK (debug_objects);
3536
3537
14.0M
              if (was_present)
3538
14.0M
                g_critical ("Object %p of type %s not finalized correctly.",
3539
14.0M
                            object, G_OBJECT_TYPE_NAME (object));
3540
14.0M
      });
3541
14.0M
          g_type_free_instance ((GTypeInstance*) object);
3542
14.0M
  }
3543
14.0M
    }
3544
19.9M
}
3545
3546
/**
3547
 * g_clear_object: (skip)
3548
 * @object_ptr: a pointer to a #GObject reference
3549
 *
3550
 * Clears a reference to a #GObject.
3551
 *
3552
 * @object_ptr must not be %NULL.
3553
 *
3554
 * If the reference is %NULL then this function does nothing.
3555
 * Otherwise, the reference count of the object is decreased and the
3556
 * pointer is set to %NULL.
3557
 *
3558
 * A macro is also included that allows this function to be used without
3559
 * pointer casts.
3560
 *
3561
 * Since: 2.28
3562
 **/
3563
#undef g_clear_object
3564
void
3565
g_clear_object (GObject **object_ptr)
3566
0
{
3567
0
  g_clear_pointer (object_ptr, g_object_unref);
3568
0
}
3569
3570
/**
3571
 * g_object_get_qdata:
3572
 * @object: The GObject to get a stored user data pointer from
3573
 * @quark: A #GQuark, naming the user data pointer
3574
 * 
3575
 * This function gets back user data pointers stored via
3576
 * g_object_set_qdata().
3577
 * 
3578
 * Returns: (transfer none) (nullable): The user data pointer set, or %NULL
3579
 */
3580
gpointer
3581
g_object_get_qdata (GObject *object,
3582
        GQuark   quark)
3583
0
{
3584
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3585
  
3586
0
  return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3587
0
}
3588
3589
/**
3590
 * g_object_set_qdata: (skip)
3591
 * @object: The GObject to set store a user data pointer
3592
 * @quark: A #GQuark, naming the user data pointer
3593
 * @data: (nullable): An opaque user data pointer
3594
 *
3595
 * This sets an opaque, named pointer on an object.
3596
 * The name is specified through a #GQuark (retrieved e.g. via
3597
 * g_quark_from_static_string()), and the pointer
3598
 * can be gotten back from the @object with g_object_get_qdata()
3599
 * until the @object is finalized.
3600
 * Setting a previously set user data pointer, overrides (frees)
3601
 * the old pointer set, using #NULL as pointer essentially
3602
 * removes the data stored.
3603
 */
3604
void
3605
g_object_set_qdata (GObject *object,
3606
        GQuark   quark,
3607
        gpointer data)
3608
0
{
3609
0
  g_return_if_fail (G_IS_OBJECT (object));
3610
0
  g_return_if_fail (quark > 0);
3611
3612
0
  g_datalist_id_set_data (&object->qdata, quark, data);
3613
0
}
3614
3615
/**
3616
 * g_object_dup_qdata: (skip)
3617
 * @object: the #GObject to store user data on
3618
 * @quark: a #GQuark, naming the user data pointer
3619
 * @dup_func: (nullable): function to dup the value
3620
 * @user_data: (nullable): passed as user_data to @dup_func
3621
 *
3622
 * This is a variant of g_object_get_qdata() which returns
3623
 * a 'duplicate' of the value. @dup_func defines the
3624
 * meaning of 'duplicate' in this context, it could e.g.
3625
 * take a reference on a ref-counted object.
3626
 *
3627
 * If the @quark is not set on the object then @dup_func
3628
 * will be called with a %NULL argument.
3629
 *
3630
 * Note that @dup_func is called while user data of @object
3631
 * is locked.
3632
 *
3633
 * This function can be useful to avoid races when multiple
3634
 * threads are using object data on the same key on the same
3635
 * object.
3636
 *
3637
 * Returns: the result of calling @dup_func on the value
3638
 *     associated with @quark on @object, or %NULL if not set.
3639
 *     If @dup_func is %NULL, the value is returned
3640
 *     unmodified.
3641
 *
3642
 * Since: 2.34
3643
 */
3644
gpointer
3645
g_object_dup_qdata (GObject        *object,
3646
                    GQuark          quark,
3647
                    GDuplicateFunc   dup_func,
3648
                    gpointer         user_data)
3649
0
{
3650
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3651
0
  g_return_val_if_fail (quark > 0, NULL);
3652
3653
0
  return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3654
0
}
3655
3656
/**
3657
 * g_object_replace_qdata: (skip)
3658
 * @object: the #GObject to store user data on
3659
 * @quark: a #GQuark, naming the user data pointer
3660
 * @oldval: (nullable): the old value to compare against
3661
 * @newval: (nullable): the new value
3662
 * @destroy: (nullable): a destroy notify for the new value
3663
 * @old_destroy: (out) (optional): destroy notify for the existing value
3664
 *
3665
 * Compares the user data for the key @quark on @object with
3666
 * @oldval, and if they are the same, replaces @oldval with
3667
 * @newval.
3668
 *
3669
 * This is like a typical atomic compare-and-exchange
3670
 * operation, for user data on an object.
3671
 *
3672
 * If the previous value was replaced then ownership of the
3673
 * old value (@oldval) is passed to the caller, including
3674
 * the registered destroy notify for it (passed out in @old_destroy).
3675
 * It’s up to the caller to free this as needed, which may
3676
 * or may not include using @old_destroy as sometimes replacement
3677
 * should not destroy the object in the normal way.
3678
 *
3679
 * Returns: %TRUE if the existing value for @quark was replaced
3680
 *  by @newval, %FALSE otherwise.
3681
 *
3682
 * Since: 2.34
3683
 */
3684
gboolean
3685
g_object_replace_qdata (GObject        *object,
3686
                        GQuark          quark,
3687
                        gpointer        oldval,
3688
                        gpointer        newval,
3689
                        GDestroyNotify  destroy,
3690
                        GDestroyNotify *old_destroy)
3691
0
{
3692
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3693
0
  g_return_val_if_fail (quark > 0, FALSE);
3694
3695
0
  return g_datalist_id_replace_data (&object->qdata, quark,
3696
0
                                     oldval, newval, destroy,
3697
0
                                     old_destroy);
3698
0
}
3699
3700
/**
3701
 * g_object_set_qdata_full: (skip)
3702
 * @object: The GObject to set store a user data pointer
3703
 * @quark: A #GQuark, naming the user data pointer
3704
 * @data: (nullable): An opaque user data pointer
3705
 * @destroy: (nullable): Function to invoke with @data as argument, when @data
3706
 *           needs to be freed
3707
 *
3708
 * This function works like g_object_set_qdata(), but in addition,
3709
 * a void (*destroy) (gpointer) function may be specified which is
3710
 * called with @data as argument when the @object is finalized, or
3711
 * the data is being overwritten by a call to g_object_set_qdata()
3712
 * with the same @quark.
3713
 */
3714
void
3715
g_object_set_qdata_full (GObject       *object,
3716
       GQuark   quark,
3717
       gpointer data,
3718
       GDestroyNotify destroy)
3719
0
{
3720
0
  g_return_if_fail (G_IS_OBJECT (object));
3721
0
  g_return_if_fail (quark > 0);
3722
  
3723
0
  g_datalist_id_set_data_full (&object->qdata, quark, data,
3724
0
             data ? destroy : (GDestroyNotify) NULL);
3725
0
}
3726
3727
/**
3728
 * g_object_steal_qdata:
3729
 * @object: The GObject to get a stored user data pointer from
3730
 * @quark: A #GQuark, naming the user data pointer
3731
 *
3732
 * This function gets back user data pointers stored via
3733
 * g_object_set_qdata() and removes the @data from object
3734
 * without invoking its destroy() function (if any was
3735
 * set).
3736
 * Usually, calling this function is only required to update
3737
 * user data pointers with a destroy notifier, for example:
3738
 * |[<!-- language="C" --> 
3739
 * void
3740
 * object_add_to_user_list (GObject     *object,
3741
 *                          const gchar *new_string)
3742
 * {
3743
 *   // the quark, naming the object data
3744
 *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3745
 *   // retrieve the old string list
3746
 *   GList *list = g_object_steal_qdata (object, quark_string_list);
3747
 *
3748
 *   // prepend new string
3749
 *   list = g_list_prepend (list, g_strdup (new_string));
3750
 *   // this changed 'list', so we need to set it again
3751
 *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3752
 * }
3753
 * static void
3754
 * free_string_list (gpointer data)
3755
 * {
3756
 *   GList *node, *list = data;
3757
 *
3758
 *   for (node = list; node; node = node->next)
3759
 *     g_free (node->data);
3760
 *   g_list_free (list);
3761
 * }
3762
 * ]|
3763
 * Using g_object_get_qdata() in the above example, instead of
3764
 * g_object_steal_qdata() would have left the destroy function set,
3765
 * and thus the partial string list would have been freed upon
3766
 * g_object_set_qdata_full().
3767
 *
3768
 * Returns: (transfer full) (nullable): The user data pointer set, or %NULL
3769
 */
3770
gpointer
3771
g_object_steal_qdata (GObject *object,
3772
          GQuark   quark)
3773
0
{
3774
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3775
0
  g_return_val_if_fail (quark > 0, NULL);
3776
  
3777
0
  return g_datalist_id_remove_no_notify (&object->qdata, quark);
3778
0
}
3779
3780
/**
3781
 * g_object_get_data:
3782
 * @object: #GObject containing the associations
3783
 * @key: name of the key for that association
3784
 * 
3785
 * Gets a named field from the objects table of associations (see g_object_set_data()).
3786
 * 
3787
 * Returns: (transfer none) (nullable): the data if found,
3788
 *          or %NULL if no such data exists.
3789
 */
3790
gpointer
3791
g_object_get_data (GObject     *object,
3792
                   const gchar *key)
3793
0
{
3794
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3795
0
  g_return_val_if_fail (key != NULL, NULL);
3796
3797
0
  return g_datalist_get_data (&object->qdata, key);
3798
0
}
3799
3800
/**
3801
 * g_object_set_data:
3802
 * @object: #GObject containing the associations.
3803
 * @key: name of the key
3804
 * @data: (nullable): data to associate with that key
3805
 *
3806
 * Each object carries around a table of associations from
3807
 * strings to pointers.  This function lets you set an association.
3808
 *
3809
 * If the object already had an association with that name,
3810
 * the old association will be destroyed.
3811
 *
3812
 * Internally, the @key is converted to a #GQuark using g_quark_from_string().
3813
 * This means a copy of @key is kept permanently (even after @object has been
3814
 * finalized) — so it is recommended to only use a small, bounded set of values
3815
 * for @key in your program, to avoid the #GQuark storage growing unbounded.
3816
 */
3817
void
3818
g_object_set_data (GObject     *object,
3819
                   const gchar *key,
3820
                   gpointer     data)
3821
0
{
3822
0
  g_return_if_fail (G_IS_OBJECT (object));
3823
0
  g_return_if_fail (key != NULL);
3824
3825
0
  g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3826
0
}
3827
3828
/**
3829
 * g_object_dup_data: (skip)
3830
 * @object: the #GObject to store user data on
3831
 * @key: a string, naming the user data pointer
3832
 * @dup_func: (nullable): function to dup the value
3833
 * @user_data: (nullable): passed as user_data to @dup_func
3834
 *
3835
 * This is a variant of g_object_get_data() which returns
3836
 * a 'duplicate' of the value. @dup_func defines the
3837
 * meaning of 'duplicate' in this context, it could e.g.
3838
 * take a reference on a ref-counted object.
3839
 *
3840
 * If the @key is not set on the object then @dup_func
3841
 * will be called with a %NULL argument.
3842
 *
3843
 * Note that @dup_func is called while user data of @object
3844
 * is locked.
3845
 *
3846
 * This function can be useful to avoid races when multiple
3847
 * threads are using object data on the same key on the same
3848
 * object.
3849
 *
3850
 * Returns: the result of calling @dup_func on the value
3851
 *     associated with @key on @object, or %NULL if not set.
3852
 *     If @dup_func is %NULL, the value is returned
3853
 *     unmodified.
3854
 *
3855
 * Since: 2.34
3856
 */
3857
gpointer
3858
g_object_dup_data (GObject        *object,
3859
                   const gchar    *key,
3860
                   GDuplicateFunc   dup_func,
3861
                   gpointer         user_data)
3862
0
{
3863
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3864
0
  g_return_val_if_fail (key != NULL, NULL);
3865
3866
0
  return g_datalist_id_dup_data (&object->qdata,
3867
0
                                 g_quark_from_string (key),
3868
0
                                 dup_func, user_data);
3869
0
}
3870
3871
/**
3872
 * g_object_replace_data: (skip)
3873
 * @object: the #GObject to store user data on
3874
 * @key: a string, naming the user data pointer
3875
 * @oldval: (nullable): the old value to compare against
3876
 * @newval: (nullable): the new value
3877
 * @destroy: (nullable): a destroy notify for the new value
3878
 * @old_destroy: (out) (optional): destroy notify for the existing value
3879
 *
3880
 * Compares the user data for the key @key on @object with
3881
 * @oldval, and if they are the same, replaces @oldval with
3882
 * @newval.
3883
 *
3884
 * This is like a typical atomic compare-and-exchange
3885
 * operation, for user data on an object.
3886
 *
3887
 * If the previous value was replaced then ownership of the
3888
 * old value (@oldval) is passed to the caller, including
3889
 * the registered destroy notify for it (passed out in @old_destroy).
3890
 * It’s up to the caller to free this as needed, which may
3891
 * or may not include using @old_destroy as sometimes replacement
3892
 * should not destroy the object in the normal way.
3893
 *
3894
 * See g_object_set_data() for guidance on using a small, bounded set of values
3895
 * for @key.
3896
 *
3897
 * Returns: %TRUE if the existing value for @key was replaced
3898
 *  by @newval, %FALSE otherwise.
3899
 *
3900
 * Since: 2.34
3901
 */
3902
gboolean
3903
g_object_replace_data (GObject        *object,
3904
                       const gchar    *key,
3905
                       gpointer        oldval,
3906
                       gpointer        newval,
3907
                       GDestroyNotify  destroy,
3908
                       GDestroyNotify *old_destroy)
3909
0
{
3910
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3911
0
  g_return_val_if_fail (key != NULL, FALSE);
3912
3913
0
  return g_datalist_id_replace_data (&object->qdata,
3914
0
                                     g_quark_from_string (key),
3915
0
                                     oldval, newval, destroy,
3916
0
                                     old_destroy);
3917
0
}
3918
3919
/**
3920
 * g_object_set_data_full: (skip)
3921
 * @object: #GObject containing the associations
3922
 * @key: name of the key
3923
 * @data: (nullable): data to associate with that key
3924
 * @destroy: (nullable): function to call when the association is destroyed
3925
 *
3926
 * Like g_object_set_data() except it adds notification
3927
 * for when the association is destroyed, either by setting it
3928
 * to a different value or when the object is destroyed.
3929
 *
3930
 * Note that the @destroy callback is not called if @data is %NULL.
3931
 */
3932
void
3933
g_object_set_data_full (GObject       *object,
3934
                        const gchar   *key,
3935
                        gpointer       data,
3936
                        GDestroyNotify destroy)
3937
0
{
3938
0
  g_return_if_fail (G_IS_OBJECT (object));
3939
0
  g_return_if_fail (key != NULL);
3940
3941
0
  g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3942
0
             data ? destroy : (GDestroyNotify) NULL);
3943
0
}
3944
3945
/**
3946
 * g_object_steal_data:
3947
 * @object: #GObject containing the associations
3948
 * @key: name of the key
3949
 *
3950
 * Remove a specified datum from the object's data associations,
3951
 * without invoking the association's destroy handler.
3952
 *
3953
 * Returns: (transfer full) (nullable): the data if found, or %NULL
3954
 *          if no such data exists.
3955
 */
3956
gpointer
3957
g_object_steal_data (GObject     *object,
3958
                     const gchar *key)
3959
0
{
3960
0
  GQuark quark;
3961
3962
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3963
0
  g_return_val_if_fail (key != NULL, NULL);
3964
3965
0
  quark = g_quark_try_string (key);
3966
3967
0
  return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3968
0
}
3969
3970
static void
3971
g_value_object_init (GValue *value)
3972
9.88M
{
3973
9.88M
  value->data[0].v_pointer = NULL;
3974
9.88M
}
3975
3976
static void
3977
g_value_object_free_value (GValue *value)
3978
19.7M
{
3979
19.7M
  if (value->data[0].v_pointer)
3980
0
    g_object_unref (value->data[0].v_pointer);
3981
19.7M
}
3982
3983
static void
3984
g_value_object_copy_value (const GValue *src_value,
3985
         GValue *dest_value)
3986
9.88M
{
3987
9.88M
  if (src_value->data[0].v_pointer)
3988
0
    dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3989
9.88M
  else
3990
9.88M
    dest_value->data[0].v_pointer = NULL;
3991
9.88M
}
3992
3993
static void
3994
g_value_object_transform_value (const GValue *src_value,
3995
        GValue       *dest_value)
3996
0
{
3997
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)))
3998
0
    dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3999
0
  else
4000
0
    dest_value->data[0].v_pointer = NULL;
4001
0
}
4002
4003
static gpointer
4004
g_value_object_peek_pointer (const GValue *value)
4005
0
{
4006
0
  return value->data[0].v_pointer;
4007
0
}
4008
4009
static gchar*
4010
g_value_object_collect_value (GValue    *value,
4011
            guint        n_collect_values,
4012
            GTypeCValue *collect_values,
4013
            guint        collect_flags)
4014
0
{
4015
0
  if (collect_values[0].v_pointer)
4016
0
    {
4017
0
      GObject *object = collect_values[0].v_pointer;
4018
      
4019
0
      if (object->g_type_instance.g_class == NULL)
4020
0
  return g_strconcat ("invalid unclassed object pointer for value type '",
4021
0
          G_VALUE_TYPE_NAME (value),
4022
0
          "'",
4023
0
          NULL);
4024
0
      else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
4025
0
  return g_strconcat ("invalid object type '",
4026
0
          G_OBJECT_TYPE_NAME (object),
4027
0
          "' for value type '",
4028
0
          G_VALUE_TYPE_NAME (value),
4029
0
          "'",
4030
0
          NULL);
4031
      /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
4032
0
      value->data[0].v_pointer = g_object_ref (object);
4033
0
    }
4034
0
  else
4035
0
    value->data[0].v_pointer = NULL;
4036
  
4037
0
  return NULL;
4038
0
}
4039
4040
static gchar*
4041
g_value_object_lcopy_value (const GValue *value,
4042
          guint        n_collect_values,
4043
          GTypeCValue *collect_values,
4044
          guint        collect_flags)
4045
0
{
4046
0
  GObject **object_p = collect_values[0].v_pointer;
4047
4048
0
  g_return_val_if_fail (object_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
4049
4050
0
  if (!value->data[0].v_pointer)
4051
0
    *object_p = NULL;
4052
0
  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
4053
0
    *object_p = value->data[0].v_pointer;
4054
0
  else
4055
0
    *object_p = g_object_ref (value->data[0].v_pointer);
4056
  
4057
0
  return NULL;
4058
0
}
4059
4060
/**
4061
 * g_value_set_object:
4062
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4063
 * @v_object: (type GObject.Object) (nullable): object value to be set
4064
 *
4065
 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
4066
 *
4067
 * g_value_set_object() increases the reference count of @v_object
4068
 * (the #GValue holds a reference to @v_object).  If you do not wish
4069
 * to increase the reference count of the object (i.e. you wish to
4070
 * pass your current reference to the #GValue because you no longer
4071
 * need it), use g_value_take_object() instead.
4072
 *
4073
 * It is important that your #GValue holds a reference to @v_object (either its
4074
 * own, or one it has taken) to ensure that the object won't be destroyed while
4075
 * the #GValue still exists).
4076
 */
4077
void
4078
g_value_set_object (GValue   *value,
4079
        gpointer  v_object)
4080
0
{
4081
0
  GObject *old;
4082
  
4083
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
4084
4085
0
  old = value->data[0].v_pointer;
4086
  
4087
0
  if (v_object)
4088
0
    {
4089
0
      g_return_if_fail (G_IS_OBJECT (v_object));
4090
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
4091
4092
0
      value->data[0].v_pointer = v_object;
4093
0
      g_object_ref (value->data[0].v_pointer);
4094
0
    }
4095
0
  else
4096
0
    value->data[0].v_pointer = NULL;
4097
  
4098
0
  if (old)
4099
0
    g_object_unref (old);
4100
0
}
4101
4102
/**
4103
 * g_value_set_object_take_ownership: (skip)
4104
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4105
 * @v_object: (nullable): object value to be set
4106
 *
4107
 * This is an internal function introduced mainly for C marshallers.
4108
 *
4109
 * Deprecated: 2.4: Use g_value_take_object() instead.
4110
 */
4111
void
4112
g_value_set_object_take_ownership (GValue  *value,
4113
           gpointer v_object)
4114
0
{
4115
0
  g_value_take_object (value, v_object);
4116
0
}
4117
4118
/**
4119
 * g_value_take_object: (skip)
4120
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4121
 * @v_object: (nullable): object value to be set
4122
 *
4123
 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
4124
 * and takes over the ownership of the caller’s reference to @v_object;
4125
 * the caller doesn’t have to unref it any more (i.e. the reference
4126
 * count of the object is not increased).
4127
 *
4128
 * If you want the #GValue to hold its own reference to @v_object, use
4129
 * g_value_set_object() instead.
4130
 *
4131
 * Since: 2.4
4132
 */
4133
void
4134
g_value_take_object (GValue  *value,
4135
         gpointer v_object)
4136
0
{
4137
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
4138
4139
0
  if (value->data[0].v_pointer)
4140
0
    {
4141
0
      g_object_unref (value->data[0].v_pointer);
4142
0
      value->data[0].v_pointer = NULL;
4143
0
    }
4144
4145
0
  if (v_object)
4146
0
    {
4147
0
      g_return_if_fail (G_IS_OBJECT (v_object));
4148
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
4149
4150
0
      value->data[0].v_pointer = v_object; /* we take over the reference count */
4151
0
    }
4152
0
}
4153
4154
/**
4155
 * g_value_get_object:
4156
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4157
 * 
4158
 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
4159
 * 
4160
 * Returns: (type GObject.Object) (transfer none): object contents of @value
4161
 */
4162
gpointer
4163
g_value_get_object (const GValue *value)
4164
9.88M
{
4165
9.88M
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
4166
  
4167
9.88M
  return value->data[0].v_pointer;
4168
9.88M
}
4169
4170
/**
4171
 * g_value_dup_object:
4172
 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
4173
 *
4174
 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
4175
 * its reference count. If the contents of the #GValue are %NULL, then
4176
 * %NULL will be returned.
4177
 *
4178
 * Returns: (type GObject.Object) (transfer full): object content of @value,
4179
 *          should be unreferenced when no longer needed.
4180
 */
4181
gpointer
4182
g_value_dup_object (const GValue *value)
4183
0
{
4184
0
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
4185
  
4186
0
  return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
4187
0
}
4188
4189
/**
4190
 * g_signal_connect_object: (skip)
4191
 * @instance: (type GObject.TypeInstance): the instance to connect to.
4192
 * @detailed_signal: a string of the form "signal-name::detail".
4193
 * @c_handler: the #GCallback to connect.
4194
 * @gobject: (type GObject.Object) (nullable): the object to pass as data
4195
 *    to @c_handler.
4196
 * @connect_flags: a combination of #GConnectFlags.
4197
 *
4198
 * This is similar to g_signal_connect_data(), but uses a closure which
4199
 * ensures that the @gobject stays alive during the call to @c_handler
4200
 * by temporarily adding a reference count to @gobject.
4201
 *
4202
 * When the @gobject is destroyed the signal handler will be automatically
4203
 * disconnected.  Note that this is not currently threadsafe (ie:
4204
 * emitting a signal while @gobject is being destroyed in another thread
4205
 * is not safe).
4206
 *
4207
 * Returns: the handler id.
4208
 */
4209
gulong
4210
g_signal_connect_object (gpointer      instance,
4211
       const gchar  *detailed_signal,
4212
       GCallback     c_handler,
4213
       gpointer      gobject,
4214
       GConnectFlags connect_flags)
4215
0
{
4216
0
  g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
4217
0
  g_return_val_if_fail (detailed_signal != NULL, 0);
4218
0
  g_return_val_if_fail (c_handler != NULL, 0);
4219
4220
0
  if (gobject)
4221
0
    {
4222
0
      GClosure *closure;
4223
4224
0
      g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
4225
4226
0
      closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
4227
4228
0
      return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
4229
0
    }
4230
0
  else
4231
0
    return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
4232
0
}
4233
4234
typedef struct {
4235
  GObject  *object;
4236
  guint     n_closures;
4237
  GClosure *closures[1]; /* flexible array */
4238
} CArray;
4239
/* don't change this structure without supplying an accessor for
4240
 * watched closures, e.g.:
4241
 * GSList* g_object_list_watched_closures (GObject *object)
4242
 * {
4243
 *   CArray *carray;
4244
 *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4245
 *   carray = g_object_get_data (object, "GObject-closure-array");
4246
 *   if (carray)
4247
 *     {
4248
 *       GSList *slist = NULL;
4249
 *       guint i;
4250
 *       for (i = 0; i < carray->n_closures; i++)
4251
 *         slist = g_slist_prepend (slist, carray->closures[i]);
4252
 *       return slist;
4253
 *     }
4254
 *   return NULL;
4255
 * }
4256
 */
4257
4258
static void
4259
object_remove_closure (gpointer  data,
4260
           GClosure *closure)
4261
0
{
4262
0
  GObject *object = data;
4263
0
  CArray *carray;
4264
0
  guint i;
4265
  
4266
0
  G_LOCK (closure_array_mutex);
4267
0
  carray = g_object_get_qdata (object, quark_closure_array);
4268
0
  for (i = 0; i < carray->n_closures; i++)
4269
0
    if (carray->closures[i] == closure)
4270
0
      {
4271
0
  carray->n_closures--;
4272
0
  if (i < carray->n_closures)
4273
0
    carray->closures[i] = carray->closures[carray->n_closures];
4274
0
  G_UNLOCK (closure_array_mutex);
4275
0
  return;
4276
0
      }
4277
0
  G_UNLOCK (closure_array_mutex);
4278
0
  g_assert_not_reached ();
4279
0
}
4280
4281
static void
4282
destroy_closure_array (gpointer data)
4283
0
{
4284
0
  CArray *carray = data;
4285
0
  GObject *object = carray->object;
4286
0
  guint i, n = carray->n_closures;
4287
  
4288
0
  for (i = 0; i < n; i++)
4289
0
    {
4290
0
      GClosure *closure = carray->closures[i];
4291
      
4292
      /* removing object_remove_closure() upfront is probably faster than
4293
       * letting it fiddle with quark_closure_array which is empty anyways
4294
       */
4295
0
      g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
4296
0
      g_closure_invalidate (closure);
4297
0
    }
4298
0
  g_free (carray);
4299
0
}
4300
4301
/**
4302
 * g_object_watch_closure:
4303
 * @object: #GObject restricting lifetime of @closure
4304
 * @closure: #GClosure to watch
4305
 *
4306
 * This function essentially limits the life time of the @closure to
4307
 * the life time of the object. That is, when the object is finalized,
4308
 * the @closure is invalidated by calling g_closure_invalidate() on
4309
 * it, in order to prevent invocations of the closure with a finalized
4310
 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
4311
 * added as marshal guards to the @closure, to ensure that an extra
4312
 * reference count is held on @object during invocation of the
4313
 * @closure.  Usually, this function will be called on closures that
4314
 * use this @object as closure data.
4315
 */
4316
void
4317
g_object_watch_closure (GObject  *object,
4318
      GClosure *closure)
4319
0
{
4320
0
  CArray *carray;
4321
0
  guint i;
4322
  
4323
0
  g_return_if_fail (G_IS_OBJECT (object));
4324
0
  g_return_if_fail (closure != NULL);
4325
0
  g_return_if_fail (closure->is_invalid == FALSE);
4326
0
  g_return_if_fail (closure->in_marshal == FALSE);
4327
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0); /* this doesn't work on finalizing objects */
4328
  
4329
0
  g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
4330
0
  g_closure_add_marshal_guards (closure,
4331
0
        object, (GClosureNotify) g_object_ref,
4332
0
        object, (GClosureNotify) g_object_unref);
4333
0
  G_LOCK (closure_array_mutex);
4334
0
  carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
4335
0
  if (!carray)
4336
0
    {
4337
0
      carray = g_renew (CArray, NULL, 1);
4338
0
      carray->object = object;
4339
0
      carray->n_closures = 1;
4340
0
      i = 0;
4341
0
    }
4342
0
  else
4343
0
    {
4344
0
      i = carray->n_closures++;
4345
0
      carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4346
0
    }
4347
0
  carray->closures[i] = closure;
4348
0
  g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4349
0
  G_UNLOCK (closure_array_mutex);
4350
0
}
4351
4352
/**
4353
 * g_closure_new_object:
4354
 * @sizeof_closure: the size of the structure to allocate, must be at least
4355
 *  `sizeof (GClosure)`
4356
 * @object: a #GObject pointer to store in the @data field of the newly
4357
 *  allocated #GClosure
4358
 *
4359
 * A variant of g_closure_new_simple() which stores @object in the
4360
 * @data field of the closure and calls g_object_watch_closure() on
4361
 * @object and the created closure. This function is mainly useful
4362
 * when implementing new types of closures.
4363
 *
4364
 * Returns: (transfer floating): a newly allocated #GClosure
4365
 */
4366
GClosure *
4367
g_closure_new_object (guint    sizeof_closure,
4368
          GObject *object)
4369
0
{
4370
0
  GClosure *closure;
4371
4372
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4373
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4374
4375
0
  closure = g_closure_new_simple (sizeof_closure, object);
4376
0
  g_object_watch_closure (object, closure);
4377
4378
0
  return closure;
4379
0
}
4380
4381
/**
4382
 * g_cclosure_new_object: (skip)
4383
 * @callback_func: the function to invoke
4384
 * @object: a #GObject pointer to pass to @callback_func
4385
 *
4386
 * A variant of g_cclosure_new() which uses @object as @user_data and
4387
 * calls g_object_watch_closure() on @object and the created
4388
 * closure. This function is useful when you have a callback closely
4389
 * associated with a #GObject, and want the callback to no longer run
4390
 * after the object is is freed.
4391
 *
4392
 * Returns: (transfer floating): a new #GCClosure
4393
 */
4394
GClosure *
4395
g_cclosure_new_object (GCallback callback_func,
4396
           GObject  *object)
4397
0
{
4398
0
  GClosure *closure;
4399
4400
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4401
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4402
0
  g_return_val_if_fail (callback_func != NULL, NULL);
4403
4404
0
  closure = g_cclosure_new (callback_func, object, NULL);
4405
0
  g_object_watch_closure (object, closure);
4406
4407
0
  return closure;
4408
0
}
4409
4410
/**
4411
 * g_cclosure_new_object_swap: (skip)
4412
 * @callback_func: the function to invoke
4413
 * @object: a #GObject pointer to pass to @callback_func
4414
 *
4415
 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4416
 * and calls g_object_watch_closure() on @object and the created
4417
 * closure. This function is useful when you have a callback closely
4418
 * associated with a #GObject, and want the callback to no longer run
4419
 * after the object is is freed.
4420
 *
4421
 * Returns: (transfer floating): a new #GCClosure
4422
 */
4423
GClosure *
4424
g_cclosure_new_object_swap (GCallback callback_func,
4425
          GObject  *object)
4426
0
{
4427
0
  GClosure *closure;
4428
4429
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4430
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4431
0
  g_return_val_if_fail (callback_func != NULL, NULL);
4432
4433
0
  closure = g_cclosure_new_swap (callback_func, object, NULL);
4434
0
  g_object_watch_closure (object, closure);
4435
4436
0
  return closure;
4437
0
}
4438
4439
gsize
4440
g_object_compat_control (gsize           what,
4441
                         gpointer        data)
4442
0
{
4443
0
  switch (what)
4444
0
    {
4445
0
      gpointer *pp;
4446
0
    case 1:     /* floating base type */
4447
0
      return G_TYPE_INITIALLY_UNOWNED;
4448
0
    case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
4449
0
      floating_flag_handler = (guint(*)(GObject*,gint)) data;
4450
0
      return 1;
4451
0
    case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
4452
0
      pp = data;
4453
0
      *pp = floating_flag_handler;
4454
0
      return 1;
4455
0
    default:
4456
0
      return 0;
4457
0
    }
4458
0
}
4459
4460
G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
4461
4462
static void
4463
g_initially_unowned_init (GInitiallyUnowned *object)
4464
0
{
4465
0
  g_object_force_floating (object);
4466
0
}
4467
4468
static void
4469
g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4470
0
{
4471
0
}
4472
4473
/**
4474
 * GWeakRef:
4475
 *
4476
 * A structure containing a weak reference to a #GObject.  It can either
4477
 * be empty (i.e. point to %NULL), or point to an object for as long as
4478
 * at least one "strong" reference to that object exists. Before the
4479
 * object's #GObjectClass.dispose method is called, every #GWeakRef
4480
 * associated with becomes empty (i.e. points to %NULL).
4481
 *
4482
 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4483
 * heap-allocated, or embedded in larger structures.
4484
 *
4485
 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4486
 * reference is thread-safe: converting a weak pointer to a reference is
4487
 * atomic with respect to invalidation of weak pointers to destroyed
4488
 * objects.
4489
 *
4490
 * If the object's #GObjectClass.dispose method results in additional
4491
 * references to the object being held, any #GWeakRefs taken
4492
 * before it was disposed will continue to point to %NULL.  If
4493
 * #GWeakRefs are taken after the object is disposed and
4494
 * re-referenced, they will continue to point to it until its refcount
4495
 * goes back to zero, at which point they too will be invalidated.
4496
 */
4497
4498
/**
4499
 * g_weak_ref_init: (skip)
4500
 * @weak_ref: (inout): uninitialized or empty location for a weak
4501
 *    reference
4502
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4503
 *
4504
 * Initialise a non-statically-allocated #GWeakRef.
4505
 *
4506
 * This function also calls g_weak_ref_set() with @object on the
4507
 * freshly-initialised weak reference.
4508
 *
4509
 * This function should always be matched with a call to
4510
 * g_weak_ref_clear().  It is not necessary to use this function for a
4511
 * #GWeakRef in static storage because it will already be
4512
 * properly initialised.  Just use g_weak_ref_set() directly.
4513
 *
4514
 * Since: 2.32
4515
 */
4516
void
4517
g_weak_ref_init (GWeakRef *weak_ref,
4518
                 gpointer  object)
4519
0
{
4520
0
  weak_ref->priv.p = NULL;
4521
4522
0
  g_weak_ref_set (weak_ref, object);
4523
0
}
4524
4525
/**
4526
 * g_weak_ref_clear: (skip)
4527
 * @weak_ref: (inout): location of a weak reference, which
4528
 *  may be empty
4529
 *
4530
 * Frees resources associated with a non-statically-allocated #GWeakRef.
4531
 * After this call, the #GWeakRef is left in an undefined state.
4532
 *
4533
 * You should only call this on a #GWeakRef that previously had
4534
 * g_weak_ref_init() called on it.
4535
 *
4536
 * Since: 2.32
4537
 */
4538
void
4539
g_weak_ref_clear (GWeakRef *weak_ref)
4540
0
{
4541
0
  g_weak_ref_set (weak_ref, NULL);
4542
4543
  /* be unkind */
4544
0
  weak_ref->priv.p = (void *) 0xccccccccu;
4545
0
}
4546
4547
/**
4548
 * g_weak_ref_get: (skip)
4549
 * @weak_ref: (inout): location of a weak reference to a #GObject
4550
 *
4551
 * If @weak_ref is not empty, atomically acquire a strong
4552
 * reference to the object it points to, and return that reference.
4553
 *
4554
 * This function is needed because of the potential race between taking
4555
 * the pointer value and g_object_ref() on it, if the object was losing
4556
 * its last reference at the same time in a different thread.
4557
 *
4558
 * The caller should release the resulting reference in the usual way,
4559
 * by using g_object_unref().
4560
 *
4561
 * Returns: (transfer full) (type GObject.Object): the object pointed to
4562
 *     by @weak_ref, or %NULL if it was empty
4563
 *
4564
 * Since: 2.32
4565
 */
4566
gpointer
4567
g_weak_ref_get (GWeakRef *weak_ref)
4568
0
{
4569
0
  gpointer object_or_null;
4570
4571
0
  g_return_val_if_fail (weak_ref!= NULL, NULL);
4572
4573
0
  g_rw_lock_reader_lock (&weak_locations_lock);
4574
4575
0
  object_or_null = weak_ref->priv.p;
4576
4577
0
  if (object_or_null != NULL)
4578
0
    g_object_ref (object_or_null);
4579
4580
0
  g_rw_lock_reader_unlock (&weak_locations_lock);
4581
4582
0
  return object_or_null;
4583
0
}
4584
4585
/**
4586
 * g_weak_ref_set: (skip)
4587
 * @weak_ref: location for a weak reference
4588
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4589
 *
4590
 * Change the object to which @weak_ref points, or set it to
4591
 * %NULL.
4592
 *
4593
 * You must own a strong reference on @object while calling this
4594
 * function.
4595
 *
4596
 * Since: 2.32
4597
 */
4598
void
4599
g_weak_ref_set (GWeakRef *weak_ref,
4600
                gpointer  object)
4601
0
{
4602
0
  GSList **weak_locations;
4603
0
  GObject *new_object;
4604
0
  GObject *old_object;
4605
4606
0
  g_return_if_fail (weak_ref != NULL);
4607
0
  g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4608
4609
0
  new_object = object;
4610
4611
0
  g_rw_lock_writer_lock (&weak_locations_lock);
4612
4613
  /* We use the extra level of indirection here so that if we have ever
4614
   * had a weak pointer installed at any point in time on this object,
4615
   * we can see that there is a non-NULL value associated with the
4616
   * weak-pointer quark and know that this value will not change at any
4617
   * point in the object's lifetime.
4618
   *
4619
   * Both properties are important for reducing the amount of times we
4620
   * need to acquire locks and for decreasing the duration of time the
4621
   * lock is held while avoiding some rather tricky races.
4622
   *
4623
   * Specifically: we can avoid having to do an extra unconditional lock
4624
   * in g_object_unref() without worrying about some extremely tricky
4625
   * races.
4626
   */
4627
4628
0
  old_object = weak_ref->priv.p;
4629
0
  if (new_object != old_object)
4630
0
    {
4631
0
      weak_ref->priv.p = new_object;
4632
4633
      /* Remove the weak ref from the old object */
4634
0
      if (old_object != NULL)
4635
0
        {
4636
0
          weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4637
          /* for it to point to an object, the object must have had it added once */
4638
0
          g_assert (weak_locations != NULL);
4639
4640
0
          *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4641
0
        }
4642
4643
      /* Add the weak ref to the new object */
4644
0
      if (new_object != NULL)
4645
0
        {
4646
0
          weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4647
4648
0
          if (weak_locations == NULL)
4649
0
            {
4650
0
              weak_locations = g_new0 (GSList *, 1);
4651
0
              g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4652
0
            }
4653
4654
0
          *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4655
0
        }
4656
0
    }
4657
4658
0
  g_rw_lock_writer_unlock (&weak_locations_lock);
4659
0
}