From: Liam R. Howlett Date: Thu, 21 Apr 2022 00:37:48 +0000 (-0400) Subject: maple_tree: Add preallocation support X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=52b644111e699d183ad4bc0fa961e572f143f79c;p=users%2Fjedix%2Flinux-maple.git maple_tree: Add preallocation support Signed-off-by: Liam R. Howlett --- diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index 8f4d87129a3e..ded42d306287 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -454,8 +454,10 @@ void *mas_walk(struct ma_state *mas); void *mas_store(struct ma_state *mas, void *entry); void *mas_erase(struct ma_state *mas); int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp); +void mas_store_prealloc(struct ma_state *mas, void *entry); void *mas_find(struct ma_state *mas, unsigned long max); void *mas_find_rev(struct ma_state *mas, unsigned long min); +int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp); bool mas_nomem(struct ma_state *mas, gfp_t gfp); void mas_pause(struct ma_state *mas); diff --git a/lib/maple_tree.c b/lib/maple_tree.c index d6a10216521e..c955cdef79be 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -5548,6 +5548,44 @@ retry: return 0; } +/** + * mas_store_prealloc() - Store a value into the tree using memeory + * preallocated in the maple state. + * @mas: The maple state + * @entry: The entry to store. + */ +void mas_store_prealloc(struct ma_state *mas, void *entry) +{ + MA_WR_STATE(wr_mas, mas, entry); + + mas_wr_store_setup(&wr_mas); + trace_ma_write(__func__, mas, 0, entry); + mas_wr_store_entry(&wr_mas); + BUG_ON(mas_is_err(mas)); + mas_destroy(mas); +} + +/** + * mas_preallocate() - Preallocate enough nodes for a store operation + * @mas: The maple state + * @entry: The entry that will be stored + * + * Return: 0 on success, -ENOMEM if memory could not be allocated. + */ +int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp) +{ + + mas_set_alloc_req(mas, 1 + mas_mt_height(mas) * 3); + mas_alloc_nodes(mas, gfp); + if (likely(mas->node != MA_ERROR(-ENOMEM))) + return 0; + + mas_set_alloc_req(mas, 0); + mas_destroy(mas); + mas->node = MAS_START; + return -ENOMEM; +} + /* * mas_expected_entries() - Set the expected number of entries that will be inserted. * @mas: The maple state