forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
6094 lines (5429 loc) · 147 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
/**********************************************************************
hash.c -
$Author$
created at: Mon Nov 22 18:51:18 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/encoding.h"
#include "ruby/st.h"
#include "ruby/util.h"
#include "internal.h"
#include <errno.h>
#include "probes.h"
#include "id.h"
#include "symbol.h"
#include "gc.h"
#include "debug_counter.h"
#include "transient_heap.h"
#include "ruby_assert.h"
#ifdef __APPLE__
# ifdef HAVE_CRT_EXTERNS_H
# include <crt_externs.h>
# else
# include "missing/crt_externs.h"
# endif
#endif
#ifndef HASH_DEBUG
#define HASH_DEBUG 0
#endif
#define HAS_EXTRA_STATES(hash, klass) ( \
((klass = has_extra_methods(rb_obj_class(hash))) != 0) || \
FL_TEST((hash), FL_EXIVAR|FL_TAINT|HASH_PROC_DEFAULT) || \
!NIL_P(RHASH_IFNONE(hash)))
#define SET_DEFAULT(hash, ifnone) ( \
FL_UNSET_RAW(hash, HASH_PROC_DEFAULT), \
RHASH_SET_IFNONE(hash, ifnone))
#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
static inline void
copy_default(struct RHash *hash, const struct RHash *hash2)
{
hash->basic.flags &= ~HASH_PROC_DEFAULT;
hash->basic.flags |= hash2->basic.flags & HASH_PROC_DEFAULT;
RHASH_SET_IFNONE(hash, RHASH_IFNONE(hash2));
}
static VALUE
has_extra_methods(VALUE klass)
{
const VALUE base = rb_cHash;
VALUE c = klass;
while (c != base) {
if (rb_class_has_methods(c)) return klass;
c = RCLASS_SUPER(c);
}
return 0;
}
static VALUE rb_hash_s_try_convert(VALUE, VALUE);
/*
* Hash WB strategy:
* 1. Check mutate st_* functions
* * st_insert()
* * st_insert2()
* * st_update()
* * st_add_direct()
* 2. Insert WBs
*/
VALUE
rb_hash_freeze(VALUE hash)
{
return rb_obj_freeze(hash);
}
VALUE rb_cHash;
static VALUE envtbl;
static ID id_hash, id_yield, id_default, id_flatten_bang;
VALUE
rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
{
RB_OBJ_WRITE(hash, (&RHASH(hash)->ifnone), ifnone);
return hash;
}
static int
rb_any_cmp(VALUE a, VALUE b)
{
if (a == b) return 0;
if (FIXNUM_P(a) && FIXNUM_P(b)) {
return a != b;
}
if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
return rb_str_hash_cmp(a, b);
}
if (a == Qundef || b == Qundef) return -1;
if (SYMBOL_P(a) && SYMBOL_P(b)) {
return a != b;
}
return !rb_eql(a, b);
}
static VALUE
hash_recursive(VALUE obj, VALUE arg, int recurse)
{
if (recurse) return INT2FIX(0);
return rb_funcallv(obj, id_hash, 0, 0);
}
VALUE
rb_hash(VALUE obj)
{
VALUE hval = rb_exec_recursive_outer(hash_recursive, obj, 0);
while (!FIXNUM_P(hval)) {
if (RB_TYPE_P(hval, T_BIGNUM)) {
int sign;
unsigned long ul;
sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
INTEGER_PACK_NATIVE_BYTE_ORDER);
ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1;
if (sign < 0)
return LONG2FIX(-(long)ul);
return LONG2FIX((long)ul);
}
hval = rb_to_int(hval);
}
return hval;
}
long rb_objid_hash(st_index_t index);
static st_index_t
dbl_to_index(double d)
{
union {double d; st_index_t i;} u;
u.d = d;
return u.i;
}
long
rb_dbl_long_hash(double d)
{
/* normalize -0.0 to 0.0 */
if (d == 0.0) d = 0.0;
#if SIZEOF_INT == SIZEOF_VOIDP
return rb_memhash(&d, sizeof(d));
#else
return rb_objid_hash(dbl_to_index(d));
#endif
}
static inline long
any_hash(VALUE a, st_index_t (*other_func)(VALUE))
{
VALUE hval;
st_index_t hnum;
if (SPECIAL_CONST_P(a)) {
if (STATIC_SYM_P(a)) {
hnum = a >> (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
hnum = rb_hash_start(hnum);
goto out;
}
else if (FLONUM_P(a)) {
/* prevent pathological behavior: [Bug #10761] */
goto flt;
}
hnum = rb_objid_hash((st_index_t)a);
}
else if (BUILTIN_TYPE(a) == T_STRING) {
hnum = rb_str_hash(a);
}
else if (BUILTIN_TYPE(a) == T_SYMBOL) {
hnum = RSYMBOL(a)->hashval;
}
else if (BUILTIN_TYPE(a) == T_BIGNUM) {
hval = rb_big_hash(a);
hnum = FIX2LONG(hval);
}
else if (BUILTIN_TYPE(a) == T_FLOAT) {
flt:
hnum = rb_dbl_long_hash(rb_float_value(a));
}
else {
hnum = other_func(a);
}
out:
#if SIZEOF_LONG < SIZEOF_ST_INDEX_T
if (hnum > 0)
hnum &= (unsigned long)-1 >> 2;
else
hnum |= ~((unsigned long)-1 >> 2);
#else
hnum <<= 1;
hnum = RSHIFT(hnum, 1);
#endif
return (long)hnum;
}
static st_index_t
obj_any_hash(VALUE obj)
{
obj = rb_hash(obj);
return FIX2LONG(obj);
}
static st_index_t
rb_any_hash(VALUE a)
{
return any_hash(a, obj_any_hash);
}
/* Here is a hash function for 64-bit key. It is about 5 times faster
(2 times faster when uint128 type is absent) on Haswell than
tailored Spooky or City hash function can be. */
/* Here we two primes with random bit generation. */
static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
static const uint32_t prime2 = 0x830fcab9;
static inline uint64_t
mult_and_mix(uint64_t m1, uint64_t m2)
{
#if defined HAVE_UINT128_T
uint128_t r = (uint128_t) m1 * (uint128_t) m2;
return (uint64_t) (r >> 64) ^ (uint64_t) r;
#else
uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
uint64_t lm1 = m1, lm2 = m2;
uint64_t v64_128 = hm1 * hm2;
uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
uint64_t v1_32 = lm1 * lm2;
return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
#endif
}
static inline uint64_t
key64_hash(uint64_t key, uint32_t seed)
{
return mult_and_mix(key + seed, prime1);
}
/* Should cast down the result for each purpose */
#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
long
rb_objid_hash(st_index_t index)
{
return (long)st_index_hash(index);
}
static st_index_t
objid_hash(VALUE obj)
{
#if SIZEOF_LONG == SIZEOF_VOIDP
return (st_index_t)st_index_hash((st_index_t)NUM2LONG(rb_obj_id(obj)));
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
return (st_index_t)st_index_hash((st_index_t)NUM2LL(rb_obj_id(obj)));
#endif
}
VALUE
rb_obj_hash(VALUE obj)
{
long hnum = any_hash(obj, objid_hash);
return ST2FIX(hnum);
}
static const struct st_hash_type objhash = {
rb_any_cmp,
rb_any_hash,
};
#define rb_ident_cmp st_numcmp
static st_index_t
rb_ident_hash(st_data_t n)
{
#ifdef USE_FLONUM /* RUBY */
/*
* - flonum (on 64-bit) is pathologically bad, mix the actual
* float value in, but do not use the float value as-is since
* many integers get interpreted as 2.0 or -2.0 [Bug #10761]
*/
if (FLONUM_P(n)) {
n ^= dbl_to_index(rb_float_value(n));
}
#endif
return (st_index_t)st_index_hash((st_index_t)n);
}
static const struct st_hash_type identhash = {
rb_ident_cmp,
rb_ident_hash,
};
#define RESERVED_HASH_VAL (~(st_hash_t) 0)
#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
typedef st_index_t st_hash_t;
extern const st_hash_t st_reserved_hash_val;
extern const st_hash_t st_reserved_hash_substitution_val;
/*
* RHASH_AR_TABLE_P(h):
* * as.ar == NULL or
* as.ar points ar_table.
* * as.ar is allocated by transient heap or xmalloc.
*
* !RHASH_AR_TABLE_P(h):
* * as.st points st_table.
*/
#define RHASH_AR_TABLE_MAX_SIZE 8
#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
typedef struct ar_table_entry {
st_hash_t hash;
VALUE key;
VALUE record;
} ar_table_entry;
typedef struct ar_table_struct {
ar_table_entry entries[RHASH_AR_TABLE_MAX_SIZE];
} ar_table;
size_t
rb_hash_ar_table_size(void)
{
return sizeof(ar_table);
}
static inline st_hash_t
ar_do_hash(st_data_t key)
{
st_hash_t hash = (st_hash_t)rb_any_hash(key);
return (RESERVED_HASH_VAL == hash) ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
}
static inline void
ar_set_entry(ar_table_entry *entry, st_data_t key, st_data_t val, st_hash_t hash)
{
entry->hash = hash;
entry->key = key;
entry->record = val;
}
static inline void
ar_clear_entry(ar_table_entry* entry)
{
entry->key = Qundef;
entry->record = Qundef;
entry->hash = RESERVED_HASH_VAL;
}
static inline int
ar_empty_entry(ar_table_entry *entry)
{
return entry->hash == RESERVED_HASH_VAL;
}
#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
RHASH_AR_TABLE_SIZE_RAW(h))
#define RHASH_AR_TABLE_BOUND_RAW(h) \
((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
(RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
#define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
RHASH_AR_TABLE_BOUND_RAW(h))
#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->entries[n])
#if HASH_DEBUG
#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(1, expr, #expr)
void
rb_hash_dump(VALUE hash)
{
rb_obj_info_dump(hash);
if (RHASH_AR_TABLE_P(hash)) {
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
fprintf(stderr, " size:%u bound:%u\n",
RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
for (i=0; i<bound; i++) {
ar_table_entry *cur_entry = RHASH_AR_TABLE_REF(hash, i);
st_data_t k, v;
if (!ar_empty_entry(cur_entry)) {
char b1[0x100], b2[0x100];
/* h = cur_entry->hash; */
k = cur_entry->key;
v = cur_entry->record;
fprintf(stderr, " %d key:%s val:%s\n", i,
rb_raw_obj_info(b1, 0x100, k),
rb_raw_obj_info(b2, 0x100, v));
n++;
}
else {
fprintf(stderr, " %d empty\n", i);
}
}
}
}
static VALUE
hash_verify_(VALUE hash, const char *file, int line)
{
HASH_ASSERT(RB_TYPE_P(hash, T_HASH));
if (RHASH_AR_TABLE_P(hash)) {
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
for (i=0; i<bound; i++) {
ar_table_entry *cur_entry = RHASH_AR_TABLE_REF(hash, i);
st_data_t h, k, v;
if (!ar_empty_entry(cur_entry)) {
h = cur_entry->hash;
k = cur_entry->key;
v = cur_entry->record;
HASH_ASSERT(h != RESERVED_HASH_VAL);
HASH_ASSERT(k != Qundef);
HASH_ASSERT(v != Qundef);
n++;
}
}
if (n != RHASH_AR_TABLE_SIZE(hash)) {
rb_bug("n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
}
}
else {
HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
}
if (RHASH_TRANSIENT_P(hash)) {
volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key; /* read */
HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
}
return hash;
}
#else
#define hash_verify(h) ((void)0)
#define HASH_ASSERT(e) ((void)0)
#endif
static inline int
RHASH_TABLE_NULL_P(VALUE hash)
{
if (RHASH(hash)->as.ar == NULL) {
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
return TRUE;
}
else {
return FALSE;
}
}
static inline int
RHASH_TABLE_EMPTY_P(VALUE hash)
{
return RHASH_SIZE(hash) == 0;
}
MJIT_FUNC_EXPORTED int
rb_hash_ar_table_p(VALUE hash)
{
if (FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG)) {
HASH_ASSERT(RHASH(hash)->as.st != NULL);
return FALSE;
}
else {
return TRUE;
}
}
ar_table *
rb_hash_ar_table(VALUE hash)
{
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
return RHASH(hash)->as.ar;
}
MJIT_FUNC_EXPORTED st_table *
rb_hash_st_table(VALUE hash)
{
HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
return RHASH(hash)->as.st;
}
void
rb_hash_st_table_set(VALUE hash, st_table *st)
{
HASH_ASSERT(st != NULL);
FL_SET_RAW((hash), RHASH_ST_TABLE_FLAG);
RHASH(hash)->as.st = st;
}
static void
hash_ar_table_set(VALUE hash, ar_table *ar)
{
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
RHASH(hash)->as.ar = ar;
hash_verify(hash);
}
#define RHASH_AR_TABLE_SET(h, a) hash_ar_table_set(h, a)
#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
#define RHASH_AR_TABLE_BOUND_SET(h, n) do { \
st_index_t tmp_n = (n); \
HASH_ASSERT(RHASH_AR_TABLE_P(h)); \
HASH_ASSERT(tmp_n <= RHASH_AR_TABLE_MAX_BOUND); \
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK; \
RBASIC(h)->flags |= (tmp_n) << RHASH_AR_TABLE_BOUND_SHIFT; \
} while (0)
#define RHASH_AR_TABLE_SIZE_SET(h, n) do { \
st_index_t tmp_n = n; \
HASH_ASSERT(RHASH_AR_TABLE_P(h)); \
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK; \
RBASIC(h)->flags |= (tmp_n) << RHASH_AR_TABLE_SIZE_SHIFT; \
} while (0)
#define HASH_AR_TABLE_SIZE_ADD(h, n) do { \
HASH_ASSERT(RHASH_AR_TABLE_P(h)); \
RHASH_AR_TABLE_SIZE_SET((h), RHASH_AR_TABLE_SIZE(h)+(n)); \
hash_verify(h); \
} while (0)
#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
static inline void
RHASH_AR_TABLE_SIZE_DEC(VALUE h) {
HASH_ASSERT(RHASH_AR_TABLE_P(h));
int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
if (new_size != 0) {
RHASH_AR_TABLE_SIZE_SET(h, new_size);
}
else {
RHASH_AR_TABLE_SIZE_SET(h, 0);
RHASH_AR_TABLE_BOUND_SET(h, 0);
}
hash_verify(h);
}
#define RHASH_AR_TABLE_CLEAR(h) do { \
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK; \
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK; \
RHASH_AR_TABLE_SET(hash, NULL); \
} while (0)
static ar_table*
ar_alloc_table(VALUE hash)
{
ar_table *tab = (ar_table*)rb_transient_heap_alloc(hash, sizeof(ar_table));
if (tab != NULL) {
RHASH_SET_TRANSIENT_FLAG(hash);
}
else {
RHASH_UNSET_TRANSIENT_FLAG(hash);
tab = (ar_table*)ruby_xmalloc(sizeof(ar_table));
}
RHASH_AR_TABLE_SIZE_SET(hash, 0);
RHASH_AR_TABLE_BOUND_SET(hash, 0);
RHASH_AR_TABLE_SET(hash, tab);
return tab;
}
static inline int
ar_equal(VALUE x, VALUE y)
{
return x == y || rb_any_cmp(x, y) == 0;
}
static inline int
ar_ptr_equal(ar_table_entry *entry, st_hash_t hash_val, VALUE key)
{
return entry->hash == hash_val && ar_equal(key, entry->key);
}
static unsigned
ar_find_entry(VALUE hash, st_hash_t hash_value, st_data_t key)
{
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
/* if table is NULL, then bound also should be 0 */
for (i = 0; i < bound; i++) {
if (ar_ptr_equal(RHASH_AR_TABLE_REF(hash, i), hash_value, key)) {
return i;
}
}
return RHASH_AR_TABLE_MAX_BOUND;
}
static inline void
ar_free_and_clear_table(VALUE hash)
{
ar_table *tab = RHASH_AR_TABLE(hash);
if (tab) {
if (RHASH_TRANSIENT_P(hash)) {
RHASH_UNSET_TRANSIENT_FLAG(hash);
}
else {
ruby_xfree(RHASH_AR_TABLE(hash));
}
RHASH_AR_TABLE_CLEAR(hash);
}
HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
}
void st_add_direct_with_hash(st_table *tab, st_data_t key, st_data_t value, st_hash_t hash); /* st.c */
static void
ar_try_convert_table(VALUE hash)
{
st_table *new_tab;
ar_table_entry *entry;
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
st_index_t i;
if (!RHASH_AR_TABLE_P(hash) || size < RHASH_AR_TABLE_MAX_SIZE) {
return;
}
new_tab = st_init_table_with_size(&objhash, size * 2);
for (i = 0; i < RHASH_AR_TABLE_MAX_BOUND; i++) {
entry = RHASH_AR_TABLE_REF(hash, i);
HASH_ASSERT(entry->hash != RESERVED_HASH_VAL);
st_add_direct_with_hash(new_tab, entry->key, entry->record, entry->hash);
}
ar_free_and_clear_table(hash);
RHASH_ST_TABLE_SET(hash, new_tab);
return;
}
static st_table *
ar_force_convert_table(VALUE hash, const char *file, int line)
{
st_table *new_tab;
if (RHASH_ST_TABLE_P(hash)) {
return RHASH_ST_TABLE(hash);
}
if (RHASH_AR_TABLE(hash)) {
ar_table_entry *entry;
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
#if RHASH_CONVERT_TABLE_DEBUG
rb_obj_info_dump(hash);
fprintf(stderr, "force_convert: %s:%d\n", file, line);
RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
#endif
new_tab = st_init_table_with_size(&objhash, RHASH_AR_TABLE_SIZE(hash));
for (i = 0; i < bound; i++) {
entry = RHASH_AR_TABLE_REF(hash, i);
if (ar_empty_entry(entry)) continue;
st_add_direct_with_hash(new_tab, entry->key, entry->record, entry->hash);
}
ar_free_and_clear_table(hash);
}
else {
new_tab = st_init_table(&objhash);
}
RHASH_ST_TABLE_SET(hash, new_tab);
return new_tab;
}
static ar_table *
hash_ar_table(VALUE hash)
{
if (RHASH_TABLE_NULL_P(hash)) {
ar_alloc_table(hash);
}
return RHASH_AR_TABLE(hash);
}
static int
ar_compact_table(VALUE hash)
{
const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
if (size == bound) {
return size;
}
else {
unsigned i, j=0;
ar_table_entry *entries = RHASH_AR_TABLE_REF(hash, 0);
for (i=0; i<bound; i++) {
if (ar_empty_entry(&entries[i])) {
if (j <= i) j = i+1;
for (; j<bound; j++) {
if (!ar_empty_entry(&entries[j])) {
entries[i] = entries[j];
ar_clear_entry(&entries[j]);
j++;
goto found;
}
}
/* non-empty is not found */
goto done;
found:;
}
}
done:
HASH_ASSERT(i<=bound);
RHASH_AR_TABLE_BOUND_SET(hash, size);
hash_verify(hash);
return size;
}
}
static int
ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
{
unsigned bin = RHASH_AR_TABLE_BOUND(hash);
if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
return 1;
}
else {
if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
bin = ar_compact_table(hash);
hash_ar_table(hash);
}
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
ar_set_entry(RHASH_AR_TABLE_REF(hash, bin), key, val, hash_value);
RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
RHASH_AR_TABLE_SIZE_INC(hash);
return 0;
}
}
static int
ar_general_foreach(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg)
{
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
for (i = 0; i < bound; i++) {
enum st_retval retval;
ar_table_entry *cur_entry = RHASH_AR_TABLE_REF(hash, i);
if (ar_empty_entry(cur_entry)) continue;
retval = (*func)(cur_entry->key, cur_entry->record, arg, 0);
/* cur_entry is not valid after that */
switch (retval) {
case ST_CONTINUE:
break;
case ST_CHECK:
case ST_STOP:
return 0;
case ST_REPLACE:
if (replace) {
VALUE key;
VALUE value;
key = cur_entry->key;
value = cur_entry->record;
retval = (*replace)(&key, &value, arg, TRUE);
ar_table_entry *entry = RHASH_AR_TABLE_REF(hash, i);
entry->key = key;
entry->record = value;
}
break;
case ST_DELETE:
ar_clear_entry(RHASH_AR_TABLE_REF(hash, i));
RHASH_AR_TABLE_SIZE_DEC(hash);
break;
}
}
}
return 0;
}
static int
ar_foreach_with_replace(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg)
{
return ar_general_foreach(hash, func, replace, arg);
}
static int
ar_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
{
return ar_general_foreach(hash, func, NULL, arg);
}
static int
ar_foreach_check(VALUE hash, int (*func)(ANYARGS), st_data_t arg,
st_data_t never)
{
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
enum st_retval retval;
ar_table_entry *cur_entry;
st_data_t key;
st_hash_t hash_value;
for (i = 0; i < bound; i++) {
cur_entry = RHASH_AR_TABLE_REF(hash, i);
if (ar_empty_entry(cur_entry))
continue;
key = cur_entry->key;
hash_value = cur_entry->hash;
retval = (*func)(key, cur_entry->record, arg, 0);
hash_verify(hash);
cur_entry = RHASH_AR_TABLE_REF(hash, i);
switch (retval) {
case ST_CHECK: {
if (cur_entry->key == never && cur_entry->hash == RESERVED_HASH_VAL)
break;
ret = ar_find_entry(hash, hash_value, key);
if (ret == RHASH_AR_TABLE_MAX_BOUND) {
retval = (*func)(0, 0, arg, 1);
return 2;
}
}
case ST_CONTINUE:
break;
case ST_STOP:
case ST_REPLACE:
return 0;
case ST_DELETE: {
if (!ar_empty_entry(cur_entry)) {
ar_clear_entry(cur_entry);
RHASH_AR_TABLE_SIZE_DEC(hash);
}
break;
}
}
}
}
return 0;
}
static int
ar_update(VALUE hash, st_data_t key,
st_update_callback_func *func, st_data_t arg)
{
int retval, existing;
unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
st_data_t value = 0, old_key;
st_hash_t hash_value = ar_do_hash(key);
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
bin = ar_find_entry(hash, hash_value, key);
existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
}
else {
hash_ar_table(hash); /* allocate ltbl if needed */
existing = FALSE;
}
if (existing) {
ar_table_entry *entry = RHASH_AR_TABLE_REF(hash, bin);
key = entry->key;
value = entry->record;
}
old_key = key;
retval = (*func)(&key, &value, arg, existing);
switch (retval) {
case ST_CONTINUE:
if (!existing) {
if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
return -1;
}
}
else {
ar_table_entry *entry = RHASH_AR_TABLE_REF(hash, bin);
if (old_key != key) {
entry->key = key;
}
entry->record = value;
}
break;
case ST_DELETE:
if (existing) {
ar_clear_entry(RHASH_AR_TABLE_REF(hash, bin));
RHASH_AR_TABLE_SIZE_DEC(hash);
}
break;
}
return existing;
}
static int
ar_insert(VALUE hash, st_data_t key, st_data_t value)
{
unsigned bin = RHASH_AR_TABLE_BOUND(hash);
st_hash_t hash_value = ar_do_hash(key);
hash_ar_table(hash); /* prepare ltbl */
bin = ar_find_entry(hash, hash_value, key);
if (bin == RHASH_AR_TABLE_MAX_BOUND) {
if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
return -1;
}
else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
bin = ar_compact_table(hash);
hash_ar_table(hash);
}
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
ar_set_entry(RHASH_AR_TABLE_REF(hash, bin), key, value, hash_value);
RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
RHASH_AR_TABLE_SIZE_INC(hash);
return 0;
}
else {
RHASH_AR_TABLE_REF(hash, bin)->record = value;
return 1;
}
}
static int
ar_lookup(VALUE hash, st_data_t key, st_data_t *value)
{
if (RHASH_AR_TABLE_SIZE(hash) == 0) {
return 0;
}
else {
st_hash_t hash_value = ar_do_hash(key);
unsigned bin = ar_find_entry(hash, hash_value, key);
if (bin == RHASH_AR_TABLE_MAX_BOUND) {
return 0;
}
else {
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
if (value != NULL) {
*value = RHASH_AR_TABLE_REF(hash, bin)->record;
}
return 1;
}
}
}
static int
ar_delete(VALUE hash, st_data_t *key, st_data_t *value)
{
unsigned bin;