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