Statistics
| Branch: | Revision:

root / range.h @ a74cdab4

History | View | Annotate | Download (867 Bytes)

1 bf1b0071 Blue Swirl
#ifndef QEMU_RANGE_H
2 bf1b0071 Blue Swirl
#define QEMU_RANGE_H
3 bf1b0071 Blue Swirl
4 bf1b0071 Blue Swirl
/* Get last byte of a range from offset + length.
5 bf1b0071 Blue Swirl
 * Undefined for ranges that wrap around 0. */
6 bf1b0071 Blue Swirl
static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
7 bf1b0071 Blue Swirl
{
8 bf1b0071 Blue Swirl
    return offset + len - 1;
9 bf1b0071 Blue Swirl
}
10 bf1b0071 Blue Swirl
11 bf1b0071 Blue Swirl
/* Check whether a given range covers a given byte. */
12 bf1b0071 Blue Swirl
static inline int range_covers_byte(uint64_t offset, uint64_t len,
13 bf1b0071 Blue Swirl
                                    uint64_t byte)
14 bf1b0071 Blue Swirl
{
15 bf1b0071 Blue Swirl
    return offset <= byte && byte <= range_get_last(offset, len);
16 bf1b0071 Blue Swirl
}
17 bf1b0071 Blue Swirl
18 bf1b0071 Blue Swirl
/* Check whether 2 given ranges overlap.
19 bf1b0071 Blue Swirl
 * Undefined if ranges that wrap around 0. */
20 bf1b0071 Blue Swirl
static inline int ranges_overlap(uint64_t first1, uint64_t len1,
21 bf1b0071 Blue Swirl
                                 uint64_t first2, uint64_t len2)
22 bf1b0071 Blue Swirl
{
23 bf1b0071 Blue Swirl
    uint64_t last1 = range_get_last(first1, len1);
24 bf1b0071 Blue Swirl
    uint64_t last2 = range_get_last(first2, len2);
25 bf1b0071 Blue Swirl
26 bf1b0071 Blue Swirl
    return !(last2 < first1 || last1 < first2);
27 bf1b0071 Blue Swirl
}
28 bf1b0071 Blue Swirl
29 bf1b0071 Blue Swirl
#endif