forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.h
1764 lines (1700 loc) · 56.9 KB
/
hash_table.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* @file
*
* High performance generic hash tables.
*
* See @ref HashTables.
*/
#ifndef DRGN_HASH_TABLE_H
#define DRGN_HASH_TABLE_H
#ifdef __SSE2__
#include <emmintrin.h> // IWYU pragma: keep
#endif
#ifdef __SSE4_2__
#include <nmmintrin.h>
#endif
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "bitops.h"
#include "cityhash.h"
#include "minmax.h"
#include "nstring.h" // IWYU pragma: export
#include "util.h"
/**
* @ingroup Internals
*
* @defgroup HashTables Hash tables
*
* High performance generic hash tables.
*
* This is an implementation of Facebook's <a
* href="https://github.com/facebook/folly/blob/master/folly/container/F14.md">
* F14</a>, which provides both high performance and good memory efficiency by
* using SIMD instructions to allow for a high load factor.
*
* These hash tables are generic, strongly typed (i.e., keys and values have
* static types rather than <tt>void *</tt>), and don't have any function
* pointer overhead.
*
* On non-x86 platforms, this falls back to a slower implementation that doesn't
* use SIMD.
*
* Abstractly, a hash table stores @em entries which can be looked up by @em
* key. A hash table is defined with @ref DEFINE_HASH_TABLE() (or the
* higher-level wrappers, @ref DEFINE_HASH_MAP() and @ref DEFINE_HASH_SET()).
* Each generated hash table interface is prefixed with a given name; the
* interface documented here uses the example name @c hash_table, which could be
* generated with this example code:
*
* @code{.c}
* key_type entry_to_key(const entry_type *entry);
* struct hash_pair hash_func(const key_type *key);
* bool eq_func(const key_type *a, const key_type *b);
* DEFINE_HASH_TABLE(hash_table, entry_type, entry_to_key, hash_func, eq_func)
* @endcode
*
* @sa BinarySearchTrees
*
* @{
*/
/**
* Double hash.
*
* @sa HashTableHelpers
*/
struct hash_pair {
/**
* First hash.
*
* F14 uses this to select the chunk.
*/
size_t first;
/**
* Second hash.
*
* F14 uses this as the tag within the chunk and as the probe stride
* when a chunk overflows.
*
* Only the 8 least-significant bits of this are used; the rest are zero
* (the folly implementation insists that storing this as @c size_t
* generates better code). The 8th bit is always set. This is derived
* from @ref hash_pair::first; see @ref
* hash_pair_from_avalanching_hash() and @ref
* hash_pair_from_non_avalanching_hash().
*/
size_t second;
};
#ifdef DOXYGEN
/**
* @struct hash_table
*
* Hash table instance.
*
* There are no requirements on how this is allocated; it may be global, on the
* stack, allocated by @c malloc(), embedded in another structure, etc.
*/
struct hash_table;
/**
* Hash table iterator.
*
* Several functions return an iterator or take one as an argument. This
* iterator has a reference to an entry, which can be @c NULL to indicate that
* there is no such entry. It also contains private bookkeeping which should not
* be used.
*
* An iterator remains valid until the table is rehashed or the entry or one
* before it is deleted.
*/
struct hash_table_iterator {
/** Pointer to the entry in the hash table. */
entry_type *entry;
};
/**
* Compute the hash for a given key.
*
* Note that this function is simply a wrapper around the hash function that was
* passed when defining the hash table. It is provided for convenience.
*/
struct hash_pair hash_table_hash(const key_type *key);
/**
* Initialize a @ref hash_table.
*
* The new hash table is empty. It must be deinitialized with @ref
* hash_table_deinit().
*
* @sa HASH_TABLE_INIT
*/
void hash_table_init(struct hash_table *table);
/**
* Free memory allocated by a @ref hash_table.
*
* After this is called, the hash table must not be used unless it is
* reinitialized with @ref hash_table_init(). Note that this only frees memory
* allocated by the hash table implementation; if the keys, values, or the hash
* table structure itself are dynamically allocated, those must be freed
* separately.
*/
void hash_table_deinit(struct hash_table *table);
/**
* Return whether a @ref hash_table has no entries.
*
* This is O(1).
*/
bool hash_table_empty(struct hash_table *table);
/**
* Return the number of entries in a @ref hash_table.
*
* This is O(1).
*/
size_t hash_table_size(struct hash_table *table);
/**
* Delete all entries in a @ref hash_table.
*
* This does not necessarily free memory used by the hash table.
*/
void hash_table_clear(struct hash_table *table);
/**
* Reserve entries in a @ref hash_table.
*
* This allocates space up front and rehashes the table to ensure that it will
* not be rehashed until it contains the given number of entries.
*
* @return @c true on success, @c false on failure.
*/
bool hash_table_reserve(struct hash_table *table, size_t capacity);
/**
* Insert an entry in a @ref hash_table.
*
* If an entry with the same key is already in the hash table, the entry is @em
* not inserted.
*
* @param[out] it_ret If not @c NULL, a returned iterator pointing to the newly
* inserted entry or the existing entry with the same key.
* @return 1 if the entry was inserted, 0 if the key already existed, -1 if
* allocating memory for a rehash failed.
*/
int hash_table_insert(struct hash_table *table, const entry_type *entry,
struct hash_table_iterator *it_ret);
/**
* Insert an entry in a @ref hash_table with a precomputed hash.
*
* Like @ref hash_table_insert(), but the hash was already computed. This saves
* recomputing the hash when doing multiple operations with the same key.
*/
int hash_table_insert_hashed(struct hash_table *table, const entry_type *entry,
struct hash_pair hp,
struct hash_table_iterator *it_ret);
/**
* Insert an entry in a @ref hash_table which is not in the table.
*
* Like @ref hash_table_insert_hashed(), but a search was previously done and
* the key is not already in the table. This saves doing a redundant search in
* that case but is unsafe otherwise.
*/
int hash_table_insert_searched(struct hash_table *table,
const entry_type *entry, struct hash_pair hp,
struct hash_table_iterator *it_ret);
/**
* Search for an entry in a @ref hash_table.
*
* @return An iterator pointing to the entry with the given key, or an iterator
* with <tt>entry == NULL</tt> if the key was not found.
*/
struct hash_table_iterator hash_table_search(struct hash_table *table,
const key_type *key);
/**
* Search for an entry in a @ref hash_table with a precomputed hash.
*
* Like @ref hash_table_search(), but the hash was already computed. This saves
* recomputing the hash when doing multiple operations with the same key.
*/
struct hash_table_iterator hash_table_search_hashed(struct hash_table *table,
const key_type *key,
struct hash_pair hp);
/**
* Delete an entry in a @ref hash_table.
*
* This deletes the entry with the given key. It will never rehash the table.
*
* @return @c true if the entry was found and deleted, @c false if not.
*/
bool hash_table_delete(struct hash_table *table, const key_type *key);
/**
* Delete an entry in a @ref hash_table with a precomputed hash.
*
* Like @ref hash_table_delete(), but the hash was already computed. This saves
* recomputing the hash when doing multiple operations with the same key.
*/
bool hash_table_delete_hashed(struct hash_table *table, struct hash_pair hp);
/**
* Delete an entry given by an iterator in a @ref hash_table.
*
* This deletes the entry pointed to by the iterator. It will never rehash the
* table.
*
* @return An iterator pointing to the next entry in the table. See @ref
* hash_table_next().
*/
struct hash_table_iterator
hash_table_delete_iterator(struct hash_table *table,
struct hash_table_iterator it);
/**
* Delete an entry given by an iterator in a @ref hash_table with a precomputed
* hash.
*
* Like @ref hash_table_delete_iterator(), but the hash was already computed.
* This saves recomputing the hash when doing multiple operations with the same
* key.
*/
struct hash_table_iterator
hash_table_delete_iterator_hashed(struct hash_table *table,
struct hash_table_iterator it,
struct hash_pair hp);
/**
* Get an iterator pointing to the first entry in a @ref hash_table.
*
* The first entry is arbitrary.
*
* @return An iterator pointing to the first entry, or an iterator with
* <tt>entry == NULL</tt> if the table is empty.
*/
struct hash_table_iterator hash_table_first(struct hash_table *table);
/**
* Get an iterator pointing to the next entry in a @ref hash_table.
*
* The order of entries is arbitrary.
*
* @return An iterator pointing to the next entry, or an iterator with <tt>entry
* == NULL</tt> if there are no more entries.
*/
struct hash_table_iterator hash_table_next(struct hash_table_iterator it);
#endif
enum {
hash_table_chunk_alignment = max_iconst(alignof(max_align_t),
(size_t)16),
};
static inline size_t hash_table_probe_delta(struct hash_pair hp)
{
return 2 * hp.second + 1;
}
static const uint8_t hosted_overflow_count_inc = 0x10;
static const uint8_t hosted_overflow_count_dec = -0x10;
/*
* We could represent an empty hash table with chunks set to NULL. However, then
* we would need a branch to check for this in insert, search, and delete. We
* could avoid this by allocating an empty chunk, but that is wasteful since it
* will never actually be used. Instead, we have a special empty chunk which is
* used by all tables.
*/
extern const uint8_t hash_table_empty_chunk_header[];
#define hash_table_empty_chunk (void *)hash_table_empty_chunk_header
#ifdef __SSE2__
#define HASH_TABLE_CHUNK_MATCH(table) \
static inline unsigned int table##_chunk_match(struct table##_chunk *chunk, \
size_t needle) \
{ \
__m128i tag_vec = _mm_load_si128((__m128i *)chunk); \
__m128i needle_vec = _mm_set1_epi8((uint8_t)needle); \
__m128i eq_vec = _mm_cmpeq_epi8(tag_vec, needle_vec); \
return _mm_movemask_epi8(eq_vec) & table##_chunk_full_mask; \
}
#define HASH_TABLE_CHUNK_OCCUPIED(table) \
static inline unsigned int table##_chunk_occupied(struct table##_chunk *chunk) \
{ \
__m128i tag_vec = _mm_load_si128((__m128i *)chunk); \
return _mm_movemask_epi8(tag_vec) & table##_chunk_full_mask; \
}
#else
#define HASH_TABLE_CHUNK_MATCH(table) \
static inline unsigned int table##_chunk_match(struct table##_chunk *chunk, \
size_t needle) \
{ \
unsigned int mask, i; \
for (mask = 0, i = 0; i < table##_chunk_capacity; i++) { \
if (chunk->tags[i] == needle) \
mask |= 1U << i; \
} \
return mask; \
}
#define HASH_TABLE_CHUNK_OCCUPIED(table) \
static inline unsigned int table##_chunk_occupied(struct table##_chunk *chunk) \
{ \
unsigned int mask, i; \
for (mask = 0, i = 0; i < table##_chunk_capacity; i++) { \
if (chunk->tags[i]) \
mask |= 1U << i; \
} \
return mask; \
}
#endif
/**
* Define a hash table type without defining its functions.
*
* This is useful when the hash table type must be defined in one place (e.g., a
* header) but the interface is defined elsewhere (e.g., a source file) with
* @ref DEFINE_HASH_TABLE_FUNCTIONS(). Otherwise, just use @ref
* DEFINE_HASH_TABLE().
*
* @sa DEFINE_HASH_TABLE()
*/
#define DEFINE_HASH_TABLE_TYPE(table, entry_type) \
typedef typeof(entry_type) table##_entry_type; \
\
enum { \
/* \
* Whether this table uses the vector storage policy. \
* \
* The vector policy provides the best performance and memory \
* efficiency for medium and large entries. \
*/ \
table##_vector_policy = sizeof(table##_entry_type) >= 24, \
}; \
\
struct table { \
struct table##_chunk *chunks; \
struct { \
/* \
* The vector storage policy stores 32-bit indices, so we only \
* need 32-bit sizes. \
*/ \
uint32_t chunk_mask; \
uint32_t size; \
/* Allocated together with chunks. */ \
table##_entry_type *entries; \
} vector[table##_vector_policy]; \
struct { \
size_t chunk_mask; \
size_t size; \
uintptr_t first_packed; \
} basic[!table##_vector_policy]; \
};
/*
* Common search function implementation returning an item iterator. This is
* shared by key lookups and index lookups.
*/
#define HASH_TABLE_SEARCH_IMPL(table, func, key_type, item_to_key, eq_func) \
static struct table##_iterator table##_##func(struct table *table, \
const key_type *key, \
struct hash_pair hp) \
{ \
const size_t delta = hash_table_probe_delta(hp); \
size_t index = hp.first; \
for (size_t tries = 0; tries <= table##_chunk_mask(table); tries++) { \
struct table##_chunk *chunk = \
&table->chunks[index & table##_chunk_mask(table)]; \
if (sizeof(*chunk) > 64) \
__builtin_prefetch(&chunk->items[8]); \
unsigned int mask = table##_chunk_match(chunk, hp.second), i; \
for_each_bit(i, mask) { \
table##_item_type *item = &chunk->items[i]; \
key_type item_key = item_to_key(table, item); \
if (likely(eq_func(key, &item_key))) { \
return (struct table##_iterator){ \
.item = item, \
.index = i, \
}; \
} \
} \
if (likely(chunk->outbound_overflow_count == 0)) \
break; \
index += delta; \
} \
return (struct table##_iterator){}; \
}
#define HASH_TABLE_SEARCH_BY_INDEX_ITEM_TO_KEY(table, item) (*(item)->index)
/**
* Define the functions for a hash table.
*
* The hash table type must have already been defined with @ref
* DEFINE_HASH_TABLE_TYPE().
*
* Unless the type and function definitions must be in separate places, use @ref
* DEFINE_HASH_TABLE() instead.
*/
#define DEFINE_HASH_TABLE_FUNCTIONS(table, entry_to_key, hash_func, eq_func) \
typedef typeof(entry_to_key((table##_entry_type *)0)) table##_key_type; \
\
static inline table##_key_type \
table##_entry_to_key(const table##_entry_type *entry) \
{ \
return entry_to_key(entry); \
} \
\
/* \
* Item stored in a chunk. \
* \
* When using the basic policy, the entry is stored directly in the item. When \
* using the vector policy, the item is an index to an out-of-band vector of \
* entries. \
* \
* C doesn't make it easy to define a type conditionally, so we use a nasty \
* hack: the member for the used policy is an array of length 1, and the unused \
* member is an array of length 0. We also have to force the struct to be \
* aligned only for the used member. \
*/ \
typedef struct { \
uint32_t index[table##_vector_policy]; \
table##_entry_type entry[!table##_vector_policy]; \
} __attribute__((__packed__, \
__aligned__(table##_vector_policy ? \
alignof(uint32_t) : alignof(table##_entry_type)))) \
table##_item_type; \
\
enum { \
/* \
* The number of items per chunk. 14 is the most space efficient, but \
* if an item is 4 bytes, 12 items makes a chunk exactly one cache \
* line. \
*/ \
table##_chunk_capacity = sizeof(table##_item_type) == 4 ? 12 : 14, \
/* The maximum load factor in terms of items per chunk. */ \
table##_chunk_desired_capacity = table##_chunk_capacity - 2, \
/* \
* If an item is 16 bytes, add an extra 16 bytes of padding to make a \
* chunk exactly four cache lines. \
*/ \
table##_chunk_allocated_capacity = \
(table##_chunk_capacity + \
(sizeof(table##_item_type) == 16 ? 1 : 0)), \
/* \
* If the chunk capacity is 12, we can use tags 12 and 13 for 16 bits. \
* Otherwise, we only get 4 from control. \
*/ \
table##_capacity_scale_bits = table##_chunk_capacity == 12 ? 16 : 4, \
table##_capacity_scale_shift = table##_capacity_scale_bits - 4, \
table##_chunk_full_mask = (1 << table##_chunk_capacity) - 1, \
}; \
\
struct table##_chunk { \
uint8_t tags[14]; \
/* \
* The lower 4 bits are capacity_scale: for the first chunk, this is \
* the scaling factor between the chunk count and the capacity; for \
* other chunks, this is zero. \
* \
* The upper 4 bits are hosted_overflow_count: the number of entries in \
* this chunk that overflowed their desired chunk. \
*/ \
uint8_t control; \
/* \
* The number of entries that would have been in this chunk if it were \
* not full. This value saturates if it hits 255, after which it will \
* not be updated. \
*/ \
uint8_t outbound_overflow_count; \
table##_item_type items[table##_chunk_allocated_capacity]; \
} __attribute__((__aligned__(hash_table_chunk_alignment))); \
\
/* \
* This may be a "public iterator" (used by the public interface to refer to an \
* entry) or an "item iterator" (used by certain internal functions to refer to \
* an item regardless of the storage policy). \
*/ \
struct table##_iterator { \
union { \
/* Entry if public iterator. */ \
table##_entry_type *entry; \
/* \
* Item if item iterator. Interchangable with entry when using \
* the basic storage policy. \
*/ \
table##_item_type *item; \
}; \
union { \
/* \
* Lowest entry if public iterator and using the vector storage \
* policy (i.e., table->vector->entries). \
*/ \
table##_entry_type *lowest; \
/* \
* Index of item in its containing chunk if item iterator or \
* using the basic storage policy. \
*/ \
size_t index; \
}; \
}; \
\
static inline struct hash_pair table##_hash(const table##_key_type *key) \
{ \
return hash_func(key); \
} \
\
static inline table##_entry_type * \
table##_item_to_entry(struct table *table, table##_item_type *item) \
{ \
if (table##_vector_policy) { \
return &table->vector->entries[*item->index]; \
} else { \
/* \
* Returning item->entry directly results in a false positive \
* -Waddress-of-packed-member warning. \
*/ \
void *entry = item->entry; \
return entry; \
} \
} \
\
static inline table##_key_type \
table##_item_to_key(struct table *table, table##_item_type *item) \
{ \
return table##_entry_to_key(table##_item_to_entry(table, item)); \
} \
\
/* \
* We cache the first position in the table as a tagged pointer: we steal the \
* bottom bits of the chunk pointer for the entry index. We can do this because \
* chunks are aligned to 16 bytes and the index is always less than 16. \
* \
* The folly implementation mentions this strategy but uses a more complicated \
* scheme in order to avoid computing the chunk pointer from an entry pointer. \
* We always have the chunk pointer readily available when we want to pack an \
* entry, so we can use this much simpler scheme. \
*/ \
static inline uintptr_t table##_pack_iterator(struct table##_chunk *chunk, \
size_t index) \
{ \
return (uintptr_t)chunk | (uintptr_t)index; \
} \
\
static inline struct table##_chunk *table##_unpack_chunk(uintptr_t packed) \
{ \
return (struct table##_chunk *)(packed & ~(uintptr_t)0xf); \
} \
\
static inline size_t table##_unpack_index(uintptr_t packed) \
{ \
return packed & 0xf; \
} \
\
static inline struct table##_iterator table##_unpack_iterator(uintptr_t packed) \
{ \
struct table##_chunk *chunk = table##_unpack_chunk(packed); \
size_t index = table##_unpack_index(packed); \
return (struct table##_iterator) { \
.item = chunk ? &chunk->items[index] : NULL, \
.index = index, \
}; \
} \
\
static inline struct table##_chunk * \
table##_iterator_chunk(struct table##_iterator it) \
{ \
return container_of(it.item - it.index, struct table##_chunk, items[0]);\
} \
\
HASH_TABLE_CHUNK_MATCH(table) \
HASH_TABLE_CHUNK_OCCUPIED(table) \
\
static inline unsigned int \
table##_chunk_first_empty(struct table##_chunk *chunk) \
{ \
unsigned int mask = \
table##_chunk_occupied(chunk) ^ table##_chunk_full_mask; \
return mask ? ctz(mask) : (unsigned int)-1; \
} \
\
static inline unsigned int \
table##_chunk_last_occupied(struct table##_chunk *chunk) \
{ \
unsigned int mask = table##_chunk_occupied(chunk); \
return mask ? fls(mask) - 1 : (unsigned int)-1; \
} \
\
static inline size_t \
table##_chunk_hosted_overflow_count(struct table##_chunk *chunk) \
{ \
return chunk->control >> 4; \
} \
\
static inline void \
table##_chunk_adjust_hosted_overflow_count(struct table##_chunk *chunk, \
size_t op) \
{ \
chunk->control += op; \
} \
\
static inline size_t table##_chunk_capacity_scale(struct table##_chunk *chunk) \
{ \
if (table##_capacity_scale_bits == 4) { \
return chunk->control & 0xf; \
} else { \
uint16_t val; \
memcpy(&val, &chunk->tags[12], 2); \
return val; \
} \
} \
\
static inline bool table##_chunk_eof(struct table##_chunk *chunk) \
{ \
return table##_chunk_capacity_scale(chunk) != 0; \
} \
\
static inline void table##_chunk_mark_eof(struct table##_chunk *chunk, \
size_t capacity_scale) \
{ \
if (table##_capacity_scale_bits == 4) { \
chunk->control = capacity_scale; \
} else { \
uint16_t val = capacity_scale; \
memcpy(&chunk->tags[12], &val, 2); \
} \
} \
\
static inline void \
table##_chunk_inc_outbound_overflow_count(struct table##_chunk *chunk) \
{ \
if (chunk->outbound_overflow_count != UINT8_MAX) \
chunk->outbound_overflow_count++; \
} \
\
static inline void \
table##_chunk_dec_outbound_overflow_count(struct table##_chunk *chunk) \
{ \
if (chunk->outbound_overflow_count != UINT8_MAX) \
chunk->outbound_overflow_count--; \
} \
\
__attribute__((__unused__)) \
static void table##_init(struct table *table) \
{ \
table->chunks = hash_table_empty_chunk; \
if (table##_vector_policy) { \
table->vector->chunk_mask = 0; \
table->vector->size = 0; \
table->vector->entries = NULL; \
} else { \
table->basic->chunk_mask = 0; \
table->basic->size = 0; \
table->basic->first_packed = 0; \
} \
} \
\
__attribute__((__unused__)) \
static void table##_deinit(struct table *table) \
{ \
if (table->chunks != hash_table_empty_chunk) \
free(table->chunks); \
} \
\
static inline size_t table##_size(struct table *table) \
{ \
if (table##_vector_policy) \
return table->vector->size; \
else \
return table->basic->size; \
} \
\
static inline void table##_set_size(struct table *table, size_t size) \
{ \
if (table##_vector_policy) \
table->vector->size = size; \
else \
table->basic->size = size; \
} \
\
static inline size_t table##_chunk_mask(struct table *table) \
{ \
if (table##_vector_policy) \
return table->vector->chunk_mask; \
else \
return table->basic->chunk_mask; \
} \
\
static inline void table##_set_chunk_mask(struct table *table, \
size_t chunk_mask) \
{ \
if (table##_vector_policy) \
table->vector->chunk_mask = chunk_mask; \
else \
table->basic->chunk_mask = chunk_mask; \
} \
\
__attribute__((__unused__)) \
static inline bool table##_empty(struct table *table) \
{ \
return table##_size(table) == 0; \
} \
\
static table##_item_type *table##_allocate_tag(struct table *table, \
uint8_t *fullness, \
struct hash_pair hp) \
{ \
const size_t delta = hash_table_probe_delta(hp); \
size_t index = hp.first; \
struct table##_chunk *chunk; \
uint8_t hosted_op = 0; \
for (;;) { \
index &= table##_chunk_mask(table); \
chunk = &table->chunks[index]; \
if (likely(fullness[index] < table##_chunk_capacity)) \
break; \
table##_chunk_inc_outbound_overflow_count(chunk); \
hosted_op = hosted_overflow_count_inc; \
index += delta; \
} \
size_t item_index = fullness[index]++; \
chunk->tags[item_index] = hp.second; \
table##_chunk_adjust_hosted_overflow_count(chunk, hosted_op); \
return &chunk->items[item_index]; \
} \
\
static size_t table##_compute_capacity(size_t chunk_count, size_t scale) \
{ \
return (((chunk_count - 1) >> table##_capacity_scale_shift) + 1) * scale;\
} \
\
static bool \
table##_compute_chunk_count_and_scale(size_t capacity, \
bool continuous_single_chunk_capacity, \
bool continuous_multi_chunk_capacity, \
size_t *chunk_count_ret, \
size_t *scale_ret) \
{ \
if (capacity <= table##_chunk_capacity) { \
if (!continuous_single_chunk_capacity) { \
if (capacity <= 2) \
capacity = 2; \
else if (capacity <= 6) \
capacity = 6; \
else \
capacity = table##_chunk_capacity; \
} \
*chunk_count_ret = 1; \
*scale_ret = capacity; \
} else { \
size_t min_chunks = \
(capacity - 1) / table##_chunk_desired_capacity + 1; \
size_t chunk_pow = fls(min_chunks - 1); \
if (chunk_pow == 8 * sizeof(size_t)) \
return false; \
size_t chunk_count = (size_t)1 << chunk_pow; \
size_t ss = (chunk_pow >= table##_capacity_scale_shift ? \
chunk_pow - table##_capacity_scale_shift : 0); \
size_t scale = \
continuous_multi_chunk_capacity ? \
((capacity - 1) >> ss) + 1 : \
table##_chunk_desired_capacity << (chunk_pow - ss); \
if (table##_vector_policy && \
table##_compute_capacity(chunk_count, scale) > UINT32_MAX) \
return false; \
*chunk_count_ret = chunk_count; \
*scale_ret = scale; \
} \
return true; \
} \
\
static inline size_t table##_chunk_alloc_size(size_t chunk_count, \
size_t capacity_scale) \
{ \
/* \
* Small hash tables are common, so for capacities of less than a full \
* chunk, we only allocate the required items. \
*/ \
if (chunk_count == 1) { \
return (offsetof(struct table##_chunk, items) + \
table##_compute_capacity(1, capacity_scale) * \
sizeof(table##_item_type)); \
} else { \
return chunk_count * sizeof(struct table##_chunk); \
} \
} \
\
static bool table##_rehash(struct table *table, size_t orig_chunk_count, \
size_t orig_capacity_scale, size_t new_chunk_count, \
size_t new_capacity_scale) \
{ \
size_t chunk_alloc_size = table##_chunk_alloc_size(new_chunk_count, \
new_capacity_scale); \
size_t alloc_size, entries_offset; \
if (table##_vector_policy) { \
entries_offset = chunk_alloc_size; \
if (alignof(table##_entry_type) > alignof(table##_item_type)) { \
entries_offset = -(-entries_offset & \
~(alignof(table##_entry_type) - 1)); \
} \
size_t new_capacity = \
table##_compute_capacity(new_chunk_count, \
new_capacity_scale); \
alloc_size = (entries_offset + \
new_capacity * sizeof(table##_entry_type)); \
} else { \
alloc_size = chunk_alloc_size; \
} \
\
void *new_chunks; \
if (posix_memalign(&new_chunks, hash_table_chunk_alignment, alloc_size))\
return false; \
\
struct table##_chunk *orig_chunks = table->chunks; \
table->chunks = new_chunks; \
table##_entry_type *orig_entries; \
if (table##_vector_policy) { \
orig_entries = table->vector->entries; \
table->vector->entries = new_chunks + entries_offset; \
if (table##_size(table) > 0) { \
memcpy(table->vector->entries, orig_entries, \
table##_size(table) * \
sizeof(table##_entry_type)); \
} \
} \
\
memset(table->chunks, 0, chunk_alloc_size); \
table##_chunk_mark_eof(table->chunks, new_capacity_scale); \
table##_set_chunk_mask(table, new_chunk_count - 1); \
\
if (table##_size(table) == 0) { \
/* Nothing to do. */ \
} else if (orig_chunk_count == 1 && new_chunk_count == 1) { \
struct table##_chunk *src = orig_chunks; \
struct table##_chunk *dst = table->chunks; \
size_t src_i = 0, dst_i = 0; \
while (dst_i < table##_size(table)) { \
if (likely(src->tags[src_i])) { \
dst->tags[dst_i] = src->tags[src_i]; \
memcpy(&dst->items[dst_i], &src->items[src_i], \
sizeof(dst->items[dst_i])); \
dst_i++; \
} \
src_i++; \
} \
if (!table##_vector_policy) { \
table->basic->first_packed = \
table##_pack_iterator(dst, dst_i - 1); \
} \
} else { \
uint8_t stack_fullness[256]; \
uint8_t *fullness; \
if (new_chunk_count <= sizeof(stack_fullness)) { \
memset(stack_fullness, 0, sizeof(stack_fullness)); \
fullness = stack_fullness; \
} else { \
fullness = calloc(new_chunk_count, 1); \
if (!fullness) \
goto err; \
} \
\
struct table##_chunk *src = &orig_chunks[orig_chunk_count - 1]; \
size_t remaining = table##_size(table); \
while (remaining) { \
unsigned int mask = table##_chunk_occupied(src), i; \
if (table##_vector_policy) { \
unsigned int pmask = mask; \
for_each_bit(i, pmask) \
__builtin_prefetch(&src->items[i]); \
} \
for_each_bit(i, mask) { \
remaining--; \
\
table##_item_type *src_item = &src->items[i]; \
table##_key_type key = \
table##_item_to_key(table, src_item); \
struct hash_pair hp = table##_hash(&key); \
table##_item_type *dst_item = \
table##_allocate_tag(table, fullness, \
hp); \
memcpy(dst_item, src_item, sizeof(*dst_item)); \
} \
src--; \
} \
\
if (!table##_vector_policy) { \
size_t i = table##_chunk_mask(table); \
while (fullness[i] == 0) \
i--; \
table->basic->first_packed = \
table##_pack_iterator(&table->chunks[i], \
fullness[i] - 1); \
} \
\
if (fullness != stack_fullness) \
free(fullness); \
} \
\
if (orig_chunks != hash_table_empty_chunk) \
free(orig_chunks); \
return true; \
\
err: \
free(table->chunks); \
table->chunks = orig_chunks; \
table##_set_chunk_mask(table, orig_chunk_count - 1); \
if (table##_vector_policy) \
table->vector->entries = orig_entries; \
return false; \
} \
\
static void table##_do_clear(struct table *table, bool reset) \
{ \
if (table->chunks == hash_table_empty_chunk) \
return; \
\
size_t chunk_count = table##_chunk_mask(table) + 1; \
/* Always reset large tables. */ \
if (chunk_count >= 16) \
reset = true; \
if (!table##_empty(table)) { \
if (!reset) { \
size_t capacity_scale = \
table##_chunk_capacity_scale(table->chunks); \
memset(table->chunks, 0, \
table##_chunk_alloc_size(chunk_count, \
capacity_scale)); \
table##_chunk_mark_eof(table->chunks, capacity_scale); \
} \
if (!table##_vector_policy) \
table->basic->first_packed = 0; \
table##_set_size(table, 0); \
} \
if (reset) { \
free(table->chunks); \
table->chunks = hash_table_empty_chunk; \
table##_set_chunk_mask(table, 0); \
if (table##_vector_policy) \
table->vector->entries = NULL; \
} \
} \
\
__attribute__((__unused__)) \
static bool table##_reserve(struct table *table, size_t capacity) \