]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: move check_bit into prefix common code
authorStephen Hemminger <shemminger@vyatta.com>
Wed, 9 Dec 2009 11:43:17 +0000 (14:43 +0300)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 9 Dec 2009 11:43:17 +0000 (14:43 +0300)
Make one version of check prefix bit, and put it inline
with proper prototype. This gets rid of some macro's and also some
assert() that can never happen on a non-broken compiler.

* bgpd/bgp_table.c
  * CHECK_BIT(): sayonara
  * check_bit(): sayonara
  * SET_LINK(): sayonara
  * set_link(): make use of prefix_bit() instead of check_bit()
  * bgp_node_match(): idem
  * bgp_node_lookup(): idem
  * bgp_node_get(): idem
* lib/prefix.h
  * prefix_bit(): new inline version of check_bit()
* lib/table.c
  * CHECK_BIT(): sayonara
  * check_bit(): sayonara
  * SET_LINK(): sayonara
  * set_link(): make use of prefix_bit() instead of check_bit()
  * route_node_match(): idem
  * route_node_lookup(): idem
  * route_node_get(): idem
* ospf6d/ospf6_lsdb.c
  * CHECK_BIT(): sayonara
  * ospf6_lsdb_lookup_next(): make use of prefix_bit() instead of
    CHECK_BIT()
  * ospf6_lsdb_type_router_head(): idem
  * ospf6_lsdb_type_head(): idem
* ospf6d/ospf6_route.c
  * CHECK_BIT(): sayonara
  * ospf6_route_match_head() make use of prefix_bit() instead of
  * CHECK_BIT()

bgpd/bgp_table.c
lib/prefix.h
lib/table.c
ospf6d/ospf6_lsdb.c
ospf6d/ospf6_route.c

index 5b8c6a490b07bef4e4ab811b4ecc218c1606ecfd..91cab60671937c07465a5ef0e54b95a291c158b1 100644 (file)
@@ -201,37 +201,10 @@ route_common (struct prefix *n, struct prefix *p, struct prefix *new)
     }
 }
 
-/* Macro version of check_bit (). */
-#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
-
-/* Check bit of the prefix. */
-static int
-check_bit (u_char *prefix, u_char prefixlen)
-{
-  int offset;
-  int shift;
-  u_char *p = (u_char *)prefix;
-
-  assert (prefixlen <= 128);
-
-  offset = prefixlen / 8;
-  shift = 7 - (prefixlen % 8);
-  
-  return (p[offset] >> shift & 1);
-}
-
-/* Macro version of set_link (). */
-#define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
-                      (Y)->parent = (X)
-
 static void
 set_link (struct bgp_node *node, struct bgp_node *new)
 {
-  int bit;
-    
-  bit = check_bit (&new->p.u.prefix, node->p.prefixlen);
-
-  assert (bit == 0 || bit == 1);
+  unsigned int bit = prefix_bit (&new->p.u.prefix, node->p.prefixlen);
 
   node->link[bit] = new;
   new->parent = node;
@@ -273,7 +246,7 @@ bgp_node_match (const struct bgp_table *table, struct prefix *p)
     {
       if (node->info)
        matched = node;
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   /* If matched route found, return it. */
@@ -325,7 +298,7 @@ bgp_node_lookup (const struct bgp_table *table, struct prefix *p)
       if (node->p.prefixlen == p->prefixlen && node->info)
        return bgp_lock_node (node);
 
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   return NULL;
@@ -350,7 +323,7 @@ bgp_node_get (struct bgp_table *const table, struct prefix *p)
          return node;
        }
       match = node;
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   if (node == NULL)
index 9cfc15563e844c89bc53efbd68f03d385bb82399..d37072098b4f3c832e16020a0c5278ad538b9549 100644 (file)
@@ -127,6 +127,16 @@ struct prefix_rd
 /* Prefix's family member. */
 #define PREFIX_FAMILY(p)  ((p)->family)
 
+/* Check bit of the prefix. */
+static inline unsigned int
+prefix_bit (const u_char *prefix, const u_char prefixlen)
+{
+  unsigned int offset = prefixlen / 8;
+  unsigned int shift  = 7 - (prefixlen % 8);
+
+  return (prefix[offset] >> shift) & 1;
+}
+
 /* Prototypes. */
 extern int afi2family (int);
 extern int family2afi (int);
index 06c6453927dc7b14013d8e6827933a2090bb2cc7..04df3af5793ccc8094af4d64f5b9e3497767c4c4 100644 (file)
@@ -165,37 +165,10 @@ route_common (struct prefix *n, struct prefix *p, struct prefix *new)
     }
 }
 
-/* Macro version of check_bit (). */
-#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
-
-/* Check bit of the prefix. */
-static int
-check_bit (const u_char *prefix, u_char prefixlen)
-{
-  unsigned int offset;
-  unsigned int shift;
-  const u_char *p = prefix;
-
-  assert (prefixlen <= 128);
-
-  offset = prefixlen / 8;
-  shift = 7 - (prefixlen % 8);
-  
-  return (p[offset] >> shift & 1);
-}
-
-/* Macro version of set_link (). */
-#define SET_LINK(X,Y) do { (X)->link[CHECK_BIT(&(Y)->p.u.prefix,(X)->p.prefixlen)] = (Y);\
-                      (Y)->parent = (X); } while (0)
-
 static void
 set_link (struct route_node *node, struct route_node *new)
 {
-  int bit;
-    
-  bit = check_bit (&new->p.u.prefix, node->p.prefixlen);
-
-  assert (bit == 0 || bit == 1);
+  unsigned int bit = prefix_bit (&new->p.u.prefix, node->p.prefixlen);
 
   node->link[bit] = new;
   new->parent = node;
@@ -236,7 +209,7 @@ route_node_match (const struct route_table *table, const struct prefix *p)
     {
       if (node->info)
        matched = node;
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   /* If matched route found, return it. */
@@ -290,7 +263,7 @@ route_node_lookup (struct route_table *table, struct prefix *p)
       if (node->p.prefixlen == p->prefixlen && node->info)
        return route_lock_node (node);
 
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   return NULL;
@@ -315,7 +288,7 @@ route_node_get (struct route_table *table, struct prefix *p)
          return node;
        }
       match = node;
-      node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   if (node == NULL)
index ea387e3d050a4d8bf2c144be5d4161c3a5c283b4..d2e3e04c486515d5909547adccb445b7a92bf7ba 100644 (file)
@@ -258,9 +258,6 @@ ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router,
   return (struct ospf6_lsa *) node->info;
 }
 
-/* Macro version of check_bit (). */
-#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
-
 struct ospf6_lsa *
 ospf6_lsdb_lookup_next (u_int16_t type, u_int32_t id, u_int32_t adv_router,
                         struct ospf6_lsdb *lsdb)
@@ -291,7 +288,7 @@ ospf6_lsdb_lookup_next (u_int16_t type, u_int32_t id, u_int32_t adv_router,
          prefix_match (&node->p, p))
     {
       matched = node;
-      node = node->link[CHECK_BIT(&p->u.prefix, node->p.prefixlen)];
+      node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
     }
 
   if (matched)
@@ -384,7 +381,7 @@ ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router,
   /* Walk down tree. */
   while (node && node->p.prefixlen <= key.prefixlen &&
         prefix_match (&node->p, (struct prefix *) &key))
-    node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
+    node = node->link[prefix_bit(&key.prefix, node->p.prefixlen)];
 
   if (node)
     route_lock_node (node);
@@ -438,7 +435,7 @@ ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb)
   node = lsdb->table->top;
   while (node && node->p.prefixlen <= key.prefixlen &&
         prefix_match (&node->p, (struct prefix *) &key))
-    node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
+    node = node->link[prefix_bit(&key.prefix, node->p.prefixlen)];
 
   if (node)
     route_lock_node (node);
index 9832f5702515fe6611c10bd4bc16be7dfaa9ef11..1e1f4fb5aae0080e788416ed306f071c10aaad7a 100644 (file)
@@ -706,9 +706,6 @@ ospf6_route_best_next (struct ospf6_route *route)
   return next;
 }
 
-/* Macro version of check_bit (). */
-#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
-
 struct ospf6_route *
 ospf6_route_match_head (struct prefix *prefix,
                         struct ospf6_route_table *table)
@@ -720,7 +717,7 @@ ospf6_route_match_head (struct prefix *prefix,
   node = table->table->top;
   while (node && node->p.prefixlen < prefix->prefixlen &&
         prefix_match (&node->p, prefix))
-    node = node->link[CHECK_BIT(&prefix->u.prefix, node->p.prefixlen)];
+    node = node->link[prefix_bit(&prefix->u.prefix, node->p.prefixlen)];
 
   if (node)
     route_lock_node (node);