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