diff --git a/radix/radix.c b/radix/radix.c index 950c50b..b77be73 100644 --- a/radix/radix.c +++ b/radix/radix.c @@ -13,7 +13,6 @@ radix_holder_t create_holder() { static radix_node_t create_node(record_t record) { radix_node_t ret = malloc(sizeof(struct radix_node)); - record->_valid = VALID_RULE; memcpy(&ret->_data, record, sizeof(struct routing_record)); ret->_l = ret->_r = NULL; return ret; @@ -29,7 +28,7 @@ static void split_node(radix_node_t n, unsigned bit) { radix_node_t tmp = clone_node(n); n->_data._mask = bit; n->_l = n->_r = tmp; - n->_data._valid = INVALID_RULE; + n->_data._rule = INVALID_RULE; } int destory_holder(struct radix_holder* t) { @@ -59,8 +58,6 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { node = t->_root; next = &t->_root; - r->_valid = INVALID_RULE; - if (!node) { t->_root = create_node(r); return t->_size = 1; @@ -99,7 +96,6 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { next = b ? &node->_r : &node->_l; *next = NULL; - r->_valid = VALID_RULE; memcpy(&node->_data, r, sizeof(struct routing_record)); ++t->_size; return 1; @@ -107,10 +103,9 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { /* exact match */ if (node->_data._mask == r->_mask && ibit == r->_mask) { - if (node->_data._valid) + if (node->_data._rule != INVALID_RULE) return 0; /* make valid */ - node->_data._valid = VALID_RULE; node->_data._rule = r->_rule; ++t->_size; return 1; @@ -134,21 +129,25 @@ int radix_search(radix_holder_t t, record_t r) { return r->_rule = INVALID_RULE; iter = t->_root; - ret = r->_rule = iter->_data._valid ? iter->_data._rule : INVALID_RULE; - for (ibit = 0;ibit < 128; ++ibit, SHIFT_MASK(mask)) { + for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) { b = GET_BIT(r->_ip, mask); - if (ibit < iter->_data._mask && + if (ibit < iter->_data._mask && /* bit match */ b == GET_BIT(iter->_data._ip, mask)) continue; - if (ibit >= iter->_data._mask) /* match upto node mask */ - ret = r->_rule = iter->_data._valid ? iter->_data._rule : ret; + if (ibit < iter->_data._mask) { /* bit mismatch */ + return r->_rule = INVALID_RULE; + } - iter = b ? iter->_l : iter->_r; + /* mask ends with full match */ + ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE; + iter = b ? iter->_l : iter->_r; if (!iter) return ret; } + + ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE; return ret; } @@ -216,7 +215,6 @@ int parse_line(FILE* f, record_t r) { r->_ip[0] = key[0]; r->_ip[1] = key[1]; - r->_valid = VALID_RULE; return 1; } @@ -267,7 +265,7 @@ int fire_tests(radix_holder_t t, struct routing_record* r) { for (row = 0; row < TEST_ROWS; ++row, ++r) { ref_pop = r->_rule; radix_search(t, r); - if ( !r->_valid || ref_pop != r->_rule) { + if ( ref_pop != r->_rule) { printf("ref: %d got: %d @ %d\n", ref_pop, r->_rule, row ); ++wrong; } diff --git a/radix/radix.h b/radix/radix.h index 505e59a..89e4529 100644 --- a/radix/radix.h +++ b/radix/radix.h @@ -5,15 +5,12 @@ #include "stdlib.h" #include "string.h" -#define VALID_RULE 1 -#define INVALID_RULE 0 -#define RULE_NOT_FOUND -1 +#define INVALID_RULE 0 struct routing_record { unsigned long _ip[2]; unsigned short _rule; - unsigned _mask:7; - unsigned _valid:1; + unsigned _mask:8; }; struct radix_node { @@ -32,12 +29,12 @@ typedef struct radix_holder* radix_holder_t; typedef struct routing_record* record_t; radix_holder_t create_holder(); -radix_node_t create_node(record_t record); +static radix_node_t create_node(record_t record); static radix_node_t clone_node(radix_node_t n); static void split_node(struct radix_node* n, unsigned bit); int destory_holder(struct radix_holder*); -int destory_node(struct radix_node*); +static int destory_node(struct radix_node*); int radix_insert(radix_holder_t, record_t); int radix_search(radix_holder_t, record_t);