bihash: remove unused counters
[vpp.git] / src / vppinfra / bihash_template.c
index af624fe..e13ceb7 100644 (file)
@@ -43,8 +43,6 @@ void BV (clib_bihash_init)
   h->name = (u8 *) name;
   h->nbuckets = nbuckets;
   h->log2_nbuckets = max_log2 (nbuckets);
-  h->cache_hits = 0;
-  h->cache_misses = 0;
 
   /*
    * Make sure the requested size is rational. The max table
@@ -269,8 +267,9 @@ BV (split_and_rehash_linear)
   return new_values;
 }
 
-int BV (clib_bihash_add_del)
-  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
+static inline int BV (clib_bihash_add_del_inline)
+  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
+   int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
 {
   u32 bucket_index;
   BVT (clib_bihash_bucket) * b, tmp_b;
@@ -366,6 +365,20 @@ int BV (clib_bihash_add_del)
              return (0);
            }
        }
+      /* look for stale data to overwrite */
+      if (is_stale_cb)
+       {
+         for (i = 0; i < limit; i++)
+           {
+             if (is_stale_cb (&(v->kvp[i]), arg))
+               {
+                 CLIB_MEMORY_BARRIER ();
+                 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
+                 BV (clib_bihash_unlock_bucket) (b);
+                 return (0);
+               }
+           }
+       }
       /* Out of space in this bucket, split the bucket... */
     }
   else                         /* delete case */
@@ -484,6 +497,19 @@ expand_ok:
   return (0);
 }
 
+int BV (clib_bihash_add_del)
+  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
+{
+  return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
+}
+
+int BV (clib_bihash_add_or_overwrite_stale)
+  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
+   int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
+{
+  return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
+}
+
 int BV (clib_bihash_search)
   (BVT (clib_bihash) * h,
    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
@@ -508,7 +534,7 @@ int BV (clib_bihash_search)
     {
       volatile BVT (clib_bihash_bucket) * bv = b;
       while (bv->lock)
-       ;
+       CLIB_PAUSE ();
     }
 
   hash >>= h->log2_nbuckets;
@@ -618,8 +644,6 @@ u8 *BV (format_bihash) (u8 * s, va_list * args)
     }
 
   s = format (s, "    %lld linear search buckets\n", linear_buckets);
-  s = format (s, "    %lld cache hits, %lld cache misses\n",
-             h->cache_hits, h->cache_misses);
   used_bytes = h->alloc_arena_next - h->alloc_arena;
   s = format (s,
              "    arena: base %llx, next %llx\n"
@@ -653,9 +677,16 @@ void BV (clib_bihash_foreach_key_value_pair)
                continue;
 
              (*fp) (&v->kvp[k], arg);
+             /*
+              * In case the callback deletes the last entry in the bucket...
+              */
+             if (BV (clib_bihash_bucket_is_empty) (b))
+               goto doublebreak;
            }
          v++;
        }
+    doublebreak:
+      ;
     }
 }