23 nbuckets = 1 << (
max_log2 (nbuckets));
25 h->name = (
u8 *) name;
26 h->nbuckets = nbuckets;
27 h->log2_nbuckets =
max_log2 (nbuckets);
42 memset (h, 0,
sizeof (*h));
52 ASSERT (h->writer_lock[0]);
53 if (log2_pages >=
vec_len (h->freelists) || h->freelists[log2_pages] == 0)
73 memset (rv, 0xff,
sizeof (*rv) * (1 << log2_pages));
82 ASSERT (h->writer_lock[0]);
101 if (cpu_number >=
vec_len (h->working_copies))
113 working_copy = h->working_copies[cpu_number];
115 h->saved_bucket.as_u64 = b->as_u64;
118 if ((1 << b->log2_pages) >
vec_len (working_copy))
122 h->working_copies[cpu_number] = working_copy;
125 _vec_len (working_copy) = 1 << b->log2_pages;
130 clib_memcpy (working_copy, v,
sizeof (*v) * (1 << b->log2_pages));
131 working_bucket.as_u64 = b->as_u64;
134 b->as_u64 = working_bucket.as_u64;
135 h->working_copies[cpu_number] = working_copy;
141 (
BVT (clib_bihash) * h,
147 new_values = BV (value_alloc) (h, new_log2_pages);
150 for (i = 0; i <
vec_len (old_values); i++)
156 if (BV (clib_bihash_is_free) (&(v->kvp[j])) == 0)
158 new_hash = BV (clib_bihash_hash) (&(v->kvp[j]));
159 new_hash >>= h->log2_nbuckets;
160 new_hash &= (1 << new_log2_pages) - 1;
162 new_v = &new_values[new_hash];
166 if (BV (clib_bihash_is_free) (&(new_v->kvp[k])))
169 sizeof (new_v->kvp[k]));
174 BV (value_free) (h, new_values);
186 (
BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * add_v,
int is_add)
198 hash = BV (clib_bihash_hash) (add_v);
200 bucket_index = hash & (h->nbuckets - 1);
201 b = &h->buckets[bucket_index];
203 hash >>= h->log2_nbuckets;
205 while (__sync_lock_test_and_set (h->writer_lock, 1))
217 v = BV (value_alloc) (h, 0);
222 b->as_u64 = tmp_b.as_u64;
229 value_index = hash & ((1 << h->saved_bucket.log2_pages) - 1);
240 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
242 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
245 b->as_u64 = h->saved_bucket.as_u64;
251 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
253 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
255 b->as_u64 = h->saved_bucket.as_u64;
265 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
267 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
269 b->as_u64 = h->saved_bucket.as_u64;
274 b->as_u64 = h->saved_bucket.as_u64;
278 new_log2_pages = h->saved_bucket.log2_pages + 1;
281 working_copy = h->working_copies[cpu_number];
291 new_hash = BV (clib_bihash_hash) (add_v);
292 new_hash >>= h->log2_nbuckets;
298 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
300 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
306 BV (value_free) (h, save_new_v);
313 b->as_u64 = tmp_b.as_u64;
315 BV (value_free) (h,
v);
319 h->writer_lock[0] = 0;
324 (
const BVT (clib_bihash) * h,
325 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
336 hash = BV (clib_bihash_hash) (search_key);
338 bucket_index = hash & (h->nbuckets - 1);
339 b = &h->buckets[bucket_index];
344 hash >>= h->log2_nbuckets;
347 value_index = hash & ((1 << b->log2_pages) - 1);
352 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
361 u8 *BV (format_bihash) (
u8 * s, va_list * args)
363 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
364 int verbose = va_arg (*args,
int);
368 u64 active_elements = 0;
370 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
372 for (i = 0; i < h->nbuckets; i++)
378 s =
format (s,
"[%d]: empty\n", i);
384 s =
format (s,
"[%d]: heap offset %d, len %d\n", i,
385 b->offset, (1 << b->log2_pages));
389 for (j = 0; j < (1 << b->log2_pages); j++)
393 if (BV (clib_bihash_is_free) (&v->kvp[k]))
396 s =
format (s,
" %d: empty\n",
397 j * BIHASH_KVP_PER_PAGE + k);
402 s =
format (s,
" %d: %U\n",
403 j * BIHASH_KVP_PER_PAGE + k,
404 BV (format_bihash_kvp), &(v->kvp[k]));
412 s =
format (s,
" %lld active elements\n", active_elements);
419 (
BVT (clib_bihash) * h,
void *callback,
void *arg)
424 void (*fp) (
BVT (clib_bihash_kv) *,
void *) = callback;
426 for (i = 0; i < h->nbuckets; i++)
433 for (j = 0; j < (1 << b->log2_pages); j++)
437 if (BV (clib_bihash_is_free) (&v->kvp[k]))
440 (*fp) (&v->kvp[k], arg);
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
sll srl srl sll sra u16x4 i
void clib_bihash_free(clib_bihash *h)
Destroy a bounded index extensible hash table.
void * mheap_alloc(void *memory, uword size)
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
static uword min_log2(uword x)
int clib_bihash_add_del(clib_bihash *h, clib_bihash_kv *add_v, int is_add)
Add or delete a (key,value) pair from a bi-hash table.
static uword clib_bihash_get_offset(clib_bihash *h, void *v)
Get clib mheap offset given a pointer.
#define BIHASH_KVP_PER_PAGE
uword os_get_cpu_number(void)
void clib_bihash_init(clib_bihash *h, char *name, u32 nbuckets, uword memory_size)
initialize a bounded index extensible hash table
void clib_bihash_foreach_key_value_pair(clib_bihash *h, void *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
static void * clib_mem_set_heap(void *heap)
#define clib_memcpy(a, b, c)
static void make_working_copy(vnet_classify_table_t *t, vnet_classify_bucket_t *b)
int clib_bihash_search(clib_bihash *h, clib_bihash_kv *search_v, clib_bihash_kv *return_v)
Search a bi-hash table.
template key/value backing page structure
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword max_log2(uword x)
static void * clib_mem_alloc_aligned(uword size, uword align)
#define CLIB_MEMORY_BARRIER()
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
#define CLIB_CACHE_LINE_BYTES
static vnet_classify_entry_t * split_and_rehash(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 new_log2_pages)