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