From 6e5f46592491703281b39c2469664873808aef59 Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Wed, 23 Nov 2005 17:54:41 +0000 Subject: [PATCH] added floating reference count. g_object_is_floating(): Wed Nov 23 18:01:46 2005 Tim Janik * gobject.[hc]: added floating reference count. g_object_is_floating(): g_object_ref_sink(): new functions to deal with floating references. g_object_force_floating(): new funciton for object implementations to set the floating flag. * gobject.c (g_object_init): make objects initially floating. Wed Nov 23 17:58:13 2005 Tim Janik * gparam.[hc]: added g_param_spec_ref_sink(). --- gobject/ChangeLog | 14 +++++++++++++ gobject/gobject.c | 45 ++++++++++++++++++++++++++++++++++++++++- gobject/gobject.h | 3 +++ gobject/gobject.symbols | 6 +++++- gobject/gparam.c | 11 ++++++++++ gobject/gparam.h | 1 + 6 files changed, 78 insertions(+), 2 deletions(-) diff --git a/gobject/ChangeLog b/gobject/ChangeLog index ab60715b..bcbd710d 100644 --- a/gobject/ChangeLog +++ b/gobject/ChangeLog @@ -1,3 +1,17 @@ +Wed Nov 23 18:01:46 2005 Tim Janik + + * gobject.[hc]: added floating reference count. + g_object_is_floating(): + g_object_ref_sink(): new functions to deal with floating references. + g_object_force_floating(): new funciton for object implementations + to set the floating flag. + + * gobject.c (g_object_init): make objects initially floating. + +Wed Nov 23 17:58:13 2005 Tim Janik + + * gparam.[hc]: added g_param_spec_ref_sink(). + Wed Nov 23 13:36:02 2005 Tim Janik * gboxed.[hc]: minor cleanups, implemented G_TYPE_HASH_TABLE. diff --git a/gobject/gobject.c b/gobject/gobject.c index de409a87..85ed73df 100644 --- a/gobject/gobject.c +++ b/gobject/gobject.c @@ -42,7 +42,8 @@ #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1 #define OBJECT_HAS_TOGGLE_REF(object) \ - ((G_DATALIST_GET_FLAGS(&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0) + ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0) +#define OBJECT_FLOATING_FLAG 0x2 /* --- signals --- */ @@ -472,6 +473,7 @@ g_object_init (GObject *object) { object->ref_count = 1; g_datalist_init (&object->qdata); + g_object_force_floating (object); /* freeze object's notification queue, g_object_newv() preserves pairedness */ g_object_notify_queue_freeze (object, &property_notify_context); @@ -1518,6 +1520,47 @@ g_object_remove_weak_pointer (GObject *object, weak_pointer_location); } +gboolean +g_object_is_floating (gpointer _object) +{ + GObject *object = _object; + g_return_val_if_fail (G_IS_OBJECT (object), FALSE); + return ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG) != 0; +} + +gpointer +g_object_ref_sink (gpointer _object) +{ + GObject *object = _object; + gpointer oldvalue; + g_return_val_if_fail (G_IS_OBJECT (object), object); + g_return_val_if_fail (object->ref_count >= 1, object); + g_object_ref (object); + do + { + oldvalue = g_atomic_pointer_get (&object->qdata); + } + while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, + (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG))); + if ((gsize) oldvalue & OBJECT_FLOATING_FLAG) + g_object_unref (object); + return object; +} + +void +g_object_force_floating (GObject *object) +{ + gpointer oldvalue; + g_return_if_fail (G_IS_OBJECT (object)); + g_return_if_fail (object->ref_count >= 1); + do + { + oldvalue = g_atomic_pointer_get (&object->qdata); + } + while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, + (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG))); +} + typedef struct { GObject *object; guint n_toggle_refs; diff --git a/gobject/gobject.h b/gobject/gobject.h index da2c47c6..bb684f44 100644 --- a/gobject/gobject.h +++ b/gobject/gobject.h @@ -166,6 +166,8 @@ void g_object_freeze_notify (GObject *object); void g_object_notify (GObject *object, const gchar *property_name); void g_object_thaw_notify (GObject *object); +gboolean g_object_is_floating (gpointer object); +gpointer g_object_ref_sink (gpointer object); gpointer g_object_ref (gpointer object); void g_object_unref (gpointer object); void g_object_weak_ref (GObject *object, @@ -232,6 +234,7 @@ gulong g_signal_connect_object (gpointer instance, /*< protected >*/ +void g_object_force_floating (GObject *object); void g_object_run_dispose (GObject *object); diff --git a/gobject/gobject.symbols b/gobject/gobject.symbols index 86c57079..d1a5476c 100644 --- a/gobject/gobject.symbols +++ b/gobject/gobject.symbols @@ -131,7 +131,11 @@ g_object_new g_object_newv g_object_new_valist g_object_notify +g_object_is_floating +g_object_ref_sink +g_object_force_floating g_object_ref +g_object_unref g_object_remove_weak_pointer g_object_run_dispose g_object_set G_GNUC_NULL_TERMINATED @@ -144,7 +148,6 @@ g_object_set_valist g_object_steal_data g_object_steal_qdata g_object_thaw_notify -g_object_unref g_object_watch_closure g_object_weak_ref g_object_weak_unref @@ -196,6 +199,7 @@ g_param_spec_get_redirect_target g_param_spec_internal g_param_type_register_static g_param_spec_ref +g_param_spec_ref_sink g_param_spec_unref g_param_spec_sink g_param_spec_steal_qdata diff --git a/gobject/gparam.c b/gobject/gparam.c index 454e3444..327e65e3 100644 --- a/gobject/gparam.c +++ b/gobject/gparam.c @@ -201,6 +201,17 @@ g_param_spec_sink (GParamSpec *pspec) g_param_spec_unref (pspec); } +GParamSpec* +g_param_spec_ref_sink (GParamSpec *pspec) +{ + g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL); + g_return_val_if_fail (pspec->ref_count > 0, NULL); + + g_param_spec_ref (pspec); + g_param_spec_sink (pspec); + return pspec; +} + G_CONST_RETURN gchar* g_param_spec_get_name (GParamSpec *pspec) { diff --git a/gobject/gparam.h b/gobject/gparam.h index 38811ddb..8676300a 100644 --- a/gobject/gparam.h +++ b/gobject/gparam.h @@ -117,6 +117,7 @@ struct _GParameter /* auxillary structure for _setv() variants */ GParamSpec* g_param_spec_ref (GParamSpec *pspec); void g_param_spec_unref (GParamSpec *pspec); void g_param_spec_sink (GParamSpec *pspec); +GParamSpec* g_param_spec_ref_sink (GParamSpec *pspec); gpointer g_param_spec_get_qdata (GParamSpec *pspec, GQuark quark); void g_param_spec_set_qdata (GParamSpec *pspec, -- 2.34.1