Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001-2005 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <vppinfra/hash.h> |
| 39 | #include <vppinfra/error.h> |
| 40 | #include <vppinfra/mem.h> |
| 41 | #include <vppinfra/byte_order.h> /* for clib_arch_is_big_endian */ |
| 42 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | always_inline void |
| 44 | zero_pair (hash_t * h, hash_pair_t * p) |
| 45 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 46 | clib_memset (p, 0, hash_pair_bytes (h)); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 47 | } |
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 | always_inline void |
| 50 | init_pair (hash_t * h, hash_pair_t * p) |
| 51 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 52 | clib_memset (p->value, ~0, hash_value_bytes (h)); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 53 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
| 55 | always_inline hash_pair_union_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | get_pair (void *v, uword i) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 58 | hash_t *h = hash_header (v); |
| 59 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | ASSERT (i < vec_len (v)); |
| 61 | p = v; |
| 62 | p += i << h->log2_pair_size; |
| 63 | return (hash_pair_union_t *) p; |
| 64 | } |
| 65 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 66 | always_inline void |
| 67 | set_is_user (void *v, uword i, uword is_user) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | hash_t *h = hash_header (v); |
| 70 | uword i0 = i / BITS (h->is_user[0]); |
| 71 | uword i1 = (uword) 1 << (i % BITS (h->is_user[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | if (is_user) |
| 73 | h->is_user[i0] |= i1; |
| 74 | else |
| 75 | h->is_user[i0] &= ~i1; |
| 76 | } |
| 77 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | static u8 *hash_format_pair_default (u8 * s, va_list * args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 80 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 81 | hash_memory (void *p, word n_bytes, uword state) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | { |
Damjan Marion | 8799768 | 2022-03-26 00:57:50 +0100 | [diff] [blame] | 83 | uword last[3] = {}; |
| 84 | uwordu *q = p; |
| 85 | u64 a, b, c, n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
Damjan Marion | 8799768 | 2022-03-26 00:57:50 +0100 | [diff] [blame] | 87 | a = b = (uword_bits == 64) ? 0x9e3779b97f4a7c13LL : 0x9e3779b9; |
| 88 | c = state; |
| 89 | n = n_bytes; |
| 90 | |
| 91 | while (n >= 3 * sizeof (uword)) |
| 92 | { |
| 93 | a += q[0]; |
| 94 | b += q[1]; |
| 95 | c += q[2]; |
| 96 | hash_mix (a, b, c); |
| 97 | n -= 3 * sizeof (uword); |
| 98 | q += 3; |
| 99 | } |
| 100 | |
| 101 | c += n_bytes; |
| 102 | |
| 103 | if (n > 0) |
| 104 | { |
| 105 | clib_memcpy_fast (&last, q, n); |
| 106 | a += last[0]; |
| 107 | b += last[1]; |
| 108 | c += last[2]; |
| 109 | } |
| 110 | |
| 111 | hash_mix (a, b, c); |
| 112 | |
| 113 | return c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | always_inline uword |
| 117 | hash_uword (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Damjan Marion | 8799768 | 2022-03-26 00:57:50 +0100 | [diff] [blame] | 119 | uword a, b, c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Damjan Marion | 8799768 | 2022-03-26 00:57:50 +0100 | [diff] [blame] | 121 | a = b = (uword_bits == 64) ? 0x9e3779b97f4a7c13LL : 0x9e3779b9; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | c = 0; |
| 123 | a += x; |
Damjan Marion | 8799768 | 2022-03-26 00:57:50 +0100 | [diff] [blame] | 124 | hash_mix (a, b, c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | return c; |
| 126 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
| 128 | /* Call sum function. Hash code will be sum function value |
| 129 | modulo the prime length of the hash table. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 130 | always_inline uword |
| 131 | key_sum (hash_t * h, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | { |
| 133 | uword sum; |
| 134 | switch (pointer_to_uword ((void *) h->key_sum)) |
| 135 | { |
| 136 | case KEY_FUNC_NONE: |
| 137 | sum = hash_uword (key); |
| 138 | break; |
| 139 | |
| 140 | case KEY_FUNC_POINTER_UWORD: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 141 | sum = hash_uword (*uword_to_pointer (key, uword *)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case KEY_FUNC_POINTER_U32: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 145 | sum = hash_uword (*uword_to_pointer (key, u32 *)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | case KEY_FUNC_STRING: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | sum = string_key_sum (h, key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | break; |
| 151 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 152 | case KEY_FUNC_MEM: |
| 153 | sum = mem_key_sum (h, key); |
| 154 | break; |
| 155 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | default: |
| 157 | sum = h->key_sum (h, key); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | return sum; |
| 162 | } |
| 163 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | always_inline uword |
| 165 | key_equal1 (hash_t * h, uword key1, uword key2, uword e) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
| 167 | switch (pointer_to_uword ((void *) h->key_equal)) |
| 168 | { |
| 169 | case KEY_FUNC_NONE: |
| 170 | break; |
| 171 | |
| 172 | case KEY_FUNC_POINTER_UWORD: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 173 | e = |
| 174 | *uword_to_pointer (key1, uword *) == *uword_to_pointer (key2, |
| 175 | uword *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | case KEY_FUNC_POINTER_U32: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 179 | e = *uword_to_pointer (key1, u32 *) == *uword_to_pointer (key2, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | break; |
| 181 | |
| 182 | case KEY_FUNC_STRING: |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 183 | e = string_key_equal (h, key1, key2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | break; |
| 185 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 186 | case KEY_FUNC_MEM: |
| 187 | e = mem_key_equal (h, key1, key2); |
| 188 | break; |
| 189 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | default: |
| 191 | e = h->key_equal (h, key1, key2); |
| 192 | break; |
| 193 | } |
| 194 | return e; |
| 195 | } |
| 196 | |
| 197 | /* Compares two keys: returns 1 if equal, 0 if not. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 198 | always_inline uword |
| 199 | key_equal (hash_t * h, uword key1, uword key2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | { |
| 201 | uword e = key1 == key2; |
| 202 | if (CLIB_DEBUG > 0 && key1 == key2) |
| 203 | ASSERT (key_equal1 (h, key1, key2, e)); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 204 | if (!e) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | e = key_equal1 (h, key1, key2, e); |
| 206 | return e; |
| 207 | } |
| 208 | |
| 209 | static hash_pair_union_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 210 | get_indirect (void *v, hash_pair_indirect_t * pi, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 212 | hash_t *h = hash_header (v); |
| 213 | hash_pair_t *p0, *p1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
| 215 | p0 = p1 = pi->pairs; |
| 216 | if (h->log2_pair_size > 0) |
| 217 | p1 = hash_forward (h, p0, indirect_pair_get_len (pi)); |
| 218 | else |
| 219 | p1 += vec_len (p0); |
| 220 | |
| 221 | while (p0 < p1) |
| 222 | { |
| 223 | if (key_equal (h, p0->key, key)) |
| 224 | return (hash_pair_union_t *) p0; |
| 225 | p0 = hash_forward1 (h, p0); |
| 226 | } |
| 227 | |
| 228 | return (hash_pair_union_t *) 0; |
| 229 | } |
| 230 | |
| 231 | static hash_pair_union_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 232 | set_indirect_is_user (void *v, uword i, hash_pair_union_t * p, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 234 | hash_t *h = hash_header (v); |
| 235 | hash_pair_t *q; |
| 236 | hash_pair_indirect_t *pi = &p->indirect; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | uword log2_bytes = 0; |
| 238 | |
| 239 | if (h->log2_pair_size == 0) |
| 240 | q = vec_new (hash_pair_t, 2); |
| 241 | else |
| 242 | { |
| 243 | log2_bytes = 1 + hash_pair_log2_bytes (h); |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 244 | q = clib_mem_alloc (1ULL << log2_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | } |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 246 | clib_memcpy_fast (q, &p->direct, hash_pair_bytes (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
| 248 | pi->pairs = q; |
| 249 | if (h->log2_pair_size > 0) |
| 250 | indirect_pair_set (pi, log2_bytes, 2); |
| 251 | |
| 252 | set_is_user (v, i, 0); |
| 253 | |
| 254 | /* First element is used by existing pair, second will be used by caller. */ |
| 255 | q = hash_forward1 (h, q); |
| 256 | q->key = key; |
| 257 | init_pair (h, q); |
| 258 | return (hash_pair_union_t *) q; |
| 259 | } |
| 260 | |
| 261 | static hash_pair_union_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 262 | set_indirect (void *v, hash_pair_indirect_t * pi, uword key, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | uword * found_key) |
| 264 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 265 | hash_t *h = hash_header (v); |
| 266 | hash_pair_t *new_pair; |
| 267 | hash_pair_union_t *q; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | q = get_indirect (v, pi, key); |
| 270 | if (q) |
| 271 | { |
| 272 | *found_key = 1; |
| 273 | return q; |
| 274 | } |
| 275 | |
| 276 | if (h->log2_pair_size == 0) |
| 277 | vec_add2 (pi->pairs, new_pair, 1); |
| 278 | else |
| 279 | { |
| 280 | uword len, new_len, log2_bytes; |
| 281 | |
| 282 | len = indirect_pair_get_len (pi); |
| 283 | log2_bytes = indirect_pair_get_log2_bytes (pi); |
| 284 | |
| 285 | new_len = len + 1; |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 286 | if (new_len * hash_pair_bytes (h) > (1ULL << log2_bytes)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | { |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 288 | pi->pairs = clib_mem_realloc (pi->pairs, 1ULL << (log2_bytes + 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | log2_bytes++; |
| 290 | } |
| 291 | |
| 292 | indirect_pair_set (pi, log2_bytes, new_len); |
| 293 | new_pair = pi->pairs + (len << h->log2_pair_size); |
| 294 | } |
| 295 | new_pair->key = key; |
| 296 | init_pair (h, new_pair); |
| 297 | *found_key = 0; |
| 298 | return (hash_pair_union_t *) new_pair; |
| 299 | } |
| 300 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 301 | static void |
| 302 | unset_indirect (void *v, uword i, hash_pair_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 304 | hash_t *h = hash_header (v); |
| 305 | hash_pair_union_t *p = get_pair (v, i); |
| 306 | hash_pair_t *e; |
| 307 | hash_pair_indirect_t *pi = &p->indirect; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | uword len, is_vec; |
| 309 | |
| 310 | is_vec = h->log2_pair_size == 0; |
| 311 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 312 | ASSERT (!hash_is_user (v, i)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | len = is_vec ? vec_len (pi->pairs) : indirect_pair_get_len (pi); |
| 314 | e = hash_forward (h, pi->pairs, len - 1); |
| 315 | ASSERT (q >= pi->pairs && q <= e); |
| 316 | |
| 317 | /* We have two or fewer pairs and we are delete one pair. |
| 318 | Make indirect pointer direct and free indirect memory. */ |
| 319 | if (len <= 2) |
| 320 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 321 | hash_pair_t *r = pi->pairs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | |
| 323 | if (len == 2) |
| 324 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 325 | clib_memcpy_fast (p, q == r ? hash_forward1 (h, r) : r, |
| 326 | hash_pair_bytes (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | set_is_user (v, i, 1); |
| 328 | } |
| 329 | else |
| 330 | zero_pair (h, &p->direct); |
| 331 | |
| 332 | if (is_vec) |
| 333 | vec_free (r); |
| 334 | else if (r) |
| 335 | clib_mem_free (r); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | /* If deleting a pair we need to keep non-null pairs together. */ |
| 340 | if (q < e) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 341 | clib_memcpy_fast (q, e, hash_pair_bytes (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | else |
| 343 | zero_pair (h, q); |
| 344 | if (is_vec) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 345 | vec_dec_len (pi->pairs, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 347 | indirect_pair_set (pi, indirect_pair_get_log2_bytes (pi), len - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 351 | enum lookup_opcode |
| 352 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | GET = 1, |
| 354 | SET = 2, |
| 355 | UNSET = 3, |
| 356 | }; |
| 357 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 358 | static hash_pair_t * |
| 359 | lookup (void *v, uword key, enum lookup_opcode op, |
| 360 | void *new_value, void *old_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 362 | hash_t *h = hash_header (v); |
| 363 | hash_pair_union_t *p = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | uword found_key = 0; |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 365 | uword value_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | uword i; |
| 367 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 368 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | |
| 371 | i = key_sum (h, key) & (_vec_len (v) - 1); |
| 372 | p = get_pair (v, i); |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 373 | value_bytes = hash_value_bytes (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | |
| 375 | if (hash_is_user (v, i)) |
| 376 | { |
| 377 | found_key = key_equal (h, p->direct.key, key); |
| 378 | if (found_key) |
| 379 | { |
| 380 | if (op == UNSET) |
| 381 | { |
| 382 | set_is_user (v, i, 0); |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 383 | if (old_value && value_bytes) |
| 384 | clib_memcpy_fast (old_value, p->direct.value, value_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | zero_pair (h, &p->direct); |
| 386 | } |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | if (op == SET) |
| 391 | p = set_indirect_is_user (v, i, p, key); |
| 392 | else |
| 393 | p = 0; |
| 394 | } |
| 395 | } |
| 396 | else |
| 397 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 398 | hash_pair_indirect_t *pi = &p->indirect; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
| 400 | if (op == SET) |
| 401 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 402 | if (!pi->pairs) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | { |
| 404 | p->direct.key = key; |
| 405 | set_is_user (v, i, 1); |
| 406 | } |
| 407 | else |
| 408 | p = set_indirect (v, pi, key, &found_key); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | p = get_indirect (v, pi, key); |
| 413 | found_key = p != 0; |
| 414 | if (found_key && op == UNSET) |
| 415 | { |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 416 | if (old_value && value_bytes) |
| 417 | clib_memcpy_fast (old_value, &p->direct.value, value_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | |
| 419 | unset_indirect (v, i, &p->direct); |
| 420 | |
| 421 | /* Nullify p (since it's just been deleted). |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 422 | Otherwise we might be tempted to play with it. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | p = 0; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 428 | if (op == SET && p != 0 && value_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | { |
| 430 | /* Save away old value for caller. */ |
| 431 | if (old_value && found_key) |
Benoît Ganne | 1a3e08a | 2021-02-11 19:46:43 +0100 | [diff] [blame] | 432 | clib_memcpy_fast (old_value, &p->direct.value, value_bytes); |
| 433 | clib_memcpy_fast (&p->direct.value, new_value, value_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (op == SET) |
| 437 | h->elts += !found_key; |
| 438 | if (op == UNSET) |
| 439 | h->elts -= found_key; |
| 440 | |
| 441 | return &p->direct; |
| 442 | } |
| 443 | |
| 444 | /* Fetch value of key. */ |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 445 | __clib_export uword * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 446 | _hash_get (void *v, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 448 | hash_t *h = hash_header (v); |
| 449 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | |
| 451 | /* Don't even search table if its empty. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 452 | if (!v || h->elts == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | return 0; |
| 454 | |
| 455 | p = lookup (v, key, GET, 0, 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 456 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | return 0; |
| 458 | if (h->log2_pair_size == 0) |
| 459 | return &p->key; |
| 460 | else |
| 461 | return &p->value[0]; |
| 462 | } |
| 463 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 464 | __clib_export hash_pair_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 465 | _hash_get_pair (void *v, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 467 | return lookup (v, key, GET, 0, 0); |
| 468 | } |
| 469 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 470 | __clib_export hash_pair_t * |
| 471 | hash_next (void *v, hash_next_t *hn) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 472 | { |
| 473 | hash_t *h = hash_header (v); |
| 474 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | |
| 476 | while (1) |
| 477 | { |
| 478 | if (hn->i == 0 && hn->j == 0) |
| 479 | { |
| 480 | /* Save flags. */ |
| 481 | hn->f = h->flags; |
| 482 | |
| 483 | /* Prevent others from re-sizing hash table. */ |
| 484 | h->flags |= |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 485 | (HASH_FLAG_NO_AUTO_GROW |
| 486 | | HASH_FLAG_NO_AUTO_SHRINK | HASH_FLAG_HASH_NEXT_IN_PROGRESS); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | } |
| 488 | else if (hn->i >= hash_capacity (v)) |
| 489 | { |
| 490 | /* Restore flags. */ |
| 491 | h->flags = hn->f; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 492 | clib_memset (hn, 0, sizeof (hn[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | p = hash_forward (h, v, hn->i); |
| 497 | if (hash_is_user (v, hn->i)) |
| 498 | { |
| 499 | hn->i++; |
| 500 | return p; |
| 501 | } |
| 502 | else |
| 503 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 504 | hash_pair_indirect_t *pi = (void *) p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | uword n; |
| 506 | |
| 507 | if (h->log2_pair_size > 0) |
| 508 | n = indirect_pair_get_len (pi); |
| 509 | else |
| 510 | n = vec_len (pi->pairs); |
| 511 | |
| 512 | if (hn->j >= n) |
| 513 | { |
| 514 | hn->i++; |
| 515 | hn->j = 0; |
| 516 | } |
| 517 | else |
| 518 | return hash_forward (h, pi->pairs, hn->j++); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* Remove key from table. */ |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 524 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 525 | _hash_unset (void *v, uword key, void *old_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 527 | hash_t *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 529 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | return v; |
| 531 | |
| 532 | (void) lookup (v, key, UNSET, 0, old_value); |
| 533 | |
| 534 | h = hash_header (v); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 535 | if (!(h->flags & HASH_FLAG_NO_AUTO_SHRINK)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | { |
| 537 | /* Resize when 1/4 full. */ |
| 538 | if (h->elts > 32 && 4 * (h->elts + 1) < vec_len (v)) |
| 539 | v = hash_resize (v, vec_len (v) / 2); |
| 540 | } |
| 541 | |
| 542 | return v; |
| 543 | } |
| 544 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 545 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 546 | _hash_create (uword elts, hash_t * h_user) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 548 | hash_t *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | uword log2_pair_size; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 550 | void *v; |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 551 | vec_attr_t va = { .hdr_sz = sizeof (h[0]), .align = sizeof (hash_pair_t) }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | |
| 553 | /* Size of hash is power of 2 >= ELTS and larger than |
| 554 | number of bits in is_user bitmap elements. */ |
| 555 | elts = clib_max (elts, BITS (h->is_user[0])); |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 556 | elts = 1ULL << max_log2 (elts); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | |
| 558 | log2_pair_size = 1; |
| 559 | if (h_user) |
| 560 | log2_pair_size = h_user->log2_pair_size; |
| 561 | |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 562 | va.elt_sz = (1 << log2_pair_size) * sizeof (hash_pair_t), |
| 563 | v = _vec_alloc_internal (elts, &va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | h = hash_header (v); |
| 565 | |
| 566 | if (h_user) |
Damjan Marion | 2b702da | 2022-03-17 17:54:48 +0100 | [diff] [blame] | 567 | { |
| 568 | h[0] = h_user[0]; |
| 569 | h->is_user = 0; |
| 570 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | |
Damjan Marion | 2b702da | 2022-03-17 17:54:48 +0100 | [diff] [blame] | 572 | vec_validate_aligned ( |
| 573 | h->is_user, ((elts / BITS (h->is_user[0])) * sizeof (h->is_user[0])) - 1, |
| 574 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | h->log2_pair_size = log2_pair_size; |
| 576 | h->elts = 0; |
| 577 | |
| 578 | /* Default flags to never shrinking hash tables. |
| 579 | Shrinking tables can cause "jackpot" cases. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 580 | if (!h_user) |
| 581 | h->flags = HASH_FLAG_NO_AUTO_SHRINK; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 583 | if (!h->format_pair) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | { |
| 585 | h->format_pair = hash_format_pair_default; |
| 586 | h->format_pair_arg = 0; |
| 587 | } |
| 588 | |
| 589 | return v; |
| 590 | } |
| 591 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 592 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 593 | _hash_free (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 595 | hash_t *h = hash_header (v); |
| 596 | hash_pair_union_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | uword i; |
| 598 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 599 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 600 | return v; |
| 601 | |
| 602 | /* We zero all freed memory in case user would be tempted to use it. */ |
| 603 | for (i = 0; i < hash_capacity (v); i++) |
| 604 | { |
| 605 | if (hash_is_user (v, i)) |
| 606 | continue; |
| 607 | p = get_pair (v, i); |
| 608 | if (h->log2_pair_size == 0) |
| 609 | vec_free (p->indirect.pairs); |
| 610 | else if (p->indirect.pairs) |
| 611 | clib_mem_free (p->indirect.pairs); |
| 612 | } |
| 613 | |
Damjan Marion | 2b702da | 2022-03-17 17:54:48 +0100 | [diff] [blame] | 614 | vec_free (h->is_user); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | vec_free_header (h); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 620 | static void * |
| 621 | hash_resize_internal (void *old, uword new_size, uword free_old) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 623 | void *new; |
| 624 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | |
| 626 | new = 0; |
| 627 | if (new_size > 0) |
| 628 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 629 | hash_t *h = old ? hash_header (old) : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | new = _hash_create (new_size, h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 631 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | hash_foreach_pair (p, old, { |
| 633 | new = _hash_set3 (new, p->key, &p->value[0], 0); |
| 634 | }); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 635 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | if (free_old) |
| 639 | hash_free (old); |
| 640 | return new; |
| 641 | } |
| 642 | |
benker | ddb8d65 | 2021-12-14 15:31:14 +0000 | [diff] [blame] | 643 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 644 | hash_resize (void *old, uword new_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 646 | return hash_resize_internal (old, new_size, 1); |
| 647 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 649 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 650 | hash_dup (void *old) |
| 651 | { |
| 652 | return hash_resize_internal (old, vec_len (old), 0); |
| 653 | } |
| 654 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 655 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 656 | _hash_set3 (void *v, uword key, void *value, void *old_value) |
| 657 | { |
| 658 | hash_t *h; |
| 659 | |
| 660 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | v = hash_create (0, sizeof (uword)); |
| 662 | |
| 663 | h = hash_header (v); |
| 664 | (void) lookup (v, key, SET, value, old_value); |
| 665 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 666 | if (!(h->flags & HASH_FLAG_NO_AUTO_GROW)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | { |
| 668 | /* Resize when 3/4 full. */ |
| 669 | if (4 * (h->elts + 1) > 3 * vec_len (v)) |
| 670 | v = hash_resize (v, 2 * vec_len (v)); |
| 671 | } |
| 672 | |
| 673 | return v; |
| 674 | } |
| 675 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 676 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 677 | vec_key_sum (hash_t * h, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 679 | void *v = uword_to_pointer (key, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | return hash_memory (v, vec_len (v) * h->user, 0); |
| 681 | } |
| 682 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 683 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 684 | vec_key_equal (hash_t * h, uword key1, uword key2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 686 | void *v1 = uword_to_pointer (key1, void *); |
| 687 | void *v2 = uword_to_pointer (key2, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | uword l1 = vec_len (v1); |
| 689 | uword l2 = vec_len (v2); |
| 690 | return l1 == l2 && 0 == memcmp (v1, v2, l1 * h->user); |
| 691 | } |
| 692 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 693 | __clib_export u8 * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 694 | vec_key_format_pair (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 695 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 696 | void *CLIB_UNUSED (user_arg) = va_arg (*args, void *); |
| 697 | void *v = va_arg (*args, void *); |
| 698 | hash_pair_t *p = va_arg (*args, hash_pair_t *); |
| 699 | hash_t *h = hash_header (v); |
| 700 | void *u = uword_to_pointer (p->key, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | int i; |
| 702 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 703 | switch (h->user) |
| 704 | { |
| 705 | case 1: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | s = format (s, "%v", u); |
| 707 | break; |
| 708 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 709 | case 2: |
| 710 | { |
| 711 | u16 *w = u; |
| 712 | for (i = 0; i < vec_len (w); i++) |
| 713 | s = format (s, "0x%x, ", w[i]); |
| 714 | break; |
| 715 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 716 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 717 | case 4: |
| 718 | { |
| 719 | u32 *w = u; |
| 720 | for (i = 0; i < vec_len (w); i++) |
| 721 | s = format (s, "0x%x, ", w[i]); |
| 722 | break; |
| 723 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 725 | case 8: |
| 726 | { |
| 727 | u64 *w = u; |
| 728 | for (i = 0; i < vec_len (w); i++) |
| 729 | s = format (s, "0x%Lx, ", w[i]); |
| 730 | break; |
| 731 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 733 | default: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | s = format (s, "0x%U", format_hex_bytes, u, vec_len (u) * h->user); |
| 735 | break; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 736 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | |
| 738 | if (hash_value_bytes (h) > 0) |
| 739 | s = format (s, " -> 0x%wx", p->value[0]); |
| 740 | |
| 741 | return s; |
| 742 | } |
| 743 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 744 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 745 | mem_key_sum (hash_t * h, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 747 | uword *v = uword_to_pointer (key, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 748 | return hash_memory (v, h->user, 0); |
| 749 | } |
| 750 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 751 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 752 | mem_key_equal (hash_t * h, uword key1, uword key2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 754 | void *v1 = uword_to_pointer (key1, void *); |
| 755 | void *v2 = uword_to_pointer (key2, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 756 | return v1 && v2 && 0 == memcmp (v1, v2, h->user); |
| 757 | } |
| 758 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 759 | uword |
| 760 | string_key_sum (hash_t * h, uword key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 762 | char *v = uword_to_pointer (key, char *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 763 | return hash_memory (v, strlen (v), 0); |
| 764 | } |
| 765 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 766 | uword |
| 767 | string_key_equal (hash_t * h, uword key1, uword key2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 769 | void *v1 = uword_to_pointer (key1, void *); |
| 770 | void *v2 = uword_to_pointer (key2, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | return v1 && v2 && 0 == strcmp (v1, v2); |
| 772 | } |
| 773 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 774 | u8 * |
| 775 | string_key_format_pair (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 777 | void *CLIB_UNUSED (user_arg) = va_arg (*args, void *); |
| 778 | void *v = va_arg (*args, void *); |
| 779 | hash_pair_t *p = va_arg (*args, hash_pair_t *); |
| 780 | hash_t *h = hash_header (v); |
| 781 | void *u = uword_to_pointer (p->key, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | |
| 783 | s = format (s, "%s", u); |
| 784 | |
| 785 | if (hash_value_bytes (h) > 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 786 | s = |
| 787 | format (s, " -> 0x%8U", format_hex_bytes, &p->value[0], |
| 788 | hash_value_bytes (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | |
| 790 | return s; |
| 791 | } |
| 792 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 793 | static u8 * |
| 794 | hash_format_pair_default (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 796 | void *CLIB_UNUSED (user_arg) = va_arg (*args, void *); |
| 797 | void *v = va_arg (*args, void *); |
| 798 | hash_pair_t *p = va_arg (*args, hash_pair_t *); |
| 799 | hash_t *h = hash_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | |
| 801 | s = format (s, "0x%08x", p->key); |
| 802 | if (hash_value_bytes (h) > 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 803 | s = |
| 804 | format (s, " -> 0x%8U", format_hex_bytes, &p->value[0], |
| 805 | hash_value_bytes (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 806 | return s; |
| 807 | } |
| 808 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 809 | __clib_export uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 810 | hash_bytes (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 811 | { |
| 812 | uword i, bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 813 | hash_t *h = hash_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 815 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | return 0; |
| 817 | |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 818 | bytes = vec_mem_size (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 819 | |
| 820 | for (i = 0; i < hash_capacity (v); i++) |
| 821 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 822 | if (!hash_is_user (v, i)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 824 | hash_pair_union_t *p = get_pair (v, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | if (h->log2_pair_size > 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 826 | bytes += 1 << indirect_pair_get_log2_bytes (&p->indirect); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | else |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 828 | bytes += vec_mem_size (p->indirect.pairs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | return bytes; |
| 832 | } |
| 833 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 834 | __clib_export u8 * |
| 835 | format_hash (u8 *s, va_list *va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 836 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 837 | void *v = va_arg (*va, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 838 | int verbose = va_arg (*va, int); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 839 | hash_pair_t *p; |
| 840 | hash_t *h = hash_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | uword i; |
| 842 | |
| 843 | s = format (s, "hash %p, %wd elts, capacity %wd, %wd bytes used,\n", |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 844 | v, hash_elts (v), hash_capacity (v), hash_bytes (v)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 845 | |
| 846 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 847 | uword *occupancy = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 848 | |
| 849 | /* Count number of buckets with each occupancy. */ |
| 850 | for (i = 0; i < hash_capacity (v); i++) |
| 851 | { |
| 852 | uword j; |
| 853 | |
| 854 | if (hash_is_user (v, i)) |
| 855 | { |
| 856 | j = 1; |
| 857 | } |
| 858 | else |
| 859 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 860 | hash_pair_union_t *p = get_pair (v, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 861 | if (h->log2_pair_size > 0) |
| 862 | j = indirect_pair_get_len (&p->indirect); |
| 863 | else |
| 864 | j = vec_len (p->indirect.pairs); |
| 865 | } |
| 866 | |
| 867 | vec_validate (occupancy, j); |
| 868 | occupancy[j]++; |
| 869 | } |
| 870 | |
| 871 | s = format (s, " profile "); |
| 872 | for (i = 0; i < vec_len (occupancy); i++) |
| 873 | s = format (s, "%wd%c", occupancy[i], |
| 874 | i + 1 == vec_len (occupancy) ? '\n' : ' '); |
| 875 | |
| 876 | s = format (s, " lookup # of compares: "); |
| 877 | for (i = 1; i < vec_len (occupancy); i++) |
| 878 | s = format (s, "%wd: .%03d%c", i, |
| 879 | (1000 * i * occupancy[i]) / hash_elts (v), |
| 880 | i + 1 == vec_len (occupancy) ? '\n' : ' '); |
| 881 | |
| 882 | vec_free (occupancy); |
| 883 | } |
| 884 | |
| 885 | if (verbose) |
| 886 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 887 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 888 | hash_foreach_pair (p, v, { |
| 889 | s = format (s, " %U\n", h->format_pair, h->format_pair_arg, v, p); |
| 890 | }); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 891 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return s; |
| 895 | } |
| 896 | |
| 897 | static uword |
| 898 | unformat_hash_string_internal (unformat_input_t * input, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 899 | va_list * va, int is_vec) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 901 | uword *hash = va_arg (*va, uword *); |
| 902 | int *result = va_arg (*va, int *); |
| 903 | u8 *string = 0; |
| 904 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 906 | if (!unformat (input, is_vec ? "%v%_" : "%s%_", &string)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | |
| 909 | p = hash_get_mem (hash, string); |
| 910 | if (p) |
| 911 | *result = *p; |
| 912 | |
| 913 | vec_free (string); |
| 914 | return p ? 1 : 0; |
| 915 | } |
| 916 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 917 | __clib_export uword |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 918 | unformat_hash_vec_string (unformat_input_t * input, va_list * va) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 919 | { |
| 920 | return unformat_hash_string_internal (input, va, /* is_vec */ 1); |
| 921 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 922 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 923 | __clib_export uword |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | unformat_hash_string (unformat_input_t * input, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 926 | return unformat_hash_string_internal (input, va, /* is_vec */ 0); |
| 927 | } |
| 928 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 929 | __clib_export clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 930 | hash_validate (void *v) |
| 931 | { |
| 932 | hash_t *h = hash_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 933 | uword i, j; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 934 | uword *keys = 0; |
| 935 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 936 | |
| 937 | #define CHECK(x) if ((error = ERROR_ASSERT (x))) goto done; |
| 938 | |
| 939 | for (i = 0; i < hash_capacity (v); i++) |
| 940 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 941 | hash_pair_union_t *pu = get_pair (v, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 942 | |
| 943 | if (hash_is_user (v, i)) |
| 944 | { |
| 945 | CHECK (pu->direct.key != 0); |
| 946 | vec_add1 (keys, pu->direct.key); |
| 947 | } |
| 948 | else |
| 949 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 950 | hash_pair_t *p; |
| 951 | hash_pair_indirect_t *pi = &pu->indirect; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 952 | uword n; |
| 953 | |
| 954 | n = h->log2_pair_size > 0 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 955 | ? indirect_pair_get_len (pi) : vec_len (pi->pairs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 956 | |
| 957 | for (p = pi->pairs; n-- > 0; p = hash_forward1 (h, p)) |
| 958 | { |
| 959 | /* Assert key uniqueness. */ |
| 960 | for (j = 0; j < vec_len (keys); j++) |
| 961 | CHECK (keys[j] != p->key); |
| 962 | vec_add1 (keys, p->key); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | CHECK (vec_len (keys) == h->elts); |
| 968 | |
| 969 | vec_free (keys); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 970 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | return error; |
| 972 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 973 | |
| 974 | /* |
| 975 | * fd.io coding-style-patch-verification: ON |
| 976 | * |
| 977 | * Local Variables: |
| 978 | * eval: (c-set-style "gnu") |
| 979 | * End: |
| 980 | */ |