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