Commit 3006ccb7 authored by Evan Hunt's avatar Evan Hunt
Browse files

improve readability of radix code

- use RADIX_V4 and RADIX_V6 instead of 0 and 1 as array indices
- remove some unused macros
parent e7b2b097
Pipeline #1961 passed with stages
in 10 minutes and 48 seconds
......@@ -219,12 +219,13 @@ dns_acl_match(const isc_netaddr_t *reqaddr,
/* Found a match. */
if (result == ISC_R_SUCCESS && node != NULL) {
int off = ISC_RADIX_OFF(&pfx);
match_num = node->node_num[off];
if (*(isc_boolean_t *) node->data[off])
int fam = ISC_RADIX_FAMILY(&pfx);
match_num = node->node_num[fam];
if (*(isc_boolean_t *) node->data[fam]) {
*match = match_num;
else
} else {
*match = -match_num;
}
}
isc_refcount_destroy(&pfx.refcount);
......
......@@ -78,16 +78,16 @@ dns_iptable_addprefix(dns_iptable_t *tab, const isc_netaddr_t *addr,
if (pfx.family == AF_UNSPEC) {
/* "any" or "none" */
INSIST(pfx.bitlen == 0);
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (node->data[i] == NULL)
node->data[i] = pos ? &dns_iptable_pos
: &dns_iptable_neg;
}
} else {
/* any other prefix */
int offset = ISC_RADIX_OFF(&pfx);
if (node->data[offset] == NULL) {
node->data[offset] = pos ? &dns_iptable_pos
int fam = ISC_RADIX_FAMILY(&pfx);
if (node->data[fam] == NULL) {
node->data[fam] = pos ? &dns_iptable_pos
: &dns_iptable_neg;
}
}
......@@ -121,7 +121,7 @@ dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, isc_boolean_t pos)
* could be a security risk. To prevent this, we
* just leave the negative nodes negative.
*/
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (!pos) {
if (node->data[i] &&
*(isc_boolean_t *) node->data[i])
......
......@@ -9,13 +9,6 @@
* information regarding copyright ownership.
*/
/*
* This source was adapted from MRT's RCS Ids:
* Id: radix.h,v 1.6 1999/08/03 03:32:53 masaki Exp
* Id: mrt.h,v 1.57.2.6 1999/12/28 23:41:27 labovit Exp
* Id: defs.h,v 1.5.2.2 2000/01/15 14:19:16 masaki Exp
*/
#include <isc/magic.h>
#include <isc/types.h>
#include <isc/mutex.h>
......@@ -85,7 +78,11 @@ typedef void (*isc_radix_processfunc_t)(isc_prefix_t *, void **);
* but matches all IPv6 addresses too.
*/
#define ISC_RADIX_OFF(p) (((p)->family == AF_INET6) ? 1 : 0)
#define RADIX_V4 0
#define RADIX_V6 1
#define RADIX_FAMILIES 2
#define ISC_RADIX_FAMILY(p) (((p)->family == AF_INET6) ? RADIX_V6 : RADIX_V4)
typedef struct isc_radix_node {
isc_mem_t *mctx;
......@@ -93,8 +90,8 @@ typedef struct isc_radix_node {
isc_prefix_t *prefix; /* who we are in radix tree */
struct isc_radix_node *l, *r; /* left and right children */
struct isc_radix_node *parent; /* may be used */
void *data[2]; /* pointers to IPv4 and IPV6 data */
int node_num[2]; /* which node this was in the tree,
void *data[RADIX_FAMILIES]; /* pointers to IPv4 and IPV6 data */
int node_num[RADIX_FAMILIES]; /* which node this was in the tree,
or -1 for glue nodes */
} isc_radix_node_t;
......@@ -193,9 +190,6 @@ isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
#define RADIX_NBIT(x) (0x80 >> ((x) & 0x7f))
#define RADIX_NBYTE(x) ((x) >> 3)
#define RADIX_DATA_GET(node, type) (type *)((node)->data)
#define RADIX_DATA_SET(node, value) ((node)->data = (void *)(value))
#define RADIX_WALK(Xhead, Xnode) \
do { \
isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
......@@ -204,22 +198,6 @@ isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
while ((Xnode = Xrn)) { \
if (Xnode->prefix)
#define RADIX_WALK_ALL(Xhead, Xnode) \
do { \
isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
isc_radix_node_t **Xsp = Xstack; \
isc_radix_node_t *Xrn = (Xhead); \
while ((Xnode = Xrn)) { \
if (1)
#define RADIX_WALK_BREAK { \
if (Xsp != Xstack) { \
Xrn = *(--Xsp); \
} else { \
Xrn = (radix_node_t *) 0; \
} \
continue; }
#define RADIX_WALK_END \
if (Xrn->l) { \
if (Xrn->r) { \
......
......@@ -178,8 +178,8 @@ _clear_radix(isc_radix_tree_t *radix, isc_radix_destroyfunc_t func) {
if (func != NULL)
func(Xrn->data);
} else {
INSIST(Xrn->data[0] == NULL &&
Xrn->data[1] == NULL);
INSIST(Xrn->data[RADIX_V4] == NULL &&
Xrn->data[RADIX_V6] == NULL);
}
isc_mem_put(radix->mctx, Xrn, sizeof(*Xrn));
......@@ -234,7 +234,7 @@ isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
isc_radix_node_t *stack[RADIX_MAXBITS + 1];
u_char *addr;
isc_uint32_t bitlen;
int toff = -1, cnt = 0;
int tfam = -1, cnt = 0;
REQUIRE(radix != NULL);
REQUIRE(prefix != NULL);
......@@ -277,13 +277,13 @@ isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
isc_prefix_tochar(prefix),
node->prefix->bitlen))
{
int off = ISC_RADIX_OFF(prefix);
if (node->node_num[off] != -1 &&
int fam = ISC_RADIX_FAMILY(prefix);
if (node->node_num[fam] != -1 &&
((*target == NULL) ||
(*target)->node_num[toff] > node->node_num[off]))
(*target)->node_num[tfam] > node->node_num[fam]))
{
*target = node;
toff = off;
tfam = fam;
}
}
}
......@@ -323,7 +323,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (node == NULL)
return (ISC_R_NOMEMORY);
node->bit = bitlen;
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = -1;
}
node->prefix = NULL;
......@@ -344,22 +344,23 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
* added to num_added_node at the end of
* the merge operation--we don't do it here.
*/
for (i = 0; i < 2; i++) {
if (source->node_num[i] != -1)
for (i = 0; i < RADIX_FAMILIES; i++) {
if (source->node_num[i] != -1) {
node->node_num[i] =
radix->num_added_node +
source->node_num[i];
}
node->data[i] = source->data[i];
}
} else {
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = next;
}
} else {
node->node_num[ISC_RADIX_OFF(prefix)] = next;
node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
memset(node->data, 0, sizeof(node->data));
......@@ -395,14 +396,14 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
/* Find the first bit different. */
check_bit = (node->bit < bitlen) ? node->bit : bitlen;
differ_bit = 0;
for (i = 0; i*8 < check_bit; i++) {
for (i = 0; i * 8 < check_bit; i++) {
if ((r = (addr[i] ^ test_addr[i])) == 0) {
differ_bit = (i + 1) * 8;
continue;
}
/* I know the better way, but for now. */
for (j = 0; j < 8; j++) {
if (BIT_TEST (r, (0x80 >> j)))
if (BIT_TEST(r, (0x80 >> j)))
break;
}
/* Must be found. */
......@@ -425,7 +426,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
/* Set node_num only if it hasn't been set before */
if (source != NULL) {
/* Merging nodes */
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (node->node_num[i] == -1 &&
source->node_num[i] != -1)
{
......@@ -439,7 +440,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (fam == AF_UNSPEC) {
/* "any" or "none" */
int next = radix->num_added_node + 1;
for (i = 0; i < 2; i++)
for (i = 0; i < RADIX_FAMILIES; i++)
{
if (node->node_num[i] == -1) {
node->node_num[i] =
......@@ -449,10 +450,11 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
}
}
} else {
int off = ISC_RADIX_OFF(prefix);
if (node->node_num[off] == -1)
node->node_num[off] =
int foff = ISC_RADIX_FAMILY(prefix);
if (node->node_num[foff] == -1) {
node->node_num[foff] =
++radix->num_added_node;
}
}
}
*target = node;
......@@ -463,11 +465,13 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (result != ISC_R_SUCCESS)
return (result);
}
INSIST(node->data[0] == NULL && node->node_num[0] == -1 &&
node->data[1] == NULL && node->node_num[1] == -1);
INSIST(node->data[RADIX_V4] == NULL &&
node->node_num[RADIX_V4] == -1 &&
node->data[RADIX_V4] == NULL &&
node->node_num[RADIX_V4] == -1);
if (source != NULL) {
/* Merging node */
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
int cur = radix->num_added_node;
if (source->node_num[i] != -1) {
node->node_num[i] =
......@@ -479,11 +483,11 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = next;
}
} else {
node->node_num[ISC_RADIX_OFF(prefix)] = next;
node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
}
*target = node;
......@@ -513,7 +517,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
}
new_node->parent = NULL;
new_node->l = new_node->r = NULL;
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
new_node->node_num[i] = -1;
new_node->data[i] = NULL;
}
......@@ -521,7 +525,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (source != NULL) {
/* Merging node */
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
int cur = radix->num_added_node;
if (source->node_num[i] != -1) {
new_node->node_num[i] =
......@@ -533,10 +537,10 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 2; i++)
for (i = 0; i < RADIX_FAMILIES; i++)
new_node->node_num[i] = next;
} else {
new_node->node_num[ISC_RADIX_OFF(prefix)] = next;
new_node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
memset(new_node->data, 0, sizeof(new_node->data));
}
......@@ -580,7 +584,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
glue->bit = differ_bit;
glue->prefix = NULL;
glue->parent = node->parent;
for (i = 0; i < 2; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
glue->data[i] = NULL;
glue->node_num[i] = -1;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment