2 Copyright (c) 2014 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
20 * Note: to instantiate the template multiple times in a single file,
21 * #undef __included_bihash_template_h__...
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
26 #include <vppinfra/heap.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/pool.h>
29 #include <vppinfra/cache.h>
32 #error BIHASH_TYPE not defined
36 #define __bv(a,b) _bv(a,b)
37 #define BV(a) __bv(a,BIHASH_TYPE)
39 #define _bvt(a,b) a##b##_t
40 #define __bvt(a,b) _bvt(a,b)
41 #define BVT(a) __bvt(a,BIHASH_TYPE)
43 typedef struct BV (clib_bihash_value)
47 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
48 struct BV (clib_bihash_value) * next_free;
50 } BVT (clib_bihash_value);
52 #define BIHASH_BUCKET_OFFSET_BITS 36
60 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
68 } BVT (clib_bihash_bucket);
70 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
74 BVT (clib_bihash_value) * values;
75 BVT (clib_bihash_bucket) * buckets;
76 volatile u32 *alloc_lock;
78 BVT (clib_bihash_value) ** working_copies;
79 int *working_copy_lengths;
80 BVT (clib_bihash_bucket) saved_bucket;
89 BVT (clib_bihash_value) ** freelists;
92 * Backing store allocation. Since bihash manages its own
93 * freelists, we simple dole out memory at alloc_arena_next.
96 uword alloc_arena_next;
97 uword alloc_arena_size;
100 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
102 format_function_t *fmt_fn;
106 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
108 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
112 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
114 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
117 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
119 BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
123 locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
124 unlocked_bucket.lock = 0;
125 locked_bucket.lock = 1;
127 while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
128 locked_bucket.as_u64, 1 /* weak */ ,
130 __ATOMIC_ACQUIRE) == 0);
133 static inline void BV (clib_bihash_unlock_bucket)
134 (BVT (clib_bihash_bucket) * b)
136 CLIB_MEMORY_BARRIER ();
140 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
143 u8 *hp = (u8 *) h->alloc_arena;
144 u8 *vp = hp + offset;
149 static inline int BV (clib_bihash_bucket_is_empty)
150 (BVT (clib_bihash_bucket) * b)
152 /* Note: applied to locked buckets, test offset */
153 return b->offset == 0;
156 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
161 hp = (u8 *) h->alloc_arena;
167 void BV (clib_bihash_init)
168 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
170 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
171 format_function_t * fmt_fn);
173 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
175 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
176 BVT (clib_bihash_kv) * add_v, int is_add);
177 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
178 BVT (clib_bihash_kv) * search_v,
179 BVT (clib_bihash_kv) * return_v);
181 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
182 void *callback, void *arg);
184 format_function_t BV (format_bihash);
185 format_function_t BV (format_bihash_kvp);
186 format_function_t BV (format_bihash_lru);
188 static inline int BV (clib_bihash_search_inline_with_hash)
189 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
192 BVT (clib_bihash_value) * v;
193 BVT (clib_bihash_bucket) * b;
196 bucket_index = hash & (h->nbuckets - 1);
197 b = &h->buckets[bucket_index];
199 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
202 if (PREDICT_FALSE (b->lock))
204 volatile BVT (clib_bihash_bucket) * bv = b;
209 hash >>= h->log2_nbuckets;
211 v = BV (clib_bihash_get_value) (h, b->offset);
213 /* If the bucket has unresolvable collisions, use linear search */
214 limit = BIHASH_KVP_PER_PAGE;
215 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
216 if (PREDICT_FALSE (b->linear_search))
217 limit <<= b->log2_pages;
219 for (i = 0; i < limit; i++)
221 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
223 *key_result = v->kvp[i];
230 static inline int BV (clib_bihash_search_inline)
231 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
235 hash = BV (clib_bihash_hash) (key_result);
237 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
240 static inline void BV (clib_bihash_prefetch_bucket)
241 (BVT (clib_bihash) * h, u64 hash)
244 BVT (clib_bihash_bucket) * b;
246 bucket_index = hash & (h->nbuckets - 1);
247 b = &h->buckets[bucket_index];
249 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
252 static inline void BV (clib_bihash_prefetch_data)
253 (BVT (clib_bihash) * h, u64 hash)
256 BVT (clib_bihash_value) * v;
257 BVT (clib_bihash_bucket) * b;
259 bucket_index = hash & (h->nbuckets - 1);
260 b = &h->buckets[bucket_index];
262 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
265 hash >>= h->log2_nbuckets;
266 v = BV (clib_bihash_get_value) (h, b->offset);
268 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
270 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
273 static inline int BV (clib_bihash_search_inline_2_with_hash)
274 (BVT (clib_bihash) * h,
275 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
278 BVT (clib_bihash_value) * v;
279 BVT (clib_bihash_bucket) * b;
284 bucket_index = hash & (h->nbuckets - 1);
285 b = &h->buckets[bucket_index];
287 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
290 if (PREDICT_FALSE (b->lock))
292 volatile BVT (clib_bihash_bucket) * bv = b;
297 hash >>= h->log2_nbuckets;
298 v = BV (clib_bihash_get_value) (h, b->offset);
300 /* If the bucket has unresolvable collisions, use linear search */
301 limit = BIHASH_KVP_PER_PAGE;
302 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
303 if (PREDICT_FALSE (b->linear_search))
304 limit <<= b->log2_pages;
306 for (i = 0; i < limit; i++)
308 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
317 static inline int BV (clib_bihash_search_inline_2)
318 (BVT (clib_bihash) * h,
319 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
323 hash = BV (clib_bihash_hash) (search_key);
325 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
330 #endif /* __included_bihash_template_h__ */
335 * fd.io coding-style-patch-verification: ON
338 * eval: (c-set-style "gnu")