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