From: Liam R. Howlett Date: Fri, 11 Oct 2019 14:39:35 +0000 (-0400) Subject: maple_tree: Move iterators to the header X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=33a9abe9f5080c7de61d623c12c99d9d83b1c362;p=users%2Fjedix%2Flinux-maple.git maple_tree: Move iterators to the header This is part of the api, so expose it through the header. Signed-off-by: Liam R. Howlett --- diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index ad7f0af2794b..eb1626010935 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -303,4 +303,33 @@ void *mas_find(struct ma_state *mas, unsigned long max); bool mas_nomem(struct ma_state *mas, gfp_t gfp); void mas_pause(struct ma_state *mas); void maple_tree_init(void); + +/** + * mas_for_each() - Iterate over a 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 for the + * entry. + * + * Note: may return the zero entry. + * + */ +#define mas_for_each(mas, entry, max) \ + while ((entry = mas_find(mas, max)) != NULL) + +/** + * mt_for_each - Searches for an entry starting at index until max. + * + * + * + * Note: Will not return the zero entry. + * + * + */ +#define mt_for_each(tree, entry, index, max) \ + for (entry = mt_find(mt, index, max); \ + entry; entry = mt_find_after(mt, &index, max)) + #endif diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 1680659dbce1..50ee13a0f83a 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -2372,6 +2372,17 @@ no_entry: mas->node = MAS_NONE; } +static inline void* mas_prev_entry(struct ma_state *mas, unsigned long min) +{ + if (mas->node == MAS_NONE) + return NULL; + + mas_prev_node(mas, min); + if (mas->node == MAS_NONE) + return NULL; + + return mte_get_rcu_slot(mas->node, mas_get_slot(mas)); +} /* * Find the next non-null entry at the same level in the tree. The next value * will be mas->node[mas_get_slot(mas)] or MAS_NONE. @@ -3327,6 +3338,14 @@ static inline int mas_dead_node(struct ma_state *mas, unsigned long index) return 1; } +void *mas_load(struct ma_state *mas) +{ + void *entry = mas_walk(&mas); + if (xa_is_zero(entry) || mt_is_empty(entry)) + return NULL; + + return entry; +} /** * mas_find - Find the first entry that overlaps the range. * If mas->node is root, finds mas->index and returns the value. If the @@ -4020,13 +4039,9 @@ void *mtree_load(struct maple_tree *mt, unsigned long index) MA_STATE(mas, mt, index, index); rcu_read_lock(); - entry = mas_walk(&mas); + entry = mas_load(mas); rcu_read_unlock(); - - if (xa_is_zero(entry) || mt_is_empty(entry)) - return NULL; - - return entry; + return mas_load(mas); } EXPORT_SYMBOL(mtree_load);