blob: a0a7844cd32ab216dce0f2fe47f597099cfb0788 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 Copyright (c) 2014 Cisco and/or its affiliates.
3
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15*/
16
Chris Luke16bcf7d2016-09-01 14:31:46 -040017/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
Dave Barachdd3a57f2016-07-27 16:58:51 -040018
Dave Barachc3799992016-08-15 11:12:27 -040019/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070020 * Note: to instantiate the template multiple times in a single file,
21 * #undef __included_bihash_template_h__...
22 */
23#ifndef __included_bihash_template_h__
24#define __included_bihash_template_h__
25
26#include <vppinfra/heap.h>
27#include <vppinfra/format.h>
28#include <vppinfra/pool.h>
29
30#ifndef BIHASH_TYPE
31#error BIHASH_TYPE not defined
32#endif
33
34#define _bv(a,b) a##b
35#define __bv(a,b) _bv(a,b)
36#define BV(a) __bv(a,BIHASH_TYPE)
37
38#define _bvt(a,b) a##b##_t
39#define __bvt(a,b) _bvt(a,b)
40#define BVT(a) __bvt(a,BIHASH_TYPE)
41
Dave Barachc3799992016-08-15 11:12:27 -040042typedef struct BV (clib_bihash_value)
43{
44 union
45 {
46 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
47 struct BV (clib_bihash_value) * next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 };
Dave Barachc3799992016-08-15 11:12:27 -040049} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Dave Barachc3799992016-08-15 11:12:27 -040051/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 * This is shared across all uses of the template, so it needs
53 * a "personal" #include recursion block
54 */
55#ifndef __defined_clib_bihash_bucket_t__
56#define __defined_clib_bihash_bucket_t__
Dave Barachc3799992016-08-15 11:12:27 -040057typedef struct
58{
59 union
60 {
61 struct
62 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 u32 offset;
Dave Barach5e6b9582016-12-12 15:37:29 -050064 u8 linear_search;
65 u8 pad[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u8 log2_pages;
67 };
68 u64 as_u64;
69 };
70} clib_bihash_bucket_t;
71#endif /* __defined_clib_bihash_bucket_t__ */
72
Dave Barachc3799992016-08-15 11:12:27 -040073typedef struct
74{
75 BVT (clib_bihash_value) * values;
76 clib_bihash_bucket_t *buckets;
77 volatile u32 *writer_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Dave Barachc3799992016-08-15 11:12:27 -040079 BVT (clib_bihash_value) ** working_copies;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 clib_bihash_bucket_t saved_bucket;
81
82 u32 nbuckets;
83 u32 log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -050084 u32 linear_buckets;
Dave Barachc3799992016-08-15 11:12:27 -040085 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barachc3799992016-08-15 11:12:27 -040087 BVT (clib_bihash_value) ** freelists;
88 void *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barachc3799992016-08-15 11:12:27 -040090} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
92
Neale Ranns0bfe5d82016-08-25 15:29:12 +010093static inline void *BV (clib_bihash_get_value) (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -040094 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095{
Dave Barachc3799992016-08-15 11:12:27 -040096 u8 *hp = h->mheap;
97 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 return (void *) vp;
100}
101
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100102static inline uword BV (clib_bihash_get_offset) (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400103 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104{
Dave Barachc3799992016-08-15 11:12:27 -0400105 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 hp = (u8 *) h->mheap;
108 vp = (u8 *) v;
109
Dave Barachc3799992016-08-15 11:12:27 -0400110 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 return vp - hp;
112}
113
Dave Barachc3799992016-08-15 11:12:27 -0400114void BV (clib_bihash_init)
115 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barachc3799992016-08-15 11:12:27 -0400117void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Dave Barachc3799992016-08-15 11:12:27 -0400119int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
120 BVT (clib_bihash_kv) * add_v, int is_add);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100121int BV (clib_bihash_search) (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400122 BVT (clib_bihash_kv) * search_v,
123 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Dave Barachc3799992016-08-15 11:12:27 -0400125void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
126 void *callback, void *arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Dave Barachc3799992016-08-15 11:12:27 -0400128format_function_t BV (format_bihash);
129format_function_t BV (format_bihash_kvp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131
Dave Barachc3799992016-08-15 11:12:27 -0400132static inline int BV (clib_bihash_search_inline)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133 (const BVT (clib_bihash) * h, BVT (clib_bihash_kv) * kvp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134{
135 u64 hash;
136 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400137 BVT (clib_bihash_value) * v;
138 clib_bihash_bucket_t *b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500139 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Dave Barachc3799992016-08-15 11:12:27 -0400141 hash = BV (clib_bihash_hash) (kvp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Dave Barachc3799992016-08-15 11:12:27 -0400143 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 b = &h->buckets[bucket_index];
145
146 if (b->offset == 0)
147 return -1;
148
149 hash >>= h->log2_nbuckets;
150
Dave Barachc3799992016-08-15 11:12:27 -0400151 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400152
Dave Barach5e6b9582016-12-12 15:37:29 -0500153 /* If the bucket has unresolvable collisions, use linear search */
154 limit = BIHASH_KVP_PER_PAGE;
155 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
156 if (PREDICT_FALSE (b->linear_search))
157 limit <<= b->log2_pages;
158
159 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 {
Dave Barachc3799992016-08-15 11:12:27 -0400161 if (BV (clib_bihash_key_compare) (v->kvp[i].key, kvp->key))
162 {
163 *kvp = v->kvp[i];
164 return 0;
165 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 }
167 return -1;
168}
169
Dave Barachc3799992016-08-15 11:12:27 -0400170static inline int BV (clib_bihash_search_inline_2)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100171 (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400172 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
174 u64 hash;
175 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400176 BVT (clib_bihash_value) * v;
177 clib_bihash_bucket_t *b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500178 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Dave Barachc3799992016-08-15 11:12:27 -0400180 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barachc3799992016-08-15 11:12:27 -0400182 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Dave Barachc3799992016-08-15 11:12:27 -0400184 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 b = &h->buckets[bucket_index];
186
187 if (b->offset == 0)
188 return -1;
189
190 hash >>= h->log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -0400191 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400192
Dave Barach5e6b9582016-12-12 15:37:29 -0500193 /* If the bucket has unresolvable collisions, use linear search */
194 limit = BIHASH_KVP_PER_PAGE;
195 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
196 if (PREDICT_FALSE (b->linear_search))
197 limit <<= b->log2_pages;
198
199 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 {
Dave Barachc3799992016-08-15 11:12:27 -0400201 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
202 {
203 *valuep = v->kvp[i];
204 return 0;
205 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 }
207 return -1;
208}
209
210
211#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400212
Chris Luke16bcf7d2016-09-01 14:31:46 -0400213/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */