git: b02d6a6b5a07 - main - pctrie: reduce code duplication in PCTRIE_INSERT_*
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 28 Oct 2024 21:43:01 UTC
The branch main has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=b02d6a6b5a0753ea8be53d6bd6d4f39f046c54f4 commit b02d6a6b5a0753ea8be53d6bd6d4f39f046c54f4 Author: Doug Moore <dougm@FreeBSD.org> AuthorDate: 2024-10-28 19:00:00 +0000 Commit: Doug Moore <dougm@FreeBSD.org> CommitDate: 2024-10-28 21:42:47 +0000 pctrie: reduce code duplication in PCTRIE_INSERT_* The four flavors of PCTRIE_INSERT inline functions share a few lines of code. Extract that code into a new function and invoke it from the others to reduce code duplication. No behavior change expected. Reviewed by: bnovkov Differential Revision: https://reviews.freebsd.org/D47288 --- sys/sys/pctrie.h | 85 +++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/sys/sys/pctrie.h b/sys/sys/pctrie.h index 209380d805b2..54b7fb401202 100644 --- a/sys/sys/pctrie.h +++ b/sys/sys/pctrie.h @@ -86,69 +86,67 @@ name##_PCTRIE_PTR2VAL(struct type *ptr) \ } \ \ static __inline __unused int \ -name##_PCTRIE_INSERT(struct pctrie *ptree, struct type *ptr) \ +name##_PCTRIE_INSERT_BASE(struct pctrie *ptree, void *parentp, \ + uint64_t *val, uint64_t *found, struct type **found_out) \ { \ struct pctrie_node *parent; \ + \ + if (__predict_false(found != NULL)) { \ + *found_out = name##_PCTRIE_VAL2PTR(found); \ + return (EEXIST); \ + } \ + if (parentp != NULL) { \ + parent = allocfn(ptree); \ + if (__predict_false(parent == NULL)) { \ + if (found_out != NULL) \ + *found_out = NULL; \ + return (ENOMEM); \ + } \ + pctrie_insert_node(parentp, parent, val); \ + } \ + return (0); \ +} \ + \ +static __inline __unused int \ +name##_PCTRIE_INSERT(struct pctrie *ptree, struct type *ptr) \ +{ \ void *parentp; \ uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ \ parentp = pctrie_insert_lookup_strict(ptree, val); \ - if (parentp == NULL) \ - return (0); \ - parent = allocfn(ptree); \ - if (__predict_false(parent == NULL)) \ - return (ENOMEM); \ - pctrie_insert_node(parentp, parent, val); \ - return (0); \ + return (name##_PCTRIE_INSERT_BASE(ptree, parentp, val, \ + NULL, NULL)); \ } \ \ static __inline __unused int \ name##_PCTRIE_FIND_OR_INSERT(struct pctrie *ptree, struct type *ptr, \ struct type **found_out_opt) \ { \ - struct pctrie_node *parent; \ void *parentp; \ uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ uint64_t *found; \ \ parentp = pctrie_insert_lookup(ptree, val, &found); \ - if (found != NULL) { \ - if (found_out_opt != NULL) \ - *found_out_opt = name##_PCTRIE_VAL2PTR(found); \ - return (EEXIST); \ - } \ - if (parentp == NULL) \ - return (0); \ - parent = allocfn(ptree); \ - if (__predict_false(parent == NULL)) \ - return (ENOMEM); \ - pctrie_insert_node(parentp, parent, val); \ - return (0); \ + return (name##_PCTRIE_INSERT_BASE(ptree, parentp, val, \ + found, found_out_opt)); \ } \ \ static __inline __unused int \ name##_PCTRIE_INSERT_LOOKUP_GE(struct pctrie *ptree, struct type *ptr, \ struct type **found_out) \ { \ - struct pctrie_node *parent, *neighbor; \ + struct pctrie_node *neighbor; \ void *parentp; \ uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ uint64_t *found; \ + int retval; \ \ parentp = pctrie_insert_lookup_gt(ptree, val, &found, \ &neighbor); \ - if (__predict_false(found != NULL)) { \ - *found_out = name##_PCTRIE_VAL2PTR(found); \ - return (EEXIST); \ - } \ - if (parentp != NULL) { \ - parent = allocfn(ptree); \ - if (__predict_false(parent == NULL)) { \ - *found_out = NULL; \ - return (ENOMEM); \ - } \ - pctrie_insert_node(parentp, parent, val); \ - } \ + retval = name##_PCTRIE_INSERT_BASE(ptree, parentp, val, \ + found, found_out); \ + if (retval != 0) \ + return (retval); \ found = pctrie_subtree_lookup_gt(neighbor, *val); \ *found_out = name##_PCTRIE_VAL2PTR(found); \ pctrie_subtree_lookup_gt_assert(neighbor, *val, ptree, found); \ @@ -159,25 +157,18 @@ static __inline __unused int \ name##_PCTRIE_INSERT_LOOKUP_LE(struct pctrie *ptree, struct type *ptr, \ struct type **found_out) \ { \ - struct pctrie_node *parent, *neighbor; \ + struct pctrie_node *neighbor; \ void *parentp; \ uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ uint64_t *found; \ + int retval; \ \ parentp = pctrie_insert_lookup_lt(ptree, val, &found, \ &neighbor); \ - if (__predict_false(found != NULL)) { \ - *found_out = name##_PCTRIE_VAL2PTR(found); \ - return (EEXIST); \ - } \ - if (parentp != NULL) { \ - parent = allocfn(ptree); \ - if (__predict_false(parent == NULL)) { \ - *found_out = NULL; \ - return (ENOMEM); \ - } \ - pctrie_insert_node(parentp, parent, val); \ - } \ + retval = name##_PCTRIE_INSERT_BASE(ptree, parentp, val, \ + found, found_out); \ + if (retval != 0) \ + return (retval); \ found = pctrie_subtree_lookup_lt(neighbor, *val); \ *found_out = name##_PCTRIE_VAL2PTR(found); \ pctrie_subtree_lookup_lt_assert(neighbor, *val, ptree, found); \