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 <imamm...@redhat.com> > --- > 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) >