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