2 * QEMU 64-bit address ranges
4 * Copyright (c) 2015-2016 Red Hat, Inc.
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
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/queue.h"
27 * Operations on 64 bit address ranges.
29 * - Ranges must not wrap around 0, but can include UINT64_MAX.
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.
38 uint64_t lob
; /* inclusive lower bound */
39 uint64_t upb
; /* inclusive upper bound */
42 static inline void range_invariant(Range
*range
)
44 assert(range
->lob
<= range
->upb
|| range
->lob
== range
->upb
+ 1);
47 /* Compound literal encoding the empty range */
48 #define range_empty ((Range){ .lob = 1, .upb = 0 })
50 /* Is @range empty? */
51 static inline bool range_is_empty(Range
*range
)
53 range_invariant(range
);
54 return range
->lob
> range
->upb
;
57 /* Does @range contain @val? */
58 static inline bool range_contains(Range
*range
, uint64_t val
)
60 return val
>= range
->lob
&& val
<= range
->upb
;
63 /* Initialize @range to the empty range */
64 static inline void range_make_empty(Range
*range
)
67 assert(range_is_empty(range
));
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
76 static inline void range_set_bounds(Range
*range
, uint64_t lob
, uint64_t upb
)
80 assert(!range_is_empty(range
));
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].
89 static inline void range_set_bounds1(Range
*range
,
90 uint64_t lob
, uint64_t upb_plus1
)
92 if (!lob
&& !upb_plus1
) {
96 range
->upb
= upb_plus1
- 1;
98 range_invariant(range
);
101 /* Return @range's lower bound. @range must not be empty. */
102 static inline uint64_t range_lob(Range
*range
)
104 assert(!range_is_empty(range
));
108 /* Return @range's upper bound. @range must not be empty. */
109 static inline uint64_t range_upb(Range
*range
)
111 assert(!range_is_empty(range
));
116 * Extend @range to the smallest interval that includes @extend_by, too.
118 static inline void range_extend(Range
*range
, Range
*extend_by
)
120 if (range_is_empty(extend_by
)) {
123 if (range_is_empty(range
)) {
127 if (range
->lob
> extend_by
->lob
) {
128 range
->lob
= extend_by
->lob
;
130 if (range
->upb
< extend_by
->upb
) {
131 range
->upb
= extend_by
->upb
;
133 range_invariant(range
);
136 /* Get last byte of a range from offset + length.
137 * Undefined for ranges that wrap around 0. */
138 static inline uint64_t range_get_last(uint64_t offset
, uint64_t len
)
140 return offset
+ len
- 1;
143 /* Check whether a given range covers a given byte. */
144 static inline int range_covers_byte(uint64_t offset
, uint64_t len
,
147 return offset
<= byte
&& byte
<= range_get_last(offset
, len
);
150 /* Check whether 2 given ranges overlap.
151 * Undefined if ranges that wrap around 0. */
152 static inline int ranges_overlap(uint64_t first1
, uint64_t len1
,
153 uint64_t first2
, uint64_t len2
)
155 uint64_t last1
= range_get_last(first1
, len1
);
156 uint64_t last2
= range_get_last(first2
, len2
);
158 return !(last2
< first1
|| last1
< first2
);
161 GList
*range_list_insert(GList
*list
, Range
*data
);