-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathCFBasicHash.c
1770 lines (1605 loc) · 75.4 KB
/
CFBasicHash.c
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) 2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFBasicHash.m
Copyright (c) 2008-2014, Apple Inc. All rights reserved.
Responsibility: Christopher Kane
*/
#import "CFBasicHash.h"
#import <CoreFoundation/CFRuntime.h>
#import <CoreFoundation/CFSet.h>
#import <Block.h>
#import <math.h>
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#import <dispatch/dispatch.h>
#endif
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#define __SetLastAllocationEventName(A, B) do { if (__CFOASafe && (A)) __CFSetLastAllocationEventName(A, B); } while (0)
#else
#define __SetLastAllocationEventName(A, B) do { } while (0)
#endif
#define __AssignWithWriteBarrier(location, value) objc_assign_strongCast((id)value, (id *)location)
#define ENABLE_DTRACE_PROBES 0
#define ENABLE_MEMORY_COUNTERS 0
#if defined(DTRACE_PROBES_DISABLED) && DTRACE_PROBES_DISABLED
#undef ENABLE_DTRACE_PROBES
#define ENABLE_DTRACE_PROBES 0
#endif
/*
// dtrace -h -s foo.d
// Note: output then changed by casts of the arguments
// dtrace macros last generated 2010-09-08 on 10.7 prerelease (11A259)
provider Cocoa_HashTable {
probe hash_key(unsigned long table, unsigned long key, unsigned long hash);
probe test_equal(unsigned long table, unsigned long key1, unsigned long key2);
probe probing_start(unsigned long table, unsigned long num_buckets);
probe probe_empty(unsigned long table, unsigned long idx);
probe probe_deleted(unsigned long table, unsigned long idx);
probe probe_valid(unsigned long table, unsigned long idx);
probe probing_end(unsigned long table, unsigned long num_probes);
probe rehash_start(unsigned long table, unsigned long num_buckets, unsigned long total_size);
probe rehash_end(unsigned long table, unsigned long num_buckets, unsigned long total_size);
};
#pragma D attributes Unstable/Unstable/Common provider Cocoa_HashTable provider
#pragma D attributes Private/Private/Unknown provider Cocoa_HashTable module
#pragma D attributes Private/Private/Unknown provider Cocoa_HashTable function
#pragma D attributes Unstable/Unstable/Common provider Cocoa_HashTable name
#pragma D attributes Unstable/Unstable/Common provider Cocoa_HashTable args
*/
#if ENABLE_DTRACE_PROBES
#define COCOA_HASHTABLE_STABILITY "___dtrace_stability$Cocoa_HashTable$v1$4_4_5_1_1_0_1_1_0_4_4_5_4_4_5"
#define COCOA_HASHTABLE_TYPEDEFS "___dtrace_typedefs$Cocoa_HashTable$v2"
#define COCOA_HASHTABLE_REHASH_END(arg0, arg1, arg2) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$rehash_end$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1), (unsigned long)(arg2)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_REHASH_END_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$rehash_end$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_REHASH_START(arg0, arg1, arg2) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$rehash_start$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1), (unsigned long)(arg2)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_REHASH_START_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$rehash_start$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_HASH_KEY(arg0, arg1, arg2) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$hash_key$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1), (unsigned long)(arg2)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_HASH_KEY_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$hash_key$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_PROBE_DELETED(arg0, arg1) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$probe_deleted$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_PROBE_DELETED_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$probe_deleted$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_PROBE_EMPTY(arg0, arg1) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$probe_empty$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_PROBE_EMPTY_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$probe_empty$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_PROBE_VALID(arg0, arg1) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$probe_valid$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_PROBE_VALID_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$probe_valid$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_PROBING_END(arg0, arg1) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$probing_end$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_PROBING_END_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$probing_end$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_PROBING_START(arg0, arg1) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$probing_start$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_PROBING_START_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$probing_start$v1(); \
__asm__ volatile(""); \
_r; })
#define COCOA_HASHTABLE_TEST_EQUAL(arg0, arg1, arg2) \
do { \
__asm__ volatile(".reference " COCOA_HASHTABLE_TYPEDEFS); \
__dtrace_probe$Cocoa_HashTable$test_equal$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67((unsigned long)(arg0), (unsigned long)(arg1), (unsigned long)(arg2)); \
__asm__ volatile(".reference " COCOA_HASHTABLE_STABILITY); \
} while (0)
#define COCOA_HASHTABLE_TEST_EQUAL_ENABLED() \
({ int _r = __dtrace_isenabled$Cocoa_HashTable$test_equal$v1(); \
__asm__ volatile(""); \
_r; })
extern void __dtrace_probe$Cocoa_HashTable$hash_key$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$hash_key$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$probe_deleted$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$probe_deleted$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$probe_empty$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$probe_empty$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$probe_valid$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$probe_valid$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$probing_end$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$probing_end$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$probing_start$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$probing_start$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$rehash_end$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$rehash_end$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$rehash_start$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$rehash_start$v1(void);
extern void __dtrace_probe$Cocoa_HashTable$test_equal$v1$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67$756e7369676e6564206c6f6e67(unsigned long, unsigned long, unsigned long);
extern int __dtrace_isenabled$Cocoa_HashTable$test_equal$v1(void);
#else
#define COCOA_HASHTABLE_REHASH_END(arg0, arg1, arg2) do {} while (0)
#define COCOA_HASHTABLE_REHASH_END_ENABLED() 0
#define COCOA_HASHTABLE_REHASH_START(arg0, arg1, arg2) do {} while (0)
#define COCOA_HASHTABLE_REHASH_START_ENABLED() 0
#define COCOA_HASHTABLE_HASH_KEY(arg0, arg1, arg2) do {} while (0)
#define COCOA_HASHTABLE_HASH_KEY_ENABLED() 0
#define COCOA_HASHTABLE_PROBE_DELETED(arg0, arg1) do {} while (0)
#define COCOA_HASHTABLE_PROBE_DELETED_ENABLED() 0
#define COCOA_HASHTABLE_PROBE_EMPTY(arg0, arg1) do {} while (0)
#define COCOA_HASHTABLE_PROBE_EMPTY_ENABLED() 0
#define COCOA_HASHTABLE_PROBE_VALID(arg0, arg1) do {} while (0)
#define COCOA_HASHTABLE_PROBE_VALID_ENABLED() 0
#define COCOA_HASHTABLE_PROBING_END(arg0, arg1) do {} while (0)
#define COCOA_HASHTABLE_PROBING_END_ENABLED() 0
#define COCOA_HASHTABLE_PROBING_START(arg0, arg1) do {} while (0)
#define COCOA_HASHTABLE_PROBING_START_ENABLED() 0
#define COCOA_HASHTABLE_TEST_EQUAL(arg0, arg1, arg2) do {} while (0)
#define COCOA_HASHTABLE_TEST_EQUAL_ENABLED() 0
#endif
#if !defined(__LP64__)
#define __LP64__ 0
#endif
// Prime numbers. Values above 100 have been adjusted up so that the
// malloced block size will be just below a multiple of 512; values
// above 1200 have been adjusted up to just below a multiple of 4096.
static const uintptr_t __CFBasicHashTableSizes[64] = {
0, 3, 7, 13, 23, 41, 71, 127, 191, 251, 383, 631, 1087, 1723,
2803, 4523, 7351, 11959, 19447, 31231, 50683, 81919, 132607,
214519, 346607, 561109, 907759, 1468927, 2376191, 3845119,
6221311, 10066421, 16287743, 26354171, 42641881, 68996069,
111638519, 180634607, 292272623, 472907251,
#if __LP64__
765180413UL, 1238087663UL, 2003267557UL, 3241355263UL, 5244622819UL,
#if 0
8485977589UL, 13730600407UL, 22216578047UL, 35947178479UL,
58163756537UL, 94110934997UL, 152274691561UL, 246385626107UL,
398660317687UL, 645045943807UL, 1043706260983UL, 1688752204787UL,
2732458465769UL, 4421210670577UL, 7153669136377UL,
11574879807461UL, 18728548943849UL, 30303428750843UL
#endif
#endif
};
static const uintptr_t __CFBasicHashTableCapacities[64] = {
0, 3, 6, 11, 19, 32, 52, 85, 118, 155, 237, 390, 672, 1065,
1732, 2795, 4543, 7391, 12019, 19302, 31324, 50629, 81956,
132580, 214215, 346784, 561026, 907847, 1468567, 2376414,
3844982, 6221390, 10066379, 16287773, 26354132, 42641916,
68996399, 111638327, 180634415, 292272755,
#if __LP64__
472907503UL, 765180257UL, 1238087439UL, 2003267722UL, 3241355160UL,
#if 0
5244622578UL, 8485977737UL, 13730600347UL, 22216578100UL,
35947178453UL, 58163756541UL, 94110935011UL, 152274691274UL,
246385626296UL, 398660317578UL, 645045943559UL, 1043706261135UL,
1688752204693UL, 2732458465840UL, 4421210670552UL,
7153669136706UL, 11574879807265UL, 18728548943682UL
#endif
#endif
};
// Primitive roots for the primes above
static const uintptr_t __CFBasicHashPrimitiveRoots[64] = {
0, 2, 3, 2, 5, 6, 7, 3, 19, 6, 5, 3, 3, 3,
2, 5, 6, 3, 3, 6, 2, 3, 3,
3, 5, 10, 3, 3, 22, 3,
3, 3, 5, 2, 22, 2,
11, 5, 5, 2,
#if __LP64__
3, 10, 2, 3, 10,
2, 3, 5, 3,
3, 2, 7, 2,
3, 3, 3, 2,
3, 5, 5,
2, 3, 2
#endif
};
CF_INLINE void *__CFBasicHashAllocateMemory(CFConstBasicHashRef ht, CFIndex count, CFIndex elem_size, Boolean strong, Boolean compactable) {
CFAllocatorRef allocator = CFGetAllocator(ht);
void *new_mem = NULL;
if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
new_mem = auto_zone_allocate_object(objc_collectableZone(), count * elem_size, strong ? (compactable ? AUTO_POINTERS_ONLY : AUTO_MEMORY_SCANNED) : AUTO_UNSCANNED, false, false);
} else {
new_mem = CFAllocatorAllocate(allocator, count * elem_size, 0);
}
return new_mem;
}
CF_INLINE void *__CFBasicHashAllocateMemory2(CFAllocatorRef allocator, CFIndex count, CFIndex elem_size, Boolean strong, Boolean compactable) {
void *new_mem = NULL;
if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
new_mem = auto_zone_allocate_object(objc_collectableZone(), count * elem_size, strong ? (compactable ? AUTO_POINTERS_ONLY : AUTO_MEMORY_SCANNED) : AUTO_UNSCANNED, false, false);
} else {
new_mem = CFAllocatorAllocate(allocator, count * elem_size, 0);
}
return new_mem;
}
#define __CFBasicHashSubABZero 0xa7baadb1
#define __CFBasicHashSubABOne 0xa5baadb9
typedef union {
uintptr_t neutral;
id strong;
id weak;
} CFBasicHashValue;
struct __CFBasicHash {
CFRuntimeBase base;
struct { // 192 bits
uint16_t mutations;
uint8_t hash_style:2;
uint8_t keys_offset:1;
uint8_t counts_offset:2;
uint8_t counts_width:2;
uint8_t hashes_offset:2;
uint8_t strong_values:1;
uint8_t strong_keys:1;
uint8_t weak_values:1;
uint8_t weak_keys:1;
uint8_t int_values:1;
uint8_t int_keys:1;
uint8_t indirect_keys:1;
uint32_t used_buckets; /* number of used buckets */
uint64_t deleted:16;
uint64_t num_buckets_idx:8; /* index to number of buckets */
uint64_t __kret:10;
uint64_t __vret:10;
uint64_t __krel:10;
uint64_t __vrel:10;
uint64_t __:1;
uint64_t null_rc:1;
uint64_t fast_grow:1;
uint64_t finalized:1;
uint64_t __kdes:10;
uint64_t __vdes:10;
uint64_t __kequ:10;
uint64_t __vequ:10;
uint64_t __khas:10;
uint64_t __kget:10;
} bits;
void *pointers[1];
};
static void *CFBasicHashCallBackPtrs[(1UL << 10)];
static int32_t CFBasicHashCallBackPtrsCount = 0;
static int32_t CFBasicHashGetPtrIndex(void *ptr) {
static dispatch_once_t once;
dispatch_once(&once, ^{
CFBasicHashCallBackPtrs[0] = NULL;
CFBasicHashCallBackPtrs[1] = (void *)CFCopyDescription;
CFBasicHashCallBackPtrs[2] = (void *)__CFTypeCollectionRelease;
CFBasicHashCallBackPtrs[3] = (void *)__CFTypeCollectionRetain;
CFBasicHashCallBackPtrs[4] = (void *)CFEqual;
CFBasicHashCallBackPtrs[5] = (void *)CFHash;
CFBasicHashCallBackPtrs[6] = (void *)__CFStringCollectionCopy;
CFBasicHashCallBackPtrs[7] = NULL;
CFBasicHashCallBackPtrsCount = 8;
});
// The uniquing here is done locklessly for best performance, and in
// a way that will keep multiple threads from stomping each other's
// newly registered values, but may result in multiple slots
// containing the same pointer value.
int32_t idx;
for (idx = 0; idx < CFBasicHashCallBackPtrsCount; idx++) {
if (CFBasicHashCallBackPtrs[idx] == ptr) return idx;
}
if (1000 < CFBasicHashCallBackPtrsCount) HALT;
idx = OSAtomicIncrement32(&CFBasicHashCallBackPtrsCount); // returns new value
CFBasicHashCallBackPtrs[idx - 1] = ptr;
return idx - 1;
}
CF_PRIVATE Boolean CFBasicHashHasStrongValues(CFConstBasicHashRef ht) {
#if DEPLOYMENT_TARGET_MACOSX
return ht->bits.strong_values ? true : false;
#else
return false;
#endif
}
CF_PRIVATE Boolean CFBasicHashHasStrongKeys(CFConstBasicHashRef ht) {
#if DEPLOYMENT_TARGET_MACOSX
return ht->bits.strong_keys ? true : false;
#else
return false;
#endif
}
CF_INLINE Boolean __CFBasicHashHasHashCache(CFConstBasicHashRef ht) {
#if DEPLOYMENT_TARGET_MACOSX
return ht->bits.hashes_offset ? true : false;
#else
return false;
#endif
}
CF_INLINE uintptr_t __CFBasicHashImportValue(CFConstBasicHashRef ht, uintptr_t stack_value) {
uintptr_t (*func)(CFAllocatorRef, uintptr_t) = (uintptr_t (*)(CFAllocatorRef, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__vret];
if (!func || ht->bits.null_rc) return stack_value;
CFAllocatorRef alloc = CFGetAllocator(ht);
return func(alloc, stack_value);
}
CF_INLINE uintptr_t __CFBasicHashImportKey(CFConstBasicHashRef ht, uintptr_t stack_key) {
uintptr_t (*func)(CFAllocatorRef, uintptr_t) = (uintptr_t (*)(CFAllocatorRef, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__kret];
if (!func || ht->bits.null_rc) return stack_key;
CFAllocatorRef alloc = CFGetAllocator(ht);
return func(alloc, stack_key);
}
CF_INLINE void __CFBasicHashEjectValue(CFConstBasicHashRef ht, uintptr_t stack_value) {
void (*func)(CFAllocatorRef, uintptr_t) = (void (*)(CFAllocatorRef, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__vrel];
if (!func || ht->bits.null_rc) return;
CFAllocatorRef alloc = CFGetAllocator(ht);
func(alloc, stack_value);
}
CF_INLINE void __CFBasicHashEjectKey(CFConstBasicHashRef ht, uintptr_t stack_key) {
void (*func)(CFAllocatorRef, uintptr_t) = (void (*)(CFAllocatorRef, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__krel];
if (!func || ht->bits.null_rc) return;
CFAllocatorRef alloc = CFGetAllocator(ht);
func(alloc, stack_key);
}
CF_INLINE CFStringRef __CFBasicHashDescValue(CFConstBasicHashRef ht, uintptr_t stack_value) {
CFStringRef (*func)(uintptr_t) = (CFStringRef (*)(uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__vdes];
if (!func) return CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<%p>"), (void *)stack_value);
return func(stack_value);
}
CF_INLINE CFStringRef __CFBasicHashDescKey(CFConstBasicHashRef ht, uintptr_t stack_key) {
CFStringRef (*func)(uintptr_t) = (CFStringRef (*)(uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__kdes];
if (!func) return CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<%p>"), (void *)stack_key);
return func(stack_key);
}
CF_INLINE Boolean __CFBasicHashTestEqualValue(CFConstBasicHashRef ht, uintptr_t stack_value_a, uintptr_t stack_value_b) {
Boolean (*func)(uintptr_t, uintptr_t) = (Boolean (*)(uintptr_t, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__vequ];
if (!func) return (stack_value_a == stack_value_b);
return func(stack_value_a, stack_value_b);
}
CF_INLINE Boolean __CFBasicHashTestEqualKey(CFConstBasicHashRef ht, uintptr_t in_coll_key, uintptr_t stack_key) {
COCOA_HASHTABLE_TEST_EQUAL(ht, in_coll_key, stack_key);
Boolean (*func)(uintptr_t, uintptr_t) = (Boolean (*)(uintptr_t, uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__kequ];
if (!func) return (in_coll_key == stack_key);
return func(in_coll_key, stack_key);
}
CF_INLINE CFHashCode __CFBasicHashHashKey(CFConstBasicHashRef ht, uintptr_t stack_key) {
CFHashCode (*func)(uintptr_t) = (CFHashCode (*)(uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__khas];
CFHashCode hash_code = func ? func(stack_key) : stack_key;
COCOA_HASHTABLE_HASH_KEY(ht, stack_key, hash_code);
return hash_code;
}
CF_INLINE uintptr_t __CFBasicHashGetIndirectKey(CFConstBasicHashRef ht, uintptr_t coll_key) {
uintptr_t (*func)(uintptr_t) = (uintptr_t (*)(uintptr_t))CFBasicHashCallBackPtrs[ht->bits.__kget];
if (!func) return coll_key;
return func(coll_key);
}
CF_INLINE CFBasicHashValue *__CFBasicHashGetValues(CFConstBasicHashRef ht) {
return (CFBasicHashValue *)ht->pointers[0];
}
CF_INLINE void __CFBasicHashSetValues(CFBasicHashRef ht, CFBasicHashValue *ptr) {
__AssignWithWriteBarrier(&ht->pointers[0], ptr);
}
CF_INLINE CFBasicHashValue *__CFBasicHashGetKeys(CFConstBasicHashRef ht) {
return (CFBasicHashValue *)ht->pointers[ht->bits.keys_offset];
}
CF_INLINE void __CFBasicHashSetKeys(CFBasicHashRef ht, CFBasicHashValue *ptr) {
__AssignWithWriteBarrier(&ht->pointers[ht->bits.keys_offset], ptr);
}
CF_INLINE void *__CFBasicHashGetCounts(CFConstBasicHashRef ht) {
return (void *)ht->pointers[ht->bits.counts_offset];
}
CF_INLINE void __CFBasicHashSetCounts(CFBasicHashRef ht, void *ptr) {
__AssignWithWriteBarrier(&ht->pointers[ht->bits.counts_offset], ptr);
}
CF_INLINE uintptr_t __CFBasicHashGetValue(CFConstBasicHashRef ht, CFIndex idx) {
uintptr_t val = __CFBasicHashGetValues(ht)[idx].neutral;
if (__CFBasicHashSubABZero == val) return 0UL;
if (__CFBasicHashSubABOne == val) return ~0UL;
return val;
}
CF_INLINE void __CFBasicHashSetValue(CFBasicHashRef ht, CFIndex idx, uintptr_t stack_value, Boolean ignoreOld, Boolean literal) {
CFBasicHashValue *valuep = &(__CFBasicHashGetValues(ht)[idx]);
uintptr_t old_value = ignoreOld ? 0 : valuep->neutral;
if (!literal) {
if (0UL == stack_value) stack_value = __CFBasicHashSubABZero;
if (~0UL == stack_value) stack_value = __CFBasicHashSubABOne;
}
if (CFBasicHashHasStrongValues(ht)) valuep->strong = (id)stack_value; else valuep->neutral = stack_value;
if (!ignoreOld) {
if (!(old_value == 0UL || old_value == ~0UL)) {
if (__CFBasicHashSubABZero == old_value) old_value = 0UL;
if (__CFBasicHashSubABOne == old_value) old_value = ~0UL;
__CFBasicHashEjectValue(ht, old_value);
}
}
}
CF_INLINE uintptr_t __CFBasicHashGetKey(CFConstBasicHashRef ht, CFIndex idx) {
if (ht->bits.keys_offset) {
uintptr_t key = __CFBasicHashGetKeys(ht)[idx].neutral;
if (__CFBasicHashSubABZero == key) return 0UL;
if (__CFBasicHashSubABOne == key) return ~0UL;
return key;
}
if (ht->bits.indirect_keys) {
uintptr_t stack_value = __CFBasicHashGetValue(ht, idx);
return __CFBasicHashGetIndirectKey(ht, stack_value);
}
return __CFBasicHashGetValue(ht, idx);
}
CF_INLINE void __CFBasicHashSetKey(CFBasicHashRef ht, CFIndex idx, uintptr_t stack_key, Boolean ignoreOld, Boolean literal) {
if (0 == ht->bits.keys_offset) HALT;
CFBasicHashValue *keyp = &(__CFBasicHashGetKeys(ht)[idx]);
uintptr_t old_key = ignoreOld ? 0 : keyp->neutral;
if (!literal) {
if (0UL == stack_key) stack_key = __CFBasicHashSubABZero;
if (~0UL == stack_key) stack_key = __CFBasicHashSubABOne;
}
if (CFBasicHashHasStrongKeys(ht)) keyp->strong = (id)stack_key; else keyp->neutral = stack_key;
if (!ignoreOld) {
if (!(old_key == 0UL || old_key == ~0UL)) {
if (__CFBasicHashSubABZero == old_key) old_key = 0UL;
if (__CFBasicHashSubABOne == old_key) old_key = ~0UL;
__CFBasicHashEjectKey(ht, old_key);
}
}
}
CF_INLINE uintptr_t __CFBasicHashIsEmptyOrDeleted(CFConstBasicHashRef ht, CFIndex idx) {
uintptr_t stack_value = __CFBasicHashGetValues(ht)[idx].neutral;
return (0UL == stack_value || ~0UL == stack_value);
}
CF_INLINE uintptr_t __CFBasicHashIsDeleted(CFConstBasicHashRef ht, CFIndex idx) {
uintptr_t stack_value = __CFBasicHashGetValues(ht)[idx].neutral;
return (~0UL == stack_value);
}
CF_INLINE uintptr_t __CFBasicHashGetSlotCount(CFConstBasicHashRef ht, CFIndex idx) {
void *counts = __CFBasicHashGetCounts(ht);
switch (ht->bits.counts_width) {
case 0: return ((uint8_t *)counts)[idx];
case 1: return ((uint16_t *)counts)[idx];
case 2: return ((uint32_t *)counts)[idx];
case 3: return ((uint64_t *)counts)[idx];
}
return 0;
}
CF_INLINE void __CFBasicHashBumpCounts(CFBasicHashRef ht) {
void *counts = __CFBasicHashGetCounts(ht);
CFAllocatorRef allocator = CFGetAllocator(ht);
switch (ht->bits.counts_width) {
case 0: {
uint8_t *counts08 = (uint8_t *)counts;
ht->bits.counts_width = 1;
CFIndex num_buckets = __CFBasicHashTableSizes[ht->bits.num_buckets_idx];
uint16_t *counts16 = (uint16_t *)__CFBasicHashAllocateMemory(ht, num_buckets, 2, false, false);
if (!counts16) HALT;
__SetLastAllocationEventName(counts16, "CFBasicHash (count-store)");
for (CFIndex idx2 = 0; idx2 < num_buckets; idx2++) {
counts16[idx2] = counts08[idx2];
}
__CFBasicHashSetCounts(ht, counts16);
if (!CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
CFAllocatorDeallocate(allocator, counts08);
}
break;
}
case 1: {
uint16_t *counts16 = (uint16_t *)counts;
ht->bits.counts_width = 2;
CFIndex num_buckets = __CFBasicHashTableSizes[ht->bits.num_buckets_idx];
uint32_t *counts32 = (uint32_t *)__CFBasicHashAllocateMemory(ht, num_buckets, 4, false, false);
if (!counts32) HALT;
__SetLastAllocationEventName(counts32, "CFBasicHash (count-store)");
for (CFIndex idx2 = 0; idx2 < num_buckets; idx2++) {
counts32[idx2] = counts16[idx2];
}
__CFBasicHashSetCounts(ht, counts32);
if (!CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
CFAllocatorDeallocate(allocator, counts16);
}
break;
}
case 2: {
uint32_t *counts32 = (uint32_t *)counts;
ht->bits.counts_width = 3;
CFIndex num_buckets = __CFBasicHashTableSizes[ht->bits.num_buckets_idx];
uint64_t *counts64 = (uint64_t *)__CFBasicHashAllocateMemory(ht, num_buckets, 8, false, false);
if (!counts64) HALT;
__SetLastAllocationEventName(counts64, "CFBasicHash (count-store)");
for (CFIndex idx2 = 0; idx2 < num_buckets; idx2++) {
counts64[idx2] = counts32[idx2];
}
__CFBasicHashSetCounts(ht, counts64);
if (!CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
CFAllocatorDeallocate(allocator, counts32);
}
break;
}
case 3: {
HALT;
break;
}
}
}
static void __CFBasicHashIncSlotCount(CFBasicHashRef ht, CFIndex idx) {
void *counts = __CFBasicHashGetCounts(ht);
switch (ht->bits.counts_width) {
case 0: {
uint8_t *counts08 = (uint8_t *)counts;
uint8_t val = counts08[idx];
if (val < INT8_MAX) {
counts08[idx] = val + 1;
return;
}
__CFBasicHashBumpCounts(ht);
__CFBasicHashIncSlotCount(ht, idx);
break;
}
case 1: {
uint16_t *counts16 = (uint16_t *)counts;
uint16_t val = counts16[idx];
if (val < INT16_MAX) {
counts16[idx] = val + 1;
return;
}
__CFBasicHashBumpCounts(ht);
__CFBasicHashIncSlotCount(ht, idx);
break;
}
case 2: {
uint32_t *counts32 = (uint32_t *)counts;
uint32_t val = counts32[idx];
if (val < INT32_MAX) {
counts32[idx] = val + 1;
return;
}
__CFBasicHashBumpCounts(ht);
__CFBasicHashIncSlotCount(ht, idx);
break;
}
case 3: {
uint64_t *counts64 = (uint64_t *)counts;
uint64_t val = counts64[idx];
if (val < INT64_MAX) {
counts64[idx] = val + 1;
return;
}
__CFBasicHashBumpCounts(ht);
__CFBasicHashIncSlotCount(ht, idx);
break;
}
}
}
CF_INLINE void __CFBasicHashDecSlotCount(CFBasicHashRef ht, CFIndex idx) {
void *counts = __CFBasicHashGetCounts(ht);
switch (ht->bits.counts_width) {
case 0: ((uint8_t *)counts)[idx]--; return;
case 1: ((uint16_t *)counts)[idx]--; return;
case 2: ((uint32_t *)counts)[idx]--; return;
case 3: ((uint64_t *)counts)[idx]--; return;
}
}
CF_INLINE uintptr_t *__CFBasicHashGetHashes(CFConstBasicHashRef ht) {
return (uintptr_t *)ht->pointers[ht->bits.hashes_offset];
}
CF_INLINE void __CFBasicHashSetHashes(CFBasicHashRef ht, uintptr_t *ptr) {
__AssignWithWriteBarrier(&ht->pointers[ht->bits.hashes_offset], ptr);
}
// to expose the load factor, expose this function to customization
CF_INLINE CFIndex __CFBasicHashGetCapacityForNumBuckets(CFConstBasicHashRef ht, CFIndex num_buckets_idx) {
return __CFBasicHashTableCapacities[num_buckets_idx];
}
CF_INLINE CFIndex __CFBasicHashGetNumBucketsIndexForCapacity(CFConstBasicHashRef ht, CFIndex capacity) {
for (CFIndex idx = 0; idx < 64; idx++) {
if (capacity <= __CFBasicHashGetCapacityForNumBuckets(ht, idx)) return idx;
}
HALT;
return 0;
}
CF_PRIVATE CFIndex CFBasicHashGetNumBuckets(CFConstBasicHashRef ht) {
return __CFBasicHashTableSizes[ht->bits.num_buckets_idx];
}
CF_PRIVATE CFIndex CFBasicHashGetCapacity(CFConstBasicHashRef ht) {
return __CFBasicHashGetCapacityForNumBuckets(ht, ht->bits.num_buckets_idx);
}
// In returned struct, .count is zero if the bucket is empty or deleted,
// and the .weak_key field indicates which. .idx is either the index of
// the found bucket or the index of the bucket which should be filled by
// an add operation. For a set or multiset, the .weak_key and .weak_value
// are the same.
CF_PRIVATE CFBasicHashBucket CFBasicHashGetBucket(CFConstBasicHashRef ht, CFIndex idx) {
CFBasicHashBucket result;
result.idx = idx;
if (__CFBasicHashIsEmptyOrDeleted(ht, idx)) {
result.count = 0;
result.weak_value = 0;
result.weak_key = 0;
} else {
result.count = (ht->bits.counts_offset) ? __CFBasicHashGetSlotCount(ht, idx) : 1;
result.weak_value = __CFBasicHashGetValue(ht, idx);
result.weak_key = __CFBasicHashGetKey(ht, idx);
}
return result;
}
#if defined(__arm__)
static uintptr_t __CFBasicHashFold(uintptr_t dividend, uint8_t idx) {
switch (idx) {
case 1: return dividend % 3;
case 2: return dividend % 7;
case 3: return dividend % 13;
case 4: return dividend % 23;
case 5: return dividend % 41;
case 6: return dividend % 71;
case 7: return dividend % 127;
case 8: return dividend % 191;
case 9: return dividend % 251;
case 10: return dividend % 383;
case 11: return dividend % 631;
case 12: return dividend % 1087;
case 13: return dividend % 1723;
case 14: return dividend % 2803;
case 15: return dividend % 4523;
case 16: return dividend % 7351;
case 17: return dividend % 11959;
case 18: return dividend % 19447;
case 19: return dividend % 31231;
case 20: return dividend % 50683;
case 21: return dividend % 81919;
case 22: return dividend % 132607;
case 23: return dividend % 214519;
case 24: return dividend % 346607;
case 25: return dividend % 561109;
case 26: return dividend % 907759;
case 27: return dividend % 1468927;
case 28: return dividend % 2376191;
case 29: return dividend % 3845119;
case 30: return dividend % 6221311;
case 31: return dividend % 10066421;
case 32: return dividend % 16287743;
case 33: return dividend % 26354171;
case 34: return dividend % 42641881;
case 35: return dividend % 68996069;
case 36: return dividend % 111638519;
case 37: return dividend % 180634607;
case 38: return dividend % 292272623;
case 39: return dividend % 472907251;
}
HALT;
return ~0;
}
#endif
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Linear
#define FIND_BUCKET_HASH_STYLE 1
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Linear_NoCollision
#define FIND_BUCKET_HASH_STYLE 1
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Linear_Indirect
#define FIND_BUCKET_HASH_STYLE 1
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Linear_Indirect_NoCollision
#define FIND_BUCKET_HASH_STYLE 1
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Double
#define FIND_BUCKET_HASH_STYLE 2
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Double_NoCollision
#define FIND_BUCKET_HASH_STYLE 2
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Double_Indirect
#define FIND_BUCKET_HASH_STYLE 2
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Double_Indirect_NoCollision
#define FIND_BUCKET_HASH_STYLE 2
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Exponential
#define FIND_BUCKET_HASH_STYLE 3
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Exponential_NoCollision
#define FIND_BUCKET_HASH_STYLE 3
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 0
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Exponential_Indirect
#define FIND_BUCKET_HASH_STYLE 3
#define FIND_BUCKET_FOR_REHASH 0
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
#define FIND_BUCKET_NAME ___CFBasicHashFindBucket_Exponential_Indirect_NoCollision
#define FIND_BUCKET_HASH_STYLE 3
#define FIND_BUCKET_FOR_REHASH 1
#define FIND_BUCKET_FOR_INDIRECT_KEY 1
#include "CFBasicHashFindBucket.m"
CF_INLINE CFBasicHashBucket __CFBasicHashFindBucket(CFConstBasicHashRef ht, uintptr_t stack_key) {
if (0 == ht->bits.num_buckets_idx) {
CFBasicHashBucket result = {kCFNotFound, 0UL, 0UL, 0};
return result;
}
if (ht->bits.indirect_keys) {
switch (ht->bits.hash_style) {
case __kCFBasicHashLinearHashingValue: return ___CFBasicHashFindBucket_Linear_Indirect(ht, stack_key);
case __kCFBasicHashDoubleHashingValue: return ___CFBasicHashFindBucket_Double_Indirect(ht, stack_key);
case __kCFBasicHashExponentialHashingValue: return ___CFBasicHashFindBucket_Exponential_Indirect(ht, stack_key);
}
} else {
switch (ht->bits.hash_style) {
case __kCFBasicHashLinearHashingValue: return ___CFBasicHashFindBucket_Linear(ht, stack_key);
case __kCFBasicHashDoubleHashingValue: return ___CFBasicHashFindBucket_Double(ht, stack_key);
case __kCFBasicHashExponentialHashingValue: return ___CFBasicHashFindBucket_Exponential(ht, stack_key);
}
}
HALT;
CFBasicHashBucket result = {kCFNotFound, 0UL, 0UL, 0};
return result;
}
CF_INLINE CFIndex __CFBasicHashFindBucket_NoCollision(CFConstBasicHashRef ht, uintptr_t stack_key, uintptr_t key_hash) {
if (0 == ht->bits.num_buckets_idx) {
return kCFNotFound;
}
if (ht->bits.indirect_keys) {
switch (ht->bits.hash_style) {
case __kCFBasicHashLinearHashingValue: return ___CFBasicHashFindBucket_Linear_Indirect_NoCollision(ht, stack_key, key_hash);
case __kCFBasicHashDoubleHashingValue: return ___CFBasicHashFindBucket_Double_Indirect_NoCollision(ht, stack_key, key_hash);
case __kCFBasicHashExponentialHashingValue: return ___CFBasicHashFindBucket_Exponential_Indirect_NoCollision(ht, stack_key, key_hash);
}
} else {
switch (ht->bits.hash_style) {
case __kCFBasicHashLinearHashingValue: return ___CFBasicHashFindBucket_Linear_NoCollision(ht, stack_key, key_hash);
case __kCFBasicHashDoubleHashingValue: return ___CFBasicHashFindBucket_Double_NoCollision(ht, stack_key, key_hash);
case __kCFBasicHashExponentialHashingValue: return ___CFBasicHashFindBucket_Exponential_NoCollision(ht, stack_key, key_hash);
}
}
HALT;
return kCFNotFound;
}
CF_PRIVATE CFBasicHashBucket CFBasicHashFindBucket(CFConstBasicHashRef ht, uintptr_t stack_key) {
if (__CFBasicHashSubABZero == stack_key || __CFBasicHashSubABOne == stack_key) {
CFBasicHashBucket result = {kCFNotFound, 0UL, 0UL, 0};
return result;
}
return __CFBasicHashFindBucket(ht, stack_key);
}
CF_PRIVATE void CFBasicHashSuppressRC(CFBasicHashRef ht) {
ht->bits.null_rc = 1;
}
CF_PRIVATE void CFBasicHashUnsuppressRC(CFBasicHashRef ht) {
ht->bits.null_rc = 0;
}
CF_PRIVATE CFOptionFlags CFBasicHashGetFlags(CFConstBasicHashRef ht) {
CFOptionFlags flags = (ht->bits.hash_style << 13);
if (CFBasicHashHasStrongValues(ht)) flags |= kCFBasicHashStrongValues;
if (CFBasicHashHasStrongKeys(ht)) flags |= kCFBasicHashStrongKeys;
if (ht->bits.fast_grow) flags |= kCFBasicHashAggressiveGrowth;
if (ht->bits.keys_offset) flags |= kCFBasicHashHasKeys;
if (ht->bits.counts_offset) flags |= kCFBasicHashHasCounts;
if (__CFBasicHashHasHashCache(ht)) flags |= kCFBasicHashHasHashCache;
return flags;
}
CF_PRIVATE CFIndex CFBasicHashGetCount(CFConstBasicHashRef ht) {
if (ht->bits.counts_offset) {
CFIndex total = 0L;
CFIndex cnt = (CFIndex)__CFBasicHashTableSizes[ht->bits.num_buckets_idx];
for (CFIndex idx = 0; idx < cnt; idx++) {
total += __CFBasicHashGetSlotCount(ht, idx);
}
return total;
}
return (CFIndex)ht->bits.used_buckets;
}
CF_PRIVATE CFIndex CFBasicHashGetCountOfKey(CFConstBasicHashRef ht, uintptr_t stack_key) {
if (__CFBasicHashSubABZero == stack_key || __CFBasicHashSubABOne == stack_key) {
return 0L;
}
if (0L == ht->bits.used_buckets) {
return 0L;
}
return __CFBasicHashFindBucket(ht, stack_key).count;
}
CF_PRIVATE CFIndex CFBasicHashGetCountOfValue(CFConstBasicHashRef ht, uintptr_t stack_value) {
if (__CFBasicHashSubABZero == stack_value) {
return 0L;
}
if (0L == ht->bits.used_buckets) {
return 0L;
}
if (!(ht->bits.keys_offset)) {
return __CFBasicHashFindBucket(ht, stack_value).count;
}
__block CFIndex total = 0L;
CFBasicHashApply(ht, ^(CFBasicHashBucket bkt) {
if ((stack_value == bkt.weak_value) || __CFBasicHashTestEqualValue(ht, bkt.weak_value, stack_value)) total += bkt.count;
return (Boolean)true;
});
return total;
}
CF_PRIVATE Boolean CFBasicHashesAreEqual(CFConstBasicHashRef ht1, CFConstBasicHashRef ht2) {
CFIndex cnt1 = CFBasicHashGetCount(ht1);
if (cnt1 != CFBasicHashGetCount(ht2)) return false;
if (0 == cnt1) return true;
__block Boolean equal = true;
CFBasicHashApply(ht1, ^(CFBasicHashBucket bkt1) {
CFBasicHashBucket bkt2 = __CFBasicHashFindBucket(ht2, bkt1.weak_key);
if (bkt1.count != bkt2.count) {
equal = false;
return (Boolean)false;
}
if ((ht1->bits.keys_offset) && (bkt1.weak_value != bkt2.weak_value) && !__CFBasicHashTestEqualValue(ht1, bkt1.weak_value, bkt2.weak_value)) {
equal = false;
return (Boolean)false;
}
return (Boolean)true;
});
return equal;
}
CF_PRIVATE void CFBasicHashApply(CFConstBasicHashRef ht, Boolean (^block)(CFBasicHashBucket)) {
CFIndex used = (CFIndex)ht->bits.used_buckets, cnt = (CFIndex)__CFBasicHashTableSizes[ht->bits.num_buckets_idx];
for (CFIndex idx = 0; 0 < used && idx < cnt; idx++) {
CFBasicHashBucket bkt = CFBasicHashGetBucket(ht, idx);
if (0 < bkt.count) {
if (!block(bkt)) {
return;
}
used--;
}