2 * QEMU 64-bit address ranges
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This program 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
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 * Operations on 64 bit address ranges.
26 * - Ranges must not wrap around 0, but can include UINT64_MAX.
31 * Do not access members directly, use the functions!
32 * A non-empty range has @lob <= @upb.
33 * An empty range has @lob == @upb + 1.
35 uint64_t lob
; /* inclusive lower bound */
36 uint64_t upb
; /* inclusive upper bound */
39 static inline void range_invariant(const Range
*range
)
41 assert(range
->lob
<= range
->upb
|| range
->lob
== range
->upb
+ 1);
44 /* Compound literal encoding the empty range */
45 #define range_empty ((Range){ .lob = 1, .upb = 0 })
47 /* Is @range empty? */
48 static inline bool range_is_empty(const Range
*range
)
50 range_invariant(range
);
51 return range
->lob
> range
->upb
;
54 /* Does @range contain @val? */
55 static inline bool range_contains(const Range
*range
, uint64_t val
)
57 return val
>= range
->lob
&& val
<= range
->upb
;
60 /* Initialize @range to the empty range */
61 static inline void range_make_empty(Range
*range
)
64 assert(range_is_empty(range
));
68 * Initialize @range to span the interval [@lob,@upb].
69 * Both bounds are inclusive.
70 * The interval must not be empty, i.e. @lob must be less than or
73 static inline void range_set_bounds(Range
*range
, uint64_t lob
, uint64_t upb
)
77 assert(!range_is_empty(range
));
81 * Initialize @range to span the interval [@lob,@upb_plus1).
82 * The lower bound is inclusive, the upper bound is exclusive.
83 * Zero @upb_plus1 is special: if @lob is also zero, set @range to the
84 * empty range. Else, set @range to [@lob,UINT64_MAX].
86 static inline void range_set_bounds1(Range
*range
,
87 uint64_t lob
, uint64_t upb_plus1
)
89 if (!lob
&& !upb_plus1
) {
93 range
->upb
= upb_plus1
- 1;
95 range_invariant(range
);
98 /* Return @range's lower bound. @range must not be empty. */
99 static inline uint64_t range_lob(Range
*range
)
101 assert(!range_is_empty(range
));
105 /* Return @range's upper bound. @range must not be empty. */
106 static inline uint64_t range_upb(Range
*range
)
108 assert(!range_is_empty(range
));
113 * Initialize @range to span the interval [@lob,@lob + @size - 1].
114 * @size may be 0. If the range would overflow, returns -ERANGE, otherwise
117 G_GNUC_WARN_UNUSED_RESULT
118 static inline int range_init(Range
*range
, uint64_t lob
, uint64_t size
)
120 if (lob
+ size
< lob
) {
124 range
->upb
= lob
+ size
- 1;
125 range_invariant(range
);
130 * Initialize @range to span the interval [@lob,@lob + @size - 1].
131 * @size may be 0. Range must not overflow.
133 static inline void range_init_nofail(Range
*range
, uint64_t lob
, uint64_t size
)
136 range
->upb
= lob
+ size
- 1;
137 range_invariant(range
);
141 * Get the size of @range.
143 static inline uint64_t range_size(const Range
*range
)
145 return range
->upb
- range
->lob
+ 1;
149 * Check if @range1 overlaps with @range2. If one of the ranges is empty,
150 * the result is always "false".
152 static inline bool range_overlaps_range(const Range
*range1
,
155 if (range_is_empty(range1
) || range_is_empty(range2
)) {
158 return !(range2
->upb
< range1
->lob
|| range1
->upb
< range2
->lob
);
162 * Check if @range1 contains @range2. If one of the ranges is empty,
163 * the result is always "false".
165 static inline bool range_contains_range(const Range
*range1
,
168 if (range_is_empty(range1
) || range_is_empty(range2
)) {
171 return range1
->lob
<= range2
->lob
&& range1
->upb
>= range2
->upb
;
175 * Extend @range to the smallest interval that includes @extend_by, too.
177 static inline void range_extend(Range
*range
, Range
*extend_by
)
179 if (range_is_empty(extend_by
)) {
182 if (range_is_empty(range
)) {
186 if (range
->lob
> extend_by
->lob
) {
187 range
->lob
= extend_by
->lob
;
189 if (range
->upb
< extend_by
->upb
) {
190 range
->upb
= extend_by
->upb
;
192 range_invariant(range
);
195 /* Get last byte of a range from offset + length.
196 * Undefined for ranges that wrap around 0. */
197 static inline uint64_t range_get_last(uint64_t offset
, uint64_t len
)
199 return offset
+ len
- 1;
202 /* Check whether a given range covers a given byte. */
203 static inline int range_covers_byte(uint64_t offset
, uint64_t len
,
206 return offset
<= byte
&& byte
<= range_get_last(offset
, len
);
209 /* Check whether 2 given ranges overlap.
210 * Undefined if ranges that wrap around 0. */
211 static inline int ranges_overlap(uint64_t first1
, uint64_t len1
,
212 uint64_t first2
, uint64_t len2
)
214 uint64_t last1
= range_get_last(first1
, len1
);
215 uint64_t last2
= range_get_last(first2
, len2
);
217 return !(last2
< first1
|| last1
< first2
);
220 GList
*range_list_insert(GList
*list
, Range
*data
);