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
This commit is contained in:
Daniel P. Berrange 2018-02-21 21:03:44 -05:00 committed by Lioncash
parent b97ab59f08
commit eddfb13c2c
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 18 additions and 35 deletions

View file

@ -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, ObjectProperty *object_class_property_find(struct uc_struct *uc, ObjectClass *klass,
const char *name, Error **errp); const char *name, Error **errp);
typedef struct ObjectPropertyIterator ObjectPropertyIterator; typedef struct ObjectPropertyIterator {
ObjectClass *nextclass;
GHashTableIter iter;
} ObjectPropertyIterator;
/** /**
* object_property_iter_init: * object_property_iter_init:
@ -845,32 +848,27 @@ typedef struct ObjectPropertyIterator ObjectPropertyIterator;
* <title>Using object property iterators</title> * <title>Using object property iterators</title>
* <programlisting> * <programlisting>
* ObjectProperty *prop; * ObjectProperty *prop;
* ObjectPropertyIterator *iter; * ObjectPropertyIterator iter;
* *
* iter = object_property_iter_init(obj); * object_property_iter_init(&iter, obj);
* while ((prop = object_property_iter_next(iter))) { * while ((prop = object_property_iter_next(&iter))) {
* ... do something with prop ... * ... do something with prop ...
* } * }
* object_property_iter_free(iter);
* </programlisting> * </programlisting>
* </example> * </example>
*
* Returns: the new iterator
*/ */
ObjectPropertyIterator *object_property_iter_init(Object *obj); void object_property_iter_init(ObjectPropertyIterator *iter,
Object *obj);
/**
* object_property_iter_free:
* @iter: the iterator instance
*
* Releases any resources associated with the iterator.
*/
void object_property_iter_free(ObjectPropertyIterator *iter);
/** /**
* object_property_iter_next: * object_property_iter_next:
* @iter: the iterator instance * @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 * Returns: the next property, or %NULL when all properties
* have been traversed. * have been traversed.
*/ */

View file

@ -66,11 +66,6 @@ struct TypeImpl
InterfaceImpl interfaces[MAX_INTERFACES]; InterfaceImpl interfaces[MAX_INTERFACES];
}; };
struct ObjectPropertyIterator {
ObjectClass *nextclass;
GHashTableIter iter;
};
static GHashTable *type_table_get(struct uc_struct *uc) static GHashTable *type_table_get(struct uc_struct *uc)
{ {
if (uc->type_table == NULL) { if (uc->type_table == NULL) {
@ -80,7 +75,6 @@ static GHashTable *type_table_get(struct uc_struct *uc)
return uc->type_table; return uc->type_table;
} }
static void type_table_add(struct uc_struct *uc, TypeImpl *ti) static void type_table_add(struct uc_struct *uc, TypeImpl *ti)
{ {
assert(!uc->enumerating_types); assert(!uc->enumerating_types);
@ -883,20 +877,11 @@ ObjectProperty *object_property_find(struct uc_struct *uc, Object *obj,
return NULL; 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(&iter->iter, obj->properties);
g_hash_table_iter_init(&ret->iter, obj->properties); iter->nextclass = object_get_class(obj);
ret->nextclass = object_get_class(obj);
return ret;
}
void object_property_iter_free(ObjectPropertyIterator *iter)
{
if (!iter) {
return;
}
g_free(iter);
} }
ObjectProperty *object_property_iter_next(struct uc_struct *uc, ObjectPropertyIterator *iter) ObjectProperty *object_property_iter_next(struct uc_struct *uc, ObjectPropertyIterator *iter)