xref: /openbmc/qemu/include/qemu/range.h (revision 56983463)
1 #ifndef QEMU_RANGE_H
2 #define QEMU_RANGE_H
3 
4 #include <inttypes.h>
5 
6 /*
7  * Operations on 64 bit address ranges.
8  * Notes:
9  *   - ranges must not wrap around 0, but can include the last byte ~0x0LL.
10  *   - this can not represent a full 0 to ~0x0LL range.
11  */
12 
13 /* A structure representing a range of addresses. */
14 struct Range {
15     uint64_t begin; /* First byte of the range, or 0 if empty. */
16     uint64_t end;   /* 1 + the last byte. 0 if range empty or ends at ~0x0LL. */
17 };
18 typedef struct Range Range;
19 
20 /* Get last byte of a range from offset + length.
21  * Undefined for ranges that wrap around 0. */
22 static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
23 {
24     return offset + len - 1;
25 }
26 
27 /* Check whether a given range covers a given byte. */
28 static inline int range_covers_byte(uint64_t offset, uint64_t len,
29                                     uint64_t byte)
30 {
31     return offset <= byte && byte <= range_get_last(offset, len);
32 }
33 
34 /* Check whether 2 given ranges overlap.
35  * Undefined if ranges that wrap around 0. */
36 static inline int ranges_overlap(uint64_t first1, uint64_t len1,
37                                  uint64_t first2, uint64_t len2)
38 {
39     uint64_t last1 = range_get_last(first1, len1);
40     uint64_t last2 = range_get_last(first2, len2);
41 
42     return !(last2 < first1 || last1 < first2);
43 }
44 
45 #endif
46