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 /** @if 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>
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) {
44 BVT(clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
45 struct BV(clib_bihash_value) * next_free;
47 } BVT(clib_bihash_value);
50 * This is shared across all uses of the template, so it needs
51 * a "personal" #include recursion block
53 #ifndef __defined_clib_bihash_bucket_t__
54 #define __defined_clib_bihash_bucket_t__
64 } clib_bihash_bucket_t;
65 #endif /* __defined_clib_bihash_bucket_t__ */
68 BVT(clib_bihash_value) * values;
69 clib_bihash_bucket_t * buckets;
70 volatile u32 * writer_lock;
72 BVT(clib_bihash_value) ** working_copies;
73 clib_bihash_bucket_t saved_bucket;
79 BVT(clib_bihash_value) **freelists;
86 BV(clib_bihash_get_value) (BVT(clib_bihash) * h, uword offset)
89 u8 * vp = hp + offset;
94 static inline uword BV(clib_bihash_get_offset) (BVT(clib_bihash) * h, void * v)
101 ASSERT((vp - hp) < 0x100000000ULL);
105 void BV(clib_bihash_init)
106 (BVT(clib_bihash) * h, char * name, u32 nbuckets, uword memory_size);
108 void BV(clib_bihash_free)
109 (BVT(clib_bihash) * h);
111 int BV(clib_bihash_add_del) (BVT(clib_bihash) * h,
112 BVT(clib_bihash_kv) * add_v,
114 int BV(clib_bihash_search) (BVT(clib_bihash) * h,
115 BVT(clib_bihash_kv) * search_v,
116 BVT(clib_bihash_kv) * return_v);
118 void BV(clib_bihash_foreach_key_value_pair) (BVT(clib_bihash) * h,
122 format_function_t BV(format_bihash);
123 format_function_t BV(format_bihash_kvp);
126 static inline int BV(clib_bihash_search_inline)
127 (BVT(clib_bihash) * h, BVT(clib_bihash_kv) * kvp)
132 BVT(clib_bihash_value) * v;
133 clib_bihash_bucket_t * b;
136 hash = BV(clib_bihash_hash) (kvp);
138 bucket_index = hash & (h->nbuckets-1);
139 b = &h->buckets[bucket_index];
144 hash >>= h->log2_nbuckets;
146 v = BV(clib_bihash_get_value) (h, b->offset);
147 value_index = hash & ((1<<b->log2_pages)-1);
150 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
152 if (BV(clib_bihash_key_compare)(v->kvp[i].key, kvp->key))
161 static inline int BV(clib_bihash_search_inline_2)
162 (BVT(clib_bihash) * h,
163 BVT(clib_bihash_kv) *search_key,
164 BVT(clib_bihash_kv) *valuep)
169 BVT(clib_bihash_value) * v;
170 clib_bihash_bucket_t * b;
175 hash = BV(clib_bihash_hash) (search_key);
177 bucket_index = hash & (h->nbuckets-1);
178 b = &h->buckets[bucket_index];
183 hash >>= h->log2_nbuckets;
185 v = BV(clib_bihash_get_value) (h, b->offset);
186 value_index = hash & ((1<<b->log2_pages)-1);
189 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
191 if (BV(clib_bihash_key_compare)(v->kvp[i].key, search_key->key))
201 #endif /* __included_bihash_template_h__ */