range.h (a0efbf16604770b9d805bcf210ec29942321134f) range.h (6dd726a2bf1b800289d90a84d5fcb5ce7b78a8e1)
1/*
2 * QEMU 64-bit address ranges
3 *
4 * Copyright (c) 2015-2016 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either

--- 12 unchanged lines hidden (view full) ---

21#ifndef QEMU_RANGE_H
22#define QEMU_RANGE_H
23
24#include "qemu/queue.h"
25
26/*
27 * Operations on 64 bit address ranges.
28 * Notes:
1/*
2 * QEMU 64-bit address ranges
3 *
4 * Copyright (c) 2015-2016 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either

--- 12 unchanged lines hidden (view full) ---

21#ifndef QEMU_RANGE_H
22#define QEMU_RANGE_H
23
24#include "qemu/queue.h"
25
26/*
27 * Operations on 64 bit address ranges.
28 * Notes:
29 * - ranges must not wrap around 0, but can include the last byte ~0x0LL.
30 * - this can not represent a full 0 to ~0x0LL range.
29 * - Ranges must not wrap around 0, but can include UINT64_MAX.
31 */
32
30 */
31
33/* A structure representing a range of addresses. */
34struct Range {
32struct Range {
35 uint64_t begin; /* First byte of the range, or 0 if empty. */
36 uint64_t end; /* 1 + the last byte. 0 if range empty or ends at ~0x0LL. */
33 /*
34 * Do not access members directly, use the functions!
35 * A non-empty range has @lob <= @upb.
36 * An empty range has @lob == @upb + 1.
37 */
38 uint64_t lob; /* inclusive lower bound */
39 uint64_t upb; /* inclusive upper bound */
37};
38
39static inline void range_invariant(Range *range)
40{
40};
41
42static inline void range_invariant(Range *range)
43{
41 assert((!range->begin && !range->end) /* empty */
42 || range->begin <= range->end - 1); /* non-empty */
44 assert(range->lob <= range->upb || range->lob == range->upb + 1);
43}
44
45/* Compound literal encoding the empty range */
45}
46
47/* Compound literal encoding the empty range */
46#define range_empty ((Range){ .begin = 0, .end = 0 })
48#define range_empty ((Range){ .lob = 1, .upb = 0 })
47
48/* Is @range empty? */
49static inline bool range_is_empty(Range *range)
50{
51 range_invariant(range);
49
50/* Is @range empty? */
51static inline bool range_is_empty(Range *range)
52{
53 range_invariant(range);
52 return !range->begin && !range->end;
54 return range->lob > range->upb;
53}
54
55/* Does @range contain @val? */
56static inline bool range_contains(Range *range, uint64_t val)
57{
55}
56
57/* Does @range contain @val? */
58static inline bool range_contains(Range *range, uint64_t val)
59{
58 return !range_is_empty(range)
59 && val >= range->begin && val <= range->end - 1;
60 return val >= range->lob && val <= range->upb;
60}
61
62/* Initialize @range to the empty range */
63static inline void range_make_empty(Range *range)
64{
65 *range = range_empty;
66 assert(range_is_empty(range));
67}
68
69/*
70 * Initialize @range to span the interval [@lob,@upb].
71 * Both bounds are inclusive.
72 * The interval must not be empty, i.e. @lob must be less than or
73 * equal @upb.
61}
62
63/* Initialize @range to the empty range */
64static inline void range_make_empty(Range *range)
65{
66 *range = range_empty;
67 assert(range_is_empty(range));
68}
69
70/*
71 * Initialize @range to span the interval [@lob,@upb].
72 * Both bounds are inclusive.
73 * The interval must not be empty, i.e. @lob must be less than or
74 * equal @upb.
74 * The interval must not be [0,UINT64_MAX], because Range can't
75 * represent that.
76 */
77static inline void range_set_bounds(Range *range, uint64_t lob, uint64_t upb)
78{
75 */
76static inline void range_set_bounds(Range *range, uint64_t lob, uint64_t upb)
77{
79 assert(lob <= upb);
80 range->begin = lob;
81 range->end = upb + 1; /* may wrap to zero, that's okay */
78 range->lob = lob;
79 range->upb = upb;
82 assert(!range_is_empty(range));
83}
84
85/*
86 * Initialize @range to span the interval [@lob,@upb_plus1).
87 * The lower bound is inclusive, the upper bound is exclusive.
88 * Zero @upb_plus1 is special: if @lob is also zero, set @range to the
89 * empty range. Else, set @range to [@lob,UINT64_MAX].
90 */
91static inline void range_set_bounds1(Range *range,
92 uint64_t lob, uint64_t upb_plus1)
93{
80 assert(!range_is_empty(range));
81}
82
83/*
84 * Initialize @range to span the interval [@lob,@upb_plus1).
85 * The lower bound is inclusive, the upper bound is exclusive.
86 * Zero @upb_plus1 is special: if @lob is also zero, set @range to the
87 * empty range. Else, set @range to [@lob,UINT64_MAX].
88 */
89static inline void range_set_bounds1(Range *range,
90 uint64_t lob, uint64_t upb_plus1)
91{
94 range->begin = lob;
95 range->end = upb_plus1;
92 if (!lob && !upb_plus1) {
93 *range = range_empty;
94 } else {
95 range->lob = lob;
96 range->upb = upb_plus1 - 1;
97 }
96 range_invariant(range);
97}
98
99/* Return @range's lower bound. @range must not be empty. */
100static inline uint64_t range_lob(Range *range)
101{
102 assert(!range_is_empty(range));
98 range_invariant(range);
99}
100
101/* Return @range's lower bound. @range must not be empty. */
102static inline uint64_t range_lob(Range *range)
103{
104 assert(!range_is_empty(range));
103 return range->begin;
105 return range->lob;
104}
105
106/* Return @range's upper bound. @range must not be empty. */
107static inline uint64_t range_upb(Range *range)
108{
109 assert(!range_is_empty(range));
106}
107
108/* Return @range's upper bound. @range must not be empty. */
109static inline uint64_t range_upb(Range *range)
110{
111 assert(!range_is_empty(range));
110 return range->end - 1;
112 return range->upb;
111}
112
113/*
114 * Extend @range to the smallest interval that includes @extend_by, too.
113}
114
115/*
116 * Extend @range to the smallest interval that includes @extend_by, too.
115 * This must not extend @range to cover the interval [0,UINT64_MAX],
116 * because Range can't represent that.
117 */
118static inline void range_extend(Range *range, Range *extend_by)
119{
120 if (range_is_empty(extend_by)) {
121 return;
122 }
123 if (range_is_empty(range)) {
124 *range = *extend_by;
125 return;
126 }
117 */
118static inline void range_extend(Range *range, Range *extend_by)
119{
120 if (range_is_empty(extend_by)) {
121 return;
122 }
123 if (range_is_empty(range)) {
124 *range = *extend_by;
125 return;
126 }
127 if (range->begin > extend_by->begin) {
128 range->begin = extend_by->begin;
127 if (range->lob > extend_by->lob) {
128 range->lob = extend_by->lob;
129 }
129 }
130 /* Compare last byte in case region ends at ~0x0LL */
131 if (range->end - 1 < extend_by->end - 1) {
132 range->end = extend_by->end;
130 if (range->upb < extend_by->upb) {
131 range->upb = extend_by->upb;
133 }
132 }
134 /* Must not extend to { .begin = 0, .end = 0 }: */
135 assert(!range_is_empty(range));
133 range_invariant(range);
136}
137
138/* Get last byte of a range from offset + length.
139 * Undefined for ranges that wrap around 0. */
140static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
141{
142 return offset + len - 1;
143}

--- 22 unchanged lines hidden ---
134}
135
136/* Get last byte of a range from offset + length.
137 * Undefined for ranges that wrap around 0. */
138static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
139{
140 return offset + len - 1;
141}

--- 22 unchanged lines hidden ---