23 #ifndef __included_bihash_template_h__ 24 #define __included_bihash_template_h__ 31 #error BIHASH_TYPE not defined 35 #define __bv(a,b) _bv(a,b) 36 #define BV(a) __bv(a,BIHASH_TYPE) 38 #define _bvt(a,b) a##b##_t 39 #define __bvt(a,b) _bvt(a,b) 40 #define BVT(a) __bvt(a,BIHASH_TYPE) 42 typedef struct BV (clib_bihash_value)
46 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
47 struct BV (clib_bihash_value) * next_free;
49 }
BVT (clib_bihash_value);
51 #if BIHASH_KVP_CACHE_SIZE > 5 52 #error Requested KVP cache LRU data exceeds 16 bits 68 #if BIHASH_KVP_CACHE_SIZE > 0 71 }
BVT (clib_bihash_bucket);
75 BVT (clib_bihash_value) * values;
76 BVT (clib_bihash_bucket) * buckets;
77 volatile u32 *writer_lock;
79 BVT (clib_bihash_value) ** working_copies;
80 int *working_copy_lengths;
81 BVT (clib_bihash_bucket) saved_bucket;
91 BVT (clib_bihash_value) ** freelists;
98 BV (clib_bihash_update_lru) (
BVT (clib_bihash_bucket) * b,
u8 slot)
100 u16 value, tmp, mask;
120 value = b->cache_lru;
127 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
129 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
131 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
133 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
138 mask = 0xFFFF << found_lru_pos;
139 mask <<= found_lru_pos;
140 mask <<= found_lru_pos;
147 save_hi = value & mask;
149 value = save_hi | (tmp << 3) | slot;
151 b->cache_lru = value;
155 BV (clib_bihash_update_lru_not_inline) (
BVT (clib_bihash_bucket) * b,
158 static inline u8 BV (clib_bihash_get_lru) (
BVT (clib_bihash_bucket) * b)
160 #if BIHASH_KVP_CACHE_SIZE > 0 167 static inline void BV (clib_bihash_reset_cache) (
BVT (clib_bihash_bucket) * b)
169 #if BIHASH_KVP_CACHE_SIZE > 0 170 u16 initial_lru_value;
172 memset (b->cache, 0xff, sizeof (b->cache));
179 initial_lru_value = 0;
181 initial_lru_value = (0 << 3) | (1 << 0);
183 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
185 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
187 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
189 b->cache_lru = initial_lru_value;
193 static inline int BV (clib_bihash_lock_bucket) (
BVT (clib_bihash_bucket) * b)
195 BVT (clib_bihash_bucket) tmp_b;
199 tmp_b.cache_lru = 1 << 15;
201 rv = __sync_fetch_and_or (&b->as_u64, tmp_b.as_u64);
204 if (tmp_b.cache_lru & (1 << 15))
209 static inline
void BV (clib_bihash_unlock_bucket)
210 (
BVT (clib_bihash_bucket) * b)
212 BVT (clib_bihash_bucket) tmp_b;
214 tmp_b.as_u64 = b->as_u64;
215 tmp_b.cache_lru &= ~(1 << 15);
216 b->as_u64 = tmp_b.as_u64;
233 hp = (
u8 *) h->mheap;
236 ASSERT ((vp - hp) < 0x100000000ULL);
246 BVT (clib_bihash_kv) * add_v,
int is_add);
248 BVT (clib_bihash_kv) * search_v,
249 BVT (clib_bihash_kv) * return_v);
252 void *callback,
void *arg);
258 static inline int BV (clib_bihash_search_inline)
259 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * key_result)
263 BVT (clib_bihash_value) *
v;
264 BVT (clib_bihash_bucket) * b;
265 #if BIHASH_KVP_CACHE_SIZE > 0 266 BVT (clib_bihash_kv) * kvp;
270 hash = BV (clib_bihash_hash) (key_result);
272 bucket_index = hash & (h->nbuckets - 1);
273 b = &h->buckets[bucket_index];
278 #if BIHASH_KVP_CACHE_SIZE > 0 284 for (i = 0; i < limit; i++)
286 if (BV (clib_bihash_key_compare) (kvp[
i].key, key_result->key))
288 *key_result = kvp[
i];
296 hash >>= h->log2_nbuckets;
302 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
304 limit <<= b->log2_pages;
306 for (i = 0; i < limit; i++)
308 if (BV (clib_bihash_key_compare) (
v->kvp[
i].key, key_result->key))
310 *key_result =
v->kvp[
i];
312 #if BIHASH_KVP_CACHE_SIZE > 0 315 if (BV (clib_bihash_lock_bucket) (b))
317 cache_slot = BV (clib_bihash_get_lru) (b);
318 b->cache[cache_slot] =
v->kvp[
i];
319 BV (clib_bihash_update_lru) (b, cache_slot);
322 BV (clib_bihash_unlock_bucket) (b);
332 static inline int BV (clib_bihash_search_inline_2)
333 (
BVT (clib_bihash) *
h,
334 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
338 BVT (clib_bihash_value) *
v;
339 BVT (clib_bihash_bucket) * b;
340 #if BIHASH_KVP_CACHE_SIZE > 0 341 BVT (clib_bihash_kv) * kvp;
347 hash = BV (clib_bihash_hash) (search_key);
349 bucket_index = hash & (h->nbuckets - 1);
350 b = &h->buckets[bucket_index];
356 #if BIHASH_KVP_CACHE_SIZE > 0 361 for (i = 0; i < limit; i++)
363 if (BV (clib_bihash_key_compare) (kvp[
i].key, search_key->key))
373 hash >>= h->log2_nbuckets;
378 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
380 limit <<= b->log2_pages;
382 for (i = 0; i < limit; i++)
384 if (BV (clib_bihash_key_compare) (
v->kvp[
i].key, search_key->key))
388 #if BIHASH_KVP_CACHE_SIZE > 0 392 if (BV (clib_bihash_lock_bucket) (b))
394 cache_slot = BV (clib_bihash_get_lru) (b);
395 b->cache[cache_slot] =
v->kvp[
i];
396 BV (clib_bihash_update_lru) (b, cache_slot);
399 BV (clib_bihash_unlock_bucket) (b);
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.
Fixed length block allocator.
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.
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.
int clib_bihash_search(clib_bihash *h, clib_bihash_kv *search_v, clib_bihash_kv *return_v)
Search a bi-hash table.
struct clib_bihash_value offset
template key/value backing page structure
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
#define BIHASH_KVP_CACHE_SIZE