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