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