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>
30 #include <vppinfra/lock.h>
33 #error BIHASH_TYPE not defined
37 #define __bv(a,b) _bv(a,b)
38 #define BV(a) __bv(a,BIHASH_TYPE)
40 #define _bvt(a,b) a##b##_t
41 #define __bvt(a,b) _bvt(a,b)
42 #define BVT(a) __bvt(a,BIHASH_TYPE)
44 typedef struct BV (clib_bihash_value)
48 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
49 struct BV (clib_bihash_value) * next_free;
51 } BVT (clib_bihash_value);
53 #define BIHASH_BUCKET_OFFSET_BITS 36
61 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
69 } BVT (clib_bihash_bucket);
71 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
75 BVT (clib_bihash_value) * values;
76 BVT (clib_bihash_bucket) * buckets;
77 volatile u32 *alloc_lock;
79 BVT (clib_bihash_value) ** working_copies;
80 int *working_copy_lengths;
81 BVT (clib_bihash_bucket) saved_bucket;
87 BVT (clib_bihash_value) ** freelists;
90 * Backing store allocation. Since bihash manages its own
91 * freelists, we simple dole out memory at alloc_arena_next.
94 uword alloc_arena_next;
95 uword alloc_arena_size;
98 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
100 format_function_t *fmt_fn;
104 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
106 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
110 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
112 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
115 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
117 BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
121 locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
122 unlocked_bucket.lock = 0;
123 locked_bucket.lock = 1;
126 while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
127 locked_bucket.as_u64, 1 /* weak */ ,
129 __ATOMIC_ACQUIRE) == 0);
132 static inline void BV (clib_bihash_unlock_bucket)
133 (BVT (clib_bihash_bucket) * b)
135 CLIB_MEMORY_BARRIER ();
139 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
142 u8 *hp = (u8 *) h->alloc_arena;
143 u8 *vp = hp + offset;
148 static inline int BV (clib_bihash_bucket_is_empty)
149 (BVT (clib_bihash_bucket) * b)
151 /* Note: applied to locked buckets, test offset */
152 return b->offset == 0;
155 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
160 hp = (u8 *) h->alloc_arena;
166 void BV (clib_bihash_init)
167 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
169 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
170 format_function_t * fmt_fn);
172 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
174 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
175 BVT (clib_bihash_kv) * add_v, int is_add);
176 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
177 BVT (clib_bihash_kv) * add_v,
178 int (*is_stale_cb) (BVT
182 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
183 BVT (clib_bihash_kv) * search_v,
184 BVT (clib_bihash_kv) * return_v);
186 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
187 void *callback, void *arg);
189 format_function_t BV (format_bihash);
190 format_function_t BV (format_bihash_kvp);
191 format_function_t BV (format_bihash_lru);
193 static inline int BV (clib_bihash_search_inline_with_hash)
194 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
197 BVT (clib_bihash_value) * v;
198 BVT (clib_bihash_bucket) * b;
201 bucket_index = hash & (h->nbuckets - 1);
202 b = &h->buckets[bucket_index];
204 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
207 if (PREDICT_FALSE (b->lock))
209 volatile BVT (clib_bihash_bucket) * bv = b;
214 hash >>= h->log2_nbuckets;
216 v = BV (clib_bihash_get_value) (h, b->offset);
218 /* If the bucket has unresolvable collisions, use linear search */
219 limit = BIHASH_KVP_PER_PAGE;
220 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
221 if (PREDICT_FALSE (b->linear_search))
222 limit <<= b->log2_pages;
224 for (i = 0; i < limit; i++)
226 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
228 *key_result = v->kvp[i];
235 static inline int BV (clib_bihash_search_inline)
236 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
240 hash = BV (clib_bihash_hash) (key_result);
242 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
245 static inline void BV (clib_bihash_prefetch_bucket)
246 (BVT (clib_bihash) * h, u64 hash)
249 BVT (clib_bihash_bucket) * b;
251 bucket_index = hash & (h->nbuckets - 1);
252 b = &h->buckets[bucket_index];
254 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
257 static inline void BV (clib_bihash_prefetch_data)
258 (BVT (clib_bihash) * h, u64 hash)
261 BVT (clib_bihash_value) * v;
262 BVT (clib_bihash_bucket) * b;
264 bucket_index = hash & (h->nbuckets - 1);
265 b = &h->buckets[bucket_index];
267 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
270 hash >>= h->log2_nbuckets;
271 v = BV (clib_bihash_get_value) (h, b->offset);
273 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
275 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
278 static inline int BV (clib_bihash_search_inline_2_with_hash)
279 (BVT (clib_bihash) * h,
280 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
283 BVT (clib_bihash_value) * v;
284 BVT (clib_bihash_bucket) * b;
289 bucket_index = hash & (h->nbuckets - 1);
290 b = &h->buckets[bucket_index];
292 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
295 if (PREDICT_FALSE (b->lock))
297 volatile BVT (clib_bihash_bucket) * bv = b;
302 hash >>= h->log2_nbuckets;
303 v = BV (clib_bihash_get_value) (h, b->offset);
305 /* If the bucket has unresolvable collisions, use linear search */
306 limit = BIHASH_KVP_PER_PAGE;
307 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
308 if (PREDICT_FALSE (b->linear_search))
309 limit <<= b->log2_pages;
311 for (i = 0; i < limit; i++)
313 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
322 static inline int BV (clib_bihash_search_inline_2)
323 (BVT (clib_bihash) * h,
324 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
328 hash = BV (clib_bihash_hash) (search_key);
330 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
335 #endif /* __included_bihash_template_h__ */
340 * fd.io coding-style-patch-verification: ON
343 * eval: (c-set-style "gnu")