aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Moore <dougm@FreeBSD.org>2024-10-28 19:00:00 +0000
committerDoug Moore <dougm@FreeBSD.org>2024-10-28 21:42:47 +0000
commitb02d6a6b5a0753ea8be53d6bd6d4f39f046c54f4 (patch)
tree234cb8a9048331af600a9bca68d855d8913b454f
parenta5ad360ff9b7fb2ef3f7f31b8c29c332026b0e01 (diff)
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
-rw-r--r--sys/sys/pctrie.h85
1 files 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); \