Most ranges in barebox identified by start and end offsets are exclusive
at the end, for example, the linker symbols for the sections.

To make handling them a bit easier while avoiding off-by-one, let's add
a region_overlap_end_exclusive helper as well.

Unlike inclusive ranges, exclusive ranges can be empty, so add a check
for invalid ranges.

Signed-off-by: Ahmad Fatoum <a.fat...@barebox.org>
---
 include/range.h | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/include/range.h b/include/range.h
index 96e0f124d5d4..e7819017e473 100644
--- a/include/range.h
+++ b/include/range.h
@@ -22,6 +22,26 @@ static inline bool region_overlap_end_inclusive(u64 starta, 
u64 enda,
        return true;
 }
 
+/**
+ * region_overlap_end_exclusive - check whether a pair of [start, end) ranges 
overlap
+ *
+ * @starta: start of the first range
+ * @enda:   end of the first range (exclusive)
+ * @startb: start of the second range
+ * @endb:   end of the second range (exclusive)
+ */
+static inline bool region_overlap_end_exclusive(u64 starta, u64 enda,
+                                               u64 startb, u64 endb)
+{
+       /* Empty ranges don't overlap */
+       if (starta >= enda || startb >= endb)
+               return false;
+
+       return region_overlap_end_inclusive(starta, enda - 1,
+                                           startb, startb - 1);
+}
+
+
 /**
  * region_overlap_end - check whether a pair of [start, end] ranges overlap
  *
@@ -36,8 +56,8 @@ static inline bool region_overlap_size(u64 starta, u64 lena,
        if (!lena || !lenb)
                return false;
 
-       return region_overlap_end_inclusive(starta, starta + lena - 1,
-                                           startb, startb + lenb - 1);
+       return region_overlap_end_exclusive(starta, starta + lena,
+                                           startb, startb + lenb);
 }
 
 /**
-- 
2.39.5


Reply via email to