Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 17 | /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 18 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 19 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | * 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 42 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | }; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | } BVT (clib_bihash_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | * 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
| 59 | union |
| 60 | { |
| 61 | struct |
| 62 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u32 offset; |
| 64 | u8 pad[3]; |
| 65 | u8 log2_pages; |
| 66 | }; |
| 67 | u64 as_u64; |
| 68 | }; |
| 69 | } clib_bihash_bucket_t; |
| 70 | #endif /* __defined_clib_bihash_bucket_t__ */ |
| 71 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 72 | typedef struct |
| 73 | { |
| 74 | BVT (clib_bihash_value) * values; |
| 75 | clib_bihash_bucket_t *buckets; |
| 76 | volatile u32 *writer_lock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | BVT (clib_bihash_value) ** working_copies; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | clib_bihash_bucket_t saved_bucket; |
| 80 | |
| 81 | u32 nbuckets; |
| 82 | u32 log2_nbuckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | BVT (clib_bihash_value) ** freelists; |
| 86 | void *mheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | } BVT (clib_bihash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
| 90 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 91 | static inline void *BV (clib_bihash_get_value) (const BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 92 | uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 94 | u8 *hp = h->mheap; |
| 95 | u8 *vp = hp + offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
| 97 | return (void *) vp; |
| 98 | } |
| 99 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 100 | static inline uword BV (clib_bihash_get_offset) (const BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 101 | void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 103 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | hp = (u8 *) h->mheap; |
| 106 | vp = (u8 *) v; |
| 107 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | ASSERT ((vp - hp) < 0x100000000ULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | return vp - hp; |
| 110 | } |
| 111 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 112 | void BV (clib_bihash_init) |
| 113 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 115 | void BV (clib_bihash_free) (BVT (clib_bihash) * h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | int BV (clib_bihash_add_del) (BVT (clib_bihash) * h, |
| 118 | BVT (clib_bihash_kv) * add_v, int is_add); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 119 | int BV (clib_bihash_search) (const BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | BVT (clib_bihash_kv) * search_v, |
| 121 | BVT (clib_bihash_kv) * return_v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h, |
| 124 | void *callback, void *arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 126 | format_function_t BV (format_bihash); |
| 127 | format_function_t BV (format_bihash_kvp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
| 129 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 130 | static inline int BV (clib_bihash_search_inline) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 131 | (const BVT (clib_bihash) * h, BVT (clib_bihash_kv) * kvp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | { |
| 133 | u64 hash; |
| 134 | u32 bucket_index; |
| 135 | uword value_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 136 | BVT (clib_bihash_value) * v; |
| 137 | clib_bihash_bucket_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | int i; |
| 139 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 140 | hash = BV (clib_bihash_hash) (kvp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | b = &h->buckets[bucket_index]; |
| 144 | |
| 145 | if (b->offset == 0) |
| 146 | return -1; |
| 147 | |
| 148 | hash >>= h->log2_nbuckets; |
| 149 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 150 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 151 | value_index = hash & ((1 << b->log2_pages) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | v += value_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 153 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | for (i = 0; i < BIHASH_KVP_PER_PAGE; i++) |
| 155 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 156 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, kvp->key)) |
| 157 | { |
| 158 | *kvp = v->kvp[i]; |
| 159 | return 0; |
| 160 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 165 | static inline int BV (clib_bihash_search_inline_2) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 166 | (const BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 167 | BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | { |
| 169 | u64 hash; |
| 170 | u32 bucket_index; |
| 171 | uword value_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 172 | BVT (clib_bihash_value) * v; |
| 173 | clib_bihash_bucket_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | int i; |
| 175 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 176 | ASSERT (valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | hash = BV (clib_bihash_hash) (search_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 180 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | b = &h->buckets[bucket_index]; |
| 182 | |
| 183 | if (b->offset == 0) |
| 184 | return -1; |
| 185 | |
| 186 | hash >>= h->log2_nbuckets; |
| 187 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 188 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 189 | value_index = hash & ((1 << b->log2_pages) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | v += value_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 191 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | for (i = 0; i < BIHASH_KVP_PER_PAGE; i++) |
| 193 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 194 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key)) |
| 195 | { |
| 196 | *valuep = v->kvp[i]; |
| 197 | return 0; |
| 198 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | } |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | #endif /* __included_bihash_template_h__ */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 205 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 206 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * fd.io coding-style-patch-verification: ON |
| 210 | * |
| 211 | * Local Variables: |
| 212 | * eval: (c-set-style "gnu") |
| 213 | * End: |
| 214 | */ |