From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41948) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YGPZ5-0005o3-Qc for qemu-devel@nongnu.org; Wed, 28 Jan 2015 05:09:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YGPZ1-0003uD-Pj for qemu-devel@nongnu.org; Wed, 28 Jan 2015 05:09:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41586) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YGPZ1-0003u5-IX for qemu-devel@nongnu.org; Wed, 28 Jan 2015 05:09:35 -0500 Message-ID: <54C8B552.3070903@redhat.com> Date: Wed, 28 Jan 2015 11:09:22 +0100 From: Paolo Bonzini MIME-Version: 1.0 References: <20150128072757.GA12987@redhat.com> <1422439417-5031-1-git-send-email-imammedo@redhat.com> <1422439417-5031-4-git-send-email-imammedo@redhat.com> In-Reply-To: <1422439417-5031-4-git-send-email-imammedo@redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 03/13] qom: add support for weak referenced object: aka UnownedObject List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Igor Mammedov , qemu-devel@nongnu.org Cc: drjones@redhat.com, zhaoshenglong@huawei.com, claudio.fontana@huawei.com, marcel.a@redhat.com On 28/01/2015 11:03, Igor Mammedov wrote: > it adds support for weak reference model used by glib's > GInitiallyUnowned to QEMU's QOM model. > > Signed-off-by: Igor Mammedov > --- > include/qom/object.h | 20 ++++++++++++++++++++ > qom/object.c | 20 +++++++++++++++++++- > 2 files changed, 39 insertions(+), 1 deletion(-) > > diff --git a/include/qom/object.h b/include/qom/object.h > index 89c3092..e0e4cc8 100644 > --- a/include/qom/object.h > +++ b/include/qom/object.h > @@ -1314,5 +1314,25 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), > */ > Object *container_get(Object *root, const char *path); > > +#define TYPE_UNOWNED_OBJECT "unowned-object" > +#define UNOWNED_OBJECT(obj) \ > + OBJECT_CHECK(UnownedObject, (obj), TYPE_UNOWNED_OBJECT) > +#define UNOWNED_OBJECT_CLASS(klass) \ > + OBJECT_CLASS_CHECK(UnownedObjectClass, (klass), TYPE_UNOWNED_OBJECT) > +#define UNOWNED_OBJECT_GET_CLASS \ > + OBJECT_GET_CLASS(UnownedObjectClass, (obj), TYPE_UNOWNED_OBJECT) > +#define OBJECT_WEAK_REF (1UL << 31) > + > +typedef struct UnownedObjectClass > +{ > + /*< private >*/ > + ObjectClass parent_class; > +} UnownedObjectClass; > + > +typedef struct UnownedObject > +{ > + /*< private >*/ > + Object parent_obj; > +} UnownedObject; > > #endif > diff --git a/qom/object.c b/qom/object.c > index 1812c73..96842c7 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -705,6 +705,10 @@ void object_ref(Object *obj) > if (!obj) { > return; > } > + if (atomic_fetch_and(&obj->ref, ~OBJECT_WEAK_REF) & OBJECT_WEAK_REF) Please first do a non-atomic test, since this is unlikely to happen but atomic_fetch_and is quite expensive (it compiles into a cmpxchg loop). Paolo > + { > + return; > + } > atomic_inc(&obj->ref); > } > > @@ -713,7 +717,7 @@ void object_unref(Object *obj) > if (!obj) { > return; > } > - g_assert(obj->ref > 0); > + g_assert((obj->ref & ~OBJECT_WEAK_REF) > 0); > > /* parent always holds a reference to its children */ > if (atomic_fetch_dec(&obj->ref) == 1) { > @@ -1709,6 +1713,12 @@ static void object_instance_init(Object *obj) > object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); > } > > + > +static void unowned_object_instance_init(Object *obj) > +{ > + obj->ref |= OBJECT_WEAK_REF; > +} > + > static void register_types(void) > { > static TypeInfo interface_info = { > @@ -1724,8 +1734,16 @@ static void register_types(void) > .abstract = true, > }; > > + static TypeInfo unowned_object_info = { > + .name = TYPE_UNOWNED_OBJECT, > + .instance_size = sizeof(UnownedObject), > + .instance_init = unowned_object_instance_init, > + .abstract = true, > + }; > + > type_interface = type_register_internal(&interface_info); > type_register_internal(&object_info); > + type_register_internal(&unowned_object_info); > } > > type_init(register_types) >