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]);
73 memset (rv, 0xff,
sizeof (*rv) * (1 <<
log2_pages));
81 ASSERT (h->writer_lock[0]);
97 int log2_working_copy_length;
99 if (thread_index >=
vec_len (h->working_copies))
112 working_copy = h->working_copies[thread_index];
113 log2_working_copy_length = h->working_copy_lengths[thread_index];
115 h->saved_bucket.as_u64 = b->as_u64;
118 if (b->log2_pages > log2_working_copy_length)
124 (
sizeof (working_copy[0]) * (1 << b->log2_pages),
126 h->working_copy_lengths[thread_index] = b->log2_pages;
127 h->working_copies[thread_index] = working_copy;
134 clib_memcpy (working_copy, v,
sizeof (*v) * (1 << b->log2_pages));
135 working_bucket.as_u64 = b->as_u64;
138 b->as_u64 = working_bucket.as_u64;
139 h->working_copies[thread_index] = working_copy;
145 (
BVT (clib_bihash) * h,
150 int i, j, length_in_kvs;
152 new_values = BV (value_alloc) (h, new_log2_pages);
153 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
155 for (i = 0; i < length_in_kvs; i++)
160 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
164 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
165 new_hash >>= h->log2_nbuckets;
166 new_hash &= (1 << new_log2_pages) - 1;
167 new_v = &new_values[new_hash];
173 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
175 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
176 sizeof (new_v->kvp[j]));
181 BV (value_free) (h, new_values, new_log2_pages);
192 (
BVT (clib_bihash) * h,
197 int i, j, new_length, old_length;
199 new_values = BV (value_alloc) (h, new_log2_pages);
200 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
201 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
205 for (i = 0; i < old_length; i++)
208 for (; j < new_length; j++)
211 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
215 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
218 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
219 sizeof (new_values->kvp[j]));
226 BV (value_free) (h, new_values, new_log2_pages);
235 (
BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * add_v,
int is_add)
243 u32 new_log2_pages, old_log2_pages;
245 int mark_bucket_linear;
248 hash = BV (clib_bihash_hash) (add_v);
250 bucket_index = hash & (h->nbuckets - 1);
251 b = &h->buckets[bucket_index];
253 hash >>= h->log2_nbuckets;
255 tmp_b.linear_search = 0;
257 while (__sync_lock_test_and_set (h->writer_lock, 1))
269 v = BV (value_alloc) (h, 0);
275 b->as_u64 = tmp_b.as_u64;
284 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
285 if (b->linear_search)
286 limit <<= b->log2_pages;
294 for (i = 0; i < limit; i++)
296 if (!memcmp (&(v->kvp[i]), &add_v->key,
sizeof (add_v->key)))
298 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
301 b->as_u64 = h->saved_bucket.as_u64;
305 for (i = 0; i < limit; i++)
307 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
309 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
311 b->as_u64 = h->saved_bucket.as_u64;
319 for (i = 0; i < limit; i++)
321 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
323 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
325 b->as_u64 = h->saved_bucket.as_u64;
330 b->as_u64 = h->saved_bucket.as_u64;
334 old_log2_pages = h->saved_bucket.log2_pages;
335 new_log2_pages = old_log2_pages + 1;
336 mark_bucket_linear = 0;
338 working_copy = h->working_copies[thread_index];
359 mark_bucket_linear = 1;
365 new_hash = BV (clib_bihash_hash) (add_v);
367 if (mark_bucket_linear)
368 limit <<= new_log2_pages;
369 new_hash >>= h->log2_nbuckets;
370 new_hash &= (1 << new_log2_pages) - 1;
371 new_v += mark_bucket_linear ? 0 : new_hash;
373 for (i = 0; i < limit; i++)
375 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
377 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
383 BV (value_free) (h, save_new_v, new_log2_pages);
395 if (tmp_b.linear_search ^ mark_bucket_linear)
396 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
398 tmp_b.log2_pages = new_log2_pages;
400 tmp_b.linear_search = mark_bucket_linear;
403 b->as_u64 = tmp_b.as_u64;
405 BV (value_free) (h,
v, old_log2_pages);
409 h->writer_lock[0] = 0;
414 (
const BVT (clib_bihash) * h,
415 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
425 hash = BV (clib_bihash_hash) (search_key);
427 bucket_index = hash & (h->nbuckets - 1);
428 b = &h->buckets[bucket_index];
433 hash >>= h->log2_nbuckets;
437 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
439 limit <<= b->log2_pages;
441 for (i = 0; i < limit; i++)
443 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, search_key->key))
452 u8 *BV (format_bihash) (
u8 * s, va_list * args)
454 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
455 int verbose = va_arg (*args,
int);
459 u64 active_elements = 0;
461 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
463 for (i = 0; i < h->nbuckets; i++)
469 s =
format (s,
"[%d]: empty\n", i);
475 s =
format (s,
"[%d]: heap offset %d, len %d, linear %d\n", i,
476 b->offset, (1 << b->log2_pages), b->linear_search);
480 for (j = 0; j < (1 << b->log2_pages); j++)
484 if (BV (clib_bihash_is_free) (&v->kvp[k]))
487 s =
format (s,
" %d: empty\n",
488 j * BIHASH_KVP_PER_PAGE + k);
493 s =
format (s,
" %d: %U\n",
494 j * BIHASH_KVP_PER_PAGE + k,
495 BV (format_bihash_kvp), &(v->kvp[k]));
503 s =
format (s,
" %lld active elements\n", active_elements);
505 s =
format (s,
" %d linear search buckets\n", h->linear_buckets);
511 (
BVT (clib_bihash) * h,
void *callback,
void *arg)
516 void (*fp) (
BVT (clib_bihash_kv) *,
void *) = callback;
518 for (i = 0; i < h->nbuckets; i++)
525 for (j = 0; j < (1 << b->log2_pages); j++)
529 if (BV (clib_bihash_is_free) (&v->kvp[k]))
532 (*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
#define BIHASH_KVP_PER_PAGE
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)
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.
static vnet_classify_entry_t * split_and_rehash_linear(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
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 vnet_classify_entry_t * split_and_rehash(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
static void * clib_mem_set_heap(void *heap)
#define clib_warning(format, args...)
#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.
static void clib_mem_free(void *p)
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_always_inline uword os_get_thread_index(void)
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 vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
#define CLIB_CACHE_LINE_BYTES