From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com, mst@redhat.com
Subject: Re: [Qemu-devel] [PATCH RFC 4/4] range: Replace internal representation of Range
Date: Thu, 16 Jun 2016 10:04:58 +0200 [thread overview]
Message-ID: <87twgteez9.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <5761EB7E.60605@redhat.com> (Eric Blake's message of "Wed, 15 Jun 2016 17:57:50 -0600")
Eric Blake <eblake@redhat.com> writes:
> On 06/15/2016 02:41 PM, Markus Armbruster wrote:
>> Range represents a range as follows. Member @start is the inclusive
>> lower bound, member @end is the exclusive upper bound. Zero @end is
>> special: if @start is also zero, the range is empty, else @end is to
>> be interpreted as 2^64. No other empty ranges may occur.
>>
>> The range [0,2^64-1] cannot be represented. If you try to create it
>> with range_set_bounds1(), you get the empty range instead. If you try
>> to create it with range_set_bounds() or range_extend(), assertions
>> fail. Before range_set_bounds() existed, the open-coded creation
>> usually got you the empty range instead. Open deathtrap.
>>
>> Moreover, the code dealing with the janus-faced @end is too clever by
>> half.
>>
>> Dumb this down to a more pedestrian representation: members @lob and
>> @upb are inclusive lower and upper bounds. The empty range is encoded
>> as @lob = 1, @upb = 0.
>
> And since all users now go through accessors, we've freed ourselves to
> change the underlying representation.
>
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>> include/qemu/range.h | 55 +++++++++++++++++++++++++---------------------------
>> util/range.c | 13 +++----------
>> 2 files changed, 29 insertions(+), 39 deletions(-)
>
> Not only does it have more power, it takes fewer lines of code!
That's the kind of change I like best :)
>> /* Compound literal encoding the empty range */
>> -#define range_empty ((Range){ .begin = 0, .end = 0 })
>> +#define range_empty ((Range){ .lob = 1, .upb = 0 })
>
> well, one particular representation of the empty range, but the comment
> is fine as-is.
The only ways to create empty ranges are range_make_empty() and
range_set_bounds1(). Both use macro range_empty below the hood.
Before this patch, there is only one empty range: { .begin=0, .end=0 }.
range_empty is this one empty range:
#define range_empty ((Range){ .begin = 0, .end = 0 })
range_invariant() enforces it:
assert((!range->begin && !range->end) /* empty */
|| range->begin <= range->end - 1); /* non-empty */
range_is_empty() relies on it:
return !range->begin && !range->end;
With this patch, the code treats any range with begin > end as empty.
range_invariant():
assert(range->lob <= range->upb || range->lob == range->upb + 1);
range_is_empty():
return range->lob > range->upb;
Nevertheless, you can still create only one:
#define range_empty ((Range){ .lob = 1, .upb = 0 })
I could tighten range_invariant(). Doesn't feel worthwhile to me.
> Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks!
next prev parent reply other threads:[~2016-06-16 8:05 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-15 20:41 [Qemu-devel] [PATCH RFC 0/4] range: Make it simpler & safer Markus Armbruster
2016-06-15 20:41 ` [Qemu-devel] [PATCH RFC 1/4] log: Clean up misuse of Range for -dfilter Markus Armbruster
2016-06-15 23:30 ` Eric Blake
2016-06-19 3:24 ` Michael S. Tsirkin
2016-06-15 20:41 ` [Qemu-devel] [PATCH RFC 2/4] range: Eliminate direct Range member access Markus Armbruster
2016-06-15 23:50 ` Eric Blake
2016-06-16 7:50 ` Markus Armbruster
2016-06-19 3:25 ` Michael S. Tsirkin
2016-06-20 7:26 ` Markus Armbruster
2016-06-15 20:41 ` [Qemu-devel] [PATCH RFC 3/4] range: Drop the previous commit's trickery Markus Armbruster
2016-06-15 23:53 ` Eric Blake
2016-06-19 3:28 ` Michael S. Tsirkin
2016-06-20 7:28 ` Markus Armbruster
2016-06-15 20:41 ` [Qemu-devel] [PATCH RFC 4/4] range: Replace internal representation of Range Markus Armbruster
2016-06-15 23:57 ` Eric Blake
2016-06-16 8:04 ` Markus Armbruster [this message]
2016-06-19 3:24 ` Michael S. Tsirkin
2016-06-20 7:33 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87twgteez9.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=eblake@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.