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