[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH RFC v1 43/74] xen: introduce rangeset_reserve_hole



Reserve a hole in a rangeset.

Signed-off-by: Roger Pau Monne <roger.pau@xxxxxxxxxx>
Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 xen/common/rangeset.c      | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h |  4 ++++
 2 files changed, 55 insertions(+)

diff --git a/xen/common/rangeset.c b/xen/common/rangeset.c
index 6c6293c15c..2633786289 100644
--- a/xen/common/rangeset.c
+++ b/xen/common/rangeset.c
@@ -298,6 +298,57 @@ int rangeset_report_ranges(
     return rc;
 }
 
+int rangeset_reserve_hole(struct rangeset *r, unsigned long size,
+                          unsigned long *s)
+{
+    struct range *prev, *next;
+
+    *s = 0;
+
+    write_lock(&r->lock);
+
+    for ( prev = NULL, next = first_range(r);
+          next;
+          prev = next, next = next_range(r, next) )
+    {
+        if ( (next->s - *s) >= size )
+            goto insert;
+
+        if ( next->e == ~0UL )
+            goto out;
+
+        *s = next->e + 1;
+    }
+
+    if ( (~0UL - *s) + 1 >= size )
+        goto insert;
+
+ out:
+    write_unlock(&r->lock);
+    return -ENOSPC;
+
+ insert:
+    if ( !prev )
+    {
+        next = alloc_range(r);
+        if ( !next )
+        {
+            write_unlock(&r->lock);
+            return -ENOMEM;
+        }
+
+        next->s = *s;
+        next->e = *s + size - 1;
+        insert_range(r, prev, next);
+    }
+    else
+        prev->e += size;
+
+    write_unlock(&r->lock);
+
+    return 0;
+}
+
 int rangeset_add_singleton(
     struct rangeset *r, unsigned long s)
 {
diff --git a/xen/include/xen/rangeset.h b/xen/include/xen/rangeset.h
index aa6408248b..a606fb7793 100644
--- a/xen/include/xen/rangeset.h
+++ b/xen/include/xen/rangeset.h
@@ -76,6 +76,10 @@ int __must_check rangeset_remove_singleton(
 bool_t __must_check rangeset_contains_singleton(
     struct rangeset *r, unsigned long s);
 
+/* Reserve a region of the specified size. */
+int __must_check rangeset_reserve_hole(struct rangeset *r, unsigned long size,
+                                       unsigned long *s);
+
 /* swap contents */
 void rangeset_swap(struct rangeset *a, struct rangeset *b);
 
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.