From eddfb13c2ce8cda563604bfbdb0ca37ff7fb7334 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 21 Feb 2018 21:03:44 -0500 Subject: [PATCH] qom: Change object property iterator API contract Currently the ObjectProperty iterator API works as follows: ObjectPropertyIterator *iter; iter = object_property_iter_init(obj); while ((prop = object_property_iter_next(iter))) { ... } object_property_iter_free(iter); This has the benefit that the ObjectPropertyIterator struct can be opaque, but has the downside that callers need to explicitly call a free function. It is also not in keeping with iterator style used elsewhere in QEMU/GLib2. This patch changes the API to use stack allocation instead: ObjectPropertyIterator iter; object_property_iter_init(&iter, obj); while ((prop = object_property_iter_next(&iter))) { ... } Backports commit 7746abd8e9ee9db20c0b0fdb19504f163ba3cbea from qemu --- qemu/include/qom/object.h | 30 ++++++++++++++---------------- qemu/qom/object.c | 23 ++++------------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/qemu/include/qom/object.h b/qemu/include/qom/object.h index 352f2f2f..5d837eb7 100644 --- a/qemu/include/qom/object.h +++ b/qemu/include/qom/object.h @@ -827,7 +827,10 @@ ObjectProperty *object_property_find(struct uc_struct *uc, Object *obj, ObjectProperty *object_class_property_find(struct uc_struct *uc, ObjectClass *klass, const char *name, Error **errp); -typedef struct ObjectPropertyIterator ObjectPropertyIterator; +typedef struct ObjectPropertyIterator { + ObjectClass *nextclass; + GHashTableIter iter; +} ObjectPropertyIterator; /** * object_property_iter_init: @@ -845,32 +848,27 @@ typedef struct ObjectPropertyIterator ObjectPropertyIterator; * Using object property iterators * * ObjectProperty *prop; - * ObjectPropertyIterator *iter; + * ObjectPropertyIterator iter; * - * iter = object_property_iter_init(obj); - * while ((prop = object_property_iter_next(iter))) { + * object_property_iter_init(&iter, obj); + * while ((prop = object_property_iter_next(&iter))) { * ... do something with prop ... * } - * object_property_iter_free(iter); * * - * - * Returns: the new iterator */ -ObjectPropertyIterator *object_property_iter_init(Object *obj); - -/** - * object_property_iter_free: - * @iter: the iterator instance - * - * Releases any resources associated with the iterator. - */ -void object_property_iter_free(ObjectPropertyIterator *iter); +void object_property_iter_init(ObjectPropertyIterator *iter, + Object *obj); /** * object_property_iter_next: * @iter: the iterator instance * + * Return the next available property. If no further properties + * are available, a %NULL value will be returned and the @iter + * pointer should not be used again after this point without + * re-initializing it. + * * Returns: the next property, or %NULL when all properties * have been traversed. */ diff --git a/qemu/qom/object.c b/qemu/qom/object.c index 18075868..ccb3f964 100644 --- a/qemu/qom/object.c +++ b/qemu/qom/object.c @@ -66,11 +66,6 @@ struct TypeImpl InterfaceImpl interfaces[MAX_INTERFACES]; }; -struct ObjectPropertyIterator { - ObjectClass *nextclass; - GHashTableIter iter; -}; - static GHashTable *type_table_get(struct uc_struct *uc) { if (uc->type_table == NULL) { @@ -80,7 +75,6 @@ static GHashTable *type_table_get(struct uc_struct *uc) return uc->type_table; } - static void type_table_add(struct uc_struct *uc, TypeImpl *ti) { assert(!uc->enumerating_types); @@ -883,20 +877,11 @@ ObjectProperty *object_property_find(struct uc_struct *uc, Object *obj, return NULL; } -ObjectPropertyIterator *object_property_iter_init(Object *obj) +void object_property_iter_init(ObjectPropertyIterator *iter, + Object *obj) { - ObjectPropertyIterator *ret = g_new0(ObjectPropertyIterator, 1); - g_hash_table_iter_init(&ret->iter, obj->properties); - ret->nextclass = object_get_class(obj); - return ret; -} - -void object_property_iter_free(ObjectPropertyIterator *iter) -{ - if (!iter) { - return; - } - g_free(iter); + g_hash_table_iter_init(&iter->iter, obj->properties); + iter->nextclass = object_get_class(obj); } ObjectProperty *object_property_iter_next(struct uc_struct *uc, ObjectPropertyIterator *iter)