LCOV - code coverage report
Current view: top level - gobject - gobject.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 86.0 % 1591 1369
Test Date: 2024-11-26 05:23:01 Functions: 94.8 % 154 146
Branches: - 0 0

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

Generated by: LCOV version 2.0-1