Add GI annotations to GObject
[platform/upstream/glib.git] / gobject / gobject.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
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 "gobject.h"
30 #include "gvaluecollector.h"
31 #include "gsignal.h"
32 #include "gparamspecs.h"
33 #include "gvaluetypes.h"
34 #include "gobject_trace.h"
35
36 #include "gobjectnotifyqueue.c"
37
38 /**
39  * SECTION:objects
40  * @short_description: The base object type
41  * @see_also: #GParamSpecObject, g_param_spec_object()
42  * @title: The Base Object Type
43  *
44  * GObject is the fundamental type providing the common attributes and
45  * methods for all object types in GTK+, Pango and other libraries
46  * based on GObject.  The GObject class provides methods for object
47  * construction and destruction, property access methods, and signal
48  * support.  Signals are described in detail in <xref
49  * linkend="gobject-Signals"/>.
50  *
51  * <para id="floating-ref">
52  * #GInitiallyUnowned is derived from #GObject. The only difference between
53  * the two is that the initial reference of a #GInitiallyUnowned is flagged
54  * as a <firstterm>floating</firstterm> reference.
55  * This means that it is not specifically claimed to be "owned" by
56  * any code portion. The main motivation for providing floating references is
57  * C convenience. In particular, it allows code to be written as:
58  * |[
59  * container = create_container();
60  * container_add_child (container, create_child());
61  * ]|
62  * If <function>container_add_child()</function> will g_object_ref_sink() the
63  * passed in child, no reference of the newly created child is leaked.
64  * Without floating references, <function>container_add_child()</function>
65  * can only g_object_ref() the new child, so to implement this code without
66  * reference leaks, it would have to be written as:
67  * |[
68  * Child *child;
69  * container = create_container();
70  * child = create_child();
71  * container_add_child (container, child);
72  * g_object_unref (child);
73  * ]|
74  * The floating reference can be converted into
75  * an ordinary reference by calling g_object_ref_sink().
76  * For already sunken objects (objects that don't have a floating reference
77  * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
78  * a new reference.
79  * Since floating references are useful almost exclusively for C convenience,
80  * language bindings that provide automated reference and memory ownership
81  * maintenance (such as smart pointers or garbage collection) therefore don't
82  * need to expose floating references in their API.
83  * </para>
84  *
85  * Some object implementations may need to save an objects floating state
86  * across certain code portions (an example is #GtkMenu), to achive this, the
87  * following sequence can be used:
88  *
89  * |[
90  * // save floating state
91  * gboolean was_floating = g_object_is_floating (object);
92  * g_object_ref_sink (object);
93  * // protected code portion
94  * ...;
95  * // restore floating state
96  * if (was_floating)
97  *   g_object_force_floating (object);
98  * g_obejct_unref (object); // release previously acquired reference
99  * ]|
100  */
101
102
103 /* --- macros --- */
104 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
105 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
106
107 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
108 #define OBJECT_HAS_TOGGLE_REF(object) \
109     ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
110 #define OBJECT_FLOATING_FLAG 0x2
111
112 #define CLASS_HAS_PROPS_FLAG 0x1
113 #define CLASS_HAS_PROPS(class) \
114     ((class)->flags & CLASS_HAS_PROPS_FLAG)
115 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
116     ((class)->constructor != g_object_constructor)
117
118 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
119 #define CLASS_HAS_DERIVED_CLASS(class) \
120     ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
121
122 /* --- signals --- */
123 enum {
124   NOTIFY,
125   LAST_SIGNAL
126 };
127
128
129 /* --- properties --- */
130 enum {
131   PROP_NONE
132 };
133
134
135 /* --- prototypes --- */
136 static void     g_object_base_class_init                (GObjectClass   *class);
137 static void     g_object_base_class_finalize            (GObjectClass   *class);
138 static void     g_object_do_class_init                  (GObjectClass   *class);
139 static void     g_object_init                           (GObject        *object,
140                                                          GObjectClass   *class);
141 static GObject* g_object_constructor                    (GType                  type,
142                                                          guint                  n_construct_properties,
143                                                          GObjectConstructParam *construct_params);
144 static void     g_object_real_dispose                   (GObject        *object);
145 static void     g_object_finalize                       (GObject        *object);
146 static void     g_object_do_set_property                (GObject        *object,
147                                                          guint           property_id,
148                                                          const GValue   *value,
149                                                          GParamSpec     *pspec);
150 static void     g_object_do_get_property                (GObject        *object,
151                                                          guint           property_id,
152                                                          GValue         *value,
153                                                          GParamSpec     *pspec);
154 static void     g_value_object_init                     (GValue         *value);
155 static void     g_value_object_free_value               (GValue         *value);
156 static void     g_value_object_copy_value               (const GValue   *src_value,
157                                                          GValue         *dest_value);
158 static void     g_value_object_transform_value          (const GValue   *src_value,
159                                                          GValue         *dest_value);
160 static gpointer g_value_object_peek_pointer             (const GValue   *value);
161 static gchar*   g_value_object_collect_value            (GValue         *value,
162                                                          guint           n_collect_values,
163                                                          GTypeCValue    *collect_values,
164                                                          guint           collect_flags);
165 static gchar*   g_value_object_lcopy_value              (const GValue   *value,
166                                                          guint           n_collect_values,
167                                                          GTypeCValue    *collect_values,
168                                                          guint           collect_flags);
169 static void     g_object_dispatch_properties_changed    (GObject        *object,
170                                                          guint           n_pspecs,
171                                                          GParamSpec    **pspecs);
172 static inline void         object_get_property          (GObject        *object,
173                                                          GParamSpec     *pspec,
174                                                          GValue         *value);
175 static inline void         object_set_property          (GObject        *object,
176                                                          GParamSpec     *pspec,
177                                                          const GValue   *value,
178                                                          GObjectNotifyQueue *nqueue);
179 static guint               object_floating_flag_handler (GObject        *object,
180                                                          gint            job);
181
182 static void object_interface_check_properties           (gpointer        func_data,
183                                                          gpointer        g_iface);
184
185
186 /* --- variables --- */
187 G_LOCK_DEFINE_STATIC (closure_array_mutex);
188 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
189 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
190 static GQuark               quark_closure_array = 0;
191 static GQuark               quark_weak_refs = 0;
192 static GQuark               quark_toggle_refs = 0;
193 static GParamSpecPool      *pspec_pool = NULL;
194 static GObjectNotifyContext property_notify_context = { 0, };
195 static gulong               gobject_signals[LAST_SIGNAL] = { 0, };
196 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
197 G_LOCK_DEFINE_STATIC (construction_mutex);
198 static GSList *construction_objects = NULL;
199
200 /* --- functions --- */
201 #ifdef  G_ENABLE_DEBUG
202 #define IF_DEBUG(debug_type)    if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
203 G_LOCK_DEFINE_STATIC     (debug_objects);
204 static volatile GObject *g_trap_object_ref = NULL;
205 static guint             debug_objects_count = 0;
206 static GHashTable       *debug_objects_ht = NULL;
207
208 static void
209 debug_objects_foreach (gpointer key,
210                        gpointer value,
211                        gpointer user_data)
212 {
213   GObject *object = value;
214
215   g_message ("[%p] stale %s\tref_count=%u",
216              object,
217              G_OBJECT_TYPE_NAME (object),
218              object->ref_count);
219 }
220
221 static void
222 debug_objects_atexit (void)
223 {
224   IF_DEBUG (OBJECTS)
225     {
226       G_LOCK (debug_objects);
227       g_message ("stale GObjects: %u", debug_objects_count);
228       g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
229       G_UNLOCK (debug_objects);
230     }
231 }
232 #endif  /* G_ENABLE_DEBUG */
233
234 void
235 g_object_type_init (void)
236 {
237   static gboolean initialized = FALSE;
238   static const GTypeFundamentalInfo finfo = {
239     G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
240   };
241   static GTypeInfo info = {
242     sizeof (GObjectClass),
243     (GBaseInitFunc) g_object_base_class_init,
244     (GBaseFinalizeFunc) g_object_base_class_finalize,
245     (GClassInitFunc) g_object_do_class_init,
246     NULL        /* class_destroy */,
247     NULL        /* class_data */,
248     sizeof (GObject),
249     0           /* n_preallocs */,
250     (GInstanceInitFunc) g_object_init,
251     NULL,       /* value_table */
252   };
253   static const GTypeValueTable value_table = {
254     g_value_object_init,          /* value_init */
255     g_value_object_free_value,    /* value_free */
256     g_value_object_copy_value,    /* value_copy */
257     g_value_object_peek_pointer,  /* value_peek_pointer */
258     "p",                          /* collect_format */
259     g_value_object_collect_value, /* collect_value */
260     "p",                          /* lcopy_format */
261     g_value_object_lcopy_value,   /* lcopy_value */
262   };
263   GType type;
264   
265   g_return_if_fail (initialized == FALSE);
266   initialized = TRUE;
267   
268   /* G_TYPE_OBJECT
269    */
270   info.value_table = &value_table;
271   type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
272   g_assert (type == G_TYPE_OBJECT);
273   g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
274   
275 #ifdef  G_ENABLE_DEBUG
276   IF_DEBUG (OBJECTS)
277     {
278       debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
279       g_atexit (debug_objects_atexit);
280     }
281 #endif  /* G_ENABLE_DEBUG */
282 }
283
284 static void
285 g_object_base_class_init (GObjectClass *class)
286 {
287   GObjectClass *pclass = g_type_class_peek_parent (class);
288
289   /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
290   class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
291
292   if (pclass)
293     pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
294
295   /* reset instance specific fields and methods that don't get inherited */
296   class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
297   class->get_property = NULL;
298   class->set_property = NULL;
299 }
300
301 static void
302 g_object_base_class_finalize (GObjectClass *class)
303 {
304   GList *list, *node;
305   
306   _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
307
308   g_slist_free (class->construct_properties);
309   class->construct_properties = NULL;
310   list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
311   for (node = list; node; node = node->next)
312     {
313       GParamSpec *pspec = node->data;
314       
315       g_param_spec_pool_remove (pspec_pool, pspec);
316       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
317       g_param_spec_unref (pspec);
318     }
319   g_list_free (list);
320 }
321
322 static void
323 g_object_notify_dispatcher (GObject     *object,
324                             guint        n_pspecs,
325                             GParamSpec **pspecs)
326 {
327   G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
328 }
329
330 static void
331 g_object_do_class_init (GObjectClass *class)
332 {
333   /* read the comment about typedef struct CArray; on why not to change this quark */
334   quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
335
336   quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
337   quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
338   pspec_pool = g_param_spec_pool_new (TRUE);
339   property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
340   property_notify_context.dispatcher = g_object_notify_dispatcher;
341   
342   class->constructor = g_object_constructor;
343   class->set_property = g_object_do_set_property;
344   class->get_property = g_object_do_get_property;
345   class->dispose = g_object_real_dispose;
346   class->finalize = g_object_finalize;
347   class->dispatch_properties_changed = g_object_dispatch_properties_changed;
348   class->notify = NULL;
349
350   /**
351    * GObject::notify:
352    * @gobject: the object which received the signal.
353    * @pspec: the #GParamSpec of the property which changed.
354    *
355    * The notify signal is emitted on an object when one of its
356    * properties has been changed. Note that getting this signal
357    * doesn't guarantee that the value of the property has actually
358    * changed, it may also be emitted when the setter for the property
359    * is called to reinstate the previous value.
360    *
361    * This signal is typically used to obtain change notification for a
362    * single property, by specifying the property name as a detail in the
363    * g_signal_connect() call, like this:
364    * |[
365    * g_signal_connect (text_view->buffer, "notify::paste-target-list",
366    *                   G_CALLBACK (gtk_text_view_target_list_notify),
367    *                   text_view)
368    * ]|
369    * It is important to note that you must use
370    * <link linkend="canonical-parameter-name">canonical</link> parameter names as
371    * detail strings for the notify signal.
372    */
373   gobject_signals[NOTIFY] =
374     g_signal_new (g_intern_static_string ("notify"),
375                   G_TYPE_FROM_CLASS (class),
376                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
377                   G_STRUCT_OFFSET (GObjectClass, notify),
378                   NULL, NULL,
379                   g_cclosure_marshal_VOID__PARAM,
380                   G_TYPE_NONE,
381                   1, G_TYPE_PARAM);
382
383   /* Install a check function that we'll use to verify that classes that
384    * implement an interface implement all properties for that interface
385    */
386   g_type_add_interface_check (NULL, object_interface_check_properties);
387 }
388
389 static inline void
390 install_property_internal (GType       g_type,
391                            guint       property_id,
392                            GParamSpec *pspec)
393 {
394   if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
395     {
396       g_warning ("When installing property: type `%s' already has a property named `%s'",
397                  g_type_name (g_type),
398                  pspec->name);
399       return;
400     }
401
402   g_param_spec_ref (pspec);
403   g_param_spec_sink (pspec);
404   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
405   g_param_spec_pool_insert (pspec_pool, pspec, g_type);
406 }
407
408 /**
409  * g_object_class_install_property:
410  * @oclass: a #GObjectClass
411  * @property_id: the id for the new property
412  * @pspec: the #GParamSpec for the new property
413  *
414  * Installs a new property. This is usually done in the class initializer.
415  *
416  * Note that it is possible to redefine a property in a derived class,
417  * by installing a property with the same name. This can be useful at times,
418  * e.g. to change the range of allowed values or the default value.
419  */
420 void
421 g_object_class_install_property (GObjectClass *class,
422                                  guint         property_id,
423                                  GParamSpec   *pspec)
424 {
425   g_return_if_fail (G_IS_OBJECT_CLASS (class));
426   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
427
428   if (CLASS_HAS_DERIVED_CLASS (class))
429     g_error ("Attempt to add property %s::%s to class after it was derived",
430              G_OBJECT_CLASS_NAME (class), pspec->name);
431
432   class->flags |= CLASS_HAS_PROPS_FLAG;
433
434   if (pspec->flags & G_PARAM_WRITABLE)
435     g_return_if_fail (class->set_property != NULL);
436   if (pspec->flags & G_PARAM_READABLE)
437     g_return_if_fail (class->get_property != NULL);
438   g_return_if_fail (property_id > 0);
439   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
440   if (pspec->flags & G_PARAM_CONSTRUCT)
441     g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
442   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
443     g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
444
445   install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
446
447   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
448     class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
449
450   /* for property overrides of construct properties, we have to get rid
451    * of the overidden inherited construct property
452    */
453   pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
454   if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
455     class->construct_properties = g_slist_remove (class->construct_properties, pspec);
456 }
457
458 /**
459  * g_object_class_install_properties:
460  * @oclass: a #GObjectClass
461  * @n_pspecs: the length of the #GParamSpec<!-- -->s array
462  * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
463  *   defining the new properties
464  *
465  * Installs new properties from an array of #GParamSpec<!-- -->s. This is
466  * usually done in the class initializer.
467  *
468  * The property id of each property is the index of each #GParamSpec in
469  * the @pspecs array.
470  *
471  * The property id of 0 is treated specially by #GObject and it should not
472  * be used to store a #GParamSpec.
473  *
474  * This function should be used if you plan to use a static array of
475  * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
476  * class initialization:
477  *
478  * |[
479  * enum {
480  *   PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
481  * };
482  *
483  * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
484  *
485  * static void
486  * my_object_class_init (MyObjectClass *klass)
487  * {
488  *   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
489  *
490  *   obj_properties[PROP_FOO] =
491  *     g_param_spec_int ("foo", "Foo", "Foo",
492  *                       -1, G_MAXINT,
493  *                       0,
494  *                       G_PARAM_READWRITE);
495  *
496  *   obj_properties[PROP_BAR] =
497  *     g_param_spec_string ("bar", "Bar", "Bar",
498  *                          NULL,
499  *                          G_PARAM_READWRITE);
500  *
501  *   gobject_class->set_property = my_object_set_property;
502  *   gobject_class->get_property = my_object_get_property;
503  *   g_object_class_install_properties (gobject_class,
504  *                                      N_PROPERTIES,
505  *                                      obj_properties);
506  * }
507  * ]|
508  *
509  * allows calling g_object_notify_by_pspec() to notify of property changes:
510  *
511  * |[
512  * void
513  * my_object_set_foo (MyObject *self, gint foo)
514  * {
515  *   if (self->foo != foo)
516  *     {
517  *       self->foo = foo;
518  *       g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
519  *     }
520  *  }
521  * ]|
522  *
523  * Since: 2.26
524  */
525 void
526 g_object_class_install_properties (GObjectClass  *oclass,
527                                    guint          n_pspecs,
528                                    GParamSpec   **pspecs)
529 {
530   GType oclass_type, parent_type;
531   gint i;
532
533   g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
534   g_return_if_fail (n_pspecs > 1);
535   g_return_if_fail (pspecs[0] == NULL);
536
537   if (CLASS_HAS_DERIVED_CLASS (oclass))
538     g_error ("Attempt to add properties to %s after it was derived",
539              G_OBJECT_CLASS_NAME (oclass));
540
541   oclass_type = G_OBJECT_CLASS_TYPE (oclass);
542   parent_type = g_type_parent (oclass_type);
543
544   /* we skip the first element of the array as it would have a 0 prop_id */
545   for (i = 1; i < n_pspecs; i++)
546     {
547       GParamSpec *pspec = pspecs[i];
548
549       g_return_if_fail (pspec != NULL);
550
551       if (pspec->flags & G_PARAM_WRITABLE)
552         g_return_if_fail (oclass->set_property != NULL);
553       if (pspec->flags & G_PARAM_READABLE)
554         g_return_if_fail (oclass->get_property != NULL);
555       g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);      /* paranoid */
556       if (pspec->flags & G_PARAM_CONSTRUCT)
557         g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
558       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
559         g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
560
561       oclass->flags |= CLASS_HAS_PROPS_FLAG;
562       install_property_internal (oclass_type, i, pspec);
563
564       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
565         oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
566
567       /* for property overrides of construct properties, we have to get rid
568        * of the overidden inherited construct property
569        */
570       pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
571       if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572         oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
573     }
574 }
575
576 /**
577  * g_object_interface_install_property:
578  * @g_iface: any interface vtable for the interface, or the default
579  *  vtable for the interface.
580  * @pspec: the #GParamSpec for the new property
581  *
582  * Add a property to an interface; this is only useful for interfaces
583  * that are added to GObject-derived types. Adding a property to an
584  * interface forces all objects classes with that interface to have a
585  * compatible property. The compatible property could be a newly
586  * created #GParamSpec, but normally
587  * g_object_class_override_property() will be used so that the object
588  * class only needs to provide an implementation and inherits the
589  * property description, default value, bounds, and so forth from the
590  * interface property.
591  *
592  * This function is meant to be called from the interface's default
593  * vtable initialization function (the @class_init member of
594  * #GTypeInfo.) It must not be called after after @class_init has
595  * been called for any object types implementing this interface.
596  *
597  * Since: 2.4
598  */
599 void
600 g_object_interface_install_property (gpointer      g_iface,
601                                      GParamSpec   *pspec)
602 {
603   GTypeInterface *iface_class = g_iface;
604         
605   g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
606   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
607   g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
608   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
609                     
610   install_property_internal (iface_class->g_type, 0, pspec);
611 }
612
613 /**
614  * g_object_class_find_property:
615  * @oclass: a #GObjectClass
616  * @property_name: the name of the property to look up
617  *
618  * Looks up the #GParamSpec for a property of a class.
619  *
620  * Returns: (transfer none): the #GParamSpec for the property, or
621  *          %NULL if the class doesn't have a property of that name
622  */
623 GParamSpec*
624 g_object_class_find_property (GObjectClass *class,
625                               const gchar  *property_name)
626 {
627   GParamSpec *pspec;
628   GParamSpec *redirect;
629         
630   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
631   g_return_val_if_fail (property_name != NULL, NULL);
632   
633   pspec = g_param_spec_pool_lookup (pspec_pool,
634                                     property_name,
635                                     G_OBJECT_CLASS_TYPE (class),
636                                     TRUE);
637   if (pspec)
638     {
639       redirect = g_param_spec_get_redirect_target (pspec);
640       if (redirect)
641         return redirect;
642       else
643         return pspec;
644     }
645   else
646     return NULL;
647 }
648
649 /**
650  * g_object_interface_find_property:
651  * @g_iface: any interface vtable for the interface, or the default
652  *  vtable for the interface
653  * @property_name: name of a property to lookup.
654  *
655  * Find the #GParamSpec with the given name for an
656  * interface. Generally, the interface vtable passed in as @g_iface
657  * will be the default vtable from g_type_default_interface_ref(), or,
658  * if you know the interface has already been loaded,
659  * g_type_default_interface_peek().
660  *
661  * Since: 2.4
662  *
663  * Returns: (transfer none): the #GParamSpec for the property of the
664  *          interface with the name @property_name, or %NULL if no
665  *          such property exists.
666  */
667 GParamSpec*
668 g_object_interface_find_property (gpointer      g_iface,
669                                   const gchar  *property_name)
670 {
671   GTypeInterface *iface_class = g_iface;
672         
673   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
674   g_return_val_if_fail (property_name != NULL, NULL);
675   
676   return g_param_spec_pool_lookup (pspec_pool,
677                                    property_name,
678                                    iface_class->g_type,
679                                    FALSE);
680 }
681
682 /**
683  * g_object_class_override_property:
684  * @oclass: a #GObjectClass
685  * @property_id: the new property ID
686  * @name: the name of a property registered in a parent class or
687  *  in an interface of this class.
688  *
689  * Registers @property_id as referring to a property with the
690  * name @name in a parent class or in an interface implemented
691  * by @oclass. This allows this class to <firstterm>override</firstterm>
692  * a property implementation in a parent class or to provide
693  * the implementation of a property from an interface.
694  *
695  * <note>
696  * Internally, overriding is implemented by creating a property of type
697  * #GParamSpecOverride; generally operations that query the properties of
698  * the object class, such as g_object_class_find_property() or
699  * g_object_class_list_properties() will return the overridden
700  * property. However, in one case, the @construct_properties argument of
701  * the @constructor virtual function, the #GParamSpecOverride is passed
702  * instead, so that the @param_id field of the #GParamSpec will be
703  * correct.  For virtually all uses, this makes no difference. If you
704  * need to get the overridden property, you can call
705  * g_param_spec_get_redirect_target().
706  * </note>
707  *
708  * Since: 2.4
709  */
710 void
711 g_object_class_override_property (GObjectClass *oclass,
712                                   guint         property_id,
713                                   const gchar  *name)
714 {
715   GParamSpec *overridden = NULL;
716   GParamSpec *new;
717   GType parent_type;
718   
719   g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
720   g_return_if_fail (property_id > 0);
721   g_return_if_fail (name != NULL);
722
723   /* Find the overridden property; first check parent types
724    */
725   parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
726   if (parent_type != G_TYPE_NONE)
727     overridden = g_param_spec_pool_lookup (pspec_pool,
728                                            name,
729                                            parent_type,
730                                            TRUE);
731   if (!overridden)
732     {
733       GType *ifaces;
734       guint n_ifaces;
735       
736       /* Now check interfaces
737        */
738       ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
739       while (n_ifaces-- && !overridden)
740         {
741           overridden = g_param_spec_pool_lookup (pspec_pool,
742                                                  name,
743                                                  ifaces[n_ifaces],
744                                                  FALSE);
745         }
746       
747       g_free (ifaces);
748     }
749
750   if (!overridden)
751     {
752       g_warning ("%s: Can't find property to override for '%s::%s'",
753                  G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
754       return;
755     }
756
757   new = g_param_spec_override (name, overridden);
758   g_object_class_install_property (oclass, property_id, new);
759 }
760
761 /**
762  * g_object_class_list_properties:
763  * @oclass: a #GObjectClass
764  * @n_properties: (out): return location for the length of the returned array
765  *
766  * Get an array of #GParamSpec* for all properties of a class.
767  *
768  * Returns: (array length=n_properties) (transfer container): an array of
769  *          #GParamSpec* which should be freed after use
770  */
771 GParamSpec** /* free result */
772 g_object_class_list_properties (GObjectClass *class,
773                                 guint        *n_properties_p)
774 {
775   GParamSpec **pspecs;
776   guint n;
777
778   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
779
780   pspecs = g_param_spec_pool_list (pspec_pool,
781                                    G_OBJECT_CLASS_TYPE (class),
782                                    &n);
783   if (n_properties_p)
784     *n_properties_p = n;
785
786   return pspecs;
787 }
788
789 /**
790  * g_object_interface_list_properties:
791  * @g_iface: any interface vtable for the interface, or the default
792  *  vtable for the interface
793  * @n_properties_p: (out): location to store number of properties returned.
794  *
795  * Lists the properties of an interface.Generally, the interface
796  * vtable passed in as @g_iface will be the default vtable from
797  * g_type_default_interface_ref(), or, if you know the interface has
798  * already been loaded, g_type_default_interface_peek().
799  *
800  * Since: 2.4
801  *
802  * Returns: (array length=n_properties_p) (transfer container): a
803  *          pointer to an array of pointers to #GParamSpec
804  *          structures. The paramspecs are owned by GLib, but the
805  *          array should be freed with g_free() when you are done with
806  *          it.
807  */
808 GParamSpec**
809 g_object_interface_list_properties (gpointer      g_iface,
810                                     guint        *n_properties_p)
811 {
812   GTypeInterface *iface_class = g_iface;
813   GParamSpec **pspecs;
814   guint n;
815
816   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
817
818   pspecs = g_param_spec_pool_list (pspec_pool,
819                                    iface_class->g_type,
820                                    &n);
821   if (n_properties_p)
822     *n_properties_p = n;
823
824   return pspecs;
825 }
826
827 static void
828 g_object_init (GObject          *object,
829                GObjectClass     *class)
830 {
831   object->ref_count = 1;
832   g_datalist_init (&object->qdata);
833
834   if (CLASS_HAS_PROPS (class))
835     {
836       /* freeze object's notification queue, g_object_newv() preserves pairedness */
837       g_object_notify_queue_freeze (object, &property_notify_context);
838     }
839
840   if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
841     {
842       /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
843       G_LOCK (construction_mutex);
844       construction_objects = g_slist_prepend (construction_objects, object);
845       G_UNLOCK (construction_mutex);
846     }
847
848 #ifdef  G_ENABLE_DEBUG
849   IF_DEBUG (OBJECTS)
850     {
851       G_LOCK (debug_objects);
852       debug_objects_count++;
853       g_hash_table_insert (debug_objects_ht, object, object);
854       G_UNLOCK (debug_objects);
855     }
856 #endif  /* G_ENABLE_DEBUG */
857 }
858
859 static void
860 g_object_do_set_property (GObject      *object,
861                           guint         property_id,
862                           const GValue *value,
863                           GParamSpec   *pspec)
864 {
865   switch (property_id)
866     {
867     default:
868       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
869       break;
870     }
871 }
872
873 static void
874 g_object_do_get_property (GObject     *object,
875                           guint        property_id,
876                           GValue      *value,
877                           GParamSpec  *pspec)
878 {
879   switch (property_id)
880     {
881     default:
882       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
883       break;
884     }
885 }
886
887 static void
888 g_object_real_dispose (GObject *object)
889 {
890   g_signal_handlers_destroy (object);
891   g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
892   g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
893 }
894
895 static void
896 g_object_finalize (GObject *object)
897 {
898   g_datalist_clear (&object->qdata);
899   
900 #ifdef  G_ENABLE_DEBUG
901   IF_DEBUG (OBJECTS)
902     {
903       G_LOCK (debug_objects);
904       g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
905       g_hash_table_remove (debug_objects_ht, object);
906       debug_objects_count--;
907       G_UNLOCK (debug_objects);
908     }
909 #endif  /* G_ENABLE_DEBUG */
910 }
911
912
913 static void
914 g_object_dispatch_properties_changed (GObject     *object,
915                                       guint        n_pspecs,
916                                       GParamSpec **pspecs)
917 {
918   guint i;
919
920   for (i = 0; i < n_pspecs; i++)
921     g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
922 }
923
924 /**
925  * g_object_run_dispose:
926  * @object: a #GObject
927  *
928  * Releases all references to other objects. This can be used to break
929  * reference cycles.
930  *
931  * This functions should only be called from object system implementations.
932  */
933 void
934 g_object_run_dispose (GObject *object)
935 {
936   g_return_if_fail (G_IS_OBJECT (object));
937   g_return_if_fail (object->ref_count > 0);
938
939   g_object_ref (object);
940   TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
941   G_OBJECT_GET_CLASS (object)->dispose (object);
942   TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
943   g_object_unref (object);
944 }
945
946 /**
947  * g_object_freeze_notify:
948  * @object: a #GObject
949  *
950  * Increases the freeze count on @object. If the freeze count is
951  * non-zero, the emission of "notify" signals on @object is
952  * stopped. The signals are queued until the freeze count is decreased
953  * to zero.
954  *
955  * This is necessary for accessors that modify multiple properties to prevent
956  * premature notification while the object is still being modified.
957  */
958 void
959 g_object_freeze_notify (GObject *object)
960 {
961   g_return_if_fail (G_IS_OBJECT (object));
962
963   if (g_atomic_int_get (&object->ref_count) == 0)
964     return;
965
966   g_object_ref (object);
967   g_object_notify_queue_freeze (object, &property_notify_context);
968   g_object_unref (object);
969 }
970
971 static inline void
972 g_object_notify_by_spec_internal (GObject    *object,
973                                   GParamSpec *pspec)
974 {
975   GObjectNotifyQueue *nqueue;
976
977   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
978   g_object_notify_queue_add (object, nqueue, pspec);
979   g_object_notify_queue_thaw (object, nqueue);
980 }
981
982 /**
983  * g_object_notify:
984  * @object: a #GObject
985  * @property_name: the name of a property installed on the class of @object.
986  *
987  * Emits a "notify" signal for the property @property_name on @object.
988  *
989  * When possible, eg. when signaling a property change from within the class
990  * that registered the property, you should use g_object_notify_by_pspec()
991  * instead.
992  */
993 void
994 g_object_notify (GObject     *object,
995                  const gchar *property_name)
996 {
997   GParamSpec *pspec;
998   
999   g_return_if_fail (G_IS_OBJECT (object));
1000   g_return_if_fail (property_name != NULL);
1001   if (g_atomic_int_get (&object->ref_count) == 0)
1002     return;
1003   
1004   g_object_ref (object);
1005   /* We don't need to get the redirect target
1006    * (by, e.g. calling g_object_class_find_property())
1007    * because g_object_notify_queue_add() does that
1008    */
1009   pspec = g_param_spec_pool_lookup (pspec_pool,
1010                                     property_name,
1011                                     G_OBJECT_TYPE (object),
1012                                     TRUE);
1013
1014   if (!pspec)
1015     g_warning ("%s: object class `%s' has no property named `%s'",
1016                G_STRFUNC,
1017                G_OBJECT_TYPE_NAME (object),
1018                property_name);
1019   else
1020     g_object_notify_by_spec_internal (object, pspec);
1021   g_object_unref (object);
1022 }
1023
1024 /**
1025  * g_object_notify_by_pspec:
1026  * @object: a #GObject
1027  * @pspec: the #GParamSpec of a property installed on the class of @object.
1028  *
1029  * Emits a "notify" signal for the property specified by @pspec on @object.
1030  *
1031  * This function omits the property name lookup, hence it is faster than
1032  * g_object_notify().
1033  *
1034  * One way to avoid using g_object_notify() from within the
1035  * class that registered the properties, and using g_object_notify_by_pspec()
1036  * instead, is to store the GParamSpec used with
1037  * g_object_class_install_property() inside a static array, e.g.:
1038  *
1039  *|[
1040  *   enum
1041  *   {
1042  *     PROP_0,
1043  *     PROP_FOO,
1044  *     PROP_LAST
1045  *   };
1046  *
1047  *   static GParamSpec *properties[PROP_LAST];
1048  *
1049  *   static void
1050  *   my_object_class_init (MyObjectClass *klass)
1051  *   {
1052  *     properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1053  *                                              0, 100,
1054  *                                              50,
1055  *                                              G_PARAM_READWRITE);
1056  *     g_object_class_install_property (gobject_class,
1057  *                                      PROP_FOO,
1058  *                                      properties[PROP_FOO]);
1059  *   }
1060  * ]|
1061  *
1062  * and then notify a change on the "foo" property with:
1063  *
1064  * |[
1065  *   g_object_notify_by_pspec (self, properties[PROP_FOO]);
1066  * ]|
1067  *
1068  * Since: 2.26
1069  */
1070 void
1071 g_object_notify_by_pspec (GObject    *object,
1072                           GParamSpec *pspec)
1073 {
1074
1075   g_return_if_fail (G_IS_OBJECT (object));
1076   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1077
1078   g_object_ref (object);
1079   g_object_notify_by_spec_internal (object, pspec);
1080   g_object_unref (object);
1081 }
1082
1083 /**
1084  * g_object_thaw_notify:
1085  * @object: a #GObject
1086  *
1087  * Reverts the effect of a previous call to
1088  * g_object_freeze_notify(). The freeze count is decreased on @object
1089  * and when it reaches zero, all queued "notify" signals are emitted.
1090  *
1091  * It is an error to call this function when the freeze count is zero.
1092  */
1093 void
1094 g_object_thaw_notify (GObject *object)
1095 {
1096   GObjectNotifyQueue *nqueue;
1097   
1098   g_return_if_fail (G_IS_OBJECT (object));
1099   if (g_atomic_int_get (&object->ref_count) == 0)
1100     return;
1101   
1102   g_object_ref (object);
1103
1104   /* FIXME: Freezing is the only way to get at the notify queue.
1105    * So we freeze once and then thaw twice.
1106    */
1107   nqueue = g_object_notify_queue_freeze (object,  &property_notify_context);
1108   g_object_notify_queue_thaw (object, nqueue);
1109   g_object_notify_queue_thaw (object, nqueue);
1110
1111   g_object_unref (object);
1112 }
1113
1114 static inline void
1115 object_get_property (GObject     *object,
1116                      GParamSpec  *pspec,
1117                      GValue      *value)
1118 {
1119   GObjectClass *class = g_type_class_peek (pspec->owner_type);
1120   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1121   GParamSpec *redirect;
1122
1123   redirect = g_param_spec_get_redirect_target (pspec);
1124   if (redirect)
1125     pspec = redirect;    
1126   
1127   class->get_property (object, param_id, value, pspec);
1128 }
1129
1130 static inline void
1131 object_set_property (GObject             *object,
1132                      GParamSpec          *pspec,
1133                      const GValue        *value,
1134                      GObjectNotifyQueue  *nqueue)
1135 {
1136   GValue tmp_value = { 0, };
1137   GObjectClass *class = g_type_class_peek (pspec->owner_type);
1138   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1139   GParamSpec *redirect;
1140   static gchar* enable_diagnostic = NULL;
1141
1142   redirect = g_param_spec_get_redirect_target (pspec);
1143   if (redirect)
1144     pspec = redirect;
1145
1146   if (G_UNLIKELY (!enable_diagnostic))
1147     {
1148       enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1149       if (!enable_diagnostic)
1150         enable_diagnostic = "0";
1151     }
1152
1153   if (enable_diagnostic[0] == '1')
1154     {
1155       if (pspec->flags & G_PARAM_DEPRECATED)
1156         g_warning ("The property %s::%s is deprecated and shouldn't be used "
1157                    "anymore. It will be removed in a future version.",
1158                    G_OBJECT_TYPE_NAME (object), pspec->name);
1159     }
1160
1161   /* provide a copy to work from, convert (if necessary) and validate */
1162   g_value_init (&tmp_value, pspec->value_type);
1163   if (!g_value_transform (value, &tmp_value))
1164     g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1165                pspec->name,
1166                g_type_name (pspec->value_type),
1167                G_VALUE_TYPE_NAME (value));
1168   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1169     {
1170       gchar *contents = g_strdup_value_contents (value);
1171
1172       g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1173                  contents,
1174                  G_VALUE_TYPE_NAME (value),
1175                  pspec->name,
1176                  g_type_name (pspec->value_type));
1177       g_free (contents);
1178     }
1179   else
1180     {
1181       class->set_property (object, param_id, &tmp_value, pspec);
1182       g_object_notify_queue_add (object, nqueue, pspec);
1183     }
1184   g_value_unset (&tmp_value);
1185 }
1186
1187 static void
1188 object_interface_check_properties (gpointer func_data,
1189                                    gpointer g_iface)
1190 {
1191   GTypeInterface *iface_class = g_iface;
1192   GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1193   GType iface_type = iface_class->g_type;
1194   GParamSpec **pspecs;
1195   guint n;
1196
1197   if (!G_IS_OBJECT_CLASS (class))
1198     return;
1199
1200   pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1201
1202   while (n--)
1203     {
1204       GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1205                                                           pspecs[n]->name,
1206                                                           G_OBJECT_CLASS_TYPE (class),
1207                                                           TRUE);
1208       
1209       if (!class_pspec)
1210         {
1211           g_critical ("Object class %s doesn't implement property "
1212                       "'%s' from interface '%s'",
1213                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1214                       pspecs[n]->name,
1215                       g_type_name (iface_type));
1216
1217           continue;
1218         }
1219
1220       /* The implementation paramspec must have a less restrictive
1221        * type than the interface parameter spec for set() and a
1222        * more restrictive type for get(). We just require equality,
1223        * rather than doing something more complicated checking
1224        * the READABLE and WRITABLE flags. We also simplify here
1225        * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1226        */
1227       if (class_pspec &&
1228           !g_type_is_a (pspecs[n]->value_type,
1229                         class_pspec->value_type))
1230         {
1231           g_critical ("Property '%s' on class '%s' has type '%s' "
1232                       "which is different from the type '%s', "
1233                       "of the property on interface '%s'\n",
1234                       pspecs[n]->name,
1235                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1236                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1237                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1238                       g_type_name (iface_type));
1239         }
1240       
1241 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1242       
1243       /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1244        * READABLE and WRITABLE remove restrictions. The implementation
1245        * paramspec must have less restrictive flags.
1246        */
1247       if (class_pspec &&
1248           (!SUBSET (class_pspec->flags,
1249                     pspecs[n]->flags,
1250                     G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1251            !SUBSET (pspecs[n]->flags,
1252                     class_pspec->flags,
1253                     G_PARAM_READABLE | G_PARAM_WRITABLE)))
1254         {
1255           g_critical ("Flags for property '%s' on class '%s' "
1256                       "are not compatible with the property on"
1257                       "interface '%s'\n",
1258                       pspecs[n]->name,
1259                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1260                       g_type_name (iface_type));
1261         }
1262 #undef SUBSET     
1263     }
1264   
1265   g_free (pspecs);
1266 }
1267
1268 GType
1269 g_object_get_type (void)
1270 {
1271     return G_TYPE_OBJECT;
1272 }
1273
1274 /**
1275  * g_object_new: (skip)
1276  * @object_type: the type id of the #GObject subtype to instantiate
1277  * @first_property_name: the name of the first property
1278  * @...: the value of the first property, followed optionally by more
1279  *  name/value pairs, followed by %NULL
1280  *
1281  * Creates a new instance of a #GObject subtype and sets its properties.
1282  *
1283  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1284  * which are not explicitly specified are set to their default values.
1285  *
1286  * Returns: (transfer full): a new instance of @object_type
1287  */
1288 gpointer
1289 g_object_new (GType        object_type,
1290               const gchar *first_property_name,
1291               ...)
1292 {
1293   GObject *object;
1294   va_list var_args;
1295   
1296   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1297   
1298   /* short circuit for calls supplying no properties */
1299   if (!first_property_name)
1300     return g_object_newv (object_type, 0, NULL);
1301
1302   va_start (var_args, first_property_name);
1303   object = g_object_new_valist (object_type, first_property_name, var_args);
1304   va_end (var_args);
1305   
1306   return object;
1307 }
1308
1309 static gboolean
1310 slist_maybe_remove (GSList       **slist,
1311                     gconstpointer  data)
1312 {
1313   GSList *last = NULL, *node = *slist;
1314   while (node)
1315     {
1316       if (node->data == data)
1317         {
1318           if (last)
1319             last->next = node->next;
1320           else
1321             *slist = node->next;
1322           g_slist_free_1 (node);
1323           return TRUE;
1324         }
1325       last = node;
1326       node = last->next;
1327     }
1328   return FALSE;
1329 }
1330
1331 static inline gboolean
1332 object_in_construction_list (GObject *object)
1333 {
1334   gboolean in_construction;
1335   G_LOCK (construction_mutex);
1336   in_construction = g_slist_find (construction_objects, object) != NULL;
1337   G_UNLOCK (construction_mutex);
1338   return in_construction;
1339 }
1340
1341 /**
1342  * g_object_newv:
1343  * @object_type: the type id of the #GObject subtype to instantiate
1344  * @n_parameters: the length of the @parameters array
1345  * @parameters: (array length=n_parameters): an array of #GParameter
1346  *
1347  * Creates a new instance of a #GObject subtype and sets its properties.
1348  *
1349  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1350  * which are not explicitly specified are set to their default values.
1351  *
1352  * Rename to: g_object_new
1353  * Returns: (type GObject.Object) (transfer full): a new instance of
1354  * @object_type
1355  */
1356 gpointer
1357 g_object_newv (GType       object_type,
1358                guint       n_parameters,
1359                GParameter *parameters)
1360 {
1361   GObjectConstructParam *cparams = NULL, *oparams;
1362   GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1363   GObject *object;
1364   GObjectClass *class, *unref_class = NULL;
1365   GSList *slist;
1366   guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1367   GValue *cvalues;
1368   GList *clist = NULL;
1369   gboolean newly_constructed;
1370   guint i;
1371
1372   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1373
1374   class = g_type_class_peek_static (object_type);
1375   if (!class)
1376     class = unref_class = g_type_class_ref (object_type);
1377   for (slist = class->construct_properties; slist; slist = slist->next)
1378     {
1379       clist = g_list_prepend (clist, slist->data);
1380       n_total_cparams += 1;
1381     }
1382
1383   if (n_parameters == 0 && n_total_cparams == 0)
1384     {
1385       /* This is a simple object with no construct properties, and
1386        * no properties are being set, so short circuit the parameter
1387        * handling. This speeds up simple object construction.
1388        */
1389       oparams = NULL;
1390       object = class->constructor (object_type, 0, NULL);
1391       goto did_construction;
1392     }
1393
1394   /* collect parameters, sort into construction and normal ones */
1395   oparams = g_new (GObjectConstructParam, n_parameters);
1396   cparams = g_new (GObjectConstructParam, n_total_cparams);
1397   for (i = 0; i < n_parameters; i++)
1398     {
1399       GValue *value = &parameters[i].value;
1400       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1401                                                     parameters[i].name,
1402                                                     object_type,
1403                                                     TRUE);
1404       if (!pspec)
1405         {
1406           g_warning ("%s: object class `%s' has no property named `%s'",
1407                      G_STRFUNC,
1408                      g_type_name (object_type),
1409                      parameters[i].name);
1410           continue;
1411         }
1412       if (!(pspec->flags & G_PARAM_WRITABLE))
1413         {
1414           g_warning ("%s: property `%s' of object class `%s' is not writable",
1415                      G_STRFUNC,
1416                      pspec->name,
1417                      g_type_name (object_type));
1418           continue;
1419         }
1420       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1421         {
1422           GList *list = g_list_find (clist, pspec);
1423
1424           if (!list)
1425             {
1426               g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1427                          G_STRFUNC, pspec->name, g_type_name (object_type));
1428               continue;
1429             }
1430           cparams[n_cparams].pspec = pspec;
1431           cparams[n_cparams].value = value;
1432           n_cparams++;
1433           if (!list->prev)
1434             clist = list->next;
1435           else
1436             list->prev->next = list->next;
1437           if (list->next)
1438             list->next->prev = list->prev;
1439           g_list_free_1 (list);
1440         }
1441       else
1442         {
1443           oparams[n_oparams].pspec = pspec;
1444           oparams[n_oparams].value = value;
1445           n_oparams++;
1446         }
1447     }
1448
1449   /* set remaining construction properties to default values */
1450   n_cvalues = n_total_cparams - n_cparams;
1451   cvalues = g_new (GValue, n_cvalues);
1452   while (clist)
1453     {
1454       GList *tmp = clist->next;
1455       GParamSpec *pspec = clist->data;
1456       GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1457
1458       value->g_type = 0;
1459       g_value_init (value, pspec->value_type);
1460       g_param_value_set_default (pspec, value);
1461
1462       cparams[n_cparams].pspec = pspec;
1463       cparams[n_cparams].value = value;
1464       n_cparams++;
1465
1466       g_list_free_1 (clist);
1467       clist = tmp;
1468     }
1469
1470   /* construct object from construction parameters */
1471   object = class->constructor (object_type, n_total_cparams, cparams);
1472   /* free construction values */
1473   g_free (cparams);
1474   while (n_cvalues--)
1475     g_value_unset (cvalues + n_cvalues);
1476   g_free (cvalues);
1477
1478  did_construction:
1479   if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1480     {
1481       /* adjust freeze_count according to g_object_init() and remaining properties */
1482       G_LOCK (construction_mutex);
1483       newly_constructed = slist_maybe_remove (&construction_objects, object);
1484       G_UNLOCK (construction_mutex);
1485     }
1486   else
1487     newly_constructed = TRUE;
1488
1489   if (CLASS_HAS_PROPS (class))
1490     {
1491       if (newly_constructed || n_oparams)
1492         nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1493       if (newly_constructed)
1494         g_object_notify_queue_thaw (object, nqueue);
1495     }
1496
1497   /* run 'constructed' handler if there is one */
1498   if (newly_constructed && class->constructed)
1499     class->constructed (object);
1500
1501   /* set remaining properties */
1502   for (i = 0; i < n_oparams; i++)
1503     object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1504   g_free (oparams);
1505
1506   if (CLASS_HAS_PROPS (class))
1507     {
1508       /* release our own freeze count and handle notifications */
1509       if (newly_constructed || n_oparams)
1510         g_object_notify_queue_thaw (object, nqueue);
1511     }
1512
1513   if (unref_class)
1514     g_type_class_unref (unref_class);
1515
1516   return object;
1517 }
1518
1519 /**
1520  * g_object_new_valist: (skip)
1521  * @object_type: the type id of the #GObject subtype to instantiate
1522  * @first_property_name: the name of the first property
1523  * @var_args: the value of the first property, followed optionally by more
1524  *  name/value pairs, followed by %NULL
1525  *
1526  * Creates a new instance of a #GObject subtype and sets its properties.
1527  *
1528  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1529  * which are not explicitly specified are set to their default values.
1530  *
1531  * Returns: a new instance of @object_type
1532  */
1533 GObject*
1534 g_object_new_valist (GType        object_type,
1535                      const gchar *first_property_name,
1536                      va_list      var_args)
1537 {
1538   GObjectClass *class;
1539   GParameter *params;
1540   const gchar *name;
1541   GObject *object;
1542   guint n_params = 0, n_alloced_params = 16;
1543   
1544   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1545
1546   if (!first_property_name)
1547     return g_object_newv (object_type, 0, NULL);
1548
1549   class = g_type_class_ref (object_type);
1550
1551   params = g_new0 (GParameter, n_alloced_params);
1552   name = first_property_name;
1553   while (name)
1554     {
1555       gchar *error = NULL;
1556       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1557                                                     name,
1558                                                     object_type,
1559                                                     TRUE);
1560       if (!pspec)
1561         {
1562           g_warning ("%s: object class `%s' has no property named `%s'",
1563                      G_STRFUNC,
1564                      g_type_name (object_type),
1565                      name);
1566           break;
1567         }
1568       if (n_params >= n_alloced_params)
1569         {
1570           n_alloced_params += 16;
1571           params = g_renew (GParameter, params, n_alloced_params);
1572           memset (params + n_params, 0, 16 * (sizeof *params));
1573         }
1574       params[n_params].name = name;
1575       G_VALUE_COLLECT_INIT (&params[n_params].value, pspec->value_type,
1576                             var_args, 0, &error);
1577       if (error)
1578         {
1579           g_warning ("%s: %s", G_STRFUNC, error);
1580           g_free (error);
1581           g_value_unset (&params[n_params].value);
1582           break;
1583         }
1584       n_params++;
1585       name = va_arg (var_args, gchar*);
1586     }
1587
1588   object = g_object_newv (object_type, n_params, params);
1589
1590   while (n_params--)
1591     g_value_unset (&params[n_params].value);
1592   g_free (params);
1593
1594   g_type_class_unref (class);
1595
1596   return object;
1597 }
1598
1599 static GObject*
1600 g_object_constructor (GType                  type,
1601                       guint                  n_construct_properties,
1602                       GObjectConstructParam *construct_params)
1603 {
1604   GObject *object;
1605
1606   /* create object */
1607   object = (GObject*) g_type_create_instance (type);
1608   
1609   /* set construction parameters */
1610   if (n_construct_properties)
1611     {
1612       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1613       
1614       /* set construct properties */
1615       while (n_construct_properties--)
1616         {
1617           GValue *value = construct_params->value;
1618           GParamSpec *pspec = construct_params->pspec;
1619
1620           construct_params++;
1621           object_set_property (object, pspec, value, nqueue);
1622         }
1623       g_object_notify_queue_thaw (object, nqueue);
1624       /* the notification queue is still frozen from g_object_init(), so
1625        * we don't need to handle it here, g_object_newv() takes
1626        * care of that
1627        */
1628     }
1629
1630   return object;
1631 }
1632
1633 /**
1634  * g_object_set_valist: (skip)
1635  * @object: a #GObject
1636  * @first_property_name: name of the first property to set
1637  * @var_args: value for the first property, followed optionally by more
1638  *  name/value pairs, followed by %NULL
1639  *
1640  * Sets properties on an object.
1641  */
1642 void
1643 g_object_set_valist (GObject     *object,
1644                      const gchar *first_property_name,
1645                      va_list      var_args)
1646 {
1647   GObjectNotifyQueue *nqueue;
1648   const gchar *name;
1649   
1650   g_return_if_fail (G_IS_OBJECT (object));
1651   
1652   g_object_ref (object);
1653   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1654   
1655   name = first_property_name;
1656   while (name)
1657     {
1658       GValue value = { 0, };
1659       GParamSpec *pspec;
1660       gchar *error = NULL;
1661       
1662       pspec = g_param_spec_pool_lookup (pspec_pool,
1663                                         name,
1664                                         G_OBJECT_TYPE (object),
1665                                         TRUE);
1666       if (!pspec)
1667         {
1668           g_warning ("%s: object class `%s' has no property named `%s'",
1669                      G_STRFUNC,
1670                      G_OBJECT_TYPE_NAME (object),
1671                      name);
1672           break;
1673         }
1674       if (!(pspec->flags & G_PARAM_WRITABLE))
1675         {
1676           g_warning ("%s: property `%s' of object class `%s' is not writable",
1677                      G_STRFUNC,
1678                      pspec->name,
1679                      G_OBJECT_TYPE_NAME (object));
1680           break;
1681         }
1682       if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1683         {
1684           g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1685                      G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1686           break;
1687         }
1688
1689       G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1690                             0, &error);
1691       if (error)
1692         {
1693           g_warning ("%s: %s", G_STRFUNC, error);
1694           g_free (error);
1695           g_value_unset (&value);
1696           break;
1697         }
1698       
1699       object_set_property (object, pspec, &value, nqueue);
1700       g_value_unset (&value);
1701       
1702       name = va_arg (var_args, gchar*);
1703     }
1704
1705   g_object_notify_queue_thaw (object, nqueue);
1706   g_object_unref (object);
1707 }
1708
1709 /**
1710  * g_object_get_valist: (skip)
1711  * @object: a #GObject
1712  * @first_property_name: name of the first property to get
1713  * @var_args: return location for the first property, followed optionally by more
1714  *  name/return location pairs, followed by %NULL
1715  *
1716  * Gets properties of an object.
1717  *
1718  * In general, a copy is made of the property contents and the caller
1719  * is responsible for freeing the memory in the appropriate manner for
1720  * the type, for instance by calling g_free() or g_object_unref().
1721  *
1722  * See g_object_get().
1723  */
1724 void
1725 g_object_get_valist (GObject     *object,
1726                      const gchar *first_property_name,
1727                      va_list      var_args)
1728 {
1729   const gchar *name;
1730   
1731   g_return_if_fail (G_IS_OBJECT (object));
1732   
1733   g_object_ref (object);
1734   
1735   name = first_property_name;
1736   
1737   while (name)
1738     {
1739       GValue value = { 0, };
1740       GParamSpec *pspec;
1741       gchar *error;
1742       
1743       pspec = g_param_spec_pool_lookup (pspec_pool,
1744                                         name,
1745                                         G_OBJECT_TYPE (object),
1746                                         TRUE);
1747       if (!pspec)
1748         {
1749           g_warning ("%s: object class `%s' has no property named `%s'",
1750                      G_STRFUNC,
1751                      G_OBJECT_TYPE_NAME (object),
1752                      name);
1753           break;
1754         }
1755       if (!(pspec->flags & G_PARAM_READABLE))
1756         {
1757           g_warning ("%s: property `%s' of object class `%s' is not readable",
1758                      G_STRFUNC,
1759                      pspec->name,
1760                      G_OBJECT_TYPE_NAME (object));
1761           break;
1762         }
1763       
1764       g_value_init (&value, pspec->value_type);
1765       
1766       object_get_property (object, pspec, &value);
1767       
1768       G_VALUE_LCOPY (&value, var_args, 0, &error);
1769       if (error)
1770         {
1771           g_warning ("%s: %s", G_STRFUNC, error);
1772           g_free (error);
1773           g_value_unset (&value);
1774           break;
1775         }
1776       
1777       g_value_unset (&value);
1778       
1779       name = va_arg (var_args, gchar*);
1780     }
1781   
1782   g_object_unref (object);
1783 }
1784
1785 /**
1786  * g_object_set: (skip)
1787  * @object: a #GObject
1788  * @first_property_name: name of the first property to set
1789  * @...: value for the first property, followed optionally by more
1790  *  name/value pairs, followed by %NULL
1791  *
1792  * Sets properties on an object.
1793  */
1794 void
1795 g_object_set (gpointer     _object,
1796               const gchar *first_property_name,
1797               ...)
1798 {
1799   GObject *object = _object;
1800   va_list var_args;
1801   
1802   g_return_if_fail (G_IS_OBJECT (object));
1803   
1804   va_start (var_args, first_property_name);
1805   g_object_set_valist (object, first_property_name, var_args);
1806   va_end (var_args);
1807 }
1808
1809 /**
1810  * g_object_get: (skip)
1811  * @object: a #GObject
1812  * @first_property_name: name of the first property to get
1813  * @...: return location for the first property, followed optionally by more
1814  *  name/return location pairs, followed by %NULL
1815  *
1816  * Gets properties of an object.
1817  *
1818  * In general, a copy is made of the property contents and the caller
1819  * is responsible for freeing the memory in the appropriate manner for
1820  * the type, for instance by calling g_free() or g_object_unref().
1821  *
1822  * <example>
1823  * <title>Using g_object_get(<!-- -->)</title>
1824  * An example of using g_object_get() to get the contents
1825  * of three properties - one of type #G_TYPE_INT,
1826  * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1827  * <programlisting>
1828  *  gint intval;
1829  *  gchar *strval;
1830  *  GObject *objval;
1831  *
1832  *  g_object_get (my_object,
1833  *                "int-property", &intval,
1834  *                "str-property", &strval,
1835  *                "obj-property", &objval,
1836  *                NULL);
1837  *
1838  *  // Do something with intval, strval, objval
1839  *
1840  *  g_free (strval);
1841  *  g_object_unref (objval);
1842  * </programlisting>
1843  * </example>
1844  */
1845 void
1846 g_object_get (gpointer     _object,
1847               const gchar *first_property_name,
1848               ...)
1849 {
1850   GObject *object = _object;
1851   va_list var_args;
1852   
1853   g_return_if_fail (G_IS_OBJECT (object));
1854   
1855   va_start (var_args, first_property_name);
1856   g_object_get_valist (object, first_property_name, var_args);
1857   va_end (var_args);
1858 }
1859
1860 /**
1861  * g_object_set_property:
1862  * @object: a #GObject
1863  * @property_name: the name of the property to set
1864  * @value: the value
1865  *
1866  * Sets a property on an object.
1867  */
1868 void
1869 g_object_set_property (GObject      *object,
1870                        const gchar  *property_name,
1871                        const GValue *value)
1872 {
1873   GObjectNotifyQueue *nqueue;
1874   GParamSpec *pspec;
1875   
1876   g_return_if_fail (G_IS_OBJECT (object));
1877   g_return_if_fail (property_name != NULL);
1878   g_return_if_fail (G_IS_VALUE (value));
1879   
1880   g_object_ref (object);
1881   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1882   
1883   pspec = g_param_spec_pool_lookup (pspec_pool,
1884                                     property_name,
1885                                     G_OBJECT_TYPE (object),
1886                                     TRUE);
1887   if (!pspec)
1888     g_warning ("%s: object class `%s' has no property named `%s'",
1889                G_STRFUNC,
1890                G_OBJECT_TYPE_NAME (object),
1891                property_name);
1892   else if (!(pspec->flags & G_PARAM_WRITABLE))
1893     g_warning ("%s: property `%s' of object class `%s' is not writable",
1894                G_STRFUNC,
1895                pspec->name,
1896                G_OBJECT_TYPE_NAME (object));
1897   else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1898     g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1899                G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1900   else
1901     object_set_property (object, pspec, value, nqueue);
1902   
1903   g_object_notify_queue_thaw (object, nqueue);
1904   g_object_unref (object);
1905 }
1906
1907 /**
1908  * g_object_get_property:
1909  * @object: a #GObject
1910  * @property_name: the name of the property to get
1911  * @value: return location for the property value
1912  *
1913  * Gets a property of an object. @value must have been initialized to the
1914  * expected type of the property (or a type to which the expected type can be
1915  * transformed) using g_value_init().
1916  *
1917  * In general, a copy is made of the property contents and the caller is
1918  * responsible for freeing the memory by calling g_value_unset().
1919  *
1920  * Note that g_object_get_property() is really intended for language
1921  * bindings, g_object_get() is much more convenient for C programming.
1922  */
1923 void
1924 g_object_get_property (GObject     *object,
1925                        const gchar *property_name,
1926                        GValue      *value)
1927 {
1928   GParamSpec *pspec;
1929   
1930   g_return_if_fail (G_IS_OBJECT (object));
1931   g_return_if_fail (property_name != NULL);
1932   g_return_if_fail (G_IS_VALUE (value));
1933   
1934   g_object_ref (object);
1935   
1936   pspec = g_param_spec_pool_lookup (pspec_pool,
1937                                     property_name,
1938                                     G_OBJECT_TYPE (object),
1939                                     TRUE);
1940   if (!pspec)
1941     g_warning ("%s: object class `%s' has no property named `%s'",
1942                G_STRFUNC,
1943                G_OBJECT_TYPE_NAME (object),
1944                property_name);
1945   else if (!(pspec->flags & G_PARAM_READABLE))
1946     g_warning ("%s: property `%s' of object class `%s' is not readable",
1947                G_STRFUNC,
1948                pspec->name,
1949                G_OBJECT_TYPE_NAME (object));
1950   else
1951     {
1952       GValue *prop_value, tmp_value = { 0, };
1953       
1954       /* auto-conversion of the callers value type
1955        */
1956       if (G_VALUE_TYPE (value) == pspec->value_type)
1957         {
1958           g_value_reset (value);
1959           prop_value = value;
1960         }
1961       else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1962         {
1963           g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1964                      G_STRFUNC, pspec->name,
1965                      g_type_name (pspec->value_type),
1966                      G_VALUE_TYPE_NAME (value));
1967           g_object_unref (object);
1968           return;
1969         }
1970       else
1971         {
1972           g_value_init (&tmp_value, pspec->value_type);
1973           prop_value = &tmp_value;
1974         }
1975       object_get_property (object, pspec, prop_value);
1976       if (prop_value != value)
1977         {
1978           g_value_transform (prop_value, value);
1979           g_value_unset (&tmp_value);
1980         }
1981     }
1982   
1983   g_object_unref (object);
1984 }
1985
1986 /**
1987  * g_object_connect: (skip)
1988  * @object: a #GObject
1989  * @signal_spec: the spec for the first signal
1990  * @...: #GCallback for the first signal, followed by data for the
1991  *       first signal, followed optionally by more signal
1992  *       spec/callback/data triples, followed by %NULL
1993  *
1994  * A convenience function to connect multiple signals at once.
1995  *
1996  * The signal specs expected by this function have the form
1997  * "modifier::signal_name", where modifier can be one of the following:
1998  * <variablelist>
1999  * <varlistentry>
2000  * <term>signal</term>
2001  * <listitem><para>
2002  * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2003  * </para></listitem>
2004  * </varlistentry>
2005  * <varlistentry>
2006  * <term>object_signal</term>
2007  * <term>object-signal</term>
2008  * <listitem><para>
2009  * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2010  * </para></listitem>
2011  * </varlistentry>
2012  * <varlistentry>
2013  * <term>swapped_signal</term>
2014  * <term>swapped-signal</term>
2015  * <listitem><para>
2016  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2017  * </para></listitem>
2018  * </varlistentry>
2019  * <varlistentry>
2020  * <term>swapped_object_signal</term>
2021  * <term>swapped-object-signal</term>
2022  * <listitem><para>
2023  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2024  * </para></listitem>
2025  * </varlistentry>
2026  * <varlistentry>
2027  * <term>signal_after</term>
2028  * <term>signal-after</term>
2029  * <listitem><para>
2030  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2031  * </para></listitem>
2032  * </varlistentry>
2033  * <varlistentry>
2034  * <term>object_signal_after</term>
2035  * <term>object-signal-after</term>
2036  * <listitem><para>
2037  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2038  * </para></listitem>
2039  * </varlistentry>
2040  * <varlistentry>
2041  * <term>swapped_signal_after</term>
2042  * <term>swapped-signal-after</term>
2043  * <listitem><para>
2044  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2045  * </para></listitem>
2046  * </varlistentry>
2047  * <varlistentry>
2048  * <term>swapped_object_signal_after</term>
2049  * <term>swapped-object-signal-after</term>
2050  * <listitem><para>
2051  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2052  * </para></listitem>
2053  * </varlistentry>
2054  * </variablelist>
2055  *
2056  * |[
2057  *   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2058  *                                                 "type", GTK_WINDOW_POPUP,
2059  *                                                 "child", menu,
2060  *                                                 NULL),
2061  *                                   "signal::event", gtk_menu_window_event, menu,
2062  *                                   "signal::size_request", gtk_menu_window_size_request, menu,
2063  *                                   "signal::destroy", gtk_widget_destroyed, &amp;menu-&gt;toplevel,
2064  *                                   NULL);
2065  * ]|
2066  *
2067  * Returns: (transfer none): @object
2068  */
2069 gpointer
2070 g_object_connect (gpointer     _object,
2071                   const gchar *signal_spec,
2072                   ...)
2073 {
2074   GObject *object = _object;
2075   va_list var_args;
2076
2077   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2078   g_return_val_if_fail (object->ref_count > 0, object);
2079
2080   va_start (var_args, signal_spec);
2081   while (signal_spec)
2082     {
2083       GCallback callback = va_arg (var_args, GCallback);
2084       gpointer data = va_arg (var_args, gpointer);
2085       gulong sid;
2086
2087       if (strncmp (signal_spec, "signal::", 8) == 0)
2088         sid = g_signal_connect_data (object, signal_spec + 8,
2089                                      callback, data, NULL,
2090                                      0);
2091       else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2092                strncmp (signal_spec, "object-signal::", 15) == 0)
2093         sid = g_signal_connect_object (object, signal_spec + 15,
2094                                        callback, data,
2095                                        0);
2096       else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2097                strncmp (signal_spec, "swapped-signal::", 16) == 0)
2098         sid = g_signal_connect_data (object, signal_spec + 16,
2099                                      callback, data, NULL,
2100                                      G_CONNECT_SWAPPED);
2101       else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2102                strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2103         sid = g_signal_connect_object (object, signal_spec + 23,
2104                                        callback, data,
2105                                        G_CONNECT_SWAPPED);
2106       else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2107                strncmp (signal_spec, "signal-after::", 14) == 0)
2108         sid = g_signal_connect_data (object, signal_spec + 14,
2109                                      callback, data, NULL,
2110                                      G_CONNECT_AFTER);
2111       else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2112                strncmp (signal_spec, "object-signal-after::", 21) == 0)
2113         sid = g_signal_connect_object (object, signal_spec + 21,
2114                                        callback, data,
2115                                        G_CONNECT_AFTER);
2116       else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2117                strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2118         sid = g_signal_connect_data (object, signal_spec + 22,
2119                                      callback, data, NULL,
2120                                      G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2121       else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2122                strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2123         sid = g_signal_connect_object (object, signal_spec + 29,
2124                                        callback, data,
2125                                        G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2126       else
2127         {
2128           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2129           break;
2130         }
2131       signal_spec = va_arg (var_args, gchar*);
2132     }
2133   va_end (var_args);
2134
2135   return object;
2136 }
2137
2138 /**
2139  * g_object_disconnect: (skip)
2140  * @object: a #GObject
2141  * @signal_spec: the spec for the first signal
2142  * @...: #GCallback for the first signal, followed by data for the first signal,
2143  *  followed optionally by more signal spec/callback/data triples,
2144  *  followed by %NULL
2145  *
2146  * A convenience function to disconnect multiple signals at once.
2147  *
2148  * The signal specs expected by this function have the form
2149  * "any_signal", which means to disconnect any signal with matching
2150  * callback and data, or "any_signal::signal_name", which only
2151  * disconnects the signal named "signal_name".
2152  */
2153 void
2154 g_object_disconnect (gpointer     _object,
2155                      const gchar *signal_spec,
2156                      ...)
2157 {
2158   GObject *object = _object;
2159   va_list var_args;
2160
2161   g_return_if_fail (G_IS_OBJECT (object));
2162   g_return_if_fail (object->ref_count > 0);
2163
2164   va_start (var_args, signal_spec);
2165   while (signal_spec)
2166     {
2167       GCallback callback = va_arg (var_args, GCallback);
2168       gpointer data = va_arg (var_args, gpointer);
2169       guint sid = 0, detail = 0, mask = 0;
2170
2171       if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2172           strncmp (signal_spec, "any-signal::", 12) == 0)
2173         {
2174           signal_spec += 12;
2175           mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2176         }
2177       else if (strcmp (signal_spec, "any_signal") == 0 ||
2178                strcmp (signal_spec, "any-signal") == 0)
2179         {
2180           signal_spec += 10;
2181           mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2182         }
2183       else
2184         {
2185           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2186           break;
2187         }
2188
2189       if ((mask & G_SIGNAL_MATCH_ID) &&
2190           !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2191         g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2192       else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2193                                                       sid, detail,
2194                                                       NULL, (gpointer)callback, data))
2195         g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2196       signal_spec = va_arg (var_args, gchar*);
2197     }
2198   va_end (var_args);
2199 }
2200
2201 typedef struct {
2202   GObject *object;
2203   guint n_weak_refs;
2204   struct {
2205     GWeakNotify notify;
2206     gpointer    data;
2207   } weak_refs[1];  /* flexible array */
2208 } WeakRefStack;
2209
2210 static void
2211 weak_refs_notify (gpointer data)
2212 {
2213   WeakRefStack *wstack = data;
2214   guint i;
2215
2216   for (i = 0; i < wstack->n_weak_refs; i++)
2217     wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2218   g_free (wstack);
2219 }
2220
2221 /**
2222  * g_object_weak_ref: (skip)
2223  * @object: #GObject to reference weakly
2224  * @notify: callback to invoke before the object is freed
2225  * @data: extra data to pass to notify
2226  *
2227  * Adds a weak reference callback to an object. Weak references are
2228  * used for notification when an object is finalized. They are called
2229  * "weak references" because they allow you to safely hold a pointer
2230  * to an object without calling g_object_ref() (g_object_ref() adds a
2231  * strong reference, that is, forces the object to stay alive).
2232  */
2233 void
2234 g_object_weak_ref (GObject    *object,
2235                    GWeakNotify notify,
2236                    gpointer    data)
2237 {
2238   WeakRefStack *wstack;
2239   guint i;
2240   
2241   g_return_if_fail (G_IS_OBJECT (object));
2242   g_return_if_fail (notify != NULL);
2243   g_return_if_fail (object->ref_count >= 1);
2244
2245   G_LOCK (weak_refs_mutex);
2246   wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2247   if (wstack)
2248     {
2249       i = wstack->n_weak_refs++;
2250       wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2251     }
2252   else
2253     {
2254       wstack = g_renew (WeakRefStack, NULL, 1);
2255       wstack->object = object;
2256       wstack->n_weak_refs = 1;
2257       i = 0;
2258     }
2259   wstack->weak_refs[i].notify = notify;
2260   wstack->weak_refs[i].data = data;
2261   g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2262   G_UNLOCK (weak_refs_mutex);
2263 }
2264
2265 /**
2266  * g_object_weak_unref: (skip)
2267  * @object: #GObject to remove a weak reference from
2268  * @notify: callback to search for
2269  * @data: data to search for
2270  *
2271  * Removes a weak reference callback to an object.
2272  */
2273 void
2274 g_object_weak_unref (GObject    *object,
2275                      GWeakNotify notify,
2276                      gpointer    data)
2277 {
2278   WeakRefStack *wstack;
2279   gboolean found_one = FALSE;
2280
2281   g_return_if_fail (G_IS_OBJECT (object));
2282   g_return_if_fail (notify != NULL);
2283
2284   G_LOCK (weak_refs_mutex);
2285   wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2286   if (wstack)
2287     {
2288       guint i;
2289
2290       for (i = 0; i < wstack->n_weak_refs; i++)
2291         if (wstack->weak_refs[i].notify == notify &&
2292             wstack->weak_refs[i].data == data)
2293           {
2294             found_one = TRUE;
2295             wstack->n_weak_refs -= 1;
2296             if (i != wstack->n_weak_refs)
2297               wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2298
2299             break;
2300           }
2301     }
2302   G_UNLOCK (weak_refs_mutex);
2303   if (!found_one)
2304     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2305 }
2306
2307 /**
2308  * g_object_add_weak_pointer: (skip)
2309  * @object: The object that should be weak referenced.
2310  * @weak_pointer_location: (inout): The memory address of a pointer.
2311  *
2312  * Adds a weak reference from weak_pointer to @object to indicate that
2313  * the pointer located at @weak_pointer_location is only valid during
2314  * the lifetime of @object. When the @object is finalized,
2315  * @weak_pointer will be set to %NULL.
2316  */
2317 void
2318 g_object_add_weak_pointer (GObject  *object, 
2319                            gpointer *weak_pointer_location)
2320 {
2321   g_return_if_fail (G_IS_OBJECT (object));
2322   g_return_if_fail (weak_pointer_location != NULL);
2323
2324   g_object_weak_ref (object, 
2325                      (GWeakNotify) g_nullify_pointer, 
2326                      weak_pointer_location);
2327 }
2328
2329 /**
2330  * g_object_remove_weak_pointer: (skip)
2331  * @object: The object that is weak referenced.
2332  * @weak_pointer_location: (inout): The memory address of a pointer.
2333  *
2334  * Removes a weak reference from @object that was previously added
2335  * using g_object_add_weak_pointer(). The @weak_pointer_location has
2336  * to match the one used with g_object_add_weak_pointer().
2337  */
2338 void
2339 g_object_remove_weak_pointer (GObject  *object, 
2340                               gpointer *weak_pointer_location)
2341 {
2342   g_return_if_fail (G_IS_OBJECT (object));
2343   g_return_if_fail (weak_pointer_location != NULL);
2344
2345   g_object_weak_unref (object, 
2346                        (GWeakNotify) g_nullify_pointer, 
2347                        weak_pointer_location);
2348 }
2349
2350 static guint
2351 object_floating_flag_handler (GObject        *object,
2352                               gint            job)
2353 {
2354   switch (job)
2355     {
2356       gpointer oldvalue;
2357     case +1:    /* force floating if possible */
2358       do
2359         oldvalue = g_atomic_pointer_get (&object->qdata);
2360       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2361                                                      (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2362       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2363     case -1:    /* sink if possible */
2364       do
2365         oldvalue = g_atomic_pointer_get (&object->qdata);
2366       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2367                                                      (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2368       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2369     default:    /* check floating */
2370       return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2371     }
2372 }
2373
2374 /**
2375  * g_object_is_floating:
2376  * @object: (type GObject.Object): a #GObject
2377  *
2378  * Checks wether @object has a <link linkend="floating-ref">floating</link>
2379  * reference.
2380  *
2381  * Since: 2.10
2382  *
2383  * Returns: %TRUE if @object has a floating reference
2384  */
2385 gboolean
2386 g_object_is_floating (gpointer _object)
2387 {
2388   GObject *object = _object;
2389   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2390   return floating_flag_handler (object, 0);
2391 }
2392
2393 /**
2394  * g_object_ref_sink:
2395  * @object: (type GObject.Object): a #GObject
2396  *
2397  * Increase the reference count of @object, and possibly remove the
2398  * <link linkend="floating-ref">floating</link> reference, if @object
2399  * has a floating reference.
2400  *
2401  * In other words, if the object is floating, then this call "assumes
2402  * ownership" of the floating reference, converting it to a normal
2403  * reference by clearing the floating flag while leaving the reference
2404  * count unchanged.  If the object is not floating, then this call
2405  * adds a new normal reference increasing the reference count by one.
2406  *
2407  * Since: 2.10
2408  *
2409  * Returns: (type GObject.Object) (transfer none): @object
2410  */
2411 gpointer
2412 g_object_ref_sink (gpointer _object)
2413 {
2414   GObject *object = _object;
2415   gboolean was_floating;
2416   g_return_val_if_fail (G_IS_OBJECT (object), object);
2417   g_return_val_if_fail (object->ref_count >= 1, object);
2418   g_object_ref (object);
2419   was_floating = floating_flag_handler (object, -1);
2420   if (was_floating)
2421     g_object_unref (object);
2422   return object;
2423 }
2424
2425 /**
2426  * g_object_force_floating:
2427  * @object: a #GObject
2428  *
2429  * This function is intended for #GObject implementations to re-enforce a
2430  * <link linkend="floating-ref">floating</link> object reference.
2431  * Doing this is seldomly required, all
2432  * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2433  * usually just needs to be sunken by calling g_object_ref_sink().
2434  *
2435  * Since: 2.10
2436  */
2437 void
2438 g_object_force_floating (GObject *object)
2439 {
2440   gboolean was_floating;
2441   g_return_if_fail (G_IS_OBJECT (object));
2442   g_return_if_fail (object->ref_count >= 1);
2443
2444   was_floating = floating_flag_handler (object, +1);
2445 }
2446
2447 typedef struct {
2448   GObject *object;
2449   guint n_toggle_refs;
2450   struct {
2451     GToggleNotify notify;
2452     gpointer    data;
2453   } toggle_refs[1];  /* flexible array */
2454 } ToggleRefStack;
2455
2456 static void
2457 toggle_refs_notify (GObject *object,
2458                     gboolean is_last_ref)
2459 {
2460   ToggleRefStack tstack, *tstackptr;
2461
2462   G_LOCK (toggle_refs_mutex);
2463   tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2464   tstack = *tstackptr;
2465   G_UNLOCK (toggle_refs_mutex);
2466
2467   /* Reentrancy here is not as tricky as it seems, because a toggle reference
2468    * will only be notified when there is exactly one of them.
2469    */
2470   g_assert (tstack.n_toggle_refs == 1);
2471   tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2472 }
2473
2474 /**
2475  * g_object_add_toggle_ref: (skip)
2476  * @object: a #GObject
2477  * @notify: a function to call when this reference is the
2478  *  last reference to the object, or is no longer
2479  *  the last reference.
2480  * @data: data to pass to @notify
2481  *
2482  * Increases the reference count of the object by one and sets a
2483  * callback to be called when all other references to the object are
2484  * dropped, or when this is already the last reference to the object
2485  * and another reference is established.
2486  *
2487  * This functionality is intended for binding @object to a proxy
2488  * object managed by another memory manager. This is done with two
2489  * paired references: the strong reference added by
2490  * g_object_add_toggle_ref() and a reverse reference to the proxy
2491  * object which is either a strong reference or weak reference.
2492  *
2493  * The setup is that when there are no other references to @object,
2494  * only a weak reference is held in the reverse direction from @object
2495  * to the proxy object, but when there are other references held to
2496  * @object, a strong reference is held. The @notify callback is called
2497  * when the reference from @object to the proxy object should be
2498  * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2499  * true) or weak to strong (@is_last_ref false).
2500  *
2501  * Since a (normal) reference must be held to the object before
2502  * calling g_object_toggle_ref(), the initial state of the reverse
2503  * link is always strong.
2504  *
2505  * Multiple toggle references may be added to the same gobject,
2506  * however if there are multiple toggle references to an object, none
2507  * of them will ever be notified until all but one are removed.  For
2508  * this reason, you should only ever use a toggle reference if there
2509  * is important state in the proxy object.
2510  *
2511  * Since: 2.8
2512  */
2513 void
2514 g_object_add_toggle_ref (GObject       *object,
2515                          GToggleNotify  notify,
2516                          gpointer       data)
2517 {
2518   ToggleRefStack *tstack;
2519   guint i;
2520   
2521   g_return_if_fail (G_IS_OBJECT (object));
2522   g_return_if_fail (notify != NULL);
2523   g_return_if_fail (object->ref_count >= 1);
2524
2525   g_object_ref (object);
2526
2527   G_LOCK (toggle_refs_mutex);
2528   tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2529   if (tstack)
2530     {
2531       i = tstack->n_toggle_refs++;
2532       /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2533        * in tstate->toggle_refs */
2534       tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2535     }
2536   else
2537     {
2538       tstack = g_renew (ToggleRefStack, NULL, 1);
2539       tstack->object = object;
2540       tstack->n_toggle_refs = 1;
2541       i = 0;
2542     }
2543
2544   /* Set a flag for fast lookup after adding the first toggle reference */
2545   if (tstack->n_toggle_refs == 1)
2546     g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2547   
2548   tstack->toggle_refs[i].notify = notify;
2549   tstack->toggle_refs[i].data = data;
2550   g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2551                                (GDestroyNotify)g_free);
2552   G_UNLOCK (toggle_refs_mutex);
2553 }
2554
2555 /**
2556  * g_object_remove_toggle_ref: (skip)
2557  * @object: a #GObject
2558  * @notify: a function to call when this reference is the
2559  *  last reference to the object, or is no longer
2560  *  the last reference.
2561  * @data: data to pass to @notify
2562  *
2563  * Removes a reference added with g_object_add_toggle_ref(). The
2564  * reference count of the object is decreased by one.
2565  *
2566  * Since: 2.8
2567  */
2568 void
2569 g_object_remove_toggle_ref (GObject       *object,
2570                             GToggleNotify  notify,
2571                             gpointer       data)
2572 {
2573   ToggleRefStack *tstack;
2574   gboolean found_one = FALSE;
2575
2576   g_return_if_fail (G_IS_OBJECT (object));
2577   g_return_if_fail (notify != NULL);
2578
2579   G_LOCK (toggle_refs_mutex);
2580   tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2581   if (tstack)
2582     {
2583       guint i;
2584
2585       for (i = 0; i < tstack->n_toggle_refs; i++)
2586         if (tstack->toggle_refs[i].notify == notify &&
2587             tstack->toggle_refs[i].data == data)
2588           {
2589             found_one = TRUE;
2590             tstack->n_toggle_refs -= 1;
2591             if (i != tstack->n_toggle_refs)
2592               tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2593
2594             if (tstack->n_toggle_refs == 0)
2595               g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2596
2597             break;
2598           }
2599     }
2600   G_UNLOCK (toggle_refs_mutex);
2601
2602   if (found_one)
2603     g_object_unref (object);
2604   else
2605     g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2606 }
2607
2608 /**
2609  * g_object_ref:
2610  * @object: (type GObject.Object): a #GObject
2611  *
2612  * Increases the reference count of @object.
2613  *
2614  * Returns: (type GObject.Object) (transfer none): the same @object
2615  */
2616 gpointer
2617 g_object_ref (gpointer _object)
2618 {
2619   GObject *object = _object;
2620   gint old_val;
2621
2622   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2623   g_return_val_if_fail (object->ref_count > 0, NULL);
2624   
2625 #ifdef  G_ENABLE_DEBUG
2626   if (g_trap_object_ref == object)
2627     G_BREAKPOINT ();
2628 #endif  /* G_ENABLE_DEBUG */
2629
2630
2631   old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2632
2633   if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2634     toggle_refs_notify (object, FALSE);
2635
2636   TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2637
2638   return object;
2639 }
2640
2641 /**
2642  * g_object_unref:
2643  * @object: (type GObject.Object): a #GObject
2644  *
2645  * Decreases the reference count of @object. When its reference count
2646  * drops to 0, the object is finalized (i.e. its memory is freed).
2647  */
2648 void
2649 g_object_unref (gpointer _object)
2650 {
2651   GObject *object = _object;
2652   gint old_ref;
2653   
2654   g_return_if_fail (G_IS_OBJECT (object));
2655   g_return_if_fail (object->ref_count > 0);
2656   
2657 #ifdef  G_ENABLE_DEBUG
2658   if (g_trap_object_ref == object)
2659     G_BREAKPOINT ();
2660 #endif  /* G_ENABLE_DEBUG */
2661
2662   /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2663  retry_atomic_decrement1:
2664   old_ref = g_atomic_int_get (&object->ref_count);
2665   if (old_ref > 1)
2666     {
2667       /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2668       gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2669
2670       if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2671         goto retry_atomic_decrement1;
2672
2673       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2674
2675       /* if we went from 2->1 we need to notify toggle refs if any */
2676       if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2677         toggle_refs_notify (object, TRUE);
2678     }
2679   else
2680     {
2681       /* we are about tp remove the last reference */
2682       TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2683       G_OBJECT_GET_CLASS (object)->dispose (object);
2684       TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2685
2686       /* may have been re-referenced meanwhile */
2687     retry_atomic_decrement2:
2688       old_ref = g_atomic_int_get ((int *)&object->ref_count);
2689       if (old_ref > 1)
2690         {
2691           /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2692           gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2693
2694           if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2695             goto retry_atomic_decrement2;
2696
2697           TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2698
2699           /* if we went from 2->1 we need to notify toggle refs if any */
2700           if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2701             toggle_refs_notify (object, TRUE);
2702
2703           return;
2704         }
2705
2706       /* we are still in the process of taking away the last ref */
2707       g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2708       g_signal_handlers_destroy (object);
2709       g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2710       
2711       /* decrement the last reference */
2712       old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2713
2714       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2715
2716       /* may have been re-referenced meanwhile */
2717       if (G_LIKELY (old_ref == 1))
2718         {
2719           TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2720           G_OBJECT_GET_CLASS (object)->finalize (object);
2721
2722           TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2723
2724 #ifdef  G_ENABLE_DEBUG
2725           IF_DEBUG (OBJECTS)
2726             {
2727               /* catch objects not chaining finalize handlers */
2728               G_LOCK (debug_objects);
2729               g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2730               G_UNLOCK (debug_objects);
2731             }
2732 #endif  /* G_ENABLE_DEBUG */
2733           g_type_free_instance ((GTypeInstance*) object);
2734         }
2735     }
2736 }
2737
2738 /**
2739  * g_clear_object: (skip)
2740  * @object_ptr: a pointer to a #GObject reference
2741  *
2742  * Clears a reference to a #GObject.
2743  *
2744  * @object_ptr must not be %NULL.
2745  *
2746  * If the reference is %NULL then this function does nothing.
2747  * Otherwise, the reference count of the object is decreased and the
2748  * pointer is set to %NULL.
2749  *
2750  * This function is threadsafe and modifies the pointer atomically,
2751  * using memory barriers where needed.
2752  *
2753  * A macro is also included that allows this function to be used without
2754  * pointer casts.
2755  *
2756  * Since: 2.28
2757  **/
2758 #undef g_clear_object
2759 void
2760 g_clear_object (volatile GObject **object_ptr)
2761 {
2762   gpointer *ptr = (gpointer) object_ptr;
2763   gpointer old;
2764
2765   /* This is a little frustrating.
2766    * Would be nice to have an atomic exchange (with no compare).
2767    */
2768   do
2769     old = g_atomic_pointer_get (ptr);
2770   while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2771
2772   if (old)
2773     g_object_unref (old);
2774 }
2775
2776 /**
2777  * g_object_get_qdata:
2778  * @object: The GObject to get a stored user data pointer from
2779  * @quark: A #GQuark, naming the user data pointer
2780  * 
2781  * This function gets back user data pointers stored via
2782  * g_object_set_qdata().
2783  * 
2784  * Returns: (transfer none): The user data pointer set, or %NULL
2785  */
2786 gpointer
2787 g_object_get_qdata (GObject *object,
2788                     GQuark   quark)
2789 {
2790   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2791   
2792   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2793 }
2794
2795 /**
2796  * g_object_set_qdata: (skip)
2797  * @object: The GObject to set store a user data pointer
2798  * @quark: A #GQuark, naming the user data pointer
2799  * @data: An opaque user data pointer
2800  *
2801  * This sets an opaque, named pointer on an object.
2802  * The name is specified through a #GQuark (retrived e.g. via
2803  * g_quark_from_static_string()), and the pointer
2804  * can be gotten back from the @object with g_object_get_qdata()
2805  * until the @object is finalized.
2806  * Setting a previously set user data pointer, overrides (frees)
2807  * the old pointer set, using #NULL as pointer essentially
2808  * removes the data stored.
2809  */
2810 void
2811 g_object_set_qdata (GObject *object,
2812                     GQuark   quark,
2813                     gpointer data)
2814 {
2815   g_return_if_fail (G_IS_OBJECT (object));
2816   g_return_if_fail (quark > 0);
2817   
2818   g_datalist_id_set_data (&object->qdata, quark, data);
2819 }
2820
2821 /**
2822  * g_object_set_qdata_full: (skip)
2823  * @object: The GObject to set store a user data pointer
2824  * @quark: A #GQuark, naming the user data pointer
2825  * @data: An opaque user data pointer
2826  * @destroy: Function to invoke with @data as argument, when @data
2827  *           needs to be freed
2828  *
2829  * This function works like g_object_set_qdata(), but in addition,
2830  * a void (*destroy) (gpointer) function may be specified which is
2831  * called with @data as argument when the @object is finalized, or
2832  * the data is being overwritten by a call to g_object_set_qdata()
2833  * with the same @quark.
2834  */
2835 void
2836 g_object_set_qdata_full (GObject       *object,
2837                          GQuark         quark,
2838                          gpointer       data,
2839                          GDestroyNotify destroy)
2840 {
2841   g_return_if_fail (G_IS_OBJECT (object));
2842   g_return_if_fail (quark > 0);
2843   
2844   g_datalist_id_set_data_full (&object->qdata, quark, data,
2845                                data ? destroy : (GDestroyNotify) NULL);
2846 }
2847
2848 /**
2849  * g_object_steal_qdata:
2850  * @object: The GObject to get a stored user data pointer from
2851  * @quark: A #GQuark, naming the user data pointer
2852  *
2853  * This function gets back user data pointers stored via
2854  * g_object_set_qdata() and removes the @data from object
2855  * without invoking its destroy() function (if any was
2856  * set).
2857  * Usually, calling this function is only required to update
2858  * user data pointers with a destroy notifier, for example:
2859  * |[
2860  * void
2861  * object_add_to_user_list (GObject     *object,
2862  *                          const gchar *new_string)
2863  * {
2864  *   // the quark, naming the object data
2865  *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2866  *   // retrive the old string list
2867  *   GList *list = g_object_steal_qdata (object, quark_string_list);
2868  *
2869  *   // prepend new string
2870  *   list = g_list_prepend (list, g_strdup (new_string));
2871  *   // this changed 'list', so we need to set it again
2872  *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2873  * }
2874  * static void
2875  * free_string_list (gpointer data)
2876  * {
2877  *   GList *node, *list = data;
2878  *
2879  *   for (node = list; node; node = node->next)
2880  *     g_free (node->data);
2881  *   g_list_free (list);
2882  * }
2883  * ]|
2884  * Using g_object_get_qdata() in the above example, instead of
2885  * g_object_steal_qdata() would have left the destroy function set,
2886  * and thus the partial string list would have been freed upon
2887  * g_object_set_qdata_full().
2888  *
2889  * Returns: (transfer full): The user data pointer set, or %NULL
2890  */
2891 gpointer
2892 g_object_steal_qdata (GObject *object,
2893                       GQuark   quark)
2894 {
2895   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2896   g_return_val_if_fail (quark > 0, NULL);
2897   
2898   return g_datalist_id_remove_no_notify (&object->qdata, quark);
2899 }
2900
2901 /**
2902  * g_object_get_data:
2903  * @object: #GObject containing the associations
2904  * @key: name of the key for that association
2905  * 
2906  * Gets a named field from the objects table of associations (see g_object_set_data()).
2907  * 
2908  * Returns: (transfer none): the data if found, or %NULL if no such data exists.
2909  */
2910 gpointer
2911 g_object_get_data (GObject     *object,
2912                    const gchar *key)
2913 {
2914   GQuark quark;
2915
2916   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2917   g_return_val_if_fail (key != NULL, NULL);
2918
2919   quark = g_quark_try_string (key);
2920
2921   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2922 }
2923
2924 /**
2925  * g_object_set_data:
2926  * @object: #GObject containing the associations.
2927  * @key: name of the key
2928  * @data: data to associate with that key
2929  *
2930  * Each object carries around a table of associations from
2931  * strings to pointers.  This function lets you set an association.
2932  *
2933  * If the object already had an association with that name,
2934  * the old association will be destroyed.
2935  */
2936 void
2937 g_object_set_data (GObject     *object,
2938                    const gchar *key,
2939                    gpointer     data)
2940 {
2941   g_return_if_fail (G_IS_OBJECT (object));
2942   g_return_if_fail (key != NULL);
2943
2944   g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2945 }
2946
2947 /**
2948  * g_object_set_data_full: (skip)
2949  * @object: #GObject containing the associations
2950  * @key: name of the key
2951  * @data: data to associate with that key
2952  * @destroy: function to call when the association is destroyed
2953  *
2954  * Like g_object_set_data() except it adds notification
2955  * for when the association is destroyed, either by setting it
2956  * to a different value or when the object is destroyed.
2957  *
2958  * Note that the @destroy callback is not called if @data is %NULL.
2959  */
2960 void
2961 g_object_set_data_full (GObject       *object,
2962                         const gchar   *key,
2963                         gpointer       data,
2964                         GDestroyNotify destroy)
2965 {
2966   g_return_if_fail (G_IS_OBJECT (object));
2967   g_return_if_fail (key != NULL);
2968
2969   g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2970                                data ? destroy : (GDestroyNotify) NULL);
2971 }
2972
2973 /**
2974  * g_object_steal_data:
2975  * @object: #GObject containing the associations
2976  * @key: name of the key
2977  *
2978  * Remove a specified datum from the object's data associations,
2979  * without invoking the association's destroy handler.
2980  *
2981  * Returns: (transfer full): the data if found, or %NULL if no such data exists.
2982  */
2983 gpointer
2984 g_object_steal_data (GObject     *object,
2985                      const gchar *key)
2986 {
2987   GQuark quark;
2988
2989   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2990   g_return_val_if_fail (key != NULL, NULL);
2991
2992   quark = g_quark_try_string (key);
2993
2994   return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2995 }
2996
2997 static void
2998 g_value_object_init (GValue *value)
2999 {
3000   value->data[0].v_pointer = NULL;
3001 }
3002
3003 static void
3004 g_value_object_free_value (GValue *value)
3005 {
3006   if (value->data[0].v_pointer)
3007     g_object_unref (value->data[0].v_pointer);
3008 }
3009
3010 static void
3011 g_value_object_copy_value (const GValue *src_value,
3012                            GValue       *dest_value)
3013 {
3014   if (src_value->data[0].v_pointer)
3015     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3016   else
3017     dest_value->data[0].v_pointer = NULL;
3018 }
3019
3020 static void
3021 g_value_object_transform_value (const GValue *src_value,
3022                                 GValue       *dest_value)
3023 {
3024   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)))
3025     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3026   else
3027     dest_value->data[0].v_pointer = NULL;
3028 }
3029
3030 static gpointer
3031 g_value_object_peek_pointer (const GValue *value)
3032 {
3033   return value->data[0].v_pointer;
3034 }
3035
3036 static gchar*
3037 g_value_object_collect_value (GValue      *value,
3038                               guint        n_collect_values,
3039                               GTypeCValue *collect_values,
3040                               guint        collect_flags)
3041 {
3042   if (collect_values[0].v_pointer)
3043     {
3044       GObject *object = collect_values[0].v_pointer;
3045       
3046       if (object->g_type_instance.g_class == NULL)
3047         return g_strconcat ("invalid unclassed object pointer for value type `",
3048                             G_VALUE_TYPE_NAME (value),
3049                             "'",
3050                             NULL);
3051       else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3052         return g_strconcat ("invalid object type `",
3053                             G_OBJECT_TYPE_NAME (object),
3054                             "' for value type `",
3055                             G_VALUE_TYPE_NAME (value),
3056                             "'",
3057                             NULL);
3058       /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3059       value->data[0].v_pointer = g_object_ref (object);
3060     }
3061   else
3062     value->data[0].v_pointer = NULL;
3063   
3064   return NULL;
3065 }
3066
3067 static gchar*
3068 g_value_object_lcopy_value (const GValue *value,
3069                             guint        n_collect_values,
3070                             GTypeCValue *collect_values,
3071                             guint        collect_flags)
3072 {
3073   GObject **object_p = collect_values[0].v_pointer;
3074   
3075   if (!object_p)
3076     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3077
3078   if (!value->data[0].v_pointer)
3079     *object_p = NULL;
3080   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3081     *object_p = value->data[0].v_pointer;
3082   else
3083     *object_p = g_object_ref (value->data[0].v_pointer);
3084   
3085   return NULL;
3086 }
3087
3088 /**
3089  * g_value_set_object:
3090  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3091  * @v_object: (type GObject.Object): object value to be set
3092  *
3093  * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3094  *
3095  * g_value_set_object() increases the reference count of @v_object
3096  * (the #GValue holds a reference to @v_object).  If you do not wish
3097  * to increase the reference count of the object (i.e. you wish to
3098  * pass your current reference to the #GValue because you no longer
3099  * need it), use g_value_take_object() instead.
3100  *
3101  * It is important that your #GValue holds a reference to @v_object (either its
3102  * own, or one it has taken) to ensure that the object won't be destroyed while
3103  * the #GValue still exists).
3104  */
3105 void
3106 g_value_set_object (GValue   *value,
3107                     gpointer  v_object)
3108 {
3109   GObject *old;
3110         
3111   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3112
3113   old = value->data[0].v_pointer;
3114   
3115   if (v_object)
3116     {
3117       g_return_if_fail (G_IS_OBJECT (v_object));
3118       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3119
3120       value->data[0].v_pointer = v_object;
3121       g_object_ref (value->data[0].v_pointer);
3122     }
3123   else
3124     value->data[0].v_pointer = NULL;
3125   
3126   if (old)
3127     g_object_unref (old);
3128 }
3129
3130 /**
3131  * g_value_set_object_take_ownership: (skip)
3132  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3133  * @v_object: object value to be set
3134  *
3135  * This is an internal function introduced mainly for C marshallers.
3136  *
3137  * Deprecated: 2.4: Use g_value_take_object() instead.
3138  */
3139 void
3140 g_value_set_object_take_ownership (GValue  *value,
3141                                    gpointer v_object)
3142 {
3143   g_value_take_object (value, v_object);
3144 }
3145
3146 /**
3147  * g_value_take_object: (skip)
3148  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3149  * @v_object: object value to be set
3150  *
3151  * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3152  * and takes over the ownership of the callers reference to @v_object;
3153  * the caller doesn't have to unref it any more (i.e. the reference
3154  * count of the object is not increased).
3155  *
3156  * If you want the #GValue to hold its own reference to @v_object, use
3157  * g_value_set_object() instead.
3158  *
3159  * Since: 2.4
3160  */
3161 void
3162 g_value_take_object (GValue  *value,
3163                      gpointer v_object)
3164 {
3165   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3166
3167   if (value->data[0].v_pointer)
3168     {
3169       g_object_unref (value->data[0].v_pointer);
3170       value->data[0].v_pointer = NULL;
3171     }
3172
3173   if (v_object)
3174     {
3175       g_return_if_fail (G_IS_OBJECT (v_object));
3176       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3177
3178       value->data[0].v_pointer = v_object; /* we take over the reference count */
3179     }
3180 }
3181
3182 /**
3183  * g_value_get_object:
3184  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3185  * 
3186  * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3187  * 
3188  * Returns: (type GObject.Object) (transfer none): object contents of @value
3189  */
3190 gpointer
3191 g_value_get_object (const GValue *value)
3192 {
3193   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3194   
3195   return value->data[0].v_pointer;
3196 }
3197
3198 /**
3199  * g_value_dup_object:
3200  * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3201  *
3202  * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3203  * its reference count.
3204  *
3205  * Returns: (type GObject.Object) (transfer full): object content of @value,
3206  *          should be unreferenced when no longer needed.
3207  */
3208 gpointer
3209 g_value_dup_object (const GValue *value)
3210 {
3211   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3212   
3213   return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3214 }
3215
3216 /**
3217  * g_signal_connect_object: (skip)
3218  * @instance: the instance to connect to.
3219  * @detailed_signal: a string of the form "signal-name::detail".
3220  * @c_handler: the #GCallback to connect.
3221  * @gobject: the object to pass as data to @c_handler.
3222  * @connect_flags: a combination of #GConnnectFlags.
3223  *
3224  * This is similar to g_signal_connect_data(), but uses a closure which
3225  * ensures that the @gobject stays alive during the call to @c_handler
3226  * by temporarily adding a reference count to @gobject.
3227  *
3228  * Note that there is a bug in GObject that makes this function
3229  * much less useful than it might seem otherwise. Once @gobject is
3230  * disposed, the callback will no longer be called, but, the signal
3231  * handler is <emphasis>not</emphasis> currently disconnected. If the
3232  * @instance is itself being freed at the same time than this doesn't
3233  * matter, since the signal will automatically be removed, but
3234  * if @instance persists, then the signal handler will leak. You
3235  * should not remove the signal yourself because in a future versions of
3236  * GObject, the handler <emphasis>will</emphasis> automatically
3237  * be disconnected.
3238  *
3239  * It's possible to work around this problem in a way that will
3240  * continue to work with future versions of GObject by checking
3241  * that the signal handler is still connected before disconnected it:
3242  * <informalexample><programlisting>
3243  *  if (g_signal_handler_is_connected (instance, id))
3244  *    g_signal_handler_disconnect (instance, id);
3245  * </programlisting></informalexample>
3246  *
3247  * Returns: the handler id.
3248  */
3249 gulong
3250 g_signal_connect_object (gpointer      instance,
3251                          const gchar  *detailed_signal,
3252                          GCallback     c_handler,
3253                          gpointer      gobject,
3254                          GConnectFlags connect_flags)
3255 {
3256   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3257   g_return_val_if_fail (detailed_signal != NULL, 0);
3258   g_return_val_if_fail (c_handler != NULL, 0);
3259
3260   if (gobject)
3261     {
3262       GClosure *closure;
3263
3264       g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3265
3266       closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3267
3268       return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3269     }
3270   else
3271     return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3272 }
3273
3274 typedef struct {
3275   GObject  *object;
3276   guint     n_closures;
3277   GClosure *closures[1]; /* flexible array */
3278 } CArray;
3279 /* don't change this structure without supplying an accessor for
3280  * watched closures, e.g.:
3281  * GSList* g_object_list_watched_closures (GObject *object)
3282  * {
3283  *   CArray *carray;
3284  *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3285  *   carray = g_object_get_data (object, "GObject-closure-array");
3286  *   if (carray)
3287  *     {
3288  *       GSList *slist = NULL;
3289  *       guint i;
3290  *       for (i = 0; i < carray->n_closures; i++)
3291  *         slist = g_slist_prepend (slist, carray->closures[i]);
3292  *       return slist;
3293  *     }
3294  *   return NULL;
3295  * }
3296  */
3297
3298 static void
3299 object_remove_closure (gpointer  data,
3300                        GClosure *closure)
3301 {
3302   GObject *object = data;
3303   CArray *carray;
3304   guint i;
3305   
3306   G_LOCK (closure_array_mutex);
3307   carray = g_object_get_qdata (object, quark_closure_array);
3308   for (i = 0; i < carray->n_closures; i++)
3309     if (carray->closures[i] == closure)
3310       {
3311         carray->n_closures--;
3312         if (i < carray->n_closures)
3313           carray->closures[i] = carray->closures[carray->n_closures];
3314         G_UNLOCK (closure_array_mutex);
3315         return;
3316       }
3317   G_UNLOCK (closure_array_mutex);
3318   g_assert_not_reached ();
3319 }
3320
3321 static void
3322 destroy_closure_array (gpointer data)
3323 {
3324   CArray *carray = data;
3325   GObject *object = carray->object;
3326   guint i, n = carray->n_closures;
3327   
3328   for (i = 0; i < n; i++)
3329     {
3330       GClosure *closure = carray->closures[i];
3331       
3332       /* removing object_remove_closure() upfront is probably faster than
3333        * letting it fiddle with quark_closure_array which is empty anyways
3334        */
3335       g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3336       g_closure_invalidate (closure);
3337     }
3338   g_free (carray);
3339 }
3340
3341 /**
3342  * g_object_watch_closure:
3343  * @object: GObject restricting lifetime of @closure
3344  * @closure: GClosure to watch
3345  *
3346  * This function essentially limits the life time of the @closure to
3347  * the life time of the object. That is, when the object is finalized,
3348  * the @closure is invalidated by calling g_closure_invalidate() on
3349  * it, in order to prevent invocations of the closure with a finalized
3350  * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3351  * added as marshal guards to the @closure, to ensure that an extra
3352  * reference count is held on @object during invocation of the
3353  * @closure.  Usually, this function will be called on closures that
3354  * use this @object as closure data.
3355  */
3356 void
3357 g_object_watch_closure (GObject  *object,
3358                         GClosure *closure)
3359 {
3360   CArray *carray;
3361   guint i;
3362   
3363   g_return_if_fail (G_IS_OBJECT (object));
3364   g_return_if_fail (closure != NULL);
3365   g_return_if_fail (closure->is_invalid == FALSE);
3366   g_return_if_fail (closure->in_marshal == FALSE);
3367   g_return_if_fail (object->ref_count > 0);     /* this doesn't work on finalizing objects */
3368   
3369   g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3370   g_closure_add_marshal_guards (closure,
3371                                 object, (GClosureNotify) g_object_ref,
3372                                 object, (GClosureNotify) g_object_unref);
3373   G_LOCK (closure_array_mutex);
3374   carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3375   if (!carray)
3376     {
3377       carray = g_renew (CArray, NULL, 1);
3378       carray->object = object;
3379       carray->n_closures = 1;
3380       i = 0;
3381     }
3382   else
3383     {
3384       i = carray->n_closures++;
3385       carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3386     }
3387   carray->closures[i] = closure;
3388   g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3389   G_UNLOCK (closure_array_mutex);
3390 }
3391
3392 /**
3393  * g_closure_new_object:
3394  * @sizeof_closure: the size of the structure to allocate, must be at least
3395  *  <literal>sizeof (GClosure)</literal>
3396  * @object: a #GObject pointer to store in the @data field of the newly
3397  *  allocated #GClosure
3398  *
3399  * A variant of g_closure_new_simple() which stores @object in the
3400  * @data field of the closure and calls g_object_watch_closure() on
3401  * @object and the created closure. This function is mainly useful
3402  * when implementing new types of closures.
3403  *
3404  * Returns: (transfer full): a newly allocated #GClosure
3405  */
3406 GClosure*
3407 g_closure_new_object (guint    sizeof_closure,
3408                       GObject *object)
3409 {
3410   GClosure *closure;
3411
3412   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3413   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3414
3415   closure = g_closure_new_simple (sizeof_closure, object);
3416   g_object_watch_closure (object, closure);
3417
3418   return closure;
3419 }
3420
3421 /**
3422  * g_cclosure_new_object: (skip)
3423  * @callback_func: the function to invoke
3424  * @object: a #GObject pointer to pass to @callback_func
3425  *
3426  * A variant of g_cclosure_new() which uses @object as @user_data and
3427  * calls g_object_watch_closure() on @object and the created
3428  * closure. This function is useful when you have a callback closely
3429  * associated with a #GObject, and want the callback to no longer run
3430  * after the object is is freed.
3431  *
3432  * Returns: a new #GCClosure
3433  */
3434 GClosure*
3435 g_cclosure_new_object (GCallback callback_func,
3436                        GObject  *object)
3437 {
3438   GClosure *closure;
3439
3440   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3441   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3442   g_return_val_if_fail (callback_func != NULL, NULL);
3443
3444   closure = g_cclosure_new (callback_func, object, NULL);
3445   g_object_watch_closure (object, closure);
3446
3447   return closure;
3448 }
3449
3450 /**
3451  * g_cclosure_new_object_swap: (skip)
3452  * @callback_func: the function to invoke
3453  * @object: a #GObject pointer to pass to @callback_func
3454  *
3455  * A variant of g_cclosure_new_swap() which uses @object as @user_data
3456  * and calls g_object_watch_closure() on @object and the created
3457  * closure. This function is useful when you have a callback closely
3458  * associated with a #GObject, and want the callback to no longer run
3459  * after the object is is freed.
3460  *
3461  * Returns: a new #GCClosure
3462  */
3463 GClosure*
3464 g_cclosure_new_object_swap (GCallback callback_func,
3465                             GObject  *object)
3466 {
3467   GClosure *closure;
3468
3469   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3470   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3471   g_return_val_if_fail (callback_func != NULL, NULL);
3472
3473   closure = g_cclosure_new_swap (callback_func, object, NULL);
3474   g_object_watch_closure (object, closure);
3475
3476   return closure;
3477 }
3478
3479 gsize
3480 g_object_compat_control (gsize           what,
3481                          gpointer        data)
3482 {
3483   switch (what)
3484     {
3485       gpointer *pp;
3486     case 1:     /* floating base type */
3487       return G_TYPE_INITIALLY_UNOWNED;
3488     case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3489       floating_flag_handler = (guint(*)(GObject*,gint)) data;
3490       return 1;
3491     case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3492       pp = data;
3493       *pp = floating_flag_handler;
3494       return 1;
3495     default:
3496       return 0;
3497     }
3498 }
3499
3500 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3501
3502 static void
3503 g_initially_unowned_init (GInitiallyUnowned *object)
3504 {
3505   g_object_force_floating (object);
3506 }
3507
3508 static void
3509 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3510 {
3511 }