-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.c
4398 lines (3819 loc) · 119 KB
/
gc.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
/**********************************************************************
gc.c -
$Author$
created at: Tue Oct 5 09:44:46 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#define rb_data_object_alloc rb_data_object_alloc
#define rb_data_typed_object_alloc rb_data_typed_object_alloc
#include "ruby/internal/config.h"
#ifdef _WIN32
# include "ruby/ruby.h"
#endif
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
# include "wasm/setjmp.h"
# include "wasm/machine.h"
#else
# include <setjmp.h>
#endif
#include <stdarg.h>
#include <stdio.h>
/* MALLOC_HEADERS_BEGIN */
#ifndef HAVE_MALLOC_USABLE_SIZE
# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
/* Alternative malloc header is included in ruby/missing.h */
# elif defined(HAVE_MALLOC_H)
# include <malloc.h>
# elif defined(HAVE_MALLOC_NP_H)
# include <malloc_np.h>
# elif defined(HAVE_MALLOC_MALLOC_H)
# include <malloc/malloc.h>
# endif
# ifdef _WIN32
# define HAVE_MALLOC_USABLE_SIZE
# define malloc_usable_size(a) _msize(a)
# elif defined HAVE_MALLOC_SIZE
# define HAVE_MALLOC_USABLE_SIZE
# define malloc_usable_size(a) malloc_size(a)
# endif
#else
# include <malloc.h>
#endif
/* MALLOC_HEADERS_END */
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#if defined _WIN32 || defined __CYGWIN__
# include <windows.h>
#elif defined(HAVE_POSIX_MEMALIGN)
#elif defined(HAVE_MEMALIGN)
# include <malloc.h>
#endif
#include <sys/types.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#undef LIST_HEAD /* ccan/list conflicts with BSD-origin sys/queue.h. */
#include "constant.h"
#include "darray.h"
#include "debug_counter.h"
#include "eval_intern.h"
#include "gc/gc.h"
#include "id_table.h"
#include "internal.h"
#include "internal/class.h"
#include "internal/compile.h"
#include "internal/complex.h"
#include "internal/cont.h"
#include "internal/error.h"
#include "internal/eval.h"
#include "internal/gc.h"
#include "internal/hash.h"
#include "internal/imemo.h"
#include "internal/io.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/proc.h"
#include "internal/rational.h"
#include "internal/sanitizers.h"
#include "internal/struct.h"
#include "internal/symbol.h"
#include "internal/thread.h"
#include "internal/variable.h"
#include "internal/warnings.h"
#include "rjit.h"
#include "probes.h"
#include "regint.h"
#include "ruby/debug.h"
#include "ruby/io.h"
#include "ruby/re.h"
#include "ruby/st.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#include "ruby/vm.h"
#include "ruby_assert.h"
#include "ruby_atomic.h"
#include "symbol.h"
#include "vm_core.h"
#include "vm_sync.h"
#include "vm_callinfo.h"
#include "ractor_core.h"
#include "yjit.h"
#include "builtin.h"
#include "shape.h"
unsigned int
rb_gc_vm_lock(void)
{
unsigned int lev;
RB_VM_LOCK_ENTER_LEV(&lev);
return lev;
}
void
rb_gc_vm_unlock(unsigned int lev)
{
RB_VM_LOCK_LEAVE_LEV(&lev);
}
unsigned int
rb_gc_cr_lock(void)
{
unsigned int lev;
RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
return lev;
}
void
rb_gc_cr_unlock(unsigned int lev)
{
RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
}
unsigned int
rb_gc_vm_lock_no_barrier(void)
{
unsigned int lev = 0;
RB_VM_LOCK_ENTER_LEV_NB(&lev);
return lev;
}
void
rb_gc_vm_unlock_no_barrier(unsigned int lev)
{
RB_VM_LOCK_LEAVE_LEV(&lev);
}
void
rb_gc_vm_barrier(void)
{
rb_vm_barrier();
}
void
rb_gc_event_hook(VALUE obj, rb_event_flag_t event)
{
if (LIKELY(!(ruby_vm_event_flags & event))) return;
rb_execution_context_t *ec = GET_EC();
if (!ec->cfp) return;
EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
}
void *
rb_gc_get_objspace(void)
{
return GET_VM()->gc.objspace;
}
void
rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data)
{
rb_ractor_t *r = NULL;
ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
func(r->newobj_cache, data);
}
}
void
rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data)
{
volatile struct {
VALUE errinfo;
VALUE final;
rb_control_frame_t *cfp;
VALUE *sp;
long finished;
} saved;
rb_execution_context_t * volatile ec = GET_EC();
#define RESTORE_FINALIZER() (\
ec->cfp = saved.cfp, \
ec->cfp->sp = saved.sp, \
ec->errinfo = saved.errinfo)
saved.errinfo = ec->errinfo;
saved.cfp = ec->cfp;
saved.sp = ec->cfp->sp;
saved.finished = 0;
saved.final = Qundef;
EC_PUSH_TAG(ec);
enum ruby_tag_type state = EC_EXEC_TAG();
if (state != TAG_NONE) {
++saved.finished; /* skip failed finalizer */
VALUE failed_final = saved.final;
saved.final = Qundef;
if (!UNDEF_P(failed_final) && !NIL_P(ruby_verbose)) {
rb_warn("Exception in finalizer %+"PRIsVALUE, failed_final);
rb_ec_error_print(ec, ec->errinfo);
}
}
for (long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
saved.final = callback(i, data);
rb_check_funcall(saved.final, idCall, 1, &objid);
}
EC_POP_TAG();
#undef RESTORE_FINALIZER
}
void
rb_gc_set_pending_interrupt(void)
{
rb_execution_context_t *ec = GET_EC();
ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
}
void
rb_gc_unset_pending_interrupt(void)
{
rb_execution_context_t *ec = GET_EC();
ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
}
bool
rb_gc_multi_ractor_p(void)
{
return rb_multi_ractor_p();
}
bool rb_obj_is_main_ractor(VALUE gv);
bool
rb_gc_shutdown_call_finalizer_p(VALUE obj)
{
switch (BUILTIN_TYPE(obj)) {
case T_DATA:
if (!ruby_free_at_exit_p() && (!DATA_PTR(obj) || !RDATA(obj)->dfree)) return false;
if (rb_obj_is_thread(obj)) return false;
if (rb_obj_is_mutex(obj)) return false;
if (rb_obj_is_fiber(obj)) return false;
if (rb_obj_is_main_ractor(obj)) return false;
return true;
case T_FILE:
return true;
case T_SYMBOL:
if (RSYMBOL(obj)->fstr &&
(BUILTIN_TYPE(RSYMBOL(obj)->fstr) == T_NONE ||
BUILTIN_TYPE(RSYMBOL(obj)->fstr) == T_ZOMBIE)) {
RSYMBOL(obj)->fstr = 0;
}
return true;
case T_NONE:
return false;
default:
return ruby_free_at_exit_p();
}
}
uint32_t
rb_gc_get_shape(VALUE obj)
{
return (uint32_t)rb_shape_get_shape_id(obj);
}
void
rb_gc_set_shape(VALUE obj, uint32_t shape_id)
{
rb_shape_set_shape_id(obj, (uint32_t)shape_id);
}
uint32_t
rb_gc_rebuild_shape(VALUE obj, size_t size_pool_id)
{
rb_shape_t *orig_shape = rb_shape_get_shape(obj);
if (rb_shape_obj_too_complex(obj)) return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(size_pool_id + FIRST_T_OBJECT_SHAPE_ID));
rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
if (!new_shape) return 0;
return (uint32_t)rb_shape_id(new_shape);
}
void rb_vm_update_references(void *ptr);
#define rb_setjmp(env) RUBY_SETJMP(env)
#define rb_jmp_buf rb_jmpbuf_t
#undef rb_data_object_wrap
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif
#define unless_objspace(objspace) \
void *objspace; \
rb_vm_t *unless_objspace_vm = GET_VM(); \
if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
else /* return; or objspace will be warned uninitialized */
#define RMOVED(obj) ((struct RMoved *)(obj))
#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
*(_type *)&(_thing) = (_type)rb_gc_impl_location(_objspace, (VALUE)_thing); \
} \
} while (0)
#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
#if RUBY_MARK_FREE_DEBUG
int ruby_gc_debug_indent = 0;
#endif
#ifndef RGENGC_CHECK_MODE
# define RGENGC_CHECK_MODE 0
#endif
#ifndef RGENGC_OBJ_INFO
# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
#endif
#ifndef CALC_EXACT_MALLOC_SIZE
# define CALC_EXACT_MALLOC_SIZE 0
#endif
VALUE rb_mGC;
static size_t malloc_offset = 0;
#if defined(HAVE_MALLOC_USABLE_SIZE)
static size_t
gc_compute_malloc_offset(void)
{
// Different allocators use different metadata storage strategies which result in different
// ideal sizes.
// For instance malloc(64) will waste 8B with glibc, but waste 0B with jemalloc.
// But malloc(56) will waste 0B with glibc, but waste 8B with jemalloc.
// So we try allocating 64, 56 and 48 bytes and select the first offset that doesn't
// waste memory.
// This was tested on Linux with glibc 2.35 and jemalloc 5, and for both it result in
// no wasted memory.
size_t offset = 0;
for (offset = 0; offset <= 16; offset += 8) {
size_t allocated = (64 - offset);
void *test_ptr = malloc(allocated);
size_t wasted = malloc_usable_size(test_ptr) - allocated;
free(test_ptr);
if (wasted == 0) {
return offset;
}
}
return 0;
}
#else
static size_t
gc_compute_malloc_offset(void)
{
// If we don't have malloc_usable_size, we use powers of 2.
return 0;
}
#endif
size_t
rb_malloc_grow_capa(size_t current, size_t type_size)
{
size_t current_capacity = current;
if (current_capacity < 4) {
current_capacity = 4;
}
current_capacity *= type_size;
// We double the current capacity.
size_t new_capacity = (current_capacity * 2);
// And round up to the next power of 2 if it's not already one.
if (rb_popcount64(new_capacity) != 1) {
new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
}
new_capacity -= malloc_offset;
new_capacity /= type_size;
if (current > new_capacity) {
rb_bug("rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
}
RUBY_ASSERT(new_capacity > current);
return new_capacity;
}
static inline struct rbimpl_size_mul_overflow_tag
size_add_overflow(size_t x, size_t y)
{
size_t z;
bool p;
#if 0
#elif defined(ckd_add)
p = ckd_add(&z, x, y);
#elif __has_builtin(__builtin_add_overflow)
p = __builtin_add_overflow(x, y, &z);
#elif defined(DSIZE_T)
RB_GNUC_EXTENSION DSIZE_T dx = x;
RB_GNUC_EXTENSION DSIZE_T dy = y;
RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
p = dz > SIZE_MAX;
z = (size_t)dz;
#else
z = x + y;
p = z < y;
#endif
return (struct rbimpl_size_mul_overflow_tag) { p, z, };
}
static inline struct rbimpl_size_mul_overflow_tag
size_mul_add_overflow(size_t x, size_t y, size_t z) /* x * y + z */
{
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
}
static inline struct rbimpl_size_mul_overflow_tag
size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
{
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
}
PRINTF_ARGS(NORETURN(static void gc_raise(VALUE, const char*, ...)), 2, 3);
static inline size_t
size_mul_or_raise(size_t x, size_t y, VALUE exc)
{
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
if (LIKELY(!t.left)) {
return t.right;
}
else if (rb_during_gc()) {
rb_memerror(); /* or...? */
}
else {
gc_raise(
exc,
"integer overflow: %"PRIuSIZE
" * %"PRIuSIZE
" > %"PRIuSIZE,
x, y, (size_t)SIZE_MAX);
}
}
size_t
rb_size_mul_or_raise(size_t x, size_t y, VALUE exc)
{
return size_mul_or_raise(x, y, exc);
}
static inline size_t
size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
{
struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
if (LIKELY(!t.left)) {
return t.right;
}
else if (rb_during_gc()) {
rb_memerror(); /* or...? */
}
else {
gc_raise(
exc,
"integer overflow: %"PRIuSIZE
" * %"PRIuSIZE
" + %"PRIuSIZE
" > %"PRIuSIZE,
x, y, z, (size_t)SIZE_MAX);
}
}
size_t
rb_size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
{
return size_mul_add_or_raise(x, y, z, exc);
}
static inline size_t
size_mul_add_mul_or_raise(size_t x, size_t y, size_t z, size_t w, VALUE exc)
{
struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
if (LIKELY(!t.left)) {
return t.right;
}
else if (rb_during_gc()) {
rb_memerror(); /* or...? */
}
else {
gc_raise(
exc,
"integer overflow: %"PRIdSIZE
" * %"PRIdSIZE
" + %"PRIdSIZE
" * %"PRIdSIZE
" > %"PRIdSIZE,
x, y, z, w, (size_t)SIZE_MAX);
}
}
#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
/* trick the compiler into thinking a external signal handler uses this */
volatile VALUE rb_gc_guarded_val;
volatile VALUE *
rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val)
{
rb_gc_guarded_val = val;
return ptr;
}
#endif
static const char *obj_type_name(VALUE obj);
#define RB_AMALGAMATED_DEFAULT_GC
#include "gc/default.c"
#if USE_SHARED_GC && !defined(HAVE_DLOPEN)
# error "Shared GC requires dlopen"
#elif USE_SHARED_GC
#include <dlfcn.h>
typedef struct gc_function_map {
// Bootup
void *(*objspace_alloc)(void);
void (*objspace_init)(void *objspace_ptr);
void (*objspace_free)(void *objspace_ptr);
void *(*ractor_cache_alloc)(void *objspace_ptr);
void (*ractor_cache_free)(void *objspace_ptr, void *cache);
void (*set_params)(void *objspace_ptr);
void (*init)(void);
size_t *(*size_pool_sizes)(void *objspace_ptr);
// Shutdown
void (*shutdown_free_objects)(void *objspace_ptr);
// GC
void (*start)(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
bool (*during_gc_p)(void *objspace_ptr);
void (*prepare_heap)(void *objspace_ptr);
void (*gc_enable)(void *objspace_ptr);
void (*gc_disable)(void *objspace_ptr, bool finish_current_gc);
bool (*gc_enabled_p)(void *objspace_ptr);
VALUE (*config_get)(void *objpace_ptr);
VALUE (*config_set)(void *objspace_ptr, VALUE hash);
void (*stress_set)(void *objspace_ptr, VALUE flag);
VALUE (*stress_get)(void *objspace_ptr);
// Object allocation
VALUE (*new_obj)(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, bool wb_protected, size_t alloc_size);
size_t (*obj_slot_size)(VALUE obj);
size_t (*size_pool_id_for_size)(void *objspace_ptr, size_t size);
bool (*size_allocatable_p)(size_t size);
// Malloc
void *(*malloc)(void *objspace_ptr, size_t size);
void *(*calloc)(void *objspace_ptr, size_t size);
void *(*realloc)(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size);
void (*free)(void *objspace_ptr, void *ptr, size_t old_size);
void (*adjust_memory_usage)(void *objspace_ptr, ssize_t diff);
// Marking
void (*mark)(void *objspace_ptr, VALUE obj);
void (*mark_and_move)(void *objspace_ptr, VALUE *ptr);
void (*mark_and_pin)(void *objspace_ptr, VALUE obj);
void (*mark_maybe)(void *objspace_ptr, VALUE obj);
void (*mark_weak)(void *objspace_ptr, VALUE *ptr);
void (*remove_weak)(void *objspace_ptr, VALUE parent_obj, VALUE *ptr);
// Compaction
bool (*object_moved_p)(void *objspace_ptr, VALUE obj);
VALUE (*location)(void *objspace_ptr, VALUE value);
// Write barriers
void (*writebarrier)(void *objspace_ptr, VALUE a, VALUE b);
void (*writebarrier_unprotect)(void *objspace_ptr, VALUE obj);
void (*writebarrier_remember)(void *objspace_ptr, VALUE obj);
// Heap walking
void (*each_objects)(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data);
void (*each_object)(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data);
// Finalizers
void (*make_zombie)(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data);
VALUE (*define_finalizer)(void *objspace_ptr, VALUE obj, VALUE block);
void (*undefine_finalizer)(void *objspace_ptr, VALUE obj);
void (*copy_finalizer)(void *objspace_ptr, VALUE dest, VALUE obj);
void (*shutdown_call_finalizer)(void *objspace_ptr);
// Object ID
VALUE (*object_id)(void *objspace_ptr, VALUE obj);
VALUE (*object_id_to_ref)(void *objspace_ptr, VALUE object_id);
// Statistics
void (*set_measure_total_time)(void *objspace_ptr, VALUE flag);
bool (*get_measure_total_time)(void *objspace_ptr);
unsigned long long (*get_total_time)(void *objspace_ptr);
size_t (*gc_count)(void *objspace_ptr);
VALUE (*latest_gc_info)(void *objspace_ptr, VALUE key);
size_t (*stat)(void *objspace_ptr, VALUE hash_or_sym);
size_t (*stat_heap)(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym);
// Miscellaneous
size_t (*obj_flags)(void *objspace_ptr, VALUE obj, ID* flags, size_t max);
bool (*pointer_to_heap_p)(void *objspace_ptr, const void *ptr);
bool (*garbage_object_p)(void *objspace_ptr, VALUE obj);
void (*set_event_hook)(void *objspace_ptr, const rb_event_flag_t event);
void (*copy_attributes)(void *objspace_ptr, VALUE dest, VALUE obj);
} rb_gc_function_map_t;
static rb_gc_function_map_t rb_gc_functions;
# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
static void
ruby_external_gc_init(void)
{
// Assert that the directory path ends with a /
GC_ASSERT(SHARED_GC_DIR[strlen(SHARED_GC_DIR) - 2] == '/');
char *gc_so_file = getenv(RUBY_GC_LIBRARY);
char *gc_so_path = NULL;
void *handle = NULL;
if (gc_so_file) {
/* Check to make sure that gc_so_file matches /[\w-_.]+/ so that it does
* not load a shared object outside of the directory. */
for (size_t i = 0; i < strlen(gc_so_file); i++) {
char c = gc_so_file[i];
if (isalnum(c)) continue;
switch (c) {
case '-':
case '_':
case '.':
break;
default:
rb_bug("Only alphanumeric, dash, underscore, and period is allowed in "RUBY_GC_LIBRARY"");
}
}
gc_so_path = alloca(strlen(SHARED_GC_DIR) + strlen(gc_so_file) + 1);
strcpy(gc_so_path, SHARED_GC_DIR);
strcpy(gc_so_path + strlen(SHARED_GC_DIR), gc_so_file);
gc_so_path[strlen(SHARED_GC_DIR) + strlen(gc_so_file)] = '\0';
handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
rb_bug("ruby_external_gc_init: Shared library %s cannot be opened", gc_so_path);
}
}
# define load_external_gc_func(name) do { \
if (handle) { \
rb_gc_functions.name = dlsym(handle, "rb_gc_impl_" #name); \
if (!rb_gc_functions.name) { \
rb_bug("ruby_external_gc_init: " #name " func not exported by library %s", gc_so_path); \
} \
} \
else { \
rb_gc_functions.name = rb_gc_impl_##name; \
} \
} while (0)
// Bootup
load_external_gc_func(objspace_alloc);
load_external_gc_func(objspace_init);
load_external_gc_func(objspace_free);
load_external_gc_func(ractor_cache_alloc);
load_external_gc_func(ractor_cache_free);
load_external_gc_func(set_params);
load_external_gc_func(init);
load_external_gc_func(size_pool_sizes);
// Shutdown
load_external_gc_func(shutdown_free_objects);
// GC
load_external_gc_func(start);
load_external_gc_func(during_gc_p);
load_external_gc_func(prepare_heap);
load_external_gc_func(gc_enable);
load_external_gc_func(gc_disable);
load_external_gc_func(gc_enabled_p);
load_external_gc_func(config_set);
load_external_gc_func(config_get);
load_external_gc_func(stress_set);
load_external_gc_func(stress_get);
// Object allocation
load_external_gc_func(new_obj);
load_external_gc_func(obj_slot_size);
load_external_gc_func(size_pool_id_for_size);
load_external_gc_func(size_allocatable_p);
// Malloc
load_external_gc_func(malloc);
load_external_gc_func(calloc);
load_external_gc_func(realloc);
load_external_gc_func(free);
load_external_gc_func(adjust_memory_usage);
// Marking
load_external_gc_func(mark);
load_external_gc_func(mark_and_move);
load_external_gc_func(mark_and_pin);
load_external_gc_func(mark_maybe);
load_external_gc_func(mark_weak);
load_external_gc_func(remove_weak);
// Compaction
load_external_gc_func(object_moved_p);
load_external_gc_func(location);
// Write barriers
load_external_gc_func(writebarrier);
load_external_gc_func(writebarrier_unprotect);
load_external_gc_func(writebarrier_remember);
// Heap walking
load_external_gc_func(each_objects);
load_external_gc_func(each_object);
// Finalizers
load_external_gc_func(make_zombie);
load_external_gc_func(define_finalizer);
load_external_gc_func(undefine_finalizer);
load_external_gc_func(copy_finalizer);
load_external_gc_func(shutdown_call_finalizer);
// Object ID
load_external_gc_func(object_id);
load_external_gc_func(object_id_to_ref);
// Statistics
load_external_gc_func(set_measure_total_time);
load_external_gc_func(get_measure_total_time);
load_external_gc_func(get_total_time);
load_external_gc_func(gc_count);
load_external_gc_func(latest_gc_info);
load_external_gc_func(stat);
load_external_gc_func(stat_heap);
// Miscellaneous
load_external_gc_func(obj_flags);
load_external_gc_func(pointer_to_heap_p);
load_external_gc_func(garbage_object_p);
load_external_gc_func(set_event_hook);
load_external_gc_func(copy_attributes);
# undef load_external_gc_func
}
// Bootup
# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
# define rb_gc_impl_set_params rb_gc_functions.set_params
# define rb_gc_impl_init rb_gc_functions.init
# define rb_gc_impl_size_pool_sizes rb_gc_functions.size_pool_sizes
// Shutdown
# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
// GC
# define rb_gc_impl_start rb_gc_functions.start
# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
# define rb_gc_impl_config_get rb_gc_functions.config_get
# define rb_gc_impl_config_set rb_gc_functions.config_set
# define rb_gc_impl_stress_set rb_gc_functions.stress_set
# define rb_gc_impl_stress_get rb_gc_functions.stress_get
// Object allocation
# define rb_gc_impl_new_obj rb_gc_functions.new_obj
# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
# define rb_gc_impl_size_pool_id_for_size rb_gc_functions.size_pool_id_for_size
# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
// Malloc
# define rb_gc_impl_malloc rb_gc_functions.malloc
# define rb_gc_impl_calloc rb_gc_functions.calloc
# define rb_gc_impl_realloc rb_gc_functions.realloc
# define rb_gc_impl_free rb_gc_functions.free
# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
// Marking
# define rb_gc_impl_mark rb_gc_functions.mark
# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
// Compaction
# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
# define rb_gc_impl_location rb_gc_functions.location
// Write barriers
# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
// Heap walking
# define rb_gc_impl_each_objects rb_gc_functions.each_objects
# define rb_gc_impl_each_object rb_gc_functions.each_object
// Finalizers
# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
// Object ID
# define rb_gc_impl_object_id rb_gc_functions.object_id
# define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
// Statistics
# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
# define rb_gc_impl_gc_count rb_gc_functions.gc_count
# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
# define rb_gc_impl_stat rb_gc_functions.stat
# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
// Miscellaneous
# define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
#endif
static VALUE initial_stress = Qfalse;
void *
rb_objspace_alloc(void)
{
#if USE_SHARED_GC
ruby_external_gc_init();
#endif
void *objspace = rb_gc_impl_objspace_alloc();
ruby_current_vm_ptr->gc.objspace = objspace;
rb_gc_impl_objspace_init(objspace);
rb_gc_impl_stress_set(objspace, initial_stress);
return objspace;
}
void
rb_objspace_free(void *objspace)
{
rb_gc_impl_objspace_free(objspace);
}
size_t
rb_gc_obj_slot_size(VALUE obj)
{
return rb_gc_impl_obj_slot_size(obj);
}
static inline VALUE
newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, bool wb_protected, size_t size)
{
VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
if (UNLIKELY(ruby_vm_event_flags & RUBY_INTERNAL_EVENT_NEWOBJ)) {
unsigned int lev;
RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
{
memset((char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
}
RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
}
return obj;
}
VALUE
rb_wb_unprotected_newobj_of(VALUE klass, VALUE flags, size_t size)
{
GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
}
VALUE
rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags, size_t size)
{
GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
}
#define UNEXPECTED_NODE(func) \
rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
static inline void
rb_data_object_check(VALUE klass)
{
if (klass != rb_cObject && (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
rb_undef_alloc_func(klass);
rb_warn("undefining the allocator of T_DATA class %"PRIsVALUE, klass);
}
}
VALUE
rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
{
RUBY_ASSERT_ALWAYS(dfree != (RUBY_DATA_FUNC)1);
if (klass) rb_data_object_check(klass);
return newobj_of(GET_RACTOR(), klass, T_DATA, (VALUE)dmark, (VALUE)dfree, (VALUE)datap, !dmark, sizeof(struct RTypedData));
}
VALUE
rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
{
VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
DATA_PTR(obj) = xcalloc(1, size);
return obj;
}
static VALUE
typed_data_alloc(VALUE klass, VALUE typed_flag, void *datap, const rb_data_type_t *type, size_t size)
{
RBIMPL_NONNULL_ARG(type);
if (klass) rb_data_object_check(klass);
bool wb_protected = (type->flags & RUBY_FL_WB_PROTECTED) || !type->function.dmark;
return newobj_of(GET_RACTOR(), klass, T_DATA, (VALUE)type, 1 | typed_flag, (VALUE)datap, wb_protected, size);
}
VALUE
rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
{
if (UNLIKELY(type->flags & RUBY_TYPED_EMBEDDABLE)) {
rb_raise(rb_eTypeError, "Cannot wrap an embeddable TypedData");
}
return typed_data_alloc(klass, 0, datap, type, sizeof(struct RTypedData));
}
VALUE
rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
{
if (type->flags & RUBY_TYPED_EMBEDDABLE) {
if (!(type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
rb_raise(rb_eTypeError, "Embeddable TypedData must be freed immediately");
}
size_t embed_size = offsetof(struct RTypedData, data) + size;
if (rb_gc_size_allocatable_p(embed_size)) {
VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0, type, embed_size);
memset((char *)obj + offsetof(struct RTypedData, data), 0, size);
return obj;
}
}
VALUE obj = typed_data_alloc(klass, 0, NULL, type, sizeof(struct RTypedData));
DATA_PTR(obj) = xcalloc(1, size);
return obj;
}
static size_t
rb_objspace_data_type_memsize(VALUE obj)
{
size_t size = 0;
if (RTYPEDDATA_P(obj)) {
const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
const void *ptr = RTYPEDDATA_GET_DATA(obj);
if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
#ifdef HAVE_MALLOC_USABLE_SIZE
size += malloc_usable_size((void *)ptr);
#endif