diff mbox

[1/3] range: add structure to pass ranges around

Message ID fbb8582d1ab59a17f76e32d6699aa2d24c954214.1367250562.git.mst@redhat.com
State New
Headers show

Commit Message

Michael S. Tsirkin April 29, 2013, 3:51 p.m. UTC
Convenient structure to pass ranges around,
and calculate length avoiding 64 bit overlap issues.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 include/qemu/range.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
diff mbox

Patch

diff --git a/include/qemu/range.h b/include/qemu/range.h
index 3502372..aeef1fb 100644
--- a/include/qemu/range.h
+++ b/include/qemu/range.h
@@ -1,6 +1,30 @@ 
 #ifndef QEMU_RANGE_H
 #define QEMU_RANGE_H
 
+#include <inttypes.h>
+
+struct Range {
+    uint64_t min;
+    uint64_t max;
+};
+typedef struct Range Range;
+
+/* max <= min means not valid */
+static inline bool range_valid(Range *r)
+{
+    return r->max > r->min;
+}
+
+static inline uint64_t range_len(Range *r)
+{
+    return range_valid(r) ? r->max - r->min + 1 : 0;
+}
+
+static inline uint64_t range_start(Range *r)
+{
+    return r->min;
+}
+
 /* Get last byte of a range from offset + length.
  * Undefined for ranges that wrap around 0. */
 static inline uint64_t range_get_last(uint64_t offset, uint64_t len)