]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
maple_tree: Add mas_next_range() and mas_find_range() interfaces
authorLiam R. Howlett <Liam.Howlett@oracle.com>
Fri, 14 Apr 2023 21:06:05 +0000 (17:06 -0400)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Fri, 21 Apr 2023 14:12:31 +0000 (10:12 -0400)
Some users of the maple tree may want to move to the next range in the
tree, even if it stores a NULL.  This family of function provides that
functionality by advancing one slot at a time and returning the result,
while mas_contiguous() will iterate over the range and stop on
encountering the first NULL.

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
include/linux/maple_tree.h
lib/maple_tree.c

index e594db58a0f147c8fec83ea398e4c81e263e8aac..0552d8b3b5b773de09fa51eeac4fa525d45552a1 100644 (file)
@@ -467,6 +467,7 @@ int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries);
 
 void *mas_prev(struct ma_state *mas, unsigned long min);
 void *mas_next(struct ma_state *mas, unsigned long max);
+void *mas_next_range(struct ma_state *mas, unsigned long max);
 
 int mas_empty_area(struct ma_state *mas, unsigned long min, unsigned long max,
                   unsigned long size);
@@ -522,6 +523,19 @@ static inline void mas_reset(struct ma_state *mas)
 #define mas_for_each(__mas, __entry, __max) \
        while (((__entry) = mas_find((__mas), (__max))) != NULL)
 
+/**
+ * mas_contiguous() - Iterate over a contiguous range of the maple tree.
+ * @__mas: Maple Tree operation state (maple_state)
+ * @__entry: Entry retrieved from the tree
+ * @__max: maximum index to retrieve from the tree
+ *
+ * When returned, mas->index and mas->last will hold the entire range of the
+ * entry.  The loop will terminate on the first NULL encountered.
+ *
+ * Note: may return the zero entry.
+ */
+#define mas_contiguous(__mas, __entry, __max) \
+       while (((__entry) = mas_find_range((__mas), (__max))) != NULL)
 
 /**
  * mas_set_range() - Set up Maple Tree operation state for a different index.
index dd502427caaac517b05deffdcc23b08ea6946f9e..803ffb84c77585ae7799c496c5db9d7ab67d1ae7 100644 (file)
@@ -5857,18 +5857,8 @@ int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
 }
 EXPORT_SYMBOL_GPL(mas_expected_entries);
 
-/**
- * mas_next() - Get the next entry.
- * @mas: The maple state
- * @max: The maximum index to check.
- *
- * Returns the next entry after @mas->index.
- * Must hold rcu_read_lock or the write lock.
- * Can return the zero entry.
- *
- * Return: The next entry or %NULL
- */
-void *mas_next(struct ma_state *mas, unsigned long max)
+static inline bool mas_next_setup(struct ma_state *mas, unsigned long max,
+               void **entry)
 {
        bool was_none = mas_is_none(mas);
 
@@ -5881,19 +5871,63 @@ void *mas_next(struct ma_state *mas, unsigned long max)
        if (mas_is_ptr(mas)) {
                if (was_none && mas->index == 0) {
                        mas->index = mas->last = 0;
-                       return mas_root(mas);
+                       *entry = mas_root(mas);
+                       return true;
                }
                mas->index = 1;
                mas->last = ULONG_MAX;
                mas->node = MAS_NONE;
-               return NULL;
+               return true;
        }
+       return false;
+}
+
+/**
+ * mas_next() - Get the next entry.
+ * @mas: The maple state
+ * @max: The maximum index to check.
+ *
+ * Returns the next entry after @mas->index.
+ * Must hold rcu_read_lock or the write lock.
+ * Can return the zero entry.
+ *
+ * Return: The next entry or %NULL
+ */
+void *mas_next(struct ma_state *mas, unsigned long max)
+{
+       void *entry = NULL;
+
+       if (mas_next_setup(mas, max, entry))
+               return entry;
 
        /* Retries on dead nodes handled by mas_next_entry */
        return mas_next_entry(mas, max);
 }
 EXPORT_SYMBOL_GPL(mas_next);
 
+/**
+ * mas_next_range() - Advance the maple state to the next range
+ * @mas: The maple state
+ * @max: The maximum index to check.
+ *
+ * Sets @mas->index and @mas->last to the range.
+ * Must hold rcu_read_lock or the write lock.
+ * Can return the zero entry.
+ *
+ * Return: The next entry or %NULL
+ */
+void *mas_next_range(struct ma_state *mas, unsigned long max)
+{
+       void *entry = NULL;
+
+       if (mas_next_setup(mas, max, entry))
+               return entry;
+
+       /* Retries on dead nodes handled by mas_next_slot */
+       return mas_next_slot(mas, max);
+}
+EXPORT_SYMBOL_GPL(mas_next_range);
+
 /**
  * mt_next() - get the next value in the maple tree
  * @mt: The maple tree
@@ -6003,22 +6037,18 @@ void mas_pause(struct ma_state *mas)
 EXPORT_SYMBOL_GPL(mas_pause);
 
 /**
- * mas_find() - On the first call, find the entry at or after mas->index up to
- * %max.  Otherwise, find the entry after mas->index.
- * @mas: The maple state
- * @max: The maximum value to check.
+ * mas_find_setup() - Internal function to set up mas_find*().
  *
- * Must hold rcu_read_lock or the write lock.
- * If an entry exists, last and index are updated accordingly.
- * May set @mas->node to MAS_NONE.
- *
- * Return: The entry or %NULL.
+ * Returns: True if entry is the answer, false otherwise.
  */
-void *mas_find(struct ma_state *mas, unsigned long max)
+static inline bool mas_find_setup(struct ma_state *mas, unsigned long max,
+               void **entry)
 {
+       *entry = NULL;
+
        if (unlikely(mas_is_none(mas))) {
                if (unlikely(mas->last >= max))
-                       return NULL;
+                       return true;
 
                mas->index = mas->last;
                mas->node = MAS_START;
@@ -6026,7 +6056,7 @@ void *mas_find(struct ma_state *mas, unsigned long max)
 
        if (unlikely(mas_is_paused(mas))) {
                if (unlikely(mas->last >= max))
-                       return NULL;
+                       return true;
 
                mas->node = MAS_START;
                mas->index = ++mas->last;
@@ -6038,14 +6068,12 @@ void *mas_find(struct ma_state *mas, unsigned long max)
 
        if (unlikely(mas_is_start(mas))) {
                /* First run or continue */
-               void *entry;
-
                if (mas->index > max)
-                       return NULL;
+                       return true;
 
-               entry = mas_walk(mas);
-               if (entry)
-                       return entry;
+               *entry = mas_walk(mas);
+               if (*entry)
+                       return true;
 
        }
 
@@ -6053,23 +6081,69 @@ void *mas_find(struct ma_state *mas, unsigned long max)
                if (unlikely(mas_is_ptr(mas)))
                        goto ptr_out_of_range;
 
-               return NULL;
+               return true;
        }
 
        if (mas->index == max)
-               return NULL;
+               return true;
 
-       /* Retries on dead nodes handled by mas_next_entry */
-       return mas_next_entry(mas, max);
+       return false;
 
 ptr_out_of_range:
        mas->node = MAS_NONE;
        mas->index = 1;
        mas->last = ULONG_MAX;
-       return NULL;
+       return true;
+}
+
+/**
+ * mas_find() - On the first call, find the entry at or after mas->index up to
+ * %max.  Otherwise, find the entry after mas->index.
+ * @mas: The maple state
+ * @max: The maximum value to check.
+ *
+ * Must hold rcu_read_lock or the write lock.
+ * If an entry exists, last and index are updated accordingly.
+ * May set @mas->node to MAS_NONE.
+ *
+ * Return: The entry or %NULL.
+ */
+void *mas_find(struct ma_state *mas, unsigned long max)
+{
+       void *entry = NULL;
+
+       if (mas_find_setup(mas, max, &entry))
+           return entry;
+
+       /* Retries on dead nodes handled by mas_next_entry */
+       return mas_next_entry(mas, max);
 }
 EXPORT_SYMBOL_GPL(mas_find);
 
+/**
+ * mas_find_range() - On the first call, find the entry at or after
+ * mas->index up to %max.  Otherwise, advance to the next slot mas->index.
+ * @mas: The maple state
+ * @max: The maximum value to check.
+ *
+ * Must hold rcu_read_lock or the write lock.
+ * If an entry exists, last and index are updated accordingly.
+ * May set @mas->node to MAS_NONE.
+ *
+ * Return: The entry or %NULL.
+ */
+void *mas_find_range(struct ma_state *mas, unsigned long max)
+{
+       void *entry;
+
+       if (mas_find_setup(mas, max, &entry))
+               return entry;
+
+       /* Retries on dead nodes handled by mas_next_entry */
+       return mas_next_slot(mas, max);
+}
+EXPORT_SYMBOL_GPL(mas_find_range);
+
 /**
  * mas_find_rev: On the first call, find the first non-null entry at or below
  * mas->index down to %min.  Otherwise find the first non-null entry below