-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
hash.c
4121 lines (3538 loc) · 114 KB
/
hash.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
#include "schpriv.h"
#include "schmach.h"
#include <ctype.h>
#include <math.h>
#include "../gc2/gc2_obj.h"
READ_ONLY static Scheme_Hash_Tree *empty_hash_tree[4];
THREAD_LOCAL_DECL(intptr_t scheme_hash_request_count);
THREAD_LOCAL_DECL(intptr_t scheme_hash_iteration_count);
READ_ONLY static Scheme_Object GONE[1];
#ifdef MZ_PRECISE_GC
static void register_traversers(void);
#endif
/* Hash calculations need to use unsigned integers, where
wraparound behavior is defined for overflow. But some
parts of the published hash API use signed integers.
The to_signed_hash() and to_unsigned_hash() macros are
supposed to marshal unsigned to signed and back without
any loss of unsigned information.
FIXME: The current implementation as a cast is *not*
consistent with the C standard (the cast to unsigned can
be implementation-dependent), but it works fine with all
compilers that we currently use. */
#define to_signed_hash(v) ((intptr_t)v)
#define to_unsigned_hash(v) ((uintptr_t)v)
#ifdef OBJHEAD_HAS_HASH_BITS
/* In 3m mode, we only have 14 bits of hash code in the
Scheme_Object header. But the GC-level object header has some
leftover bits (currently 9, 11, 41, or 43, depending on the
platform), so use those, too. That only works for GCable
objects, so we use 1 of our 14 bits to indicate whether the
other bits are present. */
# define GCABLE_OBJ_HASH_BIT 0x0004
# define OBJ_HASH_USELESS_BITS 3
#else
# define GCABLE_OBJ_HASH_BIT 0
# define OBJ_HASH_USELESS_BITS 2
#endif
#define OBJ_HASH_USEFUL_BITS (16 - OBJ_HASH_USELESS_BITS)
#define OBJ_HASH_USEFUL_MASK ((1 << OBJ_HASH_USEFUL_BITS)-1)
/* A hash code has been installed if any of these bits is
non-zero: */
#define HASH_CODE_PRESENT_BITS 0xFFFC
#ifdef MZ_PRECISE_GC
/* keygen race conditions below are "ok", because keygen is randomness
used to create a hashkey. Technically, a race condition allows
undefined behavior by some C standards, but we don't expect
compilers to actually impose a "catch fire" semantics. Make sure
that only one thread at a time sets a hash code in a specific
object, though, and watch out for a race with JIT-generated code
running in a future and setting flags on pairs. */
SHARED_OK static uintptr_t keygen = GCABLE_OBJ_HASH_BIT;
XFORM_NONGCING static MZ_INLINE
uintptr_t PTR_TO_LONG(Scheme_Object *o)
{
uintptr_t bits;
short v;
if (SCHEME_INTP(o))
return (uintptr_t)o >> 1;
v = o->keyex;
if (!(v & HASH_CODE_PRESENT_BITS)) {
uintptr_t local_keygen = keygen;
v |= (short)local_keygen;
#ifdef OBJHEAD_HAS_HASH_BITS
if (GC_is_allocated(o)) {
OBJHEAD_HASH_BITS(o) = (local_keygen >> 16);
v |= GCABLE_OBJ_HASH_BIT;
} else
v &= ~GCABLE_OBJ_HASH_BIT;
#endif
if (!v) v = 0x1AD0;
#ifdef MZ_USE_FUTURES
if (SCHEME_PAIRP(o) && scheme_is_multithreaded(1)) {
/* Use CAS to avoid losing a hash code due to a conflict with
JIT-generated `list?' test, which itself uses CAS to set "is
a list" or "not a list" flags on pairs. */
while (!mzrt_cas16(&o->keyex, o->keyex, v)) {
}
} else
#endif
o->keyex = v;
keygen += (1 << OBJ_HASH_USELESS_BITS);
}
#ifdef OBJHEAD_HAS_HASH_BITS
if (v & GCABLE_OBJ_HASH_BIT)
bits = OBJHEAD_HASH_BITS(o);
else
#endif
bits = o->type;
/* We need to drop the low two bits of `v', which
are used for non-hashing purposes in some types. */
return (bits << OBJ_HASH_USEFUL_BITS) | ((v >> OBJ_HASH_USELESS_BITS) & OBJ_HASH_USEFUL_MASK);
}
#else
# define PTR_TO_LONG(p) ((uintptr_t)(p)>>2)
#endif
void scheme_set_distinct_eq_hash(Scheme_Object *o)
/* Used after cloning a value to ensure that the
hash code is not cloned */
{
#ifdef MZ_PRECISE_GC
o->keyex = o->keyex & ~HASH_CODE_PRESENT_BITS;
#endif
}
void scheme_install_symbol_hash_code(Scheme_Object *sym, uintptr_t h)
{
#ifdef MZ_PRECISE_GC
/* Record a hash code for the symbol as its `eq?` hash code ---
intended mainly to make `equal?` hashing depend only on the
symbol content */
short v;
v = sym->keyex;
if (!(v & 0xFFFC)) {
v |= (short)(h & ~0x7);
#ifdef OBJHEAD_HAS_HASH_BITS
if (GC_is_allocated(sym)) {
OBJHEAD_HASH_BITS(sym) = (h >> 16);
v |= GCABLE_OBJ_HASH_BIT;
} else
v &= ~GCABLE_OBJ_HASH_BIT;
#endif
if (!v) v = 0x1AD0;
sym->keyex = v;
}
#endif
}
#define FILL_FACTOR 1.4
#define MIN_HTABLE_SIZE 8
typedef int (*Hash_Compare_Proc)(void*, void*);
typedef uintptr_t hash_v_t;
#define MAX_HASH_DEPTH 128
/* For detecting and debugging accidental dependencies on hash-table order,
it might be helpful to invert the order at the lowest level: */
/* #define REVERSE_HASH_TABLE_ORDER 1 */
/*========================================================================*/
/* hashing functions */
/*========================================================================*/
static void string_hash_indices(void *_key, intptr_t *_h, intptr_t *_h2)
{
const char *key = (char *)_key;
uintptr_t i, h, h2;
h2 = h = i = 0;
while (key[i]) {
int c = key[i++];
h += (h << 5) + h + c;
h2 += c;
}
if (_h)
*_h = to_signed_hash(h);
if (_h2)
*_h2 = to_signed_hash(h2);
}
/*========================================================================*/
/* equality with wraps */
/*========================================================================*/
static Scheme_Object *apply_equal_key_wraps(Scheme_Object *key, Scheme_Object *key_wraps)
{
if (key_wraps) {
GC_CAN_IGNORE const char *who = (const char *)SCHEME_CAR(key_wraps);
Scheme_Chaperone *px;
Scheme_Object *a[2], *red;
key_wraps = SCHEME_CDR(key_wraps);
while (!SCHEME_NULLP(key_wraps)) {
px = (Scheme_Chaperone *)SCHEME_CAR(key_wraps);
red = SCHEME_BOX_VAL(px->redirects);
red = SCHEME_VEC_ELS(red)[5];
a[0] = px->prev;
a[1] = key;
key = _scheme_apply(red, 2, a);
if (!(SCHEME_CHAPERONE_FLAGS(px) & SCHEME_CHAPERONE_IS_IMPERSONATOR)
&& !scheme_chaperone_of(key, a[1])) {
scheme_wrong_chaperoned(who, "key", a[1], key);
return 0;
}
key_wraps = SCHEME_CDR(key_wraps);
}
}
return key;
}
static int equal_w_key_wraps(Scheme_Object *ekey, Scheme_Object *tkey, Scheme_Object *key_wraps)
{
if (key_wraps)
tkey = apply_equal_key_wraps(tkey, key_wraps);
return scheme_equal(ekey, tkey);
}
static int equal_always_w_key_wraps(Scheme_Object *ekey, Scheme_Object *tkey, Scheme_Object *key_wraps)
{
if (key_wraps)
tkey = apply_equal_key_wraps(tkey, key_wraps);
return scheme_equal_always(ekey, tkey);
}
XFORM_NONGCING static int same_kind_via_impersonator(Scheme_Object *orig_t1,
Scheme_Object *orig_t2)
{
Scheme_Object *v, *v2;
if (SCHEME_NP_CHAPERONEP(orig_t1))
v = scheme_chaperone_props_get(((Scheme_Chaperone *)orig_t1)->props, scheme_hash_kind_key);
else
v = NULL;
if (SCHEME_NP_CHAPERONEP(orig_t2))
v2 = scheme_chaperone_props_get(((Scheme_Chaperone *)orig_t2)->props, scheme_hash_kind_key);
else
v2 = NULL;
return SAME_OBJ(v, v2);
}
/*========================================================================*/
/* normal mutable hash table */
/*========================================================================*/
#ifdef REVERSE_HASH_TABLE_ORDER
# define HASH_TO_ARRAY_INDEX(h, mask) ((mask) - (h))
#else
# define HASH_TO_ARRAY_INDEX(h, mask) (h)
#endif
/* Since mutable hash tables tend to use the low bits of a hash code,
make sure higher bits of a fixnum are represented there: */
XFORM_NONGCING static uintptr_t fixmix(uintptr_t x) {
#ifdef SIXTY_FOUR_BIT_INTEGERS
uintptr_t x1 = x ^ ((x >> 32) & (uintptr_t)0xFFFFFFFF);
#else
uintptr_t x1 = x;
#endif
uintptr_t x2 = x1 ^ ((x1 >> 16) & (uintptr_t)0xFFFF);
uintptr_t x3 = x2 ^ ((x2 >> 8) & (uintptr_t)0xFF);
return x3;
}
Scheme_Hash_Table *scheme_make_hash_table(int type)
{
Scheme_Hash_Table *table;
table = MALLOC_ONE_TAGGED(Scheme_Hash_Table);
table->size = 0;
table->iso.so.type = scheme_hash_table_type;
if (type == SCHEME_hash_string) {
table->make_hash_indices = string_hash_indices;
table->compare = (Hash_Compare_Proc)strcmp;
}
return table;
}
void scheme_clear_hash_table(Scheme_Hash_Table *ht)
{
ht->size = 0;
ht->count = 0;
ht->keys = NULL;
ht->vals = NULL;
ht->mcount = 0;
}
static Scheme_Object *do_hash(Scheme_Hash_Table *table, Scheme_Object *key, int set, Scheme_Object *val,
Scheme_Object *key_wraps,
GC_CAN_IGNORE Scheme_Object **_interned_key)
{
Scheme_Object *tkey, *ekey, **keys;
intptr_t hx, h2x;
hash_v_t h, h2 = 0, useme = 0;
uintptr_t mask;
rehash_key:
mask = table->size - 1;
if (table->make_hash_indices) {
if (table->compare == scheme_compare_equal || table->compare == scheme_compare_equal_always) {
if (key_wraps)
ekey = apply_equal_key_wraps(key, key_wraps);
else
ekey = key;
h2 = 0;
hx = scheme_equal_hash_key(ekey);
h = fixmix(to_unsigned_hash(hx)) & mask;
} else {
GC_CAN_IGNORE intptr_t *_h2x;
if (table->compare) {
h2 = 0;
_h2x = NULL;
} else
_h2x = &h2x;
table->make_hash_indices((void *)key, &hx, _h2x);
h = fixmix(to_unsigned_hash(hx)) & mask;
if (_h2x)
h2 = (fixmix(to_unsigned_hash(h2x)) & mask) | 1;
ekey = NULL;
}
} else {
uintptr_t lkey;
lkey = fixmix(PTR_TO_LONG((Scheme_Object *)key));
h = lkey & mask;
h2 = ((lkey >> 1) & mask) | 1;
ekey = NULL;
}
keys = table->keys;
if (table->compare) {
if (table->compare == scheme_compare_equal) {
/* Direct calls can be significantly faster than indirect */
scheme_hash_request_count++;
while ((tkey = keys[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(tkey, GONE)) {
if (set > 1) {
useme = h;
set = 1;
}
} else if (equal_w_key_wraps(ekey, tkey, key_wraps)) {
if (_interned_key) *_interned_key = tkey;
if (set) {
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
if (!val) {
keys[HASH_TO_ARRAY_INDEX(h, mask)] = GONE;
--table->count;
}
return val;
} else
return table->vals[HASH_TO_ARRAY_INDEX(h, mask)];
}
scheme_hash_iteration_count++;
if (!h2) {
h2x = scheme_equal_hash_key2(ekey);
h2 = (fixmix(to_unsigned_hash(h2x)) & (table->size - 1)) | 1;
}
h = (h + h2) & mask;
}
} else {
scheme_hash_request_count++;
while ((tkey = keys[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(tkey, GONE)) {
if (set > 1) {
useme = h;
set = 1;
}
} else if (!table->compare(tkey, (char *)key)) {
if (_interned_key) *_interned_key = tkey;
if (set) {
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
if (!val) {
keys[HASH_TO_ARRAY_INDEX(h, mask)] = GONE;
--table->count;
}
return val;
} else
return table->vals[HASH_TO_ARRAY_INDEX(h, mask)];
}
scheme_hash_iteration_count++;
if (!h2) {
table->make_hash_indices((void *)key, NULL, &h2x);
h2 = (fixmix(to_unsigned_hash(h2x)) & (table->size - 1)) | 1;
}
h = (h + h2) & mask;
}
}
} else {
scheme_hash_request_count++;
while ((tkey = keys[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(tkey, key)) {
if (_interned_key) *_interned_key = tkey;
if (set) {
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
if (!val) {
keys[HASH_TO_ARRAY_INDEX(h, mask)] = GONE;
--table->count;
}
return val;
} else
return table->vals[HASH_TO_ARRAY_INDEX(h, mask)];
} else if (SAME_PTR(tkey, GONE)) {
if (set > 1) {
useme = h;
set = 1;
}
}
scheme_hash_iteration_count++;
h = (h + h2) & mask;
}
}
if (!set || !val)
return NULL;
if (set == 1)
h = useme;
else if (table->mcount * FILL_FACTOR >= table->size) {
/* Rehash */
int i, oldsize = table->size, size;
Scheme_Object **oldkeys = table->keys;
Scheme_Object **oldvals = table->vals;
if (table->count << 1 >= table->mcount)
size = oldsize << 1;
else
size = oldsize;
table->size = size;
{
Scheme_Object **ba;
ba = MALLOC_N(Scheme_Object *, size);
table->vals = ba;
ba = MALLOC_N(Scheme_Object *, size);
table->keys = ba;
}
table->count = 0;
table->mcount = 0;
for (i = 0; i < oldsize; i++) {
if (oldkeys[i] && !SAME_PTR(oldkeys[i], GONE))
do_hash(table, oldkeys[i], 2, oldvals[i], key_wraps, _interned_key);
}
goto rehash_key;
} else {
table->mcount++;
}
table->count++;
table->keys[HASH_TO_ARRAY_INDEX(h, mask)] = key;
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
if (_interned_key) *_interned_key = key;
return val;
}
static Scheme_Object *do_hash_set(Scheme_Hash_Table *table, Scheme_Object *key, Scheme_Object *val)
{
Scheme_Object *tkey, **keys;
hash_v_t h, h2, useme = 0;
uintptr_t mask;
uintptr_t lkey;
int set = 2;
mask = table->size - 1;
lkey = fixmix(PTR_TO_LONG((Scheme_Object *)key));
h = lkey & mask;
h2 = (lkey >> 1) & mask;
h2 |= 1;
keys = table->keys;
scheme_hash_request_count++;
while ((tkey = keys[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(tkey, key)) {
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
if (!val) {
keys[HASH_TO_ARRAY_INDEX(h, mask)] = GONE;
--table->count;
}
return val;
} else if (SAME_PTR(tkey, GONE)) {
if (set > 1) {
useme = h;
set = 1;
}
}
scheme_hash_iteration_count++;
h = (h + h2) & mask;
}
if (!val)
return NULL;
if (set == 1)
h = useme;
else if (table->mcount * FILL_FACTOR >= table->size) {
/* Use slow path to grow table: */
return do_hash(table, key, 2, val, NULL, NULL);
} else {
table->mcount++;
}
table->count++;
table->keys[HASH_TO_ARRAY_INDEX(h, mask)] = key;
table->vals[HASH_TO_ARRAY_INDEX(h, mask)] = val;
return val;
}
XFORM_NONGCING static Scheme_Object *do_hash_get(Scheme_Hash_Table *table, Scheme_Object *key,
GC_CAN_IGNORE Scheme_Object **_interned_key)
{
Scheme_Object *tkey, **keys;
hash_v_t h, h2;
uintptr_t mask;
uintptr_t lkey;
mask = table->size - 1;
lkey = fixmix(PTR_TO_LONG((Scheme_Object *)key));
h = lkey & mask;
h2 = (lkey >> 1) & mask;
h2 |= 1;
keys = table->keys;
scheme_hash_request_count++;
while ((tkey = keys[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(tkey, key)) {
if (_interned_key) *_interned_key = tkey;
return table->vals[HASH_TO_ARRAY_INDEX(h, mask)];
}
scheme_hash_iteration_count++;
h = (h + h2) & mask;
}
return NULL;
}
void scheme_hash_set_w_key_wraps(Scheme_Hash_Table *table, Scheme_Object *key, Scheme_Object *val,
Scheme_Object *key_wraps)
{
if (!table->vals) {
Scheme_Object **ba;
table->size = 8;
ba = MALLOC_N(Scheme_Object *, table->size);
table->vals = ba;
ba = MALLOC_N(Scheme_Object *, table->size);
table->keys = ba;
}
if (table->make_hash_indices)
do_hash(table, key, 2, val, key_wraps, NULL);
else
do_hash_set(table, key, val);
}
void scheme_hash_set(Scheme_Hash_Table *table, Scheme_Object *key, Scheme_Object *val)
{
scheme_hash_set_w_key_wraps(table, key, val, NULL);
}
Scheme_Object *scheme_hash_get_w_key_wraps(Scheme_Hash_Table *table, Scheme_Object *key,
Scheme_Object *key_wraps,
GC_CAN_IGNORE Scheme_Object **_interned_key)
{
if (!table->vals)
return NULL;
else if (table->make_hash_indices)
return do_hash(table, key, 0, NULL, key_wraps, _interned_key);
else
return do_hash_get(table, key, _interned_key);
}
Scheme_Object *scheme_hash_get(Scheme_Hash_Table *table, Scheme_Object *key)
{
return scheme_hash_get_w_key_wraps(table, key, NULL, NULL);
}
Scheme_Object *scheme_hash_get_key(Scheme_Hash_Table *table, Scheme_Object *key)
{
Scheme_Object *interned_key, *v;
v = scheme_hash_get_w_key_wraps(table, key, NULL, &interned_key);
if (v)
return interned_key;
else
return NULL;
}
Scheme_Object *scheme_eq_hash_get(Scheme_Hash_Table *table, Scheme_Object *key)
/* Specialized to allow XFORM_NONGCING */
{
if (!table->vals)
return NULL;
else
return do_hash_get(table, key, NULL);
}
Scheme_Object *scheme_hash_get_atomic(Scheme_Hash_Table *table, Scheme_Object *key)
/* Mostly useful for acessing equal-based hash table when you don't want
thread switches (such as in stx object manipulations). Simply grabbing the
table's lock would be enough to make access to the table single-threaded,
but sometimes you don't want any thread switches at all. */
{
Scheme_Object *r;
scheme_start_atomic();
r = scheme_hash_get(table, key);
scheme_end_atomic_no_swap();
return r;
}
void scheme_hash_set_atomic(Scheme_Hash_Table *table, Scheme_Object *key, Scheme_Object *val)
/* See rationale with scheme_hash_get_atomic. */
{
scheme_start_atomic();
scheme_hash_set(table, key, val);
scheme_end_atomic_no_swap();
}
int scheme_hash_table_equal_rec(Scheme_Hash_Table *t1, Scheme_Object *orig_t1,
Scheme_Hash_Table *t2, Scheme_Object *orig_t2,
void *eql)
{
Scheme_Object **vals, **keys, *val1, *val2, *key;
int i;
if ((t1->count != t2->count)
|| (t1->make_hash_indices != t2->make_hash_indices)
|| (t1->compare != t2->compare))
return 0;
if (!same_kind_via_impersonator(orig_t1, orig_t2))
return 0;
keys = t1->keys;
vals = t1->vals;
for (i = t1->size; i--; ) {
if (vals[i]) {
key = keys[i];
if (!SAME_OBJ((Scheme_Object *)t1, orig_t1))
val1 = scheme_chaperone_hash_traversal_get(orig_t1, key, &key);
else
val1 = vals[i];
if (!SAME_OBJ((Scheme_Object *)t2, orig_t2))
val2 = scheme_chaperone_hash_get(orig_t2, key);
else
val2 = scheme_hash_get(t2, key);
if (!val2)
return 0;
if (!scheme_recur_equal(val1, val2, eql))
return 0;
/* since we didn't take a lock, the size could have changed */
if (i > t1->size) i = t1->size;
}
}
return 1;
}
int scheme_hash_table_equal(Scheme_Hash_Table *t1, Scheme_Hash_Table *t2)
{
return scheme_equal((Scheme_Object *)t1, (Scheme_Object *)t2);
}
Scheme_Hash_Table *scheme_clone_hash_table(Scheme_Hash_Table *ht)
{
Scheme_Hash_Table *table;
Scheme_Object **ba;
table = MALLOC_ONE_TAGGED(Scheme_Hash_Table);
memcpy(table, ht, sizeof(Scheme_Hash_Table));
MZ_OPT_HASH_KEY(&(table->iso)) = 0;
if (table->size) {
ba = MALLOC_N(Scheme_Object *, table->size);
memcpy(ba, table->vals, sizeof(Scheme_Object *) * table->size);
table->vals = ba;
ba = MALLOC_N(Scheme_Object *, table->size);
memcpy(ba, table->keys, sizeof(Scheme_Object *) * table->size);
table->keys = ba;
}
if (table->mutex) {
Scheme_Object *sema;
sema = scheme_make_sema(1);
table->mutex = sema;
}
return table;
}
void scheme_reset_hash_table(Scheme_Hash_Table *table, int *history)
{
if ((table->size <= 8)
|| (table->count * FILL_FACTOR > (table->size >> 1))) {
/* Keep same size */
} else {
/* Shrink by one step */
Scheme_Object **ba;
table->size >>= 1;
ba = MALLOC_N(Scheme_Object *, table->size);
table->vals = ba;
ba = MALLOC_N(Scheme_Object *, table->size);
table->keys = ba;
}
memset(table->vals, 0, sizeof(Scheme_Object *) * table->size);
memset(table->keys, 0, sizeof(Scheme_Object *) * table->size);
table->count = 0;
table->mcount = 0;
}
Scheme_Object *scheme_hash_table_next(Scheme_Hash_Table *hash,
mzlonglong start)
{
int i, sz = hash->size;
if (start >= 0) {
if ((start >= sz) || !hash->vals[start])
return NULL;
}
for (i = start + 1; i < sz; i++) {
if (hash->vals[i])
return scheme_make_integer(i);
}
return scheme_false;
}
int scheme_hash_table_index(Scheme_Hash_Table *hash, mzlonglong pos,
Scheme_Object **_key, Scheme_Object **_val)
{
if (pos < hash->size) {
if (hash->vals[pos]) {
*_key = hash->keys[pos];
if (_val)
*_val = hash->vals[pos];
return 1;
}
}
return 0;
}
/*========================================================================*/
/* old-style hash table, with buckets */
/*========================================================================*/
Scheme_Bucket_Table *
scheme_make_bucket_table (intptr_t size, int type)
{
Scheme_Bucket_Table *table;
size_t asize;
table = MALLOC_ONE_TAGGED(Scheme_Bucket_Table);
table->size = 4;
while (table->size < size) {
table->size <<= 1;
}
table->count = 0;
table->so.type = scheme_bucket_table_type;
asize = (size_t)table->size * sizeof(Scheme_Bucket *);
{
Scheme_Bucket **ba;
ba = (Scheme_Bucket **)scheme_malloc(asize);
table->buckets = ba;
}
if (type == SCHEME_hash_weak_ptr)
table->weak = SCHEME_BT_KIND_WEAK;
else if (type == SCHEME_hash_late_weak_ptr)
table->weak = SCHEME_BT_KIND_LATE;
else if (type == SCHEME_hash_ephemeron_ptr)
table->weak = SCHEME_BT_KIND_EPHEMERON;
else
table->weak = 0;
return table;
}
void scheme_clear_bucket_table(Scheme_Bucket_Table *bt)
{
Scheme_Bucket **ba;
bt->count = 0;
bt->size = 4;
ba = (Scheme_Bucket **)scheme_malloc(bt->size * sizeof(Scheme_Bucket *));
bt->buckets = ba;
}
static Scheme_Bucket *
allocate_bucket (Scheme_Bucket_Table *table, const char *key, void *val)
{
size_t bsize;
Scheme_Type type;
Scheme_Bucket *bucket;
if (table->with_home) {
bsize = sizeof(Scheme_Bucket_With_Home);
type = scheme_variable_type;
} else {
bsize = sizeof(Scheme_Bucket);
type = scheme_bucket_type;
}
bucket = (Scheme_Bucket *)scheme_malloc_tagged(bsize);
bucket->so.type = type;
if (type == scheme_variable_type)
((Scheme_Bucket_With_Flags *)bucket)->flags = GLOB_HAS_HOME_PTR;
if (table->weak) {
#ifdef MZ_PRECISE_GC
void *kb;
kb = GC_malloc_weak_box((void *)key, (void **)bucket, (void **)&bucket->val XFORM_OK_MINUS (void **)bucket,
(table->weak == SCHEME_BT_KIND_LATE));
bucket->key = (char *)kb;
#else
char *kb;
kb = (char *)MALLOC_ONE_WEAK(void *);
bucket->key = kb;
*(void **)bucket->key = (void *)key;
if (table->weak == SCHEME_BT_KIND_LATE) {
scheme_late_weak_reference_indirect((void **)bucket->key, (void *)key);
scheme_late_weak_reference_indirect((void **)&bucket->val, (void *)key);
} else {
scheme_weak_reference_indirect((void **)bucket->key, (void *)key);
scheme_weak_reference_indirect((void **)&bucket->val, (void *)key);
}
#endif
if (table->weak == SCHEME_BT_KIND_EPHEMERON) {
/* we expect this ephemeron to be cleared if the bcket key is cleared */
val = scheme_make_ephemeron((Scheme_Object *)key, val);
}
} else
bucket->key = (char *)key;
bucket->val = val;
return bucket;
}
static Scheme_Bucket *
get_bucket (Scheme_Bucket_Table *table, const char *key, int add, Scheme_Bucket *b,
Scheme_Object *key_wraps)
{
intptr_t hx, h2x;
hash_v_t h, h2;
void *ekey;
Scheme_Bucket *bucket;
Compare_Proc compare = table->compare;
uintptr_t mask;
rehash_key:
mask = table->size - 1;
if (table->make_hash_indices) {
if (key_wraps)
ekey = apply_equal_key_wraps((Scheme_Object *)key, key_wraps);
else
ekey = (void *)key;
table->make_hash_indices(ekey, &hx, &h2x);
h = fixmix(to_unsigned_hash(hx)) & mask;
h2 = fixmix(to_unsigned_hash(h2x)) & mask;
} else {
uintptr_t lkey;
lkey = fixmix(PTR_TO_LONG((Scheme_Object *)key));
h = lkey & mask;
h2 = (lkey >> 1) & mask;
ekey = NULL;
}
h2 |= 0x1;
if (table->weak) {
int reuse_bucket = 0;
scheme_hash_request_count++;
while ((bucket = table->buckets[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (bucket->key) {
void *hk = (void *)HT_EXTRACT_WEAK(bucket->key);
if (!hk) {
if (!reuse_bucket)
reuse_bucket = h + 1;
} else if (SAME_PTR(hk, key))
return bucket;
else if (key_wraps) {
if (equal_w_key_wraps((Scheme_Object *)ekey, (Scheme_Object *)hk, key_wraps))
return bucket;
} else if (compare && !compare((void *)hk, ekey))
return bucket;
} else if (add)
break;
scheme_hash_iteration_count++;
h = (h + h2) & mask;
}
if (reuse_bucket && add) {
/* Re-use a bucket slot whose key is collected: */
/* DON'T increment counter overall... */
h = reuse_bucket - 1;
--table->count;
}
} else {
scheme_hash_request_count++;
while ((bucket = table->buckets[HASH_TO_ARRAY_INDEX(h, mask)])) {
if (SAME_PTR(bucket->key, key))
return bucket;
else if (key_wraps) {
if (equal_w_key_wraps((Scheme_Object *)ekey, (Scheme_Object *)bucket->key, key_wraps))
return bucket;
} else if (compare && !compare((void *)bucket->key, (void *)key))
return bucket;
scheme_hash_iteration_count++;
h = (h + h2) & mask;
}
}
if (!add)
return NULL;
if (table->count * FILL_FACTOR >= table->size) {
/* Rehash */
int i, oldsize = table->size;
size_t asize;
Scheme_Bucket **old = table->buckets;
if (table->weak && (table->size > 4096)) {
int actual = 0;
/* It might be nice to force a GC so that the new table is
as small as possible, but that's too expensive. */
/* scheme_collect_garbage(); */
/* Check actual count: */
for (i = 0; i < oldsize; i++) {
if (old[i] && old[i]->key && HT_EXTRACT_WEAK(old[i]->key)) {
actual++;
}
}
if (actual * FILL_FACTOR < table->count) {
/* Decrement size so that the table won't actually grow. */
table->size >>= 1;
if ((table->size > 64) && (2 * actual * FILL_FACTOR < table->count)) {
/* Allow the table to shrink */
table->size >>= 1;
}
}
}
table->size <<= 1;
asize = (size_t)table->size * sizeof(Scheme_Bucket *);
{
Scheme_Bucket **ba;
ba = (Scheme_Bucket **)scheme_malloc(asize);
table->buckets = ba;
}
table->count = 0;
if (table->weak) {
for (i = 0; i < oldsize; i++) {
if (old[i] && old[i]->key && HT_EXTRACT_WEAK(old[i]->key))
get_bucket(table, (char *)HT_EXTRACT_WEAK(old[i]->key), 1, old[i], key_wraps);
}
} else {
for (i = 0; i < oldsize; i++) {
if (old[i] && old[i]->key)
get_bucket(table, old[i]->key, 1, old[i], key_wraps);