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