Make object_interface_check_properties work
[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;
1193   GType iface_type = iface_class->g_type;
1194   GParamSpec **pspecs;
1195   guint n;
1196
1197   class = g_type_class_ref (iface_class->g_instance_type);
1198
1199   if (!G_IS_OBJECT_CLASS (class))
1200     return;
1201
1202   pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1203
1204   while (n--)
1205     {
1206       GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1207                                                           pspecs[n]->name,
1208                                                           G_OBJECT_CLASS_TYPE (class),
1209                                                           TRUE);
1210
1211       if (!class_pspec)
1212         {
1213           g_critical ("Object class %s doesn't implement property "
1214                       "'%s' from interface '%s'",
1215                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1216                       pspecs[n]->name,
1217                       g_type_name (iface_type));
1218
1219           continue;
1220         }
1221
1222       /* The implementation paramspec must have a less restrictive
1223        * type than the interface parameter spec for set() and a
1224        * more restrictive type for get(). We just require equality,
1225        * rather than doing something more complicated checking
1226        * the READABLE and WRITABLE flags. We also simplify here
1227        * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1228        */
1229       if (class_pspec &&
1230           !g_type_is_a (pspecs[n]->value_type,
1231                         class_pspec->value_type))
1232         {
1233           g_critical ("Property '%s' on class '%s' has type '%s' "
1234                       "which is different from the type '%s', "
1235                       "of the property on interface '%s'\n",
1236                       pspecs[n]->name,
1237                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1238                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1239                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1240                       g_type_name (iface_type));
1241         }
1242
1243 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1244
1245       /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1246        * READABLE and WRITABLE remove restrictions. The implementation
1247        * paramspec must have less restrictive flags.
1248        */
1249       if (class_pspec &&
1250           (!SUBSET (class_pspec->flags,
1251                     pspecs[n]->flags,
1252                     G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1253            !SUBSET (pspecs[n]->flags,
1254                     class_pspec->flags,
1255                     G_PARAM_READABLE | G_PARAM_WRITABLE)))
1256         {
1257           g_critical ("Flags for property '%s' on class '%s' "
1258                       "are not compatible with the property on"
1259                       "interface '%s'\n",
1260                       pspecs[n]->name,
1261                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1262                       g_type_name (iface_type));
1263         }
1264 #undef SUBSET
1265     }
1266
1267   g_free (pspecs);
1268
1269   g_type_class_unref (class);
1270 }
1271
1272 GType
1273 g_object_get_type (void)
1274 {
1275     return G_TYPE_OBJECT;
1276 }
1277
1278 /**
1279  * g_object_new: (skip)
1280  * @object_type: the type id of the #GObject subtype to instantiate
1281  * @first_property_name: the name of the first property
1282  * @...: the value of the first property, followed optionally by more
1283  *  name/value pairs, followed by %NULL
1284  *
1285  * Creates a new instance of a #GObject subtype and sets its properties.
1286  *
1287  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1288  * which are not explicitly specified are set to their default values.
1289  *
1290  * Returns: (transfer full): a new instance of @object_type
1291  */
1292 gpointer
1293 g_object_new (GType        object_type,
1294               const gchar *first_property_name,
1295               ...)
1296 {
1297   GObject *object;
1298   va_list var_args;
1299   
1300   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1301   
1302   /* short circuit for calls supplying no properties */
1303   if (!first_property_name)
1304     return g_object_newv (object_type, 0, NULL);
1305
1306   va_start (var_args, first_property_name);
1307   object = g_object_new_valist (object_type, first_property_name, var_args);
1308   va_end (var_args);
1309   
1310   return object;
1311 }
1312
1313 static gboolean
1314 slist_maybe_remove (GSList       **slist,
1315                     gconstpointer  data)
1316 {
1317   GSList *last = NULL, *node = *slist;
1318   while (node)
1319     {
1320       if (node->data == data)
1321         {
1322           if (last)
1323             last->next = node->next;
1324           else
1325             *slist = node->next;
1326           g_slist_free_1 (node);
1327           return TRUE;
1328         }
1329       last = node;
1330       node = last->next;
1331     }
1332   return FALSE;
1333 }
1334
1335 static inline gboolean
1336 object_in_construction_list (GObject *object)
1337 {
1338   gboolean in_construction;
1339   G_LOCK (construction_mutex);
1340   in_construction = g_slist_find (construction_objects, object) != NULL;
1341   G_UNLOCK (construction_mutex);
1342   return in_construction;
1343 }
1344
1345 /**
1346  * g_object_newv:
1347  * @object_type: the type id of the #GObject subtype to instantiate
1348  * @n_parameters: the length of the @parameters array
1349  * @parameters: (array length=n_parameters): an array of #GParameter
1350  *
1351  * Creates a new instance of a #GObject subtype and sets its properties.
1352  *
1353  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1354  * which are not explicitly specified are set to their default values.
1355  *
1356  * Rename to: g_object_new
1357  * Returns: (type GObject.Object) (transfer full): a new instance of
1358  * @object_type
1359  */
1360 gpointer
1361 g_object_newv (GType       object_type,
1362                guint       n_parameters,
1363                GParameter *parameters)
1364 {
1365   GObjectConstructParam *cparams = NULL, *oparams;
1366   GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1367   GObject *object;
1368   GObjectClass *class, *unref_class = NULL;
1369   GSList *slist;
1370   guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1371   GValue *cvalues;
1372   GList *clist = NULL;
1373   gboolean newly_constructed;
1374   guint i;
1375
1376   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1377
1378   class = g_type_class_peek_static (object_type);
1379   if (!class)
1380     class = unref_class = g_type_class_ref (object_type);
1381   for (slist = class->construct_properties; slist; slist = slist->next)
1382     {
1383       clist = g_list_prepend (clist, slist->data);
1384       n_total_cparams += 1;
1385     }
1386
1387   if (n_parameters == 0 && n_total_cparams == 0)
1388     {
1389       /* This is a simple object with no construct properties, and
1390        * no properties are being set, so short circuit the parameter
1391        * handling. This speeds up simple object construction.
1392        */
1393       oparams = NULL;
1394       object = class->constructor (object_type, 0, NULL);
1395       goto did_construction;
1396     }
1397
1398   /* collect parameters, sort into construction and normal ones */
1399   oparams = g_new (GObjectConstructParam, n_parameters);
1400   cparams = g_new (GObjectConstructParam, n_total_cparams);
1401   for (i = 0; i < n_parameters; i++)
1402     {
1403       GValue *value = &parameters[i].value;
1404       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1405                                                     parameters[i].name,
1406                                                     object_type,
1407                                                     TRUE);
1408       if (!pspec)
1409         {
1410           g_warning ("%s: object class `%s' has no property named `%s'",
1411                      G_STRFUNC,
1412                      g_type_name (object_type),
1413                      parameters[i].name);
1414           continue;
1415         }
1416       if (!(pspec->flags & G_PARAM_WRITABLE))
1417         {
1418           g_warning ("%s: property `%s' of object class `%s' is not writable",
1419                      G_STRFUNC,
1420                      pspec->name,
1421                      g_type_name (object_type));
1422           continue;
1423         }
1424       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1425         {
1426           GList *list = g_list_find (clist, pspec);
1427
1428           if (!list)
1429             {
1430               g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1431                          G_STRFUNC, pspec->name, g_type_name (object_type));
1432               continue;
1433             }
1434           cparams[n_cparams].pspec = pspec;
1435           cparams[n_cparams].value = value;
1436           n_cparams++;
1437           if (!list->prev)
1438             clist = list->next;
1439           else
1440             list->prev->next = list->next;
1441           if (list->next)
1442             list->next->prev = list->prev;
1443           g_list_free_1 (list);
1444         }
1445       else
1446         {
1447           oparams[n_oparams].pspec = pspec;
1448           oparams[n_oparams].value = value;
1449           n_oparams++;
1450         }
1451     }
1452
1453   /* set remaining construction properties to default values */
1454   n_cvalues = n_total_cparams - n_cparams;
1455   cvalues = g_new (GValue, n_cvalues);
1456   while (clist)
1457     {
1458       GList *tmp = clist->next;
1459       GParamSpec *pspec = clist->data;
1460       GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1461
1462       value->g_type = 0;
1463       g_value_init (value, pspec->value_type);
1464       g_param_value_set_default (pspec, value);
1465
1466       cparams[n_cparams].pspec = pspec;
1467       cparams[n_cparams].value = value;
1468       n_cparams++;
1469
1470       g_list_free_1 (clist);
1471       clist = tmp;
1472     }
1473
1474   /* construct object from construction parameters */
1475   object = class->constructor (object_type, n_total_cparams, cparams);
1476   /* free construction values */
1477   g_free (cparams);
1478   while (n_cvalues--)
1479     g_value_unset (cvalues + n_cvalues);
1480   g_free (cvalues);
1481
1482  did_construction:
1483   if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1484     {
1485       /* adjust freeze_count according to g_object_init() and remaining properties */
1486       G_LOCK (construction_mutex);
1487       newly_constructed = slist_maybe_remove (&construction_objects, object);
1488       G_UNLOCK (construction_mutex);
1489     }
1490   else
1491     newly_constructed = TRUE;
1492
1493   if (CLASS_HAS_PROPS (class))
1494     {
1495       if (newly_constructed || n_oparams)
1496         nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1497       if (newly_constructed)
1498         g_object_notify_queue_thaw (object, nqueue);
1499     }
1500
1501   /* run 'constructed' handler if there is one */
1502   if (newly_constructed && class->constructed)
1503     class->constructed (object);
1504
1505   /* set remaining properties */
1506   for (i = 0; i < n_oparams; i++)
1507     object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1508   g_free (oparams);
1509
1510   if (CLASS_HAS_PROPS (class))
1511     {
1512       /* release our own freeze count and handle notifications */
1513       if (newly_constructed || n_oparams)
1514         g_object_notify_queue_thaw (object, nqueue);
1515     }
1516
1517   if (unref_class)
1518     g_type_class_unref (unref_class);
1519
1520   return object;
1521 }
1522
1523 /**
1524  * g_object_new_valist: (skip)
1525  * @object_type: the type id of the #GObject subtype to instantiate
1526  * @first_property_name: the name of the first property
1527  * @var_args: the value of the first property, followed optionally by more
1528  *  name/value pairs, followed by %NULL
1529  *
1530  * Creates a new instance of a #GObject subtype and sets its properties.
1531  *
1532  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1533  * which are not explicitly specified are set to their default values.
1534  *
1535  * Returns: a new instance of @object_type
1536  */
1537 GObject*
1538 g_object_new_valist (GType        object_type,
1539                      const gchar *first_property_name,
1540                      va_list      var_args)
1541 {
1542   GObjectClass *class;
1543   GParameter *params;
1544   const gchar *name;
1545   GObject *object;
1546   guint n_params = 0, n_alloced_params = 16;
1547   
1548   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1549
1550   if (!first_property_name)
1551     return g_object_newv (object_type, 0, NULL);
1552
1553   class = g_type_class_ref (object_type);
1554
1555   params = g_new0 (GParameter, n_alloced_params);
1556   name = first_property_name;
1557   while (name)
1558     {
1559       gchar *error = NULL;
1560       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1561                                                     name,
1562                                                     object_type,
1563                                                     TRUE);
1564       if (!pspec)
1565         {
1566           g_warning ("%s: object class `%s' has no property named `%s'",
1567                      G_STRFUNC,
1568                      g_type_name (object_type),
1569                      name);
1570           break;
1571         }
1572       if (n_params >= n_alloced_params)
1573         {
1574           n_alloced_params += 16;
1575           params = g_renew (GParameter, params, n_alloced_params);
1576           memset (params + n_params, 0, 16 * (sizeof *params));
1577         }
1578       params[n_params].name = name;
1579       G_VALUE_COLLECT_INIT (&params[n_params].value, pspec->value_type,
1580                             var_args, 0, &error);
1581       if (error)
1582         {
1583           g_warning ("%s: %s", G_STRFUNC, error);
1584           g_free (error);
1585           g_value_unset (&params[n_params].value);
1586           break;
1587         }
1588       n_params++;
1589       name = va_arg (var_args, gchar*);
1590     }
1591
1592   object = g_object_newv (object_type, n_params, params);
1593
1594   while (n_params--)
1595     g_value_unset (&params[n_params].value);
1596   g_free (params);
1597
1598   g_type_class_unref (class);
1599
1600   return object;
1601 }
1602
1603 static GObject*
1604 g_object_constructor (GType                  type,
1605                       guint                  n_construct_properties,
1606                       GObjectConstructParam *construct_params)
1607 {
1608   GObject *object;
1609
1610   /* create object */
1611   object = (GObject*) g_type_create_instance (type);
1612   
1613   /* set construction parameters */
1614   if (n_construct_properties)
1615     {
1616       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1617       
1618       /* set construct properties */
1619       while (n_construct_properties--)
1620         {
1621           GValue *value = construct_params->value;
1622           GParamSpec *pspec = construct_params->pspec;
1623
1624           construct_params++;
1625           object_set_property (object, pspec, value, nqueue);
1626         }
1627       g_object_notify_queue_thaw (object, nqueue);
1628       /* the notification queue is still frozen from g_object_init(), so
1629        * we don't need to handle it here, g_object_newv() takes
1630        * care of that
1631        */
1632     }
1633
1634   return object;
1635 }
1636
1637 /**
1638  * g_object_set_valist: (skip)
1639  * @object: a #GObject
1640  * @first_property_name: name of the first property to set
1641  * @var_args: value for the first property, followed optionally by more
1642  *  name/value pairs, followed by %NULL
1643  *
1644  * Sets properties on an object.
1645  */
1646 void
1647 g_object_set_valist (GObject     *object,
1648                      const gchar *first_property_name,
1649                      va_list      var_args)
1650 {
1651   GObjectNotifyQueue *nqueue;
1652   const gchar *name;
1653   
1654   g_return_if_fail (G_IS_OBJECT (object));
1655   
1656   g_object_ref (object);
1657   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1658   
1659   name = first_property_name;
1660   while (name)
1661     {
1662       GValue value = { 0, };
1663       GParamSpec *pspec;
1664       gchar *error = NULL;
1665       
1666       pspec = g_param_spec_pool_lookup (pspec_pool,
1667                                         name,
1668                                         G_OBJECT_TYPE (object),
1669                                         TRUE);
1670       if (!pspec)
1671         {
1672           g_warning ("%s: object class `%s' has no property named `%s'",
1673                      G_STRFUNC,
1674                      G_OBJECT_TYPE_NAME (object),
1675                      name);
1676           break;
1677         }
1678       if (!(pspec->flags & G_PARAM_WRITABLE))
1679         {
1680           g_warning ("%s: property `%s' of object class `%s' is not writable",
1681                      G_STRFUNC,
1682                      pspec->name,
1683                      G_OBJECT_TYPE_NAME (object));
1684           break;
1685         }
1686       if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1687         {
1688           g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1689                      G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1690           break;
1691         }
1692
1693       G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1694                             0, &error);
1695       if (error)
1696         {
1697           g_warning ("%s: %s", G_STRFUNC, error);
1698           g_free (error);
1699           g_value_unset (&value);
1700           break;
1701         }
1702       
1703       object_set_property (object, pspec, &value, nqueue);
1704       g_value_unset (&value);
1705       
1706       name = va_arg (var_args, gchar*);
1707     }
1708
1709   g_object_notify_queue_thaw (object, nqueue);
1710   g_object_unref (object);
1711 }
1712
1713 /**
1714  * g_object_get_valist: (skip)
1715  * @object: a #GObject
1716  * @first_property_name: name of the first property to get
1717  * @var_args: return location for the first property, followed optionally by more
1718  *  name/return location pairs, followed by %NULL
1719  *
1720  * Gets properties of an object.
1721  *
1722  * In general, a copy is made of the property contents and the caller
1723  * is responsible for freeing the memory in the appropriate manner for
1724  * the type, for instance by calling g_free() or g_object_unref().
1725  *
1726  * See g_object_get().
1727  */
1728 void
1729 g_object_get_valist (GObject     *object,
1730                      const gchar *first_property_name,
1731                      va_list      var_args)
1732 {
1733   const gchar *name;
1734   
1735   g_return_if_fail (G_IS_OBJECT (object));
1736   
1737   g_object_ref (object);
1738   
1739   name = first_property_name;
1740   
1741   while (name)
1742     {
1743       GValue value = { 0, };
1744       GParamSpec *pspec;
1745       gchar *error;
1746       
1747       pspec = g_param_spec_pool_lookup (pspec_pool,
1748                                         name,
1749                                         G_OBJECT_TYPE (object),
1750                                         TRUE);
1751       if (!pspec)
1752         {
1753           g_warning ("%s: object class `%s' has no property named `%s'",
1754                      G_STRFUNC,
1755                      G_OBJECT_TYPE_NAME (object),
1756                      name);
1757           break;
1758         }
1759       if (!(pspec->flags & G_PARAM_READABLE))
1760         {
1761           g_warning ("%s: property `%s' of object class `%s' is not readable",
1762                      G_STRFUNC,
1763                      pspec->name,
1764                      G_OBJECT_TYPE_NAME (object));
1765           break;
1766         }
1767       
1768       g_value_init (&value, pspec->value_type);
1769       
1770       object_get_property (object, pspec, &value);
1771       
1772       G_VALUE_LCOPY (&value, var_args, 0, &error);
1773       if (error)
1774         {
1775           g_warning ("%s: %s", G_STRFUNC, error);
1776           g_free (error);
1777           g_value_unset (&value);
1778           break;
1779         }
1780       
1781       g_value_unset (&value);
1782       
1783       name = va_arg (var_args, gchar*);
1784     }
1785   
1786   g_object_unref (object);
1787 }
1788
1789 /**
1790  * g_object_set: (skip)
1791  * @object: a #GObject
1792  * @first_property_name: name of the first property to set
1793  * @...: value for the first property, followed optionally by more
1794  *  name/value pairs, followed by %NULL
1795  *
1796  * Sets properties on an object.
1797  */
1798 void
1799 g_object_set (gpointer     _object,
1800               const gchar *first_property_name,
1801               ...)
1802 {
1803   GObject *object = _object;
1804   va_list var_args;
1805   
1806   g_return_if_fail (G_IS_OBJECT (object));
1807   
1808   va_start (var_args, first_property_name);
1809   g_object_set_valist (object, first_property_name, var_args);
1810   va_end (var_args);
1811 }
1812
1813 /**
1814  * g_object_get: (skip)
1815  * @object: a #GObject
1816  * @first_property_name: name of the first property to get
1817  * @...: return location for the first property, followed optionally by more
1818  *  name/return location pairs, followed by %NULL
1819  *
1820  * Gets properties of an object.
1821  *
1822  * In general, a copy is made of the property contents and the caller
1823  * is responsible for freeing the memory in the appropriate manner for
1824  * the type, for instance by calling g_free() or g_object_unref().
1825  *
1826  * <example>
1827  * <title>Using g_object_get(<!-- -->)</title>
1828  * An example of using g_object_get() to get the contents
1829  * of three properties - one of type #G_TYPE_INT,
1830  * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1831  * <programlisting>
1832  *  gint intval;
1833  *  gchar *strval;
1834  *  GObject *objval;
1835  *
1836  *  g_object_get (my_object,
1837  *                "int-property", &intval,
1838  *                "str-property", &strval,
1839  *                "obj-property", &objval,
1840  *                NULL);
1841  *
1842  *  // Do something with intval, strval, objval
1843  *
1844  *  g_free (strval);
1845  *  g_object_unref (objval);
1846  * </programlisting>
1847  * </example>
1848  */
1849 void
1850 g_object_get (gpointer     _object,
1851               const gchar *first_property_name,
1852               ...)
1853 {
1854   GObject *object = _object;
1855   va_list var_args;
1856   
1857   g_return_if_fail (G_IS_OBJECT (object));
1858   
1859   va_start (var_args, first_property_name);
1860   g_object_get_valist (object, first_property_name, var_args);
1861   va_end (var_args);
1862 }
1863
1864 /**
1865  * g_object_set_property:
1866  * @object: a #GObject
1867  * @property_name: the name of the property to set
1868  * @value: the value
1869  *
1870  * Sets a property on an object.
1871  */
1872 void
1873 g_object_set_property (GObject      *object,
1874                        const gchar  *property_name,
1875                        const GValue *value)
1876 {
1877   GObjectNotifyQueue *nqueue;
1878   GParamSpec *pspec;
1879   
1880   g_return_if_fail (G_IS_OBJECT (object));
1881   g_return_if_fail (property_name != NULL);
1882   g_return_if_fail (G_IS_VALUE (value));
1883   
1884   g_object_ref (object);
1885   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1886   
1887   pspec = g_param_spec_pool_lookup (pspec_pool,
1888                                     property_name,
1889                                     G_OBJECT_TYPE (object),
1890                                     TRUE);
1891   if (!pspec)
1892     g_warning ("%s: object class `%s' has no property named `%s'",
1893                G_STRFUNC,
1894                G_OBJECT_TYPE_NAME (object),
1895                property_name);
1896   else if (!(pspec->flags & G_PARAM_WRITABLE))
1897     g_warning ("%s: property `%s' of object class `%s' is not writable",
1898                G_STRFUNC,
1899                pspec->name,
1900                G_OBJECT_TYPE_NAME (object));
1901   else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1902     g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1903                G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1904   else
1905     object_set_property (object, pspec, value, nqueue);
1906   
1907   g_object_notify_queue_thaw (object, nqueue);
1908   g_object_unref (object);
1909 }
1910
1911 /**
1912  * g_object_get_property:
1913  * @object: a #GObject
1914  * @property_name: the name of the property to get
1915  * @value: return location for the property value
1916  *
1917  * Gets a property of an object. @value must have been initialized to the
1918  * expected type of the property (or a type to which the expected type can be
1919  * transformed) using g_value_init().
1920  *
1921  * In general, a copy is made of the property contents and the caller is
1922  * responsible for freeing the memory by calling g_value_unset().
1923  *
1924  * Note that g_object_get_property() is really intended for language
1925  * bindings, g_object_get() is much more convenient for C programming.
1926  */
1927 void
1928 g_object_get_property (GObject     *object,
1929                        const gchar *property_name,
1930                        GValue      *value)
1931 {
1932   GParamSpec *pspec;
1933   
1934   g_return_if_fail (G_IS_OBJECT (object));
1935   g_return_if_fail (property_name != NULL);
1936   g_return_if_fail (G_IS_VALUE (value));
1937   
1938   g_object_ref (object);
1939   
1940   pspec = g_param_spec_pool_lookup (pspec_pool,
1941                                     property_name,
1942                                     G_OBJECT_TYPE (object),
1943                                     TRUE);
1944   if (!pspec)
1945     g_warning ("%s: object class `%s' has no property named `%s'",
1946                G_STRFUNC,
1947                G_OBJECT_TYPE_NAME (object),
1948                property_name);
1949   else if (!(pspec->flags & G_PARAM_READABLE))
1950     g_warning ("%s: property `%s' of object class `%s' is not readable",
1951                G_STRFUNC,
1952                pspec->name,
1953                G_OBJECT_TYPE_NAME (object));
1954   else
1955     {
1956       GValue *prop_value, tmp_value = { 0, };
1957       
1958       /* auto-conversion of the callers value type
1959        */
1960       if (G_VALUE_TYPE (value) == pspec->value_type)
1961         {
1962           g_value_reset (value);
1963           prop_value = value;
1964         }
1965       else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1966         {
1967           g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1968                      G_STRFUNC, pspec->name,
1969                      g_type_name (pspec->value_type),
1970                      G_VALUE_TYPE_NAME (value));
1971           g_object_unref (object);
1972           return;
1973         }
1974       else
1975         {
1976           g_value_init (&tmp_value, pspec->value_type);
1977           prop_value = &tmp_value;
1978         }
1979       object_get_property (object, pspec, prop_value);
1980       if (prop_value != value)
1981         {
1982           g_value_transform (prop_value, value);
1983           g_value_unset (&tmp_value);
1984         }
1985     }
1986   
1987   g_object_unref (object);
1988 }
1989
1990 /**
1991  * g_object_connect: (skip)
1992  * @object: a #GObject
1993  * @signal_spec: the spec for the first signal
1994  * @...: #GCallback for the first signal, followed by data for the
1995  *       first signal, followed optionally by more signal
1996  *       spec/callback/data triples, followed by %NULL
1997  *
1998  * A convenience function to connect multiple signals at once.
1999  *
2000  * The signal specs expected by this function have the form
2001  * "modifier::signal_name", where modifier can be one of the following:
2002  * <variablelist>
2003  * <varlistentry>
2004  * <term>signal</term>
2005  * <listitem><para>
2006  * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2007  * </para></listitem>
2008  * </varlistentry>
2009  * <varlistentry>
2010  * <term>object_signal</term>
2011  * <term>object-signal</term>
2012  * <listitem><para>
2013  * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2014  * </para></listitem>
2015  * </varlistentry>
2016  * <varlistentry>
2017  * <term>swapped_signal</term>
2018  * <term>swapped-signal</term>
2019  * <listitem><para>
2020  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2021  * </para></listitem>
2022  * </varlistentry>
2023  * <varlistentry>
2024  * <term>swapped_object_signal</term>
2025  * <term>swapped-object-signal</term>
2026  * <listitem><para>
2027  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2028  * </para></listitem>
2029  * </varlistentry>
2030  * <varlistentry>
2031  * <term>signal_after</term>
2032  * <term>signal-after</term>
2033  * <listitem><para>
2034  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2035  * </para></listitem>
2036  * </varlistentry>
2037  * <varlistentry>
2038  * <term>object_signal_after</term>
2039  * <term>object-signal-after</term>
2040  * <listitem><para>
2041  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2042  * </para></listitem>
2043  * </varlistentry>
2044  * <varlistentry>
2045  * <term>swapped_signal_after</term>
2046  * <term>swapped-signal-after</term>
2047  * <listitem><para>
2048  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2049  * </para></listitem>
2050  * </varlistentry>
2051  * <varlistentry>
2052  * <term>swapped_object_signal_after</term>
2053  * <term>swapped-object-signal-after</term>
2054  * <listitem><para>
2055  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2056  * </para></listitem>
2057  * </varlistentry>
2058  * </variablelist>
2059  *
2060  * |[
2061  *   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2062  *                                                 "type", GTK_WINDOW_POPUP,
2063  *                                                 "child", menu,
2064  *                                                 NULL),
2065  *                                   "signal::event", gtk_menu_window_event, menu,
2066  *                                   "signal::size_request", gtk_menu_window_size_request, menu,
2067  *                                   "signal::destroy", gtk_widget_destroyed, &amp;menu-&gt;toplevel,
2068  *                                   NULL);
2069  * ]|
2070  *
2071  * Returns: (transfer none): @object
2072  */
2073 gpointer
2074 g_object_connect (gpointer     _object,
2075                   const gchar *signal_spec,
2076                   ...)
2077 {
2078   GObject *object = _object;
2079   va_list var_args;
2080
2081   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2082   g_return_val_if_fail (object->ref_count > 0, object);
2083
2084   va_start (var_args, signal_spec);
2085   while (signal_spec)
2086     {
2087       GCallback callback = va_arg (var_args, GCallback);
2088       gpointer data = va_arg (var_args, gpointer);
2089       gulong sid;
2090
2091       if (strncmp (signal_spec, "signal::", 8) == 0)
2092         sid = g_signal_connect_data (object, signal_spec + 8,
2093                                      callback, data, NULL,
2094                                      0);
2095       else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2096                strncmp (signal_spec, "object-signal::", 15) == 0)
2097         sid = g_signal_connect_object (object, signal_spec + 15,
2098                                        callback, data,
2099                                        0);
2100       else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2101                strncmp (signal_spec, "swapped-signal::", 16) == 0)
2102         sid = g_signal_connect_data (object, signal_spec + 16,
2103                                      callback, data, NULL,
2104                                      G_CONNECT_SWAPPED);
2105       else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2106                strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2107         sid = g_signal_connect_object (object, signal_spec + 23,
2108                                        callback, data,
2109                                        G_CONNECT_SWAPPED);
2110       else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2111                strncmp (signal_spec, "signal-after::", 14) == 0)
2112         sid = g_signal_connect_data (object, signal_spec + 14,
2113                                      callback, data, NULL,
2114                                      G_CONNECT_AFTER);
2115       else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2116                strncmp (signal_spec, "object-signal-after::", 21) == 0)
2117         sid = g_signal_connect_object (object, signal_spec + 21,
2118                                        callback, data,
2119                                        G_CONNECT_AFTER);
2120       else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2121                strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2122         sid = g_signal_connect_data (object, signal_spec + 22,
2123                                      callback, data, NULL,
2124                                      G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2125       else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2126                strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2127         sid = g_signal_connect_object (object, signal_spec + 29,
2128                                        callback, data,
2129                                        G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2130       else
2131         {
2132           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2133           break;
2134         }
2135       signal_spec = va_arg (var_args, gchar*);
2136     }
2137   va_end (var_args);
2138
2139   return object;
2140 }
2141
2142 /**
2143  * g_object_disconnect: (skip)
2144  * @object: a #GObject
2145  * @signal_spec: the spec for the first signal
2146  * @...: #GCallback for the first signal, followed by data for the first signal,
2147  *  followed optionally by more signal spec/callback/data triples,
2148  *  followed by %NULL
2149  *
2150  * A convenience function to disconnect multiple signals at once.
2151  *
2152  * The signal specs expected by this function have the form
2153  * "any_signal", which means to disconnect any signal with matching
2154  * callback and data, or "any_signal::signal_name", which only
2155  * disconnects the signal named "signal_name".
2156  */
2157 void
2158 g_object_disconnect (gpointer     _object,
2159                      const gchar *signal_spec,
2160                      ...)
2161 {
2162   GObject *object = _object;
2163   va_list var_args;
2164
2165   g_return_if_fail (G_IS_OBJECT (object));
2166   g_return_if_fail (object->ref_count > 0);
2167
2168   va_start (var_args, signal_spec);
2169   while (signal_spec)
2170     {
2171       GCallback callback = va_arg (var_args, GCallback);
2172       gpointer data = va_arg (var_args, gpointer);
2173       guint sid = 0, detail = 0, mask = 0;
2174
2175       if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2176           strncmp (signal_spec, "any-signal::", 12) == 0)
2177         {
2178           signal_spec += 12;
2179           mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2180         }
2181       else if (strcmp (signal_spec, "any_signal") == 0 ||
2182                strcmp (signal_spec, "any-signal") == 0)
2183         {
2184           signal_spec += 10;
2185           mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2186         }
2187       else
2188         {
2189           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2190           break;
2191         }
2192
2193       if ((mask & G_SIGNAL_MATCH_ID) &&
2194           !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2195         g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2196       else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2197                                                       sid, detail,
2198                                                       NULL, (gpointer)callback, data))
2199         g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2200       signal_spec = va_arg (var_args, gchar*);
2201     }
2202   va_end (var_args);
2203 }
2204
2205 typedef struct {
2206   GObject *object;
2207   guint n_weak_refs;
2208   struct {
2209     GWeakNotify notify;
2210     gpointer    data;
2211   } weak_refs[1];  /* flexible array */
2212 } WeakRefStack;
2213
2214 static void
2215 weak_refs_notify (gpointer data)
2216 {
2217   WeakRefStack *wstack = data;
2218   guint i;
2219
2220   for (i = 0; i < wstack->n_weak_refs; i++)
2221     wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2222   g_free (wstack);
2223 }
2224
2225 /**
2226  * g_object_weak_ref: (skip)
2227  * @object: #GObject to reference weakly
2228  * @notify: callback to invoke before the object is freed
2229  * @data: extra data to pass to notify
2230  *
2231  * Adds a weak reference callback to an object. Weak references are
2232  * used for notification when an object is finalized. They are called
2233  * "weak references" because they allow you to safely hold a pointer
2234  * to an object without calling g_object_ref() (g_object_ref() adds a
2235  * strong reference, that is, forces the object to stay alive).
2236  */
2237 void
2238 g_object_weak_ref (GObject    *object,
2239                    GWeakNotify notify,
2240                    gpointer    data)
2241 {
2242   WeakRefStack *wstack;
2243   guint i;
2244   
2245   g_return_if_fail (G_IS_OBJECT (object));
2246   g_return_if_fail (notify != NULL);
2247   g_return_if_fail (object->ref_count >= 1);
2248
2249   G_LOCK (weak_refs_mutex);
2250   wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2251   if (wstack)
2252     {
2253       i = wstack->n_weak_refs++;
2254       wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2255     }
2256   else
2257     {
2258       wstack = g_renew (WeakRefStack, NULL, 1);
2259       wstack->object = object;
2260       wstack->n_weak_refs = 1;
2261       i = 0;
2262     }
2263   wstack->weak_refs[i].notify = notify;
2264   wstack->weak_refs[i].data = data;
2265   g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2266   G_UNLOCK (weak_refs_mutex);
2267 }
2268
2269 /**
2270  * g_object_weak_unref: (skip)
2271  * @object: #GObject to remove a weak reference from
2272  * @notify: callback to search for
2273  * @data: data to search for
2274  *
2275  * Removes a weak reference callback to an object.
2276  */
2277 void
2278 g_object_weak_unref (GObject    *object,
2279                      GWeakNotify notify,
2280                      gpointer    data)
2281 {
2282   WeakRefStack *wstack;
2283   gboolean found_one = FALSE;
2284
2285   g_return_if_fail (G_IS_OBJECT (object));
2286   g_return_if_fail (notify != NULL);
2287
2288   G_LOCK (weak_refs_mutex);
2289   wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2290   if (wstack)
2291     {
2292       guint i;
2293
2294       for (i = 0; i < wstack->n_weak_refs; i++)
2295         if (wstack->weak_refs[i].notify == notify &&
2296             wstack->weak_refs[i].data == data)
2297           {
2298             found_one = TRUE;
2299             wstack->n_weak_refs -= 1;
2300             if (i != wstack->n_weak_refs)
2301               wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2302
2303             break;
2304           }
2305     }
2306   G_UNLOCK (weak_refs_mutex);
2307   if (!found_one)
2308     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2309 }
2310
2311 /**
2312  * g_object_add_weak_pointer: (skip)
2313  * @object: The object that should be weak referenced.
2314  * @weak_pointer_location: (inout): The memory address of a pointer.
2315  *
2316  * Adds a weak reference from weak_pointer to @object to indicate that
2317  * the pointer located at @weak_pointer_location is only valid during
2318  * the lifetime of @object. When the @object is finalized,
2319  * @weak_pointer will be set to %NULL.
2320  */
2321 void
2322 g_object_add_weak_pointer (GObject  *object, 
2323                            gpointer *weak_pointer_location)
2324 {
2325   g_return_if_fail (G_IS_OBJECT (object));
2326   g_return_if_fail (weak_pointer_location != NULL);
2327
2328   g_object_weak_ref (object, 
2329                      (GWeakNotify) g_nullify_pointer, 
2330                      weak_pointer_location);
2331 }
2332
2333 /**
2334  * g_object_remove_weak_pointer: (skip)
2335  * @object: The object that is weak referenced.
2336  * @weak_pointer_location: (inout): The memory address of a pointer.
2337  *
2338  * Removes a weak reference from @object that was previously added
2339  * using g_object_add_weak_pointer(). The @weak_pointer_location has
2340  * to match the one used with g_object_add_weak_pointer().
2341  */
2342 void
2343 g_object_remove_weak_pointer (GObject  *object, 
2344                               gpointer *weak_pointer_location)
2345 {
2346   g_return_if_fail (G_IS_OBJECT (object));
2347   g_return_if_fail (weak_pointer_location != NULL);
2348
2349   g_object_weak_unref (object, 
2350                        (GWeakNotify) g_nullify_pointer, 
2351                        weak_pointer_location);
2352 }
2353
2354 static guint
2355 object_floating_flag_handler (GObject        *object,
2356                               gint            job)
2357 {
2358   switch (job)
2359     {
2360       gpointer oldvalue;
2361     case +1:    /* force floating if possible */
2362       do
2363         oldvalue = g_atomic_pointer_get (&object->qdata);
2364       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2365                                                      (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2366       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2367     case -1:    /* sink if possible */
2368       do
2369         oldvalue = g_atomic_pointer_get (&object->qdata);
2370       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2371                                                      (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2372       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2373     default:    /* check floating */
2374       return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2375     }
2376 }
2377
2378 /**
2379  * g_object_is_floating:
2380  * @object: (type GObject.Object): a #GObject
2381  *
2382  * Checks wether @object has a <link linkend="floating-ref">floating</link>
2383  * reference.
2384  *
2385  * Since: 2.10
2386  *
2387  * Returns: %TRUE if @object has a floating reference
2388  */
2389 gboolean
2390 g_object_is_floating (gpointer _object)
2391 {
2392   GObject *object = _object;
2393   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2394   return floating_flag_handler (object, 0);
2395 }
2396
2397 /**
2398  * g_object_ref_sink:
2399  * @object: (type GObject.Object): a #GObject
2400  *
2401  * Increase the reference count of @object, and possibly remove the
2402  * <link linkend="floating-ref">floating</link> reference, if @object
2403  * has a floating reference.
2404  *
2405  * In other words, if the object is floating, then this call "assumes
2406  * ownership" of the floating reference, converting it to a normal
2407  * reference by clearing the floating flag while leaving the reference
2408  * count unchanged.  If the object is not floating, then this call
2409  * adds a new normal reference increasing the reference count by one.
2410  *
2411  * Since: 2.10
2412  *
2413  * Returns: (type GObject.Object) (transfer none): @object
2414  */
2415 gpointer
2416 g_object_ref_sink (gpointer _object)
2417 {
2418   GObject *object = _object;
2419   gboolean was_floating;
2420   g_return_val_if_fail (G_IS_OBJECT (object), object);
2421   g_return_val_if_fail (object->ref_count >= 1, object);
2422   g_object_ref (object);
2423   was_floating = floating_flag_handler (object, -1);
2424   if (was_floating)
2425     g_object_unref (object);
2426   return object;
2427 }
2428
2429 /**
2430  * g_object_force_floating:
2431  * @object: a #GObject
2432  *
2433  * This function is intended for #GObject implementations to re-enforce a
2434  * <link linkend="floating-ref">floating</link> object reference.
2435  * Doing this is seldomly required, all
2436  * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2437  * usually just needs to be sunken by calling g_object_ref_sink().
2438  *
2439  * Since: 2.10
2440  */
2441 void
2442 g_object_force_floating (GObject *object)
2443 {
2444   gboolean was_floating;
2445   g_return_if_fail (G_IS_OBJECT (object));
2446   g_return_if_fail (object->ref_count >= 1);
2447
2448   was_floating = floating_flag_handler (object, +1);
2449 }
2450
2451 typedef struct {
2452   GObject *object;
2453   guint n_toggle_refs;
2454   struct {
2455     GToggleNotify notify;
2456     gpointer    data;
2457   } toggle_refs[1];  /* flexible array */
2458 } ToggleRefStack;
2459
2460 static void
2461 toggle_refs_notify (GObject *object,
2462                     gboolean is_last_ref)
2463 {
2464   ToggleRefStack tstack, *tstackptr;
2465
2466   G_LOCK (toggle_refs_mutex);
2467   tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2468   tstack = *tstackptr;
2469   G_UNLOCK (toggle_refs_mutex);
2470
2471   /* Reentrancy here is not as tricky as it seems, because a toggle reference
2472    * will only be notified when there is exactly one of them.
2473    */
2474   g_assert (tstack.n_toggle_refs == 1);
2475   tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2476 }
2477
2478 /**
2479  * g_object_add_toggle_ref: (skip)
2480  * @object: a #GObject
2481  * @notify: a function to call when this reference is the
2482  *  last reference to the object, or is no longer
2483  *  the last reference.
2484  * @data: data to pass to @notify
2485  *
2486  * Increases the reference count of the object by one and sets a
2487  * callback to be called when all other references to the object are
2488  * dropped, or when this is already the last reference to the object
2489  * and another reference is established.
2490  *
2491  * This functionality is intended for binding @object to a proxy
2492  * object managed by another memory manager. This is done with two
2493  * paired references: the strong reference added by
2494  * g_object_add_toggle_ref() and a reverse reference to the proxy
2495  * object which is either a strong reference or weak reference.
2496  *
2497  * The setup is that when there are no other references to @object,
2498  * only a weak reference is held in the reverse direction from @object
2499  * to the proxy object, but when there are other references held to
2500  * @object, a strong reference is held. The @notify callback is called
2501  * when the reference from @object to the proxy object should be
2502  * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2503  * true) or weak to strong (@is_last_ref false).
2504  *
2505  * Since a (normal) reference must be held to the object before
2506  * calling g_object_toggle_ref(), the initial state of the reverse
2507  * link is always strong.
2508  *
2509  * Multiple toggle references may be added to the same gobject,
2510  * however if there are multiple toggle references to an object, none
2511  * of them will ever be notified until all but one are removed.  For
2512  * this reason, you should only ever use a toggle reference if there
2513  * is important state in the proxy object.
2514  *
2515  * Since: 2.8
2516  */
2517 void
2518 g_object_add_toggle_ref (GObject       *object,
2519                          GToggleNotify  notify,
2520                          gpointer       data)
2521 {
2522   ToggleRefStack *tstack;
2523   guint i;
2524   
2525   g_return_if_fail (G_IS_OBJECT (object));
2526   g_return_if_fail (notify != NULL);
2527   g_return_if_fail (object->ref_count >= 1);
2528
2529   g_object_ref (object);
2530
2531   G_LOCK (toggle_refs_mutex);
2532   tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2533   if (tstack)
2534     {
2535       i = tstack->n_toggle_refs++;
2536       /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2537        * in tstate->toggle_refs */
2538       tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2539     }
2540   else
2541     {
2542       tstack = g_renew (ToggleRefStack, NULL, 1);
2543       tstack->object = object;
2544       tstack->n_toggle_refs = 1;
2545       i = 0;
2546     }
2547
2548   /* Set a flag for fast lookup after adding the first toggle reference */
2549   if (tstack->n_toggle_refs == 1)
2550     g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2551   
2552   tstack->toggle_refs[i].notify = notify;
2553   tstack->toggle_refs[i].data = data;
2554   g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2555                                (GDestroyNotify)g_free);
2556   G_UNLOCK (toggle_refs_mutex);
2557 }
2558
2559 /**
2560  * g_object_remove_toggle_ref: (skip)
2561  * @object: a #GObject
2562  * @notify: a function to call when this reference is the
2563  *  last reference to the object, or is no longer
2564  *  the last reference.
2565  * @data: data to pass to @notify
2566  *
2567  * Removes a reference added with g_object_add_toggle_ref(). The
2568  * reference count of the object is decreased by one.
2569  *
2570  * Since: 2.8
2571  */
2572 void
2573 g_object_remove_toggle_ref (GObject       *object,
2574                             GToggleNotify  notify,
2575                             gpointer       data)
2576 {
2577   ToggleRefStack *tstack;
2578   gboolean found_one = FALSE;
2579
2580   g_return_if_fail (G_IS_OBJECT (object));
2581   g_return_if_fail (notify != NULL);
2582
2583   G_LOCK (toggle_refs_mutex);
2584   tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2585   if (tstack)
2586     {
2587       guint i;
2588
2589       for (i = 0; i < tstack->n_toggle_refs; i++)
2590         if (tstack->toggle_refs[i].notify == notify &&
2591             tstack->toggle_refs[i].data == data)
2592           {
2593             found_one = TRUE;
2594             tstack->n_toggle_refs -= 1;
2595             if (i != tstack->n_toggle_refs)
2596               tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2597
2598             if (tstack->n_toggle_refs == 0)
2599               g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2600
2601             break;
2602           }
2603     }
2604   G_UNLOCK (toggle_refs_mutex);
2605
2606   if (found_one)
2607     g_object_unref (object);
2608   else
2609     g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2610 }
2611
2612 /**
2613  * g_object_ref:
2614  * @object: (type GObject.Object): a #GObject
2615  *
2616  * Increases the reference count of @object.
2617  *
2618  * Returns: (type GObject.Object) (transfer none): the same @object
2619  */
2620 gpointer
2621 g_object_ref (gpointer _object)
2622 {
2623   GObject *object = _object;
2624   gint old_val;
2625
2626   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2627   g_return_val_if_fail (object->ref_count > 0, NULL);
2628   
2629 #ifdef  G_ENABLE_DEBUG
2630   if (g_trap_object_ref == object)
2631     G_BREAKPOINT ();
2632 #endif  /* G_ENABLE_DEBUG */
2633
2634
2635   old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2636
2637   if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2638     toggle_refs_notify (object, FALSE);
2639
2640   TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2641
2642   return object;
2643 }
2644
2645 /**
2646  * g_object_unref:
2647  * @object: (type GObject.Object): a #GObject
2648  *
2649  * Decreases the reference count of @object. When its reference count
2650  * drops to 0, the object is finalized (i.e. its memory is freed).
2651  */
2652 void
2653 g_object_unref (gpointer _object)
2654 {
2655   GObject *object = _object;
2656   gint old_ref;
2657   
2658   g_return_if_fail (G_IS_OBJECT (object));
2659   g_return_if_fail (object->ref_count > 0);
2660   
2661 #ifdef  G_ENABLE_DEBUG
2662   if (g_trap_object_ref == object)
2663     G_BREAKPOINT ();
2664 #endif  /* G_ENABLE_DEBUG */
2665
2666   /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2667  retry_atomic_decrement1:
2668   old_ref = g_atomic_int_get (&object->ref_count);
2669   if (old_ref > 1)
2670     {
2671       /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2672       gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2673
2674       if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2675         goto retry_atomic_decrement1;
2676
2677       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2678
2679       /* if we went from 2->1 we need to notify toggle refs if any */
2680       if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2681         toggle_refs_notify (object, TRUE);
2682     }
2683   else
2684     {
2685       /* we are about tp remove the last reference */
2686       TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2687       G_OBJECT_GET_CLASS (object)->dispose (object);
2688       TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2689
2690       /* may have been re-referenced meanwhile */
2691     retry_atomic_decrement2:
2692       old_ref = g_atomic_int_get ((int *)&object->ref_count);
2693       if (old_ref > 1)
2694         {
2695           /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2696           gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2697
2698           if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2699             goto retry_atomic_decrement2;
2700
2701           TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2702
2703           /* if we went from 2->1 we need to notify toggle refs if any */
2704           if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2705             toggle_refs_notify (object, TRUE);
2706
2707           return;
2708         }
2709
2710       /* we are still in the process of taking away the last ref */
2711       g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2712       g_signal_handlers_destroy (object);
2713       g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2714       
2715       /* decrement the last reference */
2716       old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2717
2718       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2719
2720       /* may have been re-referenced meanwhile */
2721       if (G_LIKELY (old_ref == 1))
2722         {
2723           TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2724           G_OBJECT_GET_CLASS (object)->finalize (object);
2725
2726           TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2727
2728 #ifdef  G_ENABLE_DEBUG
2729           IF_DEBUG (OBJECTS)
2730             {
2731               /* catch objects not chaining finalize handlers */
2732               G_LOCK (debug_objects);
2733               g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2734               G_UNLOCK (debug_objects);
2735             }
2736 #endif  /* G_ENABLE_DEBUG */
2737           g_type_free_instance ((GTypeInstance*) object);
2738         }
2739     }
2740 }
2741
2742 /**
2743  * g_clear_object: (skip)
2744  * @object_ptr: a pointer to a #GObject reference
2745  *
2746  * Clears a reference to a #GObject.
2747  *
2748  * @object_ptr must not be %NULL.
2749  *
2750  * If the reference is %NULL then this function does nothing.
2751  * Otherwise, the reference count of the object is decreased and the
2752  * pointer is set to %NULL.
2753  *
2754  * This function is threadsafe and modifies the pointer atomically,
2755  * using memory barriers where needed.
2756  *
2757  * A macro is also included that allows this function to be used without
2758  * pointer casts.
2759  *
2760  * Since: 2.28
2761  **/
2762 #undef g_clear_object
2763 void
2764 g_clear_object (volatile GObject **object_ptr)
2765 {
2766   gpointer *ptr = (gpointer) object_ptr;
2767   gpointer old;
2768
2769   /* This is a little frustrating.
2770    * Would be nice to have an atomic exchange (with no compare).
2771    */
2772   do
2773     old = g_atomic_pointer_get (ptr);
2774   while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2775
2776   if (old)
2777     g_object_unref (old);
2778 }
2779
2780 /**
2781  * g_object_get_qdata:
2782  * @object: The GObject to get a stored user data pointer from
2783  * @quark: A #GQuark, naming the user data pointer
2784  * 
2785  * This function gets back user data pointers stored via
2786  * g_object_set_qdata().
2787  * 
2788  * Returns: (transfer none): The user data pointer set, or %NULL
2789  */
2790 gpointer
2791 g_object_get_qdata (GObject *object,
2792                     GQuark   quark)
2793 {
2794   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2795   
2796   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2797 }
2798
2799 /**
2800  * g_object_set_qdata: (skip)
2801  * @object: The GObject to set store a user data pointer
2802  * @quark: A #GQuark, naming the user data pointer
2803  * @data: An opaque user data pointer
2804  *
2805  * This sets an opaque, named pointer on an object.
2806  * The name is specified through a #GQuark (retrived e.g. via
2807  * g_quark_from_static_string()), and the pointer
2808  * can be gotten back from the @object with g_object_get_qdata()
2809  * until the @object is finalized.
2810  * Setting a previously set user data pointer, overrides (frees)
2811  * the old pointer set, using #NULL as pointer essentially
2812  * removes the data stored.
2813  */
2814 void
2815 g_object_set_qdata (GObject *object,
2816                     GQuark   quark,
2817                     gpointer data)
2818 {
2819   g_return_if_fail (G_IS_OBJECT (object));
2820   g_return_if_fail (quark > 0);
2821   
2822   g_datalist_id_set_data (&object->qdata, quark, data);
2823 }
2824
2825 /**
2826  * g_object_set_qdata_full: (skip)
2827  * @object: The GObject to set store a user data pointer
2828  * @quark: A #GQuark, naming the user data pointer
2829  * @data: An opaque user data pointer
2830  * @destroy: Function to invoke with @data as argument, when @data
2831  *           needs to be freed
2832  *
2833  * This function works like g_object_set_qdata(), but in addition,
2834  * a void (*destroy) (gpointer) function may be specified which is
2835  * called with @data as argument when the @object is finalized, or
2836  * the data is being overwritten by a call to g_object_set_qdata()
2837  * with the same @quark.
2838  */
2839 void
2840 g_object_set_qdata_full (GObject       *object,
2841                          GQuark         quark,
2842                          gpointer       data,
2843                          GDestroyNotify destroy)
2844 {
2845   g_return_if_fail (G_IS_OBJECT (object));
2846   g_return_if_fail (quark > 0);
2847   
2848   g_datalist_id_set_data_full (&object->qdata, quark, data,
2849                                data ? destroy : (GDestroyNotify) NULL);
2850 }
2851
2852 /**
2853  * g_object_steal_qdata:
2854  * @object: The GObject to get a stored user data pointer from
2855  * @quark: A #GQuark, naming the user data pointer
2856  *
2857  * This function gets back user data pointers stored via
2858  * g_object_set_qdata() and removes the @data from object
2859  * without invoking its destroy() function (if any was
2860  * set).
2861  * Usually, calling this function is only required to update
2862  * user data pointers with a destroy notifier, for example:
2863  * |[
2864  * void
2865  * object_add_to_user_list (GObject     *object,
2866  *                          const gchar *new_string)
2867  * {
2868  *   // the quark, naming the object data
2869  *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2870  *   // retrive the old string list
2871  *   GList *list = g_object_steal_qdata (object, quark_string_list);
2872  *
2873  *   // prepend new string
2874  *   list = g_list_prepend (list, g_strdup (new_string));
2875  *   // this changed 'list', so we need to set it again
2876  *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2877  * }
2878  * static void
2879  * free_string_list (gpointer data)
2880  * {
2881  *   GList *node, *list = data;
2882  *
2883  *   for (node = list; node; node = node->next)
2884  *     g_free (node->data);
2885  *   g_list_free (list);
2886  * }
2887  * ]|
2888  * Using g_object_get_qdata() in the above example, instead of
2889  * g_object_steal_qdata() would have left the destroy function set,
2890  * and thus the partial string list would have been freed upon
2891  * g_object_set_qdata_full().
2892  *
2893  * Returns: (transfer full): The user data pointer set, or %NULL
2894  */
2895 gpointer
2896 g_object_steal_qdata (GObject *object,
2897                       GQuark   quark)
2898 {
2899   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2900   g_return_val_if_fail (quark > 0, NULL);
2901   
2902   return g_datalist_id_remove_no_notify (&object->qdata, quark);
2903 }
2904
2905 /**
2906  * g_object_get_data:
2907  * @object: #GObject containing the associations
2908  * @key: name of the key for that association
2909  * 
2910  * Gets a named field from the objects table of associations (see g_object_set_data()).
2911  * 
2912  * Returns: (transfer none): the data if found, or %NULL if no such data exists.
2913  */
2914 gpointer
2915 g_object_get_data (GObject     *object,
2916                    const gchar *key)
2917 {
2918   GQuark quark;
2919
2920   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2921   g_return_val_if_fail (key != NULL, NULL);
2922
2923   quark = g_quark_try_string (key);
2924
2925   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2926 }
2927
2928 /**
2929  * g_object_set_data:
2930  * @object: #GObject containing the associations.
2931  * @key: name of the key
2932  * @data: data to associate with that key
2933  *
2934  * Each object carries around a table of associations from
2935  * strings to pointers.  This function lets you set an association.
2936  *
2937  * If the object already had an association with that name,
2938  * the old association will be destroyed.
2939  */
2940 void
2941 g_object_set_data (GObject     *object,
2942                    const gchar *key,
2943                    gpointer     data)
2944 {
2945   g_return_if_fail (G_IS_OBJECT (object));
2946   g_return_if_fail (key != NULL);
2947
2948   g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2949 }
2950
2951 /**
2952  * g_object_set_data_full: (skip)
2953  * @object: #GObject containing the associations
2954  * @key: name of the key
2955  * @data: data to associate with that key
2956  * @destroy: function to call when the association is destroyed
2957  *
2958  * Like g_object_set_data() except it adds notification
2959  * for when the association is destroyed, either by setting it
2960  * to a different value or when the object is destroyed.
2961  *
2962  * Note that the @destroy callback is not called if @data is %NULL.
2963  */
2964 void
2965 g_object_set_data_full (GObject       *object,
2966                         const gchar   *key,
2967                         gpointer       data,
2968                         GDestroyNotify destroy)
2969 {
2970   g_return_if_fail (G_IS_OBJECT (object));
2971   g_return_if_fail (key != NULL);
2972
2973   g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2974                                data ? destroy : (GDestroyNotify) NULL);
2975 }
2976
2977 /**
2978  * g_object_steal_data:
2979  * @object: #GObject containing the associations
2980  * @key: name of the key
2981  *
2982  * Remove a specified datum from the object's data associations,
2983  * without invoking the association's destroy handler.
2984  *
2985  * Returns: (transfer full): the data if found, or %NULL if no such data exists.
2986  */
2987 gpointer
2988 g_object_steal_data (GObject     *object,
2989                      const gchar *key)
2990 {
2991   GQuark quark;
2992
2993   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2994   g_return_val_if_fail (key != NULL, NULL);
2995
2996   quark = g_quark_try_string (key);
2997
2998   return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2999 }
3000
3001 static void
3002 g_value_object_init (GValue *value)
3003 {
3004   value->data[0].v_pointer = NULL;
3005 }
3006
3007 static void
3008 g_value_object_free_value (GValue *value)
3009 {
3010   if (value->data[0].v_pointer)
3011     g_object_unref (value->data[0].v_pointer);
3012 }
3013
3014 static void
3015 g_value_object_copy_value (const GValue *src_value,
3016                            GValue       *dest_value)
3017 {
3018   if (src_value->data[0].v_pointer)
3019     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3020   else
3021     dest_value->data[0].v_pointer = NULL;
3022 }
3023
3024 static void
3025 g_value_object_transform_value (const GValue *src_value,
3026                                 GValue       *dest_value)
3027 {
3028   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)))
3029     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3030   else
3031     dest_value->data[0].v_pointer = NULL;
3032 }
3033
3034 static gpointer
3035 g_value_object_peek_pointer (const GValue *value)
3036 {
3037   return value->data[0].v_pointer;
3038 }
3039
3040 static gchar*
3041 g_value_object_collect_value (GValue      *value,
3042                               guint        n_collect_values,
3043                               GTypeCValue *collect_values,
3044                               guint        collect_flags)
3045 {
3046   if (collect_values[0].v_pointer)
3047     {
3048       GObject *object = collect_values[0].v_pointer;
3049       
3050       if (object->g_type_instance.g_class == NULL)
3051         return g_strconcat ("invalid unclassed object pointer for value type `",
3052                             G_VALUE_TYPE_NAME (value),
3053                             "'",
3054                             NULL);
3055       else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3056         return g_strconcat ("invalid object type `",
3057                             G_OBJECT_TYPE_NAME (object),
3058                             "' for value type `",
3059                             G_VALUE_TYPE_NAME (value),
3060                             "'",
3061                             NULL);
3062       /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3063       value->data[0].v_pointer = g_object_ref (object);
3064     }
3065   else
3066     value->data[0].v_pointer = NULL;
3067   
3068   return NULL;
3069 }
3070
3071 static gchar*
3072 g_value_object_lcopy_value (const GValue *value,
3073                             guint        n_collect_values,
3074                             GTypeCValue *collect_values,
3075                             guint        collect_flags)
3076 {
3077   GObject **object_p = collect_values[0].v_pointer;
3078   
3079   if (!object_p)
3080     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3081
3082   if (!value->data[0].v_pointer)
3083     *object_p = NULL;
3084   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3085     *object_p = value->data[0].v_pointer;
3086   else
3087     *object_p = g_object_ref (value->data[0].v_pointer);
3088   
3089   return NULL;
3090 }
3091
3092 /**
3093  * g_value_set_object:
3094  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3095  * @v_object: (type GObject.Object): object value to be set
3096  *
3097  * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3098  *
3099  * g_value_set_object() increases the reference count of @v_object
3100  * (the #GValue holds a reference to @v_object).  If you do not wish
3101  * to increase the reference count of the object (i.e. you wish to
3102  * pass your current reference to the #GValue because you no longer
3103  * need it), use g_value_take_object() instead.
3104  *
3105  * It is important that your #GValue holds a reference to @v_object (either its
3106  * own, or one it has taken) to ensure that the object won't be destroyed while
3107  * the #GValue still exists).
3108  */
3109 void
3110 g_value_set_object (GValue   *value,
3111                     gpointer  v_object)
3112 {
3113   GObject *old;
3114         
3115   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3116
3117   old = value->data[0].v_pointer;
3118   
3119   if (v_object)
3120     {
3121       g_return_if_fail (G_IS_OBJECT (v_object));
3122       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3123
3124       value->data[0].v_pointer = v_object;
3125       g_object_ref (value->data[0].v_pointer);
3126     }
3127   else
3128     value->data[0].v_pointer = NULL;
3129   
3130   if (old)
3131     g_object_unref (old);
3132 }
3133
3134 /**
3135  * g_value_set_object_take_ownership: (skip)
3136  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3137  * @v_object: object value to be set
3138  *
3139  * This is an internal function introduced mainly for C marshallers.
3140  *
3141  * Deprecated: 2.4: Use g_value_take_object() instead.
3142  */
3143 void
3144 g_value_set_object_take_ownership (GValue  *value,
3145                                    gpointer v_object)
3146 {
3147   g_value_take_object (value, v_object);
3148 }
3149
3150 /**
3151  * g_value_take_object: (skip)
3152  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3153  * @v_object: object value to be set
3154  *
3155  * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3156  * and takes over the ownership of the callers reference to @v_object;
3157  * the caller doesn't have to unref it any more (i.e. the reference
3158  * count of the object is not increased).
3159  *
3160  * If you want the #GValue to hold its own reference to @v_object, use
3161  * g_value_set_object() instead.
3162  *
3163  * Since: 2.4
3164  */
3165 void
3166 g_value_take_object (GValue  *value,
3167                      gpointer v_object)
3168 {
3169   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3170
3171   if (value->data[0].v_pointer)
3172     {
3173       g_object_unref (value->data[0].v_pointer);
3174       value->data[0].v_pointer = NULL;
3175     }
3176
3177   if (v_object)
3178     {
3179       g_return_if_fail (G_IS_OBJECT (v_object));
3180       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3181
3182       value->data[0].v_pointer = v_object; /* we take over the reference count */
3183     }
3184 }
3185
3186 /**
3187  * g_value_get_object:
3188  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3189  * 
3190  * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3191  * 
3192  * Returns: (type GObject.Object) (transfer none): object contents of @value
3193  */
3194 gpointer
3195 g_value_get_object (const GValue *value)
3196 {
3197   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3198   
3199   return value->data[0].v_pointer;
3200 }
3201
3202 /**
3203  * g_value_dup_object:
3204  * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3205  *
3206  * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3207  * its reference count.
3208  *
3209  * Returns: (type GObject.Object) (transfer full): object content of @value,
3210  *          should be unreferenced when no longer needed.
3211  */
3212 gpointer
3213 g_value_dup_object (const GValue *value)
3214 {
3215   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3216   
3217   return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3218 }
3219
3220 /**
3221  * g_signal_connect_object: (skip)
3222  * @instance: the instance to connect to.
3223  * @detailed_signal: a string of the form "signal-name::detail".
3224  * @c_handler: the #GCallback to connect.
3225  * @gobject: the object to pass as data to @c_handler.
3226  * @connect_flags: a combination of #GConnnectFlags.
3227  *
3228  * This is similar to g_signal_connect_data(), but uses a closure which
3229  * ensures that the @gobject stays alive during the call to @c_handler
3230  * by temporarily adding a reference count to @gobject.
3231  *
3232  * Note that there is a bug in GObject that makes this function
3233  * much less useful than it might seem otherwise. Once @gobject is
3234  * disposed, the callback will no longer be called, but, the signal
3235  * handler is <emphasis>not</emphasis> currently disconnected. If the
3236  * @instance is itself being freed at the same time than this doesn't
3237  * matter, since the signal will automatically be removed, but
3238  * if @instance persists, then the signal handler will leak. You
3239  * should not remove the signal yourself because in a future versions of
3240  * GObject, the handler <emphasis>will</emphasis> automatically
3241  * be disconnected.
3242  *
3243  * It's possible to work around this problem in a way that will
3244  * continue to work with future versions of GObject by checking
3245  * that the signal handler is still connected before disconnected it:
3246  * <informalexample><programlisting>
3247  *  if (g_signal_handler_is_connected (instance, id))
3248  *    g_signal_handler_disconnect (instance, id);
3249  * </programlisting></informalexample>
3250  *
3251  * Returns: the handler id.
3252  */
3253 gulong
3254 g_signal_connect_object (gpointer      instance,
3255                          const gchar  *detailed_signal,
3256                          GCallback     c_handler,
3257                          gpointer      gobject,
3258                          GConnectFlags connect_flags)
3259 {
3260   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3261   g_return_val_if_fail (detailed_signal != NULL, 0);
3262   g_return_val_if_fail (c_handler != NULL, 0);
3263
3264   if (gobject)
3265     {
3266       GClosure *closure;
3267
3268       g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3269
3270       closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3271
3272       return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3273     }
3274   else
3275     return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3276 }
3277
3278 typedef struct {
3279   GObject  *object;
3280   guint     n_closures;
3281   GClosure *closures[1]; /* flexible array */
3282 } CArray;
3283 /* don't change this structure without supplying an accessor for
3284  * watched closures, e.g.:
3285  * GSList* g_object_list_watched_closures (GObject *object)
3286  * {
3287  *   CArray *carray;
3288  *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3289  *   carray = g_object_get_data (object, "GObject-closure-array");
3290  *   if (carray)
3291  *     {
3292  *       GSList *slist = NULL;
3293  *       guint i;
3294  *       for (i = 0; i < carray->n_closures; i++)
3295  *         slist = g_slist_prepend (slist, carray->closures[i]);
3296  *       return slist;
3297  *     }
3298  *   return NULL;
3299  * }
3300  */
3301
3302 static void
3303 object_remove_closure (gpointer  data,
3304                        GClosure *closure)
3305 {
3306   GObject *object = data;
3307   CArray *carray;
3308   guint i;
3309   
3310   G_LOCK (closure_array_mutex);
3311   carray = g_object_get_qdata (object, quark_closure_array);
3312   for (i = 0; i < carray->n_closures; i++)
3313     if (carray->closures[i] == closure)
3314       {
3315         carray->n_closures--;
3316         if (i < carray->n_closures)
3317           carray->closures[i] = carray->closures[carray->n_closures];
3318         G_UNLOCK (closure_array_mutex);
3319         return;
3320       }
3321   G_UNLOCK (closure_array_mutex);
3322   g_assert_not_reached ();
3323 }
3324
3325 static void
3326 destroy_closure_array (gpointer data)
3327 {
3328   CArray *carray = data;
3329   GObject *object = carray->object;
3330   guint i, n = carray->n_closures;
3331   
3332   for (i = 0; i < n; i++)
3333     {
3334       GClosure *closure = carray->closures[i];
3335       
3336       /* removing object_remove_closure() upfront is probably faster than
3337        * letting it fiddle with quark_closure_array which is empty anyways
3338        */
3339       g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3340       g_closure_invalidate (closure);
3341     }
3342   g_free (carray);
3343 }
3344
3345 /**
3346  * g_object_watch_closure:
3347  * @object: GObject restricting lifetime of @closure
3348  * @closure: GClosure to watch
3349  *
3350  * This function essentially limits the life time of the @closure to
3351  * the life time of the object. That is, when the object is finalized,
3352  * the @closure is invalidated by calling g_closure_invalidate() on
3353  * it, in order to prevent invocations of the closure with a finalized
3354  * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3355  * added as marshal guards to the @closure, to ensure that an extra
3356  * reference count is held on @object during invocation of the
3357  * @closure.  Usually, this function will be called on closures that
3358  * use this @object as closure data.
3359  */
3360 void
3361 g_object_watch_closure (GObject  *object,
3362                         GClosure *closure)
3363 {
3364   CArray *carray;
3365   guint i;
3366   
3367   g_return_if_fail (G_IS_OBJECT (object));
3368   g_return_if_fail (closure != NULL);
3369   g_return_if_fail (closure->is_invalid == FALSE);
3370   g_return_if_fail (closure->in_marshal == FALSE);
3371   g_return_if_fail (object->ref_count > 0);     /* this doesn't work on finalizing objects */
3372   
3373   g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3374   g_closure_add_marshal_guards (closure,
3375                                 object, (GClosureNotify) g_object_ref,
3376                                 object, (GClosureNotify) g_object_unref);
3377   G_LOCK (closure_array_mutex);
3378   carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3379   if (!carray)
3380     {
3381       carray = g_renew (CArray, NULL, 1);
3382       carray->object = object;
3383       carray->n_closures = 1;
3384       i = 0;
3385     }
3386   else
3387     {
3388       i = carray->n_closures++;
3389       carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3390     }
3391   carray->closures[i] = closure;
3392   g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3393   G_UNLOCK (closure_array_mutex);
3394 }
3395
3396 /**
3397  * g_closure_new_object:
3398  * @sizeof_closure: the size of the structure to allocate, must be at least
3399  *  <literal>sizeof (GClosure)</literal>
3400  * @object: a #GObject pointer to store in the @data field of the newly
3401  *  allocated #GClosure
3402  *
3403  * A variant of g_closure_new_simple() which stores @object in the
3404  * @data field of the closure and calls g_object_watch_closure() on
3405  * @object and the created closure. This function is mainly useful
3406  * when implementing new types of closures.
3407  *
3408  * Returns: (transfer full): a newly allocated #GClosure
3409  */
3410 GClosure*
3411 g_closure_new_object (guint    sizeof_closure,
3412                       GObject *object)
3413 {
3414   GClosure *closure;
3415
3416   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3417   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3418
3419   closure = g_closure_new_simple (sizeof_closure, object);
3420   g_object_watch_closure (object, closure);
3421
3422   return closure;
3423 }
3424
3425 /**
3426  * g_cclosure_new_object: (skip)
3427  * @callback_func: the function to invoke
3428  * @object: a #GObject pointer to pass to @callback_func
3429  *
3430  * A variant of g_cclosure_new() which uses @object as @user_data and
3431  * calls g_object_watch_closure() on @object and the created
3432  * closure. This function is useful when you have a callback closely
3433  * associated with a #GObject, and want the callback to no longer run
3434  * after the object is is freed.
3435  *
3436  * Returns: a new #GCClosure
3437  */
3438 GClosure*
3439 g_cclosure_new_object (GCallback callback_func,
3440                        GObject  *object)
3441 {
3442   GClosure *closure;
3443
3444   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3445   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3446   g_return_val_if_fail (callback_func != NULL, NULL);
3447
3448   closure = g_cclosure_new (callback_func, object, NULL);
3449   g_object_watch_closure (object, closure);
3450
3451   return closure;
3452 }
3453
3454 /**
3455  * g_cclosure_new_object_swap: (skip)
3456  * @callback_func: the function to invoke
3457  * @object: a #GObject pointer to pass to @callback_func
3458  *
3459  * A variant of g_cclosure_new_swap() which uses @object as @user_data
3460  * and calls g_object_watch_closure() on @object and the created
3461  * closure. This function is useful when you have a callback closely
3462  * associated with a #GObject, and want the callback to no longer run
3463  * after the object is is freed.
3464  *
3465  * Returns: a new #GCClosure
3466  */
3467 GClosure*
3468 g_cclosure_new_object_swap (GCallback callback_func,
3469                             GObject  *object)
3470 {
3471   GClosure *closure;
3472
3473   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3474   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3475   g_return_val_if_fail (callback_func != NULL, NULL);
3476
3477   closure = g_cclosure_new_swap (callback_func, object, NULL);
3478   g_object_watch_closure (object, closure);
3479
3480   return closure;
3481 }
3482
3483 gsize
3484 g_object_compat_control (gsize           what,
3485                          gpointer        data)
3486 {
3487   switch (what)
3488     {
3489       gpointer *pp;
3490     case 1:     /* floating base type */
3491       return G_TYPE_INITIALLY_UNOWNED;
3492     case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3493       floating_flag_handler = (guint(*)(GObject*,gint)) data;
3494       return 1;
3495     case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3496       pp = data;
3497       *pp = floating_flag_handler;
3498       return 1;
3499     default:
3500       return 0;
3501     }
3502 }
3503
3504 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3505
3506 static void
3507 g_initially_unowned_init (GInitiallyUnowned *object)
3508 {
3509   g_object_force_floating (object);
3510 }
3511
3512 static void
3513 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3514 {
3515 }