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