38 #ifndef included_hash_h 39 #define included_hash_h 62 #define HASH_FLAG_NO_AUTO_GROW (1 << 0) 64 #define HASH_FLAG_NO_AUTO_SHRINK (1 << 1) 66 #define HASH_FLAG_HASH_NEXT_IN_PROGRESS (1 << 2) 76 #define KEY_FUNC_NONE (0) 77 #define KEY_FUNC_POINTER_UWORD (1) 78 #define KEY_FUNC_POINTER_U32 (2) 79 #define KEY_FUNC_STRING (3) 103 return sizeof (h[0]) + is_user_bytes;
114 return v ? h->
elts : 0;
174 #define LOG2_ALLOC_BITS (5) 175 #define PAIR_BITS (BITS (uword) - LOG2_ALLOC_BITS) 209 void * _hash_unset (
void * v,
uword key,
void * old_value);
212 void * _hash_set3 (
void * v,
uword key,
void * value,
void * old_value);
224 #define hash_set3(h,key,value,old_value) \ 226 uword _v = (uword) (value); \ 227 (h) = _hash_set3 ((h), (uword) (key), (void *) &_v, (old_value)); \ 231 #define hash_get(h,key) _hash_get ((h), (uword) (key)) 234 #define hash_get_pair(h,key) _hash_get_pair ((h), (uword) (key)) 237 #define hash_set(h,key,value) hash_set3(h,key,value,0) 240 #define hash_set1(h,key) (h) = _hash_set3(h,(uword) (key),0,0) 243 #define hash_unset(h,key) ((h) = _hash_unset ((h), (uword) (key),0)) 246 #define hash_unset3(h,key,old_value) ((h) = _hash_unset ((h), (uword) (key), (void *) (old_value))) 251 #define hash_get_mem(h,key) _hash_get ((h), pointer_to_uword (key)) 254 #define hash_get_pair_mem(h,key) _hash_get_pair ((h), pointer_to_uword (key)) 257 #define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0) 260 #define hash_set1_mem(h,key) hash_set3 ((h), pointer_to_uword (key), 0, 0) 263 #define hash_unset_mem(h,key) ((h) = _hash_unset ((h), pointer_to_uword (key),0)) 266 extern void * _hash_free (
void * v);
269 #define hash_free(h) (h) = _hash_free ((h)) 311 #define hash_foreach_pair(p,v,body) \ 313 __label__ _hash_foreach_done; \ 314 hash_t * _h = hash_header (v); \ 315 hash_pair_union_t * _p; \ 316 hash_pair_t * _q, * _q_end; \ 317 uword _i, _i1, _id, _pair_increment; \ 319 _p = (hash_pair_union_t *) (v); \ 321 _pair_increment = 1; \ 323 _pair_increment = 1 << _h->log2_pair_size; \ 324 while (_i < hash_capacity (v)) \ 326 _id = _h->is_user[_i / BITS (_h->is_user[0])]; \ 327 _i1 = _i + BITS (_h->is_user[0]); \ 333 _q_end = _q + _pair_increment; \ 337 hash_pair_indirect_t * _pi = &_p->indirect; \ 339 if (_h->log2_pair_size > 0) \ 340 _q_end = hash_forward (_h, _q, indirect_pair_get_len (_pi)); \ 342 _q_end = vec_end (_q); \ 347 while (_q < _q_end) \ 349 uword _break_in_body = 1; \ 353 _break_in_body = 0; \ 355 if (_break_in_body) \ 356 goto _hash_foreach_done; \ 357 _q += _pair_increment; \ 360 _p = (hash_pair_union_t *) (&_p->direct + _pair_increment); \ 363 } while (_i < _i1); \ 365 _hash_foreach_done: \ 380 #define hash_foreach(key_var,value_var,h,body) \ 383 hash_foreach_pair (_r, (h), { \ 384 (key_var) = (__typeof__ (key_var)) _r->key; \ 385 (value_var) = (__typeof__ (value_var)) _r->value[0]; \ 386 do { body; } while (0); \ 399 #define hash_foreach_mem(key_var,value_var,h,body) \ 402 hash_foreach_pair (_r, (h), { \ 403 (key_var) = (__typeof__ (key_var)) uword_to_pointer (_r->key, void *); \ 404 (value_var) = (__typeof__ (value_var)) _r->value[0]; \ 405 do { body; } while (0); \ 426 max_log2 ((
sizeof (p->
key) + value_bytes + sizeof (p->
key) - 1) / sizeof (p->
key));
429 #define hash_create2(_elts,_user,_value_bytes, \ 430 _key_sum,_key_equal, \ 431 _format_pair,_format_pair_arg) \ 434 memset (&_h, 0, sizeof (_h)); \ 436 _h.key_sum = (hash_key_sum_function_t *) (_key_sum); \ 437 _h.key_equal = (_key_equal); \ 438 hash_set_value_bytes (&_h, (_value_bytes)); \ 439 _h.format_pair = (format_function_t *) (_format_pair); \ 440 _h.format_pair_arg = (_format_pair_arg); \ 441 _hash_create ((_elts), &_h); \ 448 #define hash_mix_step(a,b,c,s0,s1,s2) \ 450 (a) -= (b) + (c); (a) ^= (c) >> (s0); \ 451 (b) -= (c) + (a); (b) ^= (a) << (s1); \ 452 (c) -= (a) + (b); (c) ^= (b) >> (s2); \ 455 #define hash_mix32_step_1(a,b,c) hash_mix_step(a,b,c,13,8,13) 456 #define hash_mix32_step_2(a,b,c) hash_mix_step(a,b,c,12,16,5) 457 #define hash_mix32_step_3(a,b,c) hash_mix_step(a,b,c,3,10,15) 459 #define hash_mix64_step_1(a,b,c) hash_mix_step(a,b,c,43,9,8) 460 #define hash_mix64_step_2(a,b,c) hash_mix_step(a,b,c,38,23,5) 461 #define hash_mix64_step_3(a,b,c) hash_mix_step(a,b,c,35,49,11) 462 #define hash_mix64_step_4(a,b,c) hash_mix_step(a,b,c,12,18,22) 466 #define hash_mix64(a0,b0,c0) \ 468 hash_mix64_step_1 (a0, b0, c0); \ 469 hash_mix64_step_2 (a0, b0, c0); \ 470 hash_mix64_step_3 (a0, b0, c0); \ 471 hash_mix64_step_4 (a0, b0, c0); \ 474 #define hash_mix32(a0,b0,c0) \ 476 hash_mix32_step_1 (a0, b0, c0); \ 477 hash_mix32_step_2 (a0, b0, c0); \ 478 hash_mix32_step_3 (a0, b0, c0); \ 485 {
return (x << i) | (x >> (
BITS (i) -
i)); }
487 #define hash_v3_mix32(a,b,c) \ 489 (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \ 490 (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \ 491 (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \ 492 (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \ 493 (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \ 494 (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \ 497 #define hash_v3_finalize32(a,b,c) \ 499 (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \ 500 (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \ 501 (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \ 502 (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \ 503 (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \ 504 (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \ 505 (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \ 510 #define hash_v3_mix32_step1(a,b,c) \ 512 (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \ 513 (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \ 516 #define hash_v3_mix32_step2(a,b,c) \ 518 (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \ 519 (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \ 522 #define hash_v3_mix32_step3(a,b,c) \ 524 (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \ 525 (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \ 528 #define hash_v3_finalize32_step1(a,b,c) \ 530 (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \ 531 (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \ 534 #define hash_v3_finalize32_step2(a,b,c) \ 536 (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \ 537 (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \ 540 #define hash_v3_finalize32_step3(a,b,c) \ 542 (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \ 543 (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \ 544 (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \ 548 #define hash_v3_mix_step_1_u32x(a,b,c) \ 550 (a) -= (c); (a) ^= u32x_irotate_left ((c), 4); (c) += (b); \ 551 (b) -= (a); (b) ^= u32x_irotate_left ((a), 6); (a) += (c); \ 552 (c) -= (b); (c) ^= u32x_irotate_left ((b), 8); (b) += (a); \ 555 #define hash_v3_mix_step_2_u32x(a,b,c) \ 557 (a) -= (c); (a) ^= u32x_irotate_left ((c),16); (c) += (b); \ 558 (b) -= (a); (b) ^= u32x_irotate_left ((a),19); (a) += (c); \ 559 (c) -= (b); (c) ^= u32x_irotate_left ((b), 4); (b) += (a); \ 562 #define hash_v3_finalize_step_1_u32x(a,b,c) \ 564 (c) ^= (b); (c) -= u32x_irotate_left ((b), 14); \ 565 (a) ^= (c); (a) -= u32x_irotate_left ((c), 11); \ 566 (b) ^= (a); (b) -= u32x_irotate_left ((a), 25); \ 569 #define hash_v3_finalize_step_2_u32x(a,b,c) \ 571 (c) ^= (b); (c) -= u32x_irotate_left ((b), 16); \ 572 (a) ^= (c); (a) -= u32x_irotate_left ((c), 4); \ 573 (b) ^= (a); (b) -= u32x_irotate_left ((a), 14); \ 574 (c) ^= (b); (c) -= u32x_irotate_left ((b), 24); \ 577 #define hash_v3_mix_u32x(a,b,c) \ 579 hash_v3_mix_step_1_u32x(a,b,c); \ 580 hash_v3_mix_step_2_u32x(a,b,c); \ 583 #define hash_v3_finalize_u32x(a,b,c) \ 585 hash_v3_finalize_step_1_u32x(a,b,c); \ 586 hash_v3_finalize_step_2_u32x(a,b,c); \ 594 #define hash_create_mem(elts,key_bytes,value_bytes) \ 595 hash_create2((elts),(key_bytes),(value_bytes),mem_key_sum,mem_key_equal,0,0) 601 #define hash_create_vec(elts,key_bytes,value_bytes) \ 602 hash_create2((elts),(key_bytes),(value_bytes),\ 603 vec_key_sum,vec_key_equal,vec_key_format_pair,0) 609 #define hash_create_string(elts,value_bytes) \ 610 hash_create2((elts),0,(value_bytes), \ 611 (hash_key_sum_function_t *) KEY_FUNC_STRING, \ 612 (hash_key_equal_function_t *)KEY_FUNC_STRING, \ 615 #define hash_create(elts,value_bytes) \ 616 hash_create2((elts),0,(value_bytes), \ 617 (hash_key_sum_function_t *) KEY_FUNC_NONE, \ 618 (hash_key_equal_function_t *) KEY_FUNC_NONE, \ 621 #define hash_create_uword(elts,value_bytes) \ 622 hash_create2((elts),0,(value_bytes), \ 623 (hash_key_sum_function_t *) KEY_FUNC_POINTER_UWORD, \ 624 (hash_key_equal_function_t *) KEY_FUNC_POINTER_UWORD, \ 627 #define hash_create_u32(elts,value_bytes) \ 628 hash_create2((elts),0,(value_bytes), \ 629 (hash_key_sum_function_t *) KEY_FUNC_POINTER_U32, \ 630 (hash_key_equal_function_t *) KEY_FUNC_POINTER_U32, \
clib_error_t * hash_validate(void *v)
sll srl srl sll sra u16x4 i
uword hash_bytes(void *v)
unformat_function_t unformat_hash_string
always_inline void hash_set_value_bytes(hash_t *h, uword value_bytes)
uword hash_memory(void *p, word n_bytes, uword state)
always_inline void indirect_pair_set(hash_pair_indirect_t *p, uword log2_alloc, uword len)
u8 * format_hash(u8 *s, va_list *va)
always_inline hash_t * hash_header(void *v)
always_inline uword hash_header_bytes(void *v)
uword mem_key_sum(hash_t *h, uword key)
int test_hash_main(unformat_input_t *input)
always_inline uword max_log2(uword x)
uword vec_key_equal(hash_t *h, uword key1, uword key2)
uword( hash_key_equal_function_t)(struct hash_header *, uword key1, uword key2)
always_inline void * hash_forward(hash_t *h, void *v, uword n)
always_inline uword hash_capacity(void *v)
always_inline uword hash_is_user(void *v, uword i)
always_inline void hash_set_pair_format(void *v, format_function_t *format_pair, void *format_pair_arg)
void * hash_dup(void *old)
uword string_key_equal(hash_t *h, uword key1, uword key2)
void * hash_resize(void *old, uword new_size)
always_inline void hash_set_flags(void *v, uword flags)
unformat_function_t unformat_hash_vec_string
always_inline uword hash_pair_log2_bytes(hash_t *h)
always_inline uword hash32_rotate_left(u32 x, u32 i)
hash_pair_indirect_t indirect
always_inline uword hash_pair_bytes(hash_t *h)
u8 * string_key_format_pair(u8 *s, va_list *args)
uword mem_key_equal(hash_t *h, uword key1, uword key2)
always_inline uword hash_elts(void *v)
always_inline uword indirect_pair_get_len(hash_pair_indirect_t *p)
always_inline uword indirect_pair_get_log2_bytes(hash_pair_indirect_t *p)
vhost_vring_state_t state
hash_pair_t * hash_next(void *v, hash_next_t *hn)
uword( hash_key_sum_function_t)(struct hash_header *, uword key)
u8 * vec_key_format_pair(u8 *s, va_list *args)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
struct hash_header hash_t
always_inline void * hash_forward1(hash_t *h, void *v)
always_inline uword hash_value_bytes(hash_t *h)
always_inline void * vec_header(void *v, uword header_bytes)
Find a user vector header.
uword string_key_sum(hash_t *h, uword key)
uword vec_key_sum(hash_t *h, uword key)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".