master - radix-tree: fix some bugs in remove_prefix and iterate
Gitweb: https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=272ec3fa73c92764ef6fe5466fc367fed66dd7f9 Commit: 272ec3fa73c92764ef6fe5466fc367fed66dd7f9 Parent: 1924426ad1e8a569c168e3d85c368ed531f382fe Author: Joe Thornber <[email protected]> AuthorDate: Wed May 30 14:14:59 2018 +0100 Committer: Joe Thornber <[email protected]> CommitterDate: Wed May 30 14:14:59 2018 +0100
radix-tree: fix some bugs in remove_prefix and iterate
These weren't working if the prefix key was part of a prefix_chain.
--- base/data-struct/radix-tree.c | 22 ++++++++++++++- test/unit/radix_tree_t.c | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-)
base/data-struct/radix-tree.c +20 -2
@@ -736,11 +736,29 @@ bool radix_tree_remove(struct radix_tree *rt, uint8_t *key_begin, uint8_t *key_ereturn false; } +static bool _prefix_chain_matches (struct lookup_result *lr,uint8_t *ke) +{ +// It's possible the top node is a prefix chain, and +// the remaining key matches part of it. +if (lr->v->type ==PREFIX_CHAIN ) { +unsigned i, rlen = ke - lr->kb; +struct prefix_chain *pc = lr->v->value.ptr; +if (rlen < pc->len) { +for (i =0 ; i < rlen; i++) +if (pc->prefix[i] != lr->kb[i]) +return false; +return true; + } + } + +return false; +} +unsigned radix_tree_remove_prefix (struct radix_tree *rt,uint8_t *kb,uint8_t *ke) {unsigned count =0 ;struct lookup_result lr =_lookup_prefix (&rt->root, kb, ke); -if (lr.kb == ke) { +if (lr.kb == ke ||_prefix_chain_matches (&lr, ke)) { count =_free_node (rt, *lr.v); lr.v->type =UNSET ; }
@@ -837,7 +855,7 @@ void radix_tree_iterate(struct radix_tree *rt, uint8_t *kb, uint8_t *ke, struct radix_tree_iterator *it) {struct lookup_result lr =_lookup_prefix (&rt->root, kb, ke); -if (lr.kb == ke) +if (lr.kb == ke ||_prefix_chain_matches (&lr, ke))_iterate (lr.v, it); }
test/unit/radix_tree_t.c +56 -0
77 lines not on this page · raw message