From 6dd726a2bf1b800289d90a84d5fcb5ce7b78a8e1 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 1 Jul 2016 13:47:48 +0200 Subject: range: Replace internal representation of Range 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. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- util/range.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'util') diff --git a/util/range.c b/util/range.c index e5f2e71142..416df7cdae 100644 --- a/util/range.c +++ b/util/range.c @@ -22,20 +22,18 @@ #include "qemu/range.h" /* - * Operations on 64 bit address ranges. - * Notes: - * - ranges must not wrap around 0, but can include the last byte ~0x0LL. - * - this can not represent a full 0 to ~0x0LL range. + * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. + * Both @a and @b must not be empty. */ - -/* Return -1 if @a < @b, 1 if greater, and 0 if they touch or overlap. */ static inline int range_compare(Range *a, Range *b) { - /* Zero a->end is 2**64, and therefore not less than any b->begin */ - if (a->end && a->end < b->begin) { + assert(!range_is_empty(a) && !range_is_empty(b)); + + /* Careful, avoid wraparound */ + if (b->lob && b->lob - 1 > a->upb) { return -1; } - if (b->end && a->begin > b->end) { + if (a->lob && a->lob - 1 > b->upb) { return 1; } return 0; -- cgit v1.2.3