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