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