Patchwork [v2,3/6] range: add min/max operations on ranges

login
register
mail settings
Submitter Michael S. Tsirkin
Date Sept. 15, 2013, 8:46 a.m.
Message ID <1379234759-31527-4-git-send-email-mst@redhat.com>
Download mbox | patch
Permalink /patch/275007/
State New
Headers show

Comments

Michael S. Tsirkin - Sept. 15, 2013, 8:46 a.m.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 include/qemu/range.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Patch

diff --git a/include/qemu/range.h b/include/qemu/range.h
index 4a0780d..1c688ca 100644
--- a/include/qemu/range.h
+++ b/include/qemu/range.h
@@ -17,6 +17,23 @@  struct Range {
     uint64_t end;   /* 1 + the last byte. 0 if range empty or ends at ~0x0LL. */
 };
 
+static inline void range_extend(Range *range, Range *extend_by)
+{
+    if (!extend_by->begin && !extend_by->end) {
+        return;
+    }
+    if (!range->begin && !range->end) {
+        *range = *extend_by;
+        return;
+    }
+    if (range->begin > extend_by->begin) {
+        range->begin = extend_by->begin;
+    }
+    if (range->end - 1 < extend_by->end - 1) {
+        range->end = extend_by->end;
+    }
+}
+
 /* 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)