1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-21 07:15:49 +00:00

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
This commit is contained in:
Doug Moore 2024-10-28 14:00:00 -05:00
parent a5ad360ff9
commit b02d6a6b5a

View File

@ -86,57 +86,11 @@ 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; \
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); \
} \
\
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); \
} \
\
static __inline __unused int \
name##_PCTRIE_INSERT_LOOKUP_GE(struct pctrie *ptree, struct type *ptr, \
struct type **found_out) \
{ \
struct pctrie_node *parent, *neighbor; \
void *parentp; \
uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
uint64_t *found; \
\
parentp = pctrie_insert_lookup_gt(ptree, val, &found, \
&neighbor); \
if (__predict_false(found != NULL)) { \
*found_out = name##_PCTRIE_VAL2PTR(found); \
return (EEXIST); \
@ -144,11 +98,55 @@ name##_PCTRIE_INSERT_LOOKUP_GE(struct pctrie *ptree, struct type *ptr, \
if (parentp != NULL) { \
parent = allocfn(ptree); \
if (__predict_false(parent == NULL)) { \
*found_out = 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); \
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) \
{ \
void *parentp; \
uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
uint64_t *found; \
\
parentp = pctrie_insert_lookup(ptree, val, &found); \
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 *neighbor; \
void *parentp; \
uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
uint64_t *found; \
int retval; \
\
parentp = pctrie_insert_lookup_gt(ptree, val, &found, \
&neighbor); \
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); \