qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] Stick to loops (was: [PATCH v3 1/5] qom: introduce object_p


From: Markus Armbruster
Subject: [Qemu-devel] Stick to loops (was: [PATCH v3 1/5] qom: introduce object_property_foreach method)
Date: Fri, 09 Oct 2015 10:31:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Eric Blake <address@hidden> writes:

> On 10/08/2015 08:09 AM, Daniel P. Berrange wrote:
>> Some users of QOM need to be able to iterate over properties
>> defined against an object instance. Currently they are just
>> directly using the QTAIL macros against the object properties
>> data structure.
>> 
>> This is bad because it exposes them to changes in the data
>> structure used to store properties, as well as changes in
>> functionality such as ability to register properties against
>> the class.
>> 
>> Providing an explicit object_property_foreach method provides
>> a layer of insulation between the QOM user and the QOM internal
>> implementation.
>> 
>> Signed-off-by: Daniel P. Berrange <address@hidden>
>> ---
>>  include/qom/object.h | 23 +++++++++++++++++++++++
>>  qom/object.c         | 17 +++++++++++++++++
>>  2 files changed, 40 insertions(+)
>> 
>> diff --git a/include/qom/object.h b/include/qom/object.h
>> index be7280c..71503af 100644
>> --- a/include/qom/object.h
>> +++ b/include/qom/object.h
>> @@ -960,6 +960,29 @@ void object_property_del(Object *obj, const char *name, 
>> Error **errp);
>>  ObjectProperty *object_property_find(Object *obj, const char *name,
>>                                       Error **errp);
>>  
>> +typedef void (*ObjectPropertyIterator)(Object *obj,
>> +                                       ObjectProperty *prop,
>> +                                       Error **errp,
>> +                                       void *opaque);
>
> Do we want the iterator to be able to return a value, and possibly allow
> a non-zero value to abort iteration? [1]
>
>> +
>> +/**
>> + * object_property_foreach:
>> + * @obj: the object
>> + * @iter: the iterator callback function
>> + * @errp: returns an error if iterator function fails
>> + * @opaque: opaque data to pass to @iter
>> + *
>> + * Iterates over all properties defined against the object
>> + * instance calling @iter for each property.
>
> Probably should mention that there is an early exit if error gets set [2]
>
>> + *
>> + * It is forbidden to modify the property list from @iter
>> + * whether removing or adding properties.
>> + */
>> +void object_property_foreach(Object *obj,
>> +                             ObjectPropertyIterator iter,
>> +                             Error **errp,
>> +                             void *opaque);
>
> [1] if we allow the iterator to return a non-zero value to abort
> iteration (particularly if it wants to abort iteration without setting
> an error, just to save CPU cycles), should this foreach() function
> return that value?
>
> Of course, a caller can always use opaque to achieve the same purpose,
> but it gets more verbose (the caller has to reserve space in their
> opaque for tracking whether an interesting exit value is needed, as well
> as having an early check on whether the value was already set in a
> previous visit) and burns more CPU cycles (the iterator runs to
> completion, even though the later callbacks are doing nothing).
>
>> +++ b/qom/object.c
>> @@ -917,6 +917,23 @@ ObjectProperty *object_property_find(Object *obj, const 
>> char *name,
>>      return NULL;
>>  }
>>  
>> +void object_property_foreach(Object *obj,
>> +                             ObjectPropertyIterator iter,
>> +                             Error **errp,
>> +                             void *opaque)
>> +{
>> +    ObjectProperty *prop;
>> +    Error *local_err = NULL;
>> +
>> +    QTAILQ_FOREACH(prop, &obj->properties, node) {
>> +        iter(obj, prop, &local_err, opaque);
>> +        if (local_err) {
>> +            error_propagate(errp, local_err);
>> +            return;
>
> [2] there's the early exit if an error is set.
>
> The code looks fine, but I think we need a documentation improvement for
> issue [2], and we may want a design change for issue [1] if a non-void
> return would be useful to any client later in the series.

We have quite a few _foreach-functions to help iterate over various
things.  They are easy enough to write, but I find them awkward to use.

Have a look at this straightforward loop from monitor.c:

            for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
                name = bdrv_get_device_name(bs);
                if (str[0] == '\0' ||
                    !strncmp(name, str, strlen(str))) {
                    readline_add_completion(mon->rs, name);
                }
            }

Note that it encapsulates iteration details just fine.  The loop body
accesses local variables the obvious way.  Breaking the loop would also
be done the obvious way.

Before commit fea68bb, it looked like this:

            mbs.mon = mon;
            mbs.input = str;
            readline_set_completion_index(mon->rs, strlen(str));
            bdrv_iterate(block_completion_it, &mbs);

To actually figure out what this does, you have to look up

* The definition of block_completion_it(), 500 lines up:

    static void block_completion_it(void *opaque, BlockDriverState *bs)
    {
        const char *name = bdrv_get_device_name(bs);
        MonitorBlockComplete *mbc = opaque;
        Monitor *mon = mbc->mon;
        const char *input = mbc->input;

        if (input[0] == '\0' ||
            !strncmp(name, (char *)input, strlen(input))) {
            readline_add_completion(mon->rs, name);
        }
    }

* The definition of mbs, 50 lines up.  It's a MonitorBlockComplete, so
  you get too look that up, too.

* Thankfully, it's defined right next to block_completion_it():

    typedef struct MonitorBlockComplete {
        Monitor *mon;
        const char *input;
    } MonitorBlockComplete;

Twice the code, spread over two distant places (not counting definition
of mbs), type punning, and things would get even more complicated if you
needed to break the loop.

Implementing bdrv_next() is no harder than bdrv_iterate().  Compare:

    BlockDriverState *bdrv_next(BlockDriverState *bs)
    {
        if (!bs) {
            return QTAILQ_FIRST(&bdrv_states);
        }
        return QTAILQ_NEXT(bs, device_list);
    }

    void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void 
*opaque)
    {
        BlockDriverState *bs;

        QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
            it(opaque, bs);
        }
    }

Higher-order functions are a wonderful tool if the language is equipped
for them.  In Lisp, for instance, you'd have everything in one place and
no need for the awkward marshalling and unmarshalling of arguments,
thanks to nested functions.

In C, stick to loops.  That's what the language supports.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]