Uh oh!
There was an error while loading. Please reload this page.
Expand file tree
/
Copy pathinternal.h
More file actions
Latest commit
2682 lines (2370 loc) · 84.4 KB
/
Copy pathinternal.h
File metadata and controls
2682 lines (2370 loc) · 84.4 KB
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
/**********************************************************************
internal.h -
$Author$
created at: Tue May 17 11:42:20 JST 2011
Copyright (C) 2011 Yukihiro Matsumoto
**********************************************************************/
#ifndefRUBY_INTERNAL_H
#defineRUBY_INTERNAL_H 1
#include"ruby.h"
#if defined(__cplusplus)
extern"C" {
#if0
} /* satisfy cc-mode */
#endif
#endif
#ifdefHAVE_STDBOOL_H
# include<stdbool.h>
#else
# include"missing/stdbool.h"
#endif
/* The most significant bit of the lower part of half-long integer.
* If sizeof(long) == 4, this is 0x8000.
* If sizeof(long) == 8, this is 0x80000000.
*/
#defineHALF_LONG_MSB ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
#defineLIKELY(x) RB_LIKELY(x)
#defineUNLIKELY(x) RB_UNLIKELY(x)
#ifndefMAYBE_UNUSED
# defineMAYBE_UNUSED(x) x
#endif
#ifndefWARN_UNUSED_RESULT
# defineWARN_UNUSED_RESULT(x) x
#endif
#ifndef__has_feature
# define__has_feature(x) 0
#endif
#ifndef__has_extension
# define__has_extension __has_feature
#endif
#if0
#elif defined(NO_SANITIZE) &&__has_feature(memory_sanitizer)
# defineATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
NO_SANITIZE("memory", NO_SANITIZE("address", NOINLINE(x)))
#elif defined(NO_SANITIZE)
# defineATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
NO_SANITIZE("address", NOINLINE(x))
#elif defined(NO_SANITIZE_ADDRESS)
# defineATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
NO_SANITIZE_ADDRESS(NOINLINE(x))
#elif defined(NO_ADDRESS_SAFETY_ANALYSIS)
# defineATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
NO_ADDRESS_SAFETY_ANALYSIS(NOINLINE(x))
#else
# defineATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) x
#endif
#if defined(NO_SANITIZE) && defined(__GNUC__) &&! defined(__clang__)
/* GCC warns about unknown sanitizer, which is annoying. */
#undef NO_SANITIZE
#defineNO_SANITIZE(x, y) \
COMPILER_WARNING_PUSH; \
COMPILER_WARNING_IGNORED(-Wattributes); \
__attribute__((__no_sanitize__(x))) y; \
COMPILER_WARNING_POP
#endif
#ifndefNO_SANITIZE
# defineNO_SANITIZE(x, y) y
#endif
#ifdefHAVE_VALGRIND_MEMCHECK_H
# include<valgrind/memcheck.h>
# ifndefVALGRIND_MAKE_MEM_DEFINED
# defineVALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
# endif
# ifndefVALGRIND_MAKE_MEM_UNDEFINED
# defineVALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
# endif
#else
# defineVALGRIND_MAKE_MEM_DEFINED(p, n) 0
# defineVALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
#endif
#definenumberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
#ifndefMJIT_HEADER
#ifdefHAVE_SANITIZER_ASAN_INTERFACE_H
# include<sanitizer/asan_interface.h>
#endif
#if !__has_feature(address_sanitizer)
# define__asan_poison_memory_region(x, y)
# define__asan_unpoison_memory_region(x, y)
# define__asan_region_is_poisoned(x, y) 0
#endif
#ifdefHAVE_SANITIZER_MSAN_INTERFACE_H
# if__has_feature(memory_sanitizer)
# include<sanitizer/msan_interface.h>
# endif
#endif
#if !__has_feature(memory_sanitizer)
# define__msan_allocated_memory(x, y) ((void)(x), (void)(y))
# define__msan_poison(x, y) ((void)(x), (void)(y))
# define__msan_unpoison(x, y) ((void)(x), (void)(y))
# define__msan_unpoison_string(x) ((void)(x))
#endif
/*!
* This function asserts that a (continuous) memory region from ptr to size
* being "poisoned". Both read / write access to such memory region are
* prohibited until properly unpoisoned. The region must be previously
* allocated (do not pass a freed pointer here), but not necessarily be an
* entire object that the malloc returns. You can punch hole a part of a
* gigantic heap arena. This is handy when you do not free an allocated memory
* region to reuse later: poison when you keep it unused, and unpoison when you
* reuse.
*
* \param[in] ptr pointer to the beginning of the memory region to poison.
* \param[in] size the length of the memory region to poison.
*/
staticinlinevoid
asan_poison_memory_region(constvolatilevoid*ptr, size_tsize)
{
__msan_poison(ptr, size);
__asan_poison_memory_region(ptr, size);
}
/*!
* This is a variant of asan_poison_memory_region that takes a VALUE.
*
* \param[in] obj target object.
*/
staticinlinevoid
asan_poison_object(VALUEobj)
{
MAYBE_UNUSED(structRVALUE*) ptr= (void*)obj;
asan_poison_memory_region(ptr, SIZEOF_VALUE);
}
#if !__has_feature(address_sanitizer)
#defineasan_poison_object_if(ptr, obj) ((void)(ptr), (void)(obj))
#else
#defineasan_poison_object_if(ptr, obj) do { \
if (ptr) asan_poison_object(obj); \
} while (0)
#endif
/*!
* This function predicates if the given object is fully addressable or not.
*
* \param[in] obj target object.
* \retval 0 the given object is fully addressable.
* \retval otherwise pointer to first such byte who is poisoned.
*/
staticinlinevoid*
asan_poisoned_object_p(VALUEobj)
{
MAYBE_UNUSED(structRVALUE*) ptr= (void*)obj;
return__asan_region_is_poisoned(ptr, SIZEOF_VALUE);
}
/*!
* This function asserts that a (formally poisoned) memory region from ptr to
* size is now addressable. Write access to such memory region gets allowed.
* However read access might or might not be possible depending on situations,
* because the region can have contents of previous usages. That information
* should be passed by the malloc_p flag. If that is true, the contents of the
* region is _not_ fully defined (like the return value of malloc behaves).
* Reading from there is NG; write something first. If malloc_p is false on
* the other hand, that memory region is fully defined and can be read
* immediately.
*
* \param[in] ptr pointer to the beginning of the memory region to unpoison.
* \param[in] size the length of the memory region.
* \param[in] malloc_p if the memory region is like a malloc's return value or not.
*/
staticinlinevoid
asan_unpoison_memory_region(constvolatilevoid*ptr, size_tsize, boolmalloc_p)
{
__asan_unpoison_memory_region(ptr, size);
if (malloc_p) {
__msan_allocated_memory(ptr, size);
}
else {
__msan_unpoison(ptr, size);
}
}
/*!
* This is a variant of asan_unpoison_memory_region that takes a VALUE.
*
* \param[in] obj target object.
* \param[in] malloc_p if the memory region is like a malloc's return value or not.
*/
staticinlinevoid
asan_unpoison_object(VALUEobj, boolnewobj_p)
{
MAYBE_UNUSED(structRVALUE*) ptr= (void*)obj;
asan_unpoison_memory_region(ptr, SIZEOF_VALUE, newobj_p);
}
#endif
/* Prevent compiler from reordering access */
#defineACCESS_ONCE(type,x) (*((volatile type *)&(x)))
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
# defineSTATIC_ASSERT(name, expr) _Static_assert(expr, #name ": " #expr)
#elifGCC_VERSION_SINCE(4, 6, 0) ||__has_extension(c_static_assert)
# defineSTATIC_ASSERT(name, expr) RB_GNUC_EXTENSION _Static_assert(expr, #name ": " #expr)
#else
# defineSTATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[1 - 2*!(expr)]
#endif
#defineSIGNED_INTEGER_TYPE_P(int_type) (0 > ((int_type)0)-1)
#defineSIGNED_INTEGER_MAX(sint_type) \
(sint_type) \
((((sint_type)1) << (sizeof(sint_type) * CHAR_BIT - 2)) | \
((((sint_type)1) << (sizeof(sint_type) * CHAR_BIT - 2)) - 1))
#defineSIGNED_INTEGER_MIN(sint_type) (-SIGNED_INTEGER_MAX(sint_type)-1)
#defineUNSIGNED_INTEGER_MAX(uint_type) (~(uint_type)0)
#ifSIGNEDNESS_OF_TIME_T<0/* signed */
# defineTIMET_MAX SIGNED_INTEGER_MAX(time_t)
# defineTIMET_MIN SIGNED_INTEGER_MIN(time_t)
#elifSIGNEDNESS_OF_TIME_T>0/* unsigned */
# defineTIMET_MAX UNSIGNED_INTEGER_MAX(time_t)
# defineTIMET_MIN ((time_t)0)
#endif
#defineTIMET_MAX_PLUS_ONE (2*(double)(TIMET_MAX/2+1))
#ifdefHAVE_BUILTIN___BUILTIN_MUL_OVERFLOW_P
#defineMUL_OVERFLOW_P(a, b) \
__builtin_mul_overflow_p((a), (b), (__typeof__(a * b))0)
#elif defined HAVE_BUILTIN___BUILTIN_MUL_OVERFLOW
#defineMUL_OVERFLOW_P(a, b) \
RB_GNUC_EXTENSION_BLOCK(__typeof__(a) c; __builtin_mul_overflow((a), (b), &c))
#endif
#defineMUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
(a) == 0 ? 0 : \
(a) == -1 ? (b) < -(max) : \
(a) > 0 ? \
((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
#ifdefHAVE_BUILTIN___BUILTIN_MUL_OVERFLOW_P
/* __builtin_mul_overflow_p can take bitfield */
/* and GCC permits bitfields for integers other than int */
#defineMUL_OVERFLOW_FIXNUM_P(a, b) RB_GNUC_EXTENSION_BLOCK( \
struct { long fixnum : SIZEOF_LONG * CHAR_BIT - 1; } c; \
__builtin_mul_overflow_p((a), (b), c.fixnum); \
)
#else
#defineMUL_OVERFLOW_FIXNUM_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXNUM_MIN, FIXNUM_MAX)
#endif
#ifdefMUL_OVERFLOW_P
#defineMUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_P(a, b)
#defineMUL_OVERFLOW_LONG_P(a, b) MUL_OVERFLOW_P(a, b)
#defineMUL_OVERFLOW_INT_P(a, b) MUL_OVERFLOW_P(a, b)
#else
#defineMUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LLONG_MIN, LLONG_MAX)
#defineMUL_OVERFLOW_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LONG_MIN, LONG_MAX)
#defineMUL_OVERFLOW_INT_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, INT_MIN, INT_MAX)
#endif
#ifndefswap16
# ifdefHAVE_BUILTIN___BUILTIN_BSWAP16
# defineswap16(x) __builtin_bswap16(x)
# endif
#endif
#ifndefswap16
# defineswap16(x) ((uint16_t)((((x)&0xFF)<<8) | (((x)>>8)&0xFF)))
#endif
#ifndefswap32
# ifdefHAVE_BUILTIN___BUILTIN_BSWAP32
# defineswap32(x) __builtin_bswap32(x)
# endif
#endif
#ifndefswap32
# defineswap32(x) ((uint32_t)((((x)&0xFF)<<24) \
|(((x)>>24)&0xFF) \
|(((x)&0x0000FF00)<<8) \
|(((x)&0x00FF0000)>>8) ))
#endif
#ifndefswap64
# ifdefHAVE_BUILTIN___BUILTIN_BSWAP64
# defineswap64(x) __builtin_bswap64(x)
# endif
#endif
#ifndefswap64
# ifdefHAVE_INT64_T
# definebyte_in_64bit(n) ((uint64_t)0xff << (n))
# defineswap64(x) ((uint64_t)((((x)&byte_in_64bit(0))<<56) \
|(((x)>>56)&0xFF) \
|(((x)&byte_in_64bit(8))<<40) \
|(((x)&byte_in_64bit(48))>>40) \
|(((x)&byte_in_64bit(16))<<24) \
|(((x)&byte_in_64bit(40))>>24) \
|(((x)&byte_in_64bit(24))<<8) \
|(((x)&byte_in_64bit(32))>>8)))
# endif
#endif
staticinlineunsigned int
nlz_int(unsigned intx)
{
#if defined(HAVE_BUILTIN___BUILTIN_CLZ)
if (x==0) returnSIZEOF_INT*CHAR_BIT;
return (unsigned int)__builtin_clz(x);
#else
unsigned inty;
# if64<SIZEOF_INT*CHAR_BIT
unsigned intn=128;
# elif32<SIZEOF_INT*CHAR_BIT
unsigned intn=64;
# else
unsigned intn=32;
# endif
# if64<SIZEOF_INT*CHAR_BIT
y=x >> 64; if (y) {n-=64; x=y;}
# endif
# if32<SIZEOF_INT*CHAR_BIT
y=x >> 32; if (y) {n-=32; x=y;}
# endif
y=x >> 16; if (y) {n-=16; x=y;}
y=x >> 8; if (y) {n-=8; x=y;}
y=x >> 4; if (y) {n-=4; x=y;}
y=x >> 2; if (y) {n-=2; x=y;}
y=x >> 1; if (y) {returnn-2;}
return (unsigned int)(n-x);
#endif
}
staticinlineunsigned int
nlz_long(unsigned longx)
{
#if defined(HAVE_BUILTIN___BUILTIN_CLZL)
if (x==0) returnSIZEOF_LONG*CHAR_BIT;
return (unsigned int)__builtin_clzl(x);
#else
unsigned longy;
# if64<SIZEOF_LONG*CHAR_BIT
unsigned intn=128;
# elif32<SIZEOF_LONG*CHAR_BIT
unsigned intn=64;
# else
unsigned intn=32;
# endif
# if64<SIZEOF_LONG*CHAR_BIT
y=x >> 64; if (y) {n-=64; x=y;}
# endif
# if32<SIZEOF_LONG*CHAR_BIT
y=x >> 32; if (y) {n-=32; x=y;}
# endif
y=x >> 16; if (y) {n-=16; x=y;}
y=x >> 8; if (y) {n-=8; x=y;}
y=x >> 4; if (y) {n-=4; x=y;}
y=x >> 2; if (y) {n-=2; x=y;}
y=x >> 1; if (y) {returnn-2;}
return (unsigned int)(n-x);
#endif
}
#ifdefHAVE_LONG_LONG
staticinlineunsigned int
nlz_long_long(unsigned LONG_LONGx)
{
#if defined(HAVE_BUILTIN___BUILTIN_CLZLL)
if (x==0) returnSIZEOF_LONG_LONG*CHAR_BIT;
return (unsigned int)__builtin_clzll(x);
#else
unsigned LONG_LONGy;
# if64<SIZEOF_LONG_LONG*CHAR_BIT
unsigned intn=128;
# elif32<SIZEOF_LONG_LONG*CHAR_BIT
unsigned intn=64;
# else
unsigned intn=32;
# endif
# if64<SIZEOF_LONG_LONG*CHAR_BIT
y=x >> 64; if (y) {n-=64; x=y;}
# endif
# if32<SIZEOF_LONG_LONG*CHAR_BIT
y=x >> 32; if (y) {n-=32; x=y;}
# endif
y=x >> 16; if (y) {n-=16; x=y;}
y=x >> 8; if (y) {n-=8; x=y;}
y=x >> 4; if (y) {n-=4; x=y;}
y=x >> 2; if (y) {n-=2; x=y;}
y=x >> 1; if (y) {returnn-2;}
return (unsigned int)(n-x);
#endif
}
#endif
#ifdefHAVE_UINT128_T
staticinlineunsigned int
nlz_int128(uint128_tx)
{
uint128_ty;
unsigned intn=128;
y=x >> 64; if (y) {n-=64; x=y;}
y=x >> 32; if (y) {n-=32; x=y;}
y=x >> 16; if (y) {n-=16; x=y;}
y=x >> 8; if (y) {n-=8; x=y;}
y=x >> 4; if (y) {n-=4; x=y;}
y=x >> 2; if (y) {n-=2; x=y;}
y=x >> 1; if (y) {returnn-2;}
return (unsigned int)(n-x);
}
#endif
staticinlineunsigned int
nlz_intptr(uintptr_tx)
{
#ifSIZEOF_UINTPTR_T==SIZEOF_INT
returnnlz_int(x);
#elifSIZEOF_UINTPTR_T==SIZEOF_LONG
returnnlz_long(x);
#elifSIZEOF_UINTPTR_T==SIZEOF_LONG_LONG
returnnlz_long_long(x);
#else
#error no known integer type corresponds uintptr_t
return/* sane compiler */ ~0;
#endif
}
staticinlineunsigned int
rb_popcount32(uint32_tx)
{
#ifdefHAVE_BUILTIN___BUILTIN_POPCOUNT
return (unsigned int)__builtin_popcount(x);
#else
x= (x&0x55555555) + (x >> 1&0x55555555);
x= (x&0x33333333) + (x >> 2&0x33333333);
x= (x&0x0f0f0f0f) + (x >> 4&0x0f0f0f0f);
x= (x&0x001f001f) + (x >> 8&0x001f001f);
return (x&0x0000003f) + (x >>16&0x0000003f);
#endif
}
staticinlineint
rb_popcount64(uint64_tx)
{
#ifdefHAVE_BUILTIN___BUILTIN_POPCOUNT
return__builtin_popcountll(x);
#else
x= (x&0x5555555555555555) + (x >> 1&0x5555555555555555);
x= (x&0x3333333333333333) + (x >> 2&0x3333333333333333);
x= (x&0x0707070707070707) + (x >> 4&0x0707070707070707);
x= (x&0x001f001f001f001f) + (x >> 8&0x001f001f001f001f);
x= (x&0x0000003f0000003f) + (x >>16&0x0000003f0000003f);
return (x&0x7f) + (x >>32&0x7f);
#endif
}
staticinlineint
rb_popcount_intptr(uintptr_tx)
{
#ifSIZEOF_VOIDP==8
returnrb_popcount64(x);
#elifSIZEOF_VOIDP==4
returnrb_popcount32(x);
#endif
}
staticinlineint
ntz_int32(uint32_tx)
{
#ifdefHAVE_BUILTIN___BUILTIN_CTZ
return__builtin_ctz(x);
#else
returnrb_popcount32((~x) & (x-1));
#endif
}
staticinlineint
ntz_int64(uint64_tx)
{
#ifdefHAVE_BUILTIN___BUILTIN_CTZLL
return__builtin_ctzll(x);
#else
returnrb_popcount64((~x) & (x-1));
#endif
}
staticinlineint
ntz_intptr(uintptr_tx)
{
#ifSIZEOF_VOIDP==8
returnntz_int64(x);
#elifSIZEOF_VOIDP==4
returnntz_int32(x);
#endif
}
#ifHAVE_LONG_LONG&&SIZEOF_LONG*2 <= SIZEOF_LONG_LONG
# defineDLONG LONG_LONG
# defineDL2NUM(x) LL2NUM(x)
#elif defined(HAVE_INT128_T)
# defineDLONG int128_t
# defineDL2NUM(x) (RB_FIXABLE(x) ? LONG2FIX(x) : rb_int128t2big(x))
VALUErb_int128t2big(int128_tn);
#endif
staticinlinelong
rb_overflowed_fix_to_int(longx)
{
return (long)((unsigned long)(x >> 1) ^ (1LU << (SIZEOF_LONG*CHAR_BIT-1)));
}
staticinlineVALUE
rb_fix_plus_fix(VALUEx, VALUEy)
{
#ifdefHAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
longlz;
/* NOTE
* (1) `LONG2FIX(FIX2LONG(x)+FIX2LONG(y))`
+ = `((lx*2+1)/2 + (ly*2+1)/2)*2+1`
+ = `lx*2 + ly*2 + 1`
+ = `(lx*2+1) + (ly*2+1) - 1`
+ = `x + y - 1`
* (2) Fixnum's LSB is always 1.
* It means you can always run `x - 1` without overflow.
* (3) Of course `z = x + (y-1)` may overflow.
* At that time true value is
* * positive: 0b0 1xxx...1, and z = 0b1xxx...1
* * nevative: 0b1 0xxx...1, and z = 0b0xxx...1
* To convert this true value to long,
* (a) Use arithmetic shift
* * positive: 0b11xxx...
* * negative: 0b00xxx...
* (b) invert MSB
* * positive: 0b01xxx...
* * negative: 0b10xxx...
*/
if (__builtin_add_overflow((long)x, (long)y-1, &lz)) {
returnrb_int2big(rb_overflowed_fix_to_int(lz));
}
else {
return (VALUE)lz;
}
#else
longlz=FIX2LONG(x) +FIX2LONG(y);
returnLONG2NUM(lz);
#endif
}
staticinlineVALUE
rb_fix_minus_fix(VALUEx, VALUEy)
{
#ifdefHAVE_BUILTIN___BUILTIN_SUB_OVERFLOW
longlz;
if (__builtin_sub_overflow((long)x, (long)y-1, &lz)) {
returnrb_int2big(rb_overflowed_fix_to_int(lz));
}
else {
return (VALUE)lz;
}
#else
longlz=FIX2LONG(x) -FIX2LONG(y);
returnLONG2NUM(lz);
#endif
}
/* arguments must be Fixnum */
staticinlineVALUE
rb_fix_mul_fix(VALUEx, VALUEy)
{
longlx=FIX2LONG(x);
longly=FIX2LONG(y);
#ifdefDLONG
returnDL2NUM((DLONG)lx* (DLONG)ly);
#else
if (MUL_OVERFLOW_FIXNUM_P(lx, ly)) {
returnrb_big_mul(rb_int2big(lx), rb_int2big(ly));
}
else {
returnLONG2FIX(lx*ly);
}
#endif
}
/*
* This behaves different from C99 for negative arguments.
* Note that div may overflow fixnum.
*/
staticinlinevoid
rb_fix_divmod_fix(VALUEa, VALUEb, VALUE*divp, VALUE*modp)
{
/* assume / and % comply C99.
* ldiv(3) won't be inlined by GCC and clang.
* I expect / and % are compiled as single idiv.
*/
longx=FIX2LONG(a);
longy=FIX2LONG(b);
longdiv, mod;
if (x==FIXNUM_MIN&&y==-1) {
if (divp) *divp=LONG2NUM(-FIXNUM_MIN);
if (modp) *modp=LONG2FIX(0);
return;
}
div=x / y;
mod=x % y;
if (y>0 ? mod<0 : mod>0) {
mod+=y;
div-=1;
}
if (divp) *divp=LONG2FIX(div);
if (modp) *modp=LONG2FIX(mod);
}
/* div() for Ruby
* This behaves different from C99 for negative arguments.
*/
staticinlineVALUE
rb_fix_div_fix(VALUEx, VALUEy)
{
VALUEdiv;
rb_fix_divmod_fix(x, y, &div, NULL);
returndiv;
}
/* mod() for Ruby
* This behaves different from C99 for negative arguments.
*/
staticinlineVALUE
rb_fix_mod_fix(VALUEx, VALUEy)
{
VALUEmod;
rb_fix_divmod_fix(x, y, NULL, &mod);
returnmod;
}
#if defined(HAVE_UINT128_T) && defined(HAVE_LONG_LONG)
# definebit_length(x) \
(unsigned int) \
(sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \
sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)) : \
sizeof(x) <= SIZEOF_LONG_LONG ? SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long((unsigned LONG_LONG)(x)) : \
SIZEOF_INT128_T * CHAR_BIT - nlz_int128((uint128_t)(x)))
#elif defined(HAVE_UINT128_T)
# definebit_length(x) \
(unsigned int) \
(sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \
sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)) : \
SIZEOF_INT128_T * CHAR_BIT - nlz_int128((uint128_t)(x)))
#elif defined(HAVE_LONG_LONG)
# definebit_length(x) \
(unsigned int) \
(sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \
sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)) : \
SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long((unsigned LONG_LONG)(x)))
#else
# definebit_length(x) \
(unsigned int) \
(sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \
SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)))
#endif
#ifndefBDIGIT
# ifSIZEOF_INT*2 <= SIZEOF_LONG_LONG
# defineBDIGIT unsigned int
# defineSIZEOF_BDIGIT SIZEOF_INT
# defineBDIGIT_DBL unsigned LONG_LONG
# defineBDIGIT_DBL_SIGNED LONG_LONG
# definePRI_BDIGIT_PREFIX ""
# definePRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
# elifSIZEOF_INT*2 <= SIZEOF_LONG
# defineBDIGIT unsigned int
# defineSIZEOF_BDIGIT SIZEOF_INT
# defineBDIGIT_DBL unsigned long
# defineBDIGIT_DBL_SIGNED long
# definePRI_BDIGIT_PREFIX ""
# definePRI_BDIGIT_DBL_PREFIX "l"
# elifSIZEOF_SHORT*2 <= SIZEOF_LONG
# defineBDIGIT unsigned short
# defineSIZEOF_BDIGIT SIZEOF_SHORT
# defineBDIGIT_DBL unsigned long
# defineBDIGIT_DBL_SIGNED long
# definePRI_BDIGIT_PREFIX "h"
# definePRI_BDIGIT_DBL_PREFIX "l"
# else
# defineBDIGIT unsigned short
# defineSIZEOF_BDIGIT (SIZEOF_LONG/2)
# defineSIZEOF_ACTUAL_BDIGIT SIZEOF_LONG
# defineBDIGIT_DBL unsigned long
# defineBDIGIT_DBL_SIGNED long
# definePRI_BDIGIT_PREFIX "h"
# definePRI_BDIGIT_DBL_PREFIX "l"
# endif
#endif
#ifndefSIZEOF_ACTUAL_BDIGIT
# defineSIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
#endif
#ifdefPRI_BDIGIT_PREFIX
# definePRIdBDIGIT PRI_BDIGIT_PREFIX"d"
# definePRIiBDIGIT PRI_BDIGIT_PREFIX"i"
# definePRIoBDIGIT PRI_BDIGIT_PREFIX"o"
# definePRIuBDIGIT PRI_BDIGIT_PREFIX"u"
# definePRIxBDIGIT PRI_BDIGIT_PREFIX"x"
# definePRIXBDIGIT PRI_BDIGIT_PREFIX"X"
#endif
#ifdefPRI_BDIGIT_DBL_PREFIX
# definePRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
# definePRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
# definePRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
# definePRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
# definePRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
# definePRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
#endif
#defineBIGNUM_EMBED_LEN_NUMBITS 3
#ifndefBIGNUM_EMBED_LEN_MAX
# if (SIZEOF_VALUE*RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
# defineBIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT)
# else
# defineBIGNUM_EMBED_LEN_MAX ((1 << BIGNUM_EMBED_LEN_NUMBITS)-1)
# endif
#endif
structRBignum {
structRBasicbasic;
union {
struct {
size_tlen;
BDIGIT*digits;
} heap;
BDIGITary[BIGNUM_EMBED_LEN_MAX];
} as;
};
#defineBIGNUM_SIGN_BIT ((VALUE)FL_USER1)
/* sign: positive:1, negative:0 */
#defineBIGNUM_SIGN(b) ((RBASIC(b)->flags & BIGNUM_SIGN_BIT) != 0)
#defineBIGNUM_SET_SIGN(b,sign) \
((sign) ? (RBASIC(b)->flags |= BIGNUM_SIGN_BIT) \
: (RBASIC(b)->flags &= ~BIGNUM_SIGN_BIT))
#defineBIGNUM_POSITIVE_P(b) BIGNUM_SIGN(b)
#defineBIGNUM_NEGATIVE_P(b) (!BIGNUM_SIGN(b))
#defineBIGNUM_NEGATE(b) (RBASIC(b)->flags ^= BIGNUM_SIGN_BIT)
#defineBIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
#defineBIGNUM_EMBED_LEN_MASK \
(~(~(VALUE)0U << BIGNUM_EMBED_LEN_NUMBITS) << BIGNUM_EMBED_LEN_SHIFT)
#defineBIGNUM_EMBED_LEN_SHIFT \
(FL_USHIFT+3) /* bit offset of BIGNUM_EMBED_LEN_MASK */
#defineBIGNUM_LEN(b) \
((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
(size_t)((RBASIC(b)->flags >> BIGNUM_EMBED_LEN_SHIFT) & \
(BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)) : \
RBIGNUM(b)->as.heap.len)
/* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */
#defineBIGNUM_DIGITS(b) \
((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
RBIGNUM(b)->as.ary : \
RBIGNUM(b)->as.heap.digits)
#defineBIGNUM_LENINT(b) rb_long2int(BIGNUM_LEN(b))
#defineRBIGNUM(obj) (R_CAST(RBignum)(obj))
structRRational {
structRBasicbasic;
VALUEnum;
VALUEden;
};
#defineRRATIONAL(obj) (R_CAST(RRational)(obj))
#defineRRATIONAL_SET_NUM(rat, n) RB_OBJ_WRITE((rat), &((struct RRational *)(rat))->num,(n))
#defineRRATIONAL_SET_DEN(rat, d) RB_OBJ_WRITE((rat), &((struct RRational *)(rat))->den,(d))
structRFloat {
structRBasicbasic;
doublefloat_value;
};
#defineRFLOAT(obj) (R_CAST(RFloat)(obj))
structRComplex {
structRBasicbasic;
VALUEreal;
VALUEimag;
};
#defineRCOMPLEX(obj) (R_CAST(RComplex)(obj))
/* shortcut macro for internal only */
#defineRCOMPLEX_SET_REAL(cmp, r) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->real,(r))
#defineRCOMPLEX_SET_IMAG(cmp, i) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->imag,(i))
enumruby_rhash_flags {
RHASH_PASS_AS_KEYWORDS=FL_USER1, /* FL 1 */
RHASH_PROC_DEFAULT=FL_USER2, /* FL 2 */
RHASH_ST_TABLE_FLAG=FL_USER3, /* FL 3 */
#define RHASH_AR_TABLE_MAX_SIZE SIZEOF_VALUE
RHASH_AR_TABLE_SIZE_MASK= (FL_USER4|FL_USER5|FL_USER6|FL_USER7), /* FL 4..7 */
RHASH_AR_TABLE_SIZE_SHIFT= (FL_USHIFT+4),
RHASH_AR_TABLE_BOUND_MASK= (FL_USER8|FL_USER9|FL_USER10|FL_USER11), /* FL 8..11 */
RHASH_AR_TABLE_BOUND_SHIFT= (FL_USHIFT+8),
// we can not put it in "enum" because it can exceed "int" range.
#define RHASH_LEV_MASK (FL_USER13 | FL_USER14 | FL_USER15 | /* FL 13..19 */ \
FL_USER16 | FL_USER17 | FL_USER18 | FL_USER19)
#ifUSE_TRANSIENT_HEAP
RHASH_TRANSIENT_FLAG=FL_USER12, /* FL 12 */
#endif
RHASH_LEV_SHIFT= (FL_USHIFT+13),
RHASH_LEV_MAX=127, /* 7 bits */
RHASH_ENUM_END
};
#defineRHASH_AR_TABLE_SIZE_RAW(h) \
((unsigned int)((RBASIC(h)->flags & RHASH_AR_TABLE_SIZE_MASK) >> RHASH_AR_TABLE_SIZE_SHIFT))
voidrb_hash_st_table_set(VALUEhash, st_table*st);
#if0/* for debug */
intrb_hash_ar_table_p(VALUEhash);
structar_table_struct*rb_hash_ar_table(VALUEhash);
st_table*rb_hash_st_table(VALUEhash);
#defineRHASH_AR_TABLE_P(hash) rb_hash_ar_table_p(hash)
#defineRHASH_AR_TABLE(h) rb_hash_ar_table(h)
#defineRHASH_ST_TABLE(h) rb_hash_st_table(h)
#else
#defineRHASH_AR_TABLE_P(hash) (!FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG))
#defineRHASH_AR_TABLE(hash) (RHASH(hash)->as.ar)
#defineRHASH_ST_TABLE(hash) (RHASH(hash)->as.st)
#endif
#defineRHASH(obj) (R_CAST(RHash)(obj))
#defineRHASH_ST_SIZE(h) (RHASH_ST_TABLE(h)->num_entries)
#defineRHASH_ST_TABLE_P(h) (!RHASH_AR_TABLE_P(h))
#defineRHASH_ST_CLEAR(h) (FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG), RHASH(h)->as.ar = NULL)
#defineRHASH_AR_TABLE_SIZE_MASK (VALUE)RHASH_AR_TABLE_SIZE_MASK
#defineRHASH_AR_TABLE_SIZE_SHIFT RHASH_AR_TABLE_SIZE_SHIFT
#defineRHASH_AR_TABLE_BOUND_MASK (VALUE)RHASH_AR_TABLE_BOUND_MASK
#defineRHASH_AR_TABLE_BOUND_SHIFT RHASH_AR_TABLE_BOUND_SHIFT
#ifUSE_TRANSIENT_HEAP
#defineRHASH_TRANSIENT_P(hash) FL_TEST_RAW((hash), RHASH_TRANSIENT_FLAG)
#defineRHASH_SET_TRANSIENT_FLAG(h) FL_SET_RAW(h, RHASH_TRANSIENT_FLAG)
#defineRHASH_UNSET_TRANSIENT_FLAG(h) FL_UNSET_RAW(h, RHASH_TRANSIENT_FLAG)
#else
#defineRHASH_TRANSIENT_P(hash) 0
#defineRHASH_SET_TRANSIENT_FLAG(h) ((void)0)
#defineRHASH_UNSET_TRANSIENT_FLAG(h) ((void)0)
#endif
#ifSIZEOF_VALUE / RHASH_AR_TABLE_MAX_SIZE==2
typedefuint16_tar_hint_t;
#elifSIZEOF_VALUE / RHASH_AR_TABLE_MAX_SIZE==1
typedefunsigned charar_hint_t;
#else
#error unsupported
#endif
structRHash {
structRBasicbasic;
union {
st_table*st;
structar_table_struct*ar; /* possibly 0 */
} as;
constVALUEifnone;
union {
ar_hint_tary[RHASH_AR_TABLE_MAX_SIZE];
VALUEword;
} ar_hint;
};
#ifdefRHASH_IFNONE
# undef RHASH_IFNONE
# undef RHASH_SIZE
# defineRHASH_IFNONE(h) (RHASH(h)->ifnone)
# defineRHASH_SIZE(h) (RHASH_AR_TABLE_P(h) ? RHASH_AR_TABLE_SIZE_RAW(h) : RHASH_ST_SIZE(h))
#endif/* ifdef RHASH_IFNONE */
structRMoved {
VALUEflags;
VALUEdestination;
VALUEnext;
};
/* missing/setproctitle.c */
#ifndefHAVE_SETPROCTITLE
externvoidruby_init_setproctitle(intargc, char*argv[]);
#endif
#defineRSTRUCT_EMBED_LEN_MAX RSTRUCT_EMBED_LEN_MAX
#defineRSTRUCT_EMBED_LEN_MASK RSTRUCT_EMBED_LEN_MASK
#defineRSTRUCT_EMBED_LEN_SHIFT RSTRUCT_EMBED_LEN_SHIFT
enum {
RSTRUCT_EMBED_LEN_MAX=RVALUE_EMBED_LEN_MAX,
RSTRUCT_EMBED_LEN_MASK= (RUBY_FL_USER2|RUBY_FL_USER1),
RSTRUCT_EMBED_LEN_SHIFT= (RUBY_FL_USHIFT+1),
RSTRUCT_TRANSIENT_FLAG=FL_USER3,
RSTRUCT_ENUM_END
};
#ifUSE_TRANSIENT_HEAP
#defineRSTRUCT_TRANSIENT_P(st) FL_TEST_RAW((obj), RSTRUCT_TRANSIENT_FLAG)
#defineRSTRUCT_TRANSIENT_SET(st) FL_SET_RAW((st), RSTRUCT_TRANSIENT_FLAG)
#defineRSTRUCT_TRANSIENT_UNSET(st) FL_UNSET_RAW((st), RSTRUCT_TRANSIENT_FLAG)
#else
#defineRSTRUCT_TRANSIENT_P(st) 0
#defineRSTRUCT_TRANSIENT_SET(st) ((void)0)
#defineRSTRUCT_TRANSIENT_UNSET(st) ((void)0)
#endif
structRStruct {
structRBasicbasic;
union {
struct {
longlen;
constVALUE*ptr;
} heap;
constVALUEary[RSTRUCT_EMBED_LEN_MAX];
} as;
};
#undef RSTRUCT_LEN
#undef RSTRUCT_PTR
#undef RSTRUCT_SET
#undef RSTRUCT_GET
#defineRSTRUCT_EMBED_LEN(st) \
(long)((RBASIC(st)->flags >> RSTRUCT_EMBED_LEN_SHIFT) & \
(RSTRUCT_EMBED_LEN_MASK >> RSTRUCT_EMBED_LEN_SHIFT))
#defineRSTRUCT_LEN(st) rb_struct_len(st)
#defineRSTRUCT_LENINT(st) rb_long2int(RSTRUCT_LEN(st))
#defineRSTRUCT_CONST_PTR(st) rb_struct_const_ptr(st)
#defineRSTRUCT_PTR(st) ((VALUE *)RSTRUCT_CONST_PTR(RB_OBJ_WB_UNPROTECT_FOR(STRUCT, st)))
#defineRSTRUCT_SET(st, idx, v) RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[idx], (v))
#defineRSTRUCT_GET(st, idx) (RSTRUCT_CONST_PTR(st)[idx])
#defineRSTRUCT(obj) (R_CAST(RStruct)(obj))
staticinlinelong
rb_struct_len(VALUEst)
{
return (RBASIC(st)->flags&RSTRUCT_EMBED_LEN_MASK) ?
RSTRUCT_EMBED_LEN(st) : RSTRUCT(st)->as.heap.len;
}
staticinlineconstVALUE*
rb_struct_const_ptr(VALUEst)
{
returnFIX_CONST_VALUE_PTR((RBASIC(st)->flags&RSTRUCT_EMBED_LEN_MASK) ?
RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr);
}
staticinlineconstVALUE*
rb_struct_const_heap_ptr(VALUEst)
{
/* TODO: check embed on debug mode */
returnRSTRUCT(st)->as.heap.ptr;
}
/* class.c */
structrb_deprecated_classext_struct {
charconflict[sizeof(VALUE) *3];
};
structrb_subclass_entry;
typedefstructrb_subclass_entryrb_subclass_entry_t;
structrb_subclass_entry {
VALUEklass;
rb_subclass_entry_t*next;