Ruby  2.4.2p198(2017-09-14revision59899)
gc.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  gc.c -
4 
5  $Author: nagachika $
6  created at: Tue Oct 5 09:44:46 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #define rb_data_object_alloc rb_data_object_alloc
15 #define rb_data_typed_object_alloc rb_data_typed_object_alloc
16 
17 #include "internal.h"
18 #include "ruby/st.h"
19 #include "ruby/re.h"
20 #include "ruby/io.h"
21 #include "ruby/thread.h"
22 #include "ruby/util.h"
23 #include "ruby/debug.h"
24 #include "eval_intern.h"
25 #include "vm_core.h"
26 #include "gc.h"
27 #include "constant.h"
28 #include "ruby_atomic.h"
29 #include "probes.h"
30 #include "id_table.h"
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <sys/types.h>
35 #include "ruby_assert.h"
36 
37 #undef rb_data_object_wrap
38 
39 #ifndef HAVE_MALLOC_USABLE_SIZE
40 # ifdef _WIN32
41 # define HAVE_MALLOC_USABLE_SIZE
42 # define malloc_usable_size(a) _msize(a)
43 # elif defined HAVE_MALLOC_SIZE
44 # define HAVE_MALLOC_USABLE_SIZE
45 # define malloc_usable_size(a) malloc_size(a)
46 # endif
47 #endif
48 #ifdef HAVE_MALLOC_USABLE_SIZE
49 # ifdef HAVE_MALLOC_H
50 # include <malloc.h>
51 # elif defined(HAVE_MALLOC_NP_H)
52 # include <malloc_np.h>
53 # elif defined(HAVE_MALLOC_MALLOC_H)
54 # include <malloc/malloc.h>
55 # endif
56 #endif
57 
58 #if /* is ASAN enabled? */ \
59  __has_feature(address_sanitizer) /* Clang */ || \
60  defined(__SANITIZE_ADDRESS__) /* GCC 4.8.x */
61  #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
62  __attribute__((no_address_safety_analysis)) \
63  __attribute__((noinline))
64 #else
65  #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
66 #endif
67 
68 #ifdef HAVE_SYS_TIME_H
69 #include <sys/time.h>
70 #endif
71 
72 #ifdef HAVE_SYS_RESOURCE_H
73 #include <sys/resource.h>
74 #endif
75 #if defined(__native_client__) && defined(NACL_NEWLIB)
76 # include "nacl/resource.h"
77 # undef HAVE_POSIX_MEMALIGN
78 # undef HAVE_MEMALIGN
79 
80 #endif
81 
82 #if defined _WIN32 || defined __CYGWIN__
83 #include <windows.h>
84 #elif defined(HAVE_POSIX_MEMALIGN)
85 #elif defined(HAVE_MEMALIGN)
86 #include <malloc.h>
87 #endif
88 
89 #define rb_setjmp(env) RUBY_SETJMP(env)
90 #define rb_jmp_buf rb_jmpbuf_t
91 
92 #if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
93 /* trick the compiler into thinking a external signal handler uses this */
95 volatile VALUE *
97 {
99 
100  return ptr;
101 }
102 #endif
103 
104 #ifndef GC_HEAP_INIT_SLOTS
105 #define GC_HEAP_INIT_SLOTS 10000
106 #endif
107 #ifndef GC_HEAP_FREE_SLOTS
108 #define GC_HEAP_FREE_SLOTS 4096
109 #endif
110 #ifndef GC_HEAP_GROWTH_FACTOR
111 #define GC_HEAP_GROWTH_FACTOR 1.8
112 #endif
113 #ifndef GC_HEAP_GROWTH_MAX_SLOTS
114 #define GC_HEAP_GROWTH_MAX_SLOTS 0 /* 0 is disable */
115 #endif
116 #ifndef GC_HEAP_OLDOBJECT_LIMIT_FACTOR
117 #define GC_HEAP_OLDOBJECT_LIMIT_FACTOR 2.0
118 #endif
119 
120 #ifndef GC_HEAP_FREE_SLOTS_MIN_RATIO
121 #define GC_HEAP_FREE_SLOTS_MIN_RATIO 0.20
122 #endif
123 #ifndef GC_HEAP_FREE_SLOTS_GOAL_RATIO
124 #define GC_HEAP_FREE_SLOTS_GOAL_RATIO 0.40
125 #endif
126 #ifndef GC_HEAP_FREE_SLOTS_MAX_RATIO
127 #define GC_HEAP_FREE_SLOTS_MAX_RATIO 0.65
128 #endif
129 
130 #ifndef GC_MALLOC_LIMIT_MIN
131 #define GC_MALLOC_LIMIT_MIN (16 * 1024 * 1024 /* 16MB */)
132 #endif
133 #ifndef GC_MALLOC_LIMIT_MAX
134 #define GC_MALLOC_LIMIT_MAX (32 * 1024 * 1024 /* 32MB */)
135 #endif
136 #ifndef GC_MALLOC_LIMIT_GROWTH_FACTOR
137 #define GC_MALLOC_LIMIT_GROWTH_FACTOR 1.4
138 #endif
139 
140 #ifndef GC_OLDMALLOC_LIMIT_MIN
141 #define GC_OLDMALLOC_LIMIT_MIN (16 * 1024 * 1024 /* 16MB */)
142 #endif
143 #ifndef GC_OLDMALLOC_LIMIT_GROWTH_FACTOR
144 #define GC_OLDMALLOC_LIMIT_GROWTH_FACTOR 1.2
145 #endif
146 #ifndef GC_OLDMALLOC_LIMIT_MAX
147 #define GC_OLDMALLOC_LIMIT_MAX (128 * 1024 * 1024 /* 128MB */)
148 #endif
149 
150 #ifndef PRINT_MEASURE_LINE
151 #define PRINT_MEASURE_LINE 0
152 #endif
153 #ifndef PRINT_ENTER_EXIT_TICK
154 #define PRINT_ENTER_EXIT_TICK 0
155 #endif
156 #ifndef PRINT_ROOT_TICKS
157 #define PRINT_ROOT_TICKS 0
158 #endif
159 
160 #define USE_TICK_T (PRINT_ENTER_EXIT_TICK || PRINT_MEASURE_LINE || PRINT_ROOT_TICKS)
161 #define TICK_TYPE 1
162 
163 typedef struct {
168 
173 
177 
181 
184 
190 
195 
199 
203 
204  FALSE,
205 };
206 
207 /* GC_DEBUG:
208  * enable to embed GC debugging information.
209  */
210 #ifndef GC_DEBUG
211 #define GC_DEBUG 0
212 #endif
213 
214 #if USE_RGENGC
215 /* RGENGC_DEBUG:
216  * 1: basic information
217  * 2: remember set operation
218  * 3: mark
219  * 4:
220  * 5: sweep
221  */
222 #ifndef RGENGC_DEBUG
223 #define RGENGC_DEBUG 0
224 #endif
225 
226 /* RGENGC_CHECK_MODE
227  * 0: disable all assertions
228  * 1: enable assertions (to debug RGenGC)
229  * 2: enable internal consistency check at each GC (for debugging)
230  * 3: enable internal consistency check at each GC steps (for debugging)
231  * 4: enable liveness check
232  * 5: show all references
233  */
234 #ifndef RGENGC_CHECK_MODE
235 #define RGENGC_CHECK_MODE 0
236 #endif
237 
238 /* RGENGC_OLD_NEWOBJ_CHECK
239  * 0: disable all assertions
240  * >0: make a OLD object when new object creation.
241  *
242  * Make one OLD object per RGENGC_OLD_NEWOBJ_CHECK WB protected objects creation.
243  */
244 #ifndef RGENGC_OLD_NEWOBJ_CHECK
245 #define RGENGC_OLD_NEWOBJ_CHECK 0
246 #endif
247 
248 /* RGENGC_PROFILE
249  * 0: disable RGenGC profiling
250  * 1: enable profiling for basic information
251  * 2: enable profiling for each types
252  */
253 #ifndef RGENGC_PROFILE
254 #define RGENGC_PROFILE 0
255 #endif
256 
257 /* RGENGC_ESTIMATE_OLDMALLOC
258  * Enable/disable to estimate increase size of malloc'ed size by old objects.
259  * If estimation exceeds threshold, then will invoke full GC.
260  * 0: disable estimation.
261  * 1: enable estimation.
262  */
263 #ifndef RGENGC_ESTIMATE_OLDMALLOC
264 #define RGENGC_ESTIMATE_OLDMALLOC 1
265 #endif
266 
267 /* RGENGC_FORCE_MAJOR_GC
268  * Force major/full GC if this macro is not 0.
269  */
270 #ifndef RGENGC_FORCE_MAJOR_GC
271 #define RGENGC_FORCE_MAJOR_GC 0
272 #endif
273 
274 #else /* USE_RGENGC */
275 
276 #ifdef RGENGC_DEBUG
277 #undef RGENGC_DEBUG
278 #endif
279 #define RGENGC_DEBUG 0
280 #ifdef RGENGC_CHECK_MODE
281 #undef RGENGC_CHECK_MODE
282 #endif
283 #define RGENGC_CHECK_MODE 0
284 #define RGENGC_PROFILE 0
285 #define RGENGC_ESTIMATE_OLDMALLOC 0
286 #define RGENGC_FORCE_MAJOR_GC 0
287 
288 #endif /* USE_RGENGC */
289 
290 #ifndef GC_PROFILE_MORE_DETAIL
291 #define GC_PROFILE_MORE_DETAIL 0
292 #endif
293 #ifndef GC_PROFILE_DETAIL_MEMORY
294 #define GC_PROFILE_DETAIL_MEMORY 0
295 #endif
296 #ifndef GC_ENABLE_INCREMENTAL_MARK
297 #define GC_ENABLE_INCREMENTAL_MARK USE_RINCGC
298 #endif
299 #ifndef GC_ENABLE_LAZY_SWEEP
300 #define GC_ENABLE_LAZY_SWEEP 1
301 #endif
302 #ifndef CALC_EXACT_MALLOC_SIZE
303 #define CALC_EXACT_MALLOC_SIZE 0
304 #endif
305 #if defined(HAVE_MALLOC_USABLE_SIZE) || CALC_EXACT_MALLOC_SIZE > 0
306 #ifndef MALLOC_ALLOCATED_SIZE
307 #define MALLOC_ALLOCATED_SIZE 0
308 #endif
309 #else
310 #define MALLOC_ALLOCATED_SIZE 0
311 #endif
312 #ifndef MALLOC_ALLOCATED_SIZE_CHECK
313 #define MALLOC_ALLOCATED_SIZE_CHECK 0
314 #endif
315 
316 #ifndef GC_DEBUG_STRESS_TO_CLASS
317 #define GC_DEBUG_STRESS_TO_CLASS 0
318 #endif
319 
320 #ifndef RGENGC_OBJ_INFO
321 #define RGENGC_OBJ_INFO (RGENGC_DEBUG | RGENGC_CHECK_MODE)
322 #endif
323 
324 typedef enum {
325  GPR_FLAG_NONE = 0x000,
326  /* major reason */
331 #if RGENGC_ESTIMATE_OLDMALLOC
333 #endif
335 
336  /* gc reason */
340  GPR_FLAG_CAPI = 0x800,
341  GPR_FLAG_STRESS = 0x1000,
342 
343  /* others */
347 
348 typedef struct gc_profile_record {
349  int flags;
350 
351  double gc_time;
353 
357 
358 #if GC_PROFILE_MORE_DETAIL
359  double gc_mark_time;
360  double gc_sweep_time;
361 
362  size_t heap_use_pages;
363  size_t heap_live_objects;
364  size_t heap_free_objects;
365 
366  size_t allocate_increase;
367  size_t allocate_limit;
368 
369  double prepare_time;
370  size_t removing_objects;
371  size_t empty_objects;
372 #if GC_PROFILE_DETAIL_MEMORY
373  long maxrss;
374  long minflt;
375  long majflt;
376 #endif
377 #endif
378 #if MALLOC_ALLOCATED_SIZE
379  size_t allocated_size;
380 #endif
381 
382 #if RGENGC_PROFILE > 0
383  size_t old_objects;
384  size_t remembered_normal_objects;
385  size_t remembered_shady_objects;
386 #endif
388 
389 #if defined(_MSC_VER) || defined(__CYGWIN__)
390 #pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
391 #endif
392 
393 typedef struct RVALUE {
394  union {
395  struct {
396  VALUE flags; /* always 0 for freed obj */
397  struct RVALUE *next;
398  } free;
399  struct RBasic basic;
400  struct RObject object;
401  struct RClass klass;
402  struct RFloat flonum;
403  struct RString string;
404  struct RArray array;
405  struct RRegexp regexp;
406  struct RHash hash;
407  struct RData data;
408  struct RTypedData typeddata;
409  struct RStruct rstruct;
410  struct RBignum bignum;
411  struct RFile file;
412  struct RNode node;
413  struct RMatch match;
414  struct RRational rational;
415  struct RComplex complex;
416  union {
418  struct vm_svar svar;
419  struct vm_throw_data throw_data;
420  struct vm_ifunc ifunc;
421  struct MEMO memo;
425  } imemo;
426  struct {
427  struct RBasic basic;
431  } values;
432  } as;
433 #if GC_DEBUG
434  const char *file;
435  int line;
436 #endif
437 } RVALUE;
438 
439 #if defined(_MSC_VER) || defined(__CYGWIN__)
440 #pragma pack(pop)
441 #endif
442 
444 enum {
445  BITS_SIZE = sizeof(bits_t),
447 };
448 
450  struct heap_page *page;
451 };
452 
454  struct heap_page_header header;
455  /* char gap[]; */
456  /* RVALUE values[]; */
457 };
458 
459 struct gc_list {
461  struct gc_list *next;
462 };
463 
464 #define STACK_CHUNK_SIZE 500
465 
466 typedef struct stack_chunk {
468  struct stack_chunk *next;
469 } stack_chunk_t;
470 
471 typedef struct mark_stack {
474  int index;
475  int limit;
476  size_t cache_size;
478 } mark_stack_t;
479 
480 typedef struct rb_heap_struct {
482 
485  struct heap_page *pages;
487 #if GC_ENABLE_INCREMENTAL_MARK
489 #endif
490  size_t total_pages; /* total page count in a heap */
491  size_t total_slots; /* total slot count (about total_pages * HEAP_PAGE_OBJ_LIMIT) */
492 } rb_heap_t;
493 
494 enum gc_mode {
498 };
499 
500 typedef struct rb_objspace {
501  struct {
502  size_t limit;
503  size_t increase;
504 #if MALLOC_ALLOCATED_SIZE
505  size_t allocated_size;
506  size_t allocations;
507 #endif
508  } malloc_params;
509 
510  struct {
511  unsigned int mode : 2;
512  unsigned int immediate_sweep : 1;
513  unsigned int dont_gc : 1;
514  unsigned int dont_incremental : 1;
515  unsigned int during_gc : 1;
516  unsigned int gc_stressful: 1;
517  unsigned int has_hook: 1;
518 #if USE_RGENGC
519  unsigned int during_minor_gc : 1;
520 #endif
521 #if GC_ENABLE_INCREMENTAL_MARK
522  unsigned int during_incremental_marking : 1;
523 #endif
524  } flags;
525 
528 
530  rb_heap_t tomb_heap; /* heap for zombies and ghosts */
531 
532  struct {
534  } atomic_flags;
535 
537  void *data;
538  void (*mark_func)(VALUE v, void *data);
539  } *mark_func_data;
540 
542  size_t marked_slots;
543 
544  struct {
545  struct heap_page **sorted;
551 
552  /* final */
553  size_t final_slots;
555  } heap_pages;
556 
558 
559  struct {
560  int run;
564  size_t next_index;
565  size_t size;
566 
567 #if GC_PROFILE_MORE_DETAIL
568  double prepare_time;
569 #endif
570  double invoke_time;
571 
572 #if USE_RGENGC
575 #if RGENGC_PROFILE > 0
576  size_t total_generated_normal_object_count;
577  size_t total_generated_shady_object_count;
578  size_t total_shade_operation_count;
579  size_t total_promoted_count;
580  size_t total_remembered_normal_object_count;
581  size_t total_remembered_shady_object_count;
582 
583 #if RGENGC_PROFILE >= 2
584  size_t generated_normal_object_count_types[RUBY_T_MASK];
585  size_t generated_shady_object_count_types[RUBY_T_MASK];
586  size_t shade_operation_count_types[RUBY_T_MASK];
587  size_t promoted_types[RUBY_T_MASK];
588  size_t remembered_normal_object_count_types[RUBY_T_MASK];
589  size_t remembered_shady_object_count_types[RUBY_T_MASK];
590 #endif
591 #endif /* RGENGC_PROFILE */
592 #endif /* USE_RGENGC */
593 
594  /* temporary profiling space */
598 
599  /* basic statistics */
600  size_t count;
604  } profile;
606 
608 
609 #if USE_RGENGC
610  struct {
616  size_t old_objects;
618 
619 #if RGENGC_ESTIMATE_OLDMALLOC
622 #endif
623 
624 #if RGENGC_CHECK_MODE >= 2
625  struct st_table *allrefs_table;
626  size_t error_count;
627 #endif
628  } rgengc;
629 #if GC_ENABLE_INCREMENTAL_MARK
630  struct {
631  size_t pooled_slots;
632  size_t step_slots;
633  } rincgc;
634 #endif
635 #endif /* USE_RGENGC */
636 
637 #if GC_DEBUG_STRESS_TO_CLASS
639 #endif
640 } rb_objspace_t;
641 
642 
643 #ifndef HEAP_PAGE_ALIGN_LOG
644 /* default tiny heap size: 16KB */
645 #define HEAP_PAGE_ALIGN_LOG 14
646 #endif
647 #define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod))
648 enum {
651  REQUIRED_SIZE_BY_MALLOC = (sizeof(size_t) * 5),
653  HEAP_PAGE_OBJ_LIMIT = (unsigned int)((HEAP_PAGE_SIZE - sizeof(struct heap_page_header))/sizeof(struct RVALUE)),
656  HEAP_PAGE_BITMAP_PLANES = USE_RGENGC ? 4 : 1 /* RGENGC: mark, unprotected, uncollectible, marking */
657 };
658 
659 struct heap_page {
660  struct heap_page *prev;
661  short total_slots;
662  short free_slots;
663  short final_slots;
664  struct {
665  unsigned int before_sweep : 1;
666  unsigned int has_remembered_objects : 1;
668  unsigned int in_tomb : 1;
669  } flags;
670 
674  struct heap_page *next;
675 
676 #if USE_RGENGC
678 #endif
679  /* the following three bitmaps are cleared at the beginning of full GC */
681 #if USE_RGENGC
684 #endif
685 };
686 
687 #define GET_PAGE_BODY(x) ((struct heap_page_body *)((bits_t)(x) & ~(HEAP_PAGE_ALIGN_MASK)))
688 #define GET_PAGE_HEADER(x) (&GET_PAGE_BODY(x)->header)
689 #define GET_HEAP_PAGE(x) (GET_PAGE_HEADER(x)->page)
690 
691 #define NUM_IN_PAGE(p) (((bits_t)(p) & HEAP_PAGE_ALIGN_MASK)/sizeof(RVALUE))
692 #define BITMAP_INDEX(p) (NUM_IN_PAGE(p) / BITS_BITLENGTH )
693 #define BITMAP_OFFSET(p) (NUM_IN_PAGE(p) & (BITS_BITLENGTH-1))
694 #define BITMAP_BIT(p) ((bits_t)1 << BITMAP_OFFSET(p))
695 
696 /* Bitmap Operations */
697 #define MARKED_IN_BITMAP(bits, p) ((bits)[BITMAP_INDEX(p)] & BITMAP_BIT(p))
698 #define MARK_IN_BITMAP(bits, p) ((bits)[BITMAP_INDEX(p)] = (bits)[BITMAP_INDEX(p)] | BITMAP_BIT(p))
699 #define CLEAR_IN_BITMAP(bits, p) ((bits)[BITMAP_INDEX(p)] = (bits)[BITMAP_INDEX(p)] & ~BITMAP_BIT(p))
700 
701 /* getting bitmap */
702 #define GET_HEAP_MARK_BITS(x) (&GET_HEAP_PAGE(x)->mark_bits[0])
703 #if USE_RGENGC
704 #define GET_HEAP_UNCOLLECTIBLE_BITS(x) (&GET_HEAP_PAGE(x)->uncollectible_bits[0])
705 #define GET_HEAP_WB_UNPROTECTED_BITS(x) (&GET_HEAP_PAGE(x)->wb_unprotected_bits[0])
706 #define GET_HEAP_MARKING_BITS(x) (&GET_HEAP_PAGE(x)->marking_bits[0])
707 #endif
708 
709 /* Aliases */
710 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
711 #define rb_objspace (*rb_objspace_of(GET_VM()))
712 #define rb_objspace_of(vm) ((vm)->objspace)
713 #else
715 #define rb_objspace_of(vm) (&rb_objspace)
716 #endif
717 
718 #define ruby_initial_gc_stress gc_params.gc_stress
719 
721 
722 #define malloc_limit objspace->malloc_params.limit
723 #define malloc_increase objspace->malloc_params.increase
724 #define malloc_allocated_size objspace->malloc_params.allocated_size
725 #define heap_pages_sorted objspace->heap_pages.sorted
726 #define heap_allocated_pages objspace->heap_pages.allocated_pages
727 #define heap_pages_sorted_length objspace->heap_pages.sorted_length
728 #define heap_pages_lomem objspace->heap_pages.range[0]
729 #define heap_pages_himem objspace->heap_pages.range[1]
730 #define heap_allocatable_pages objspace->heap_pages.allocatable_pages
731 #define heap_pages_freeable_pages objspace->heap_pages.freeable_pages
732 #define heap_pages_final_slots objspace->heap_pages.final_slots
733 #define heap_pages_deferred_final objspace->heap_pages.deferred_final
734 #define heap_eden (&objspace->eden_heap)
735 #define heap_tomb (&objspace->tomb_heap)
736 #define dont_gc objspace->flags.dont_gc
737 #define during_gc objspace->flags.during_gc
738 #define finalizing objspace->atomic_flags.finalizing
739 #define finalizer_table objspace->finalizer_table
740 #define global_list objspace->global_list
741 #define ruby_gc_stressful objspace->flags.gc_stressful
742 #define ruby_gc_stress_mode objspace->gc_stress_mode
743 #if GC_DEBUG_STRESS_TO_CLASS
744 #define stress_to_class objspace->stress_to_class
745 #else
746 #define stress_to_class 0
747 #endif
748 
749 static inline enum gc_mode
751 {
752 #if RGENGC_CHECK_MODE > 0
753  switch (mode) {
754  case gc_mode_none:
755  case gc_mode_marking:
756  case gc_mode_sweeping:
757  break;
758  default:
759  rb_bug("gc_mode_verify: unreachable (%d)", (int)mode);
760  }
761 #endif
762  return mode;
763 }
764 
765 #define gc_mode(objspace) gc_mode_verify((enum gc_mode)(objspace)->flags.mode)
766 #define gc_mode_set(objspace, mode) ((objspace)->flags.mode = (unsigned int)gc_mode_verify(mode))
767 
768 #define is_marking(objspace) (gc_mode(objspace) == gc_mode_marking)
769 #define is_sweeping(objspace) (gc_mode(objspace) == gc_mode_sweeping)
770 #if USE_RGENGC
771 #define is_full_marking(objspace) ((objspace)->flags.during_minor_gc == FALSE)
772 #else
773 #define is_full_marking(objspace) TRUE
774 #endif
775 #if GC_ENABLE_INCREMENTAL_MARK
776 #define is_incremental_marking(objspace) ((objspace)->flags.during_incremental_marking != FALSE)
777 #else
778 #define is_incremental_marking(objspace) FALSE
779 #endif
780 #if GC_ENABLE_INCREMENTAL_MARK
781 #define will_be_incremental_marking(objspace) ((objspace)->rgengc.need_major_gc != GPR_FLAG_NONE)
782 #else
783 #define will_be_incremental_marking(objspace) FALSE
784 #endif
785 #define has_sweeping_pages(heap) ((heap)->sweep_pages != 0)
786 #define is_lazy_sweeping(heap) (GC_ENABLE_LAZY_SWEEP && has_sweeping_pages(heap))
787 
788 #if SIZEOF_LONG == SIZEOF_VOIDP
789 # define nonspecial_obj_id(obj) (VALUE)((SIGNED_VALUE)(obj)|FIXNUM_FLAG)
790 # define obj_id_to_ref(objid) ((objid) ^ FIXNUM_FLAG) /* unset FIXNUM_FLAG */
791 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
792 # define nonspecial_obj_id(obj) LL2NUM((SIGNED_VALUE)(obj) / 2)
793 # define obj_id_to_ref(objid) (FIXNUM_P(objid) ? \
794  ((objid) ^ FIXNUM_FLAG) : (NUM2PTR(objid) << 1))
795 #else
796 # error not supported
797 #endif
798 
799 #define RANY(o) ((RVALUE*)(o))
800 
801 struct RZombie {
802  struct RBasic basic;
804  void (*dfree)(void *);
805  void *data;
806 };
807 
808 #define RZOMBIE(o) ((struct RZombie *)(o))
809 
810 #define nomem_error GET_VM()->special_exceptions[ruby_error_nomemory]
811 
815 
816 void rb_iseq_mark(const rb_iseq_t *iseq);
817 void rb_iseq_free(const rb_iseq_t *iseq);
818 
820 
821 static void rb_objspace_call_finalizer(rb_objspace_t *objspace);
822 static VALUE define_final0(VALUE obj, VALUE block);
823 
824 static void negative_size_allocation_error(const char *);
825 static void *aligned_malloc(size_t, size_t);
826 static void aligned_free(void *);
827 
828 static void init_mark_stack(mark_stack_t *stack);
829 
830 static int ready_to_gc(rb_objspace_t *objspace);
831 
832 static int garbage_collect(rb_objspace_t *, int full_mark, int immediate_mark, int immediate_sweep, int reason);
833 
834 static int gc_start(rb_objspace_t *objspace, const int full_mark, const int immediate_mark, const unsigned int immediate_sweep, int reason);
835 static void gc_rest(rb_objspace_t *objspace);
836 static inline void gc_enter(rb_objspace_t *objspace, const char *event);
837 static inline void gc_exit(rb_objspace_t *objspace, const char *event);
838 
839 static void gc_marks(rb_objspace_t *objspace, int full_mark);
840 static void gc_marks_start(rb_objspace_t *objspace, int full);
841 static int gc_marks_finish(rb_objspace_t *objspace);
842 static void gc_marks_rest(rb_objspace_t *objspace);
843 #if GC_ENABLE_INCREMENTAL_MARK
844 static void gc_marks_step(rb_objspace_t *objspace, int slots);
845 static void gc_marks_continue(rb_objspace_t *objspace, rb_heap_t *heap);
846 #endif
847 
848 static void gc_sweep(rb_objspace_t *objspace);
849 static void gc_sweep_start(rb_objspace_t *objspace);
850 static void gc_sweep_finish(rb_objspace_t *objspace);
851 static int gc_sweep_step(rb_objspace_t *objspace, rb_heap_t *heap);
852 static void gc_sweep_rest(rb_objspace_t *objspace);
853 #if GC_ENABLE_LAZY_SWEEP
854 static void gc_sweep_continue(rb_objspace_t *objspace, rb_heap_t *heap);
855 #endif
856 
857 static inline void gc_mark(rb_objspace_t *objspace, VALUE ptr);
858 static void gc_mark_ptr(rb_objspace_t *objspace, VALUE ptr);
859 static void gc_mark_maybe(rb_objspace_t *objspace, VALUE ptr);
860 static void gc_mark_children(rb_objspace_t *objspace, VALUE ptr);
861 
864 static void gc_grey(rb_objspace_t *objspace, VALUE ptr);
865 
866 static inline int gc_mark_set(rb_objspace_t *objspace, VALUE obj);
867 static inline int is_pointer_to_heap(rb_objspace_t *objspace, void *ptr);
868 
869 static void push_mark_stack(mark_stack_t *, VALUE);
870 static int pop_mark_stack(mark_stack_t *, VALUE *);
871 static size_t mark_stack_size(mark_stack_t *stack);
872 static void shrink_stack_chunk_cache(mark_stack_t *stack);
873 
874 static size_t obj_memsize_of(VALUE obj, int use_all_types);
876 static int gc_verify_heap_page(rb_objspace_t *objspace, struct heap_page *page, VALUE obj);
877 static int gc_verify_heap_pages(rb_objspace_t *objspace);
878 
879 static void gc_stress_set(rb_objspace_t *objspace, VALUE flag);
880 
881 static double getrusage_time(void);
882 static inline void gc_prof_setup_new_record(rb_objspace_t *objspace, int reason);
883 static inline void gc_prof_timer_start(rb_objspace_t *);
884 static inline void gc_prof_timer_stop(rb_objspace_t *);
885 static inline void gc_prof_mark_timer_start(rb_objspace_t *);
886 static inline void gc_prof_mark_timer_stop(rb_objspace_t *);
887 static inline void gc_prof_sweep_timer_start(rb_objspace_t *);
888 static inline void gc_prof_sweep_timer_stop(rb_objspace_t *);
889 static inline void gc_prof_set_malloc_info(rb_objspace_t *);
890 static inline void gc_prof_set_heap_info(rb_objspace_t *);
891 
892 #define gc_prof_record(objspace) (objspace)->profile.current_record
893 #define gc_prof_enabled(objspace) ((objspace)->profile.run && (objspace)->profile.current_record)
894 
895 #ifdef HAVE_VA_ARGS_MACRO
896 # define gc_report(level, objspace, fmt, ...) \
897  if ((level) > RGENGC_DEBUG) {} else gc_report_body(level, objspace, fmt, ##__VA_ARGS__)
898 #else
899 # define gc_report if (!(RGENGC_DEBUG)) {} else gc_report_body
900 #endif
901 PRINTF_ARGS(static void gc_report_body(int level, rb_objspace_t *objspace, const char *fmt, ...), 3, 4);
902 static const char *obj_info(VALUE obj);
903 
904 #define PUSH_MARK_FUNC_DATA(v) do { \
905  struct mark_func_data_struct *prev_mark_func_data = objspace->mark_func_data; \
906  objspace->mark_func_data = (v);
907 
908 #define POP_MARK_FUNC_DATA() objspace->mark_func_data = prev_mark_func_data;} while (0)
909 
910 /*
911  * 1 - TSC (H/W Time Stamp Counter)
912  * 2 - getrusage
913  */
914 #ifndef TICK_TYPE
915 #define TICK_TYPE 1
916 #endif
917 
918 #if USE_TICK_T
919 
920 #if TICK_TYPE == 1
921 /* the following code is only for internal tuning. */
922 
923 /* Source code to use RDTSC is quoted and modified from
924  * http://www.mcs.anl.gov/~kazutomo/rdtsc.html
925  * written by Kazutomo Yoshii <kazutomo@mcs.anl.gov>
926  */
927 
928 #if defined(__GNUC__) && defined(__i386__)
929 typedef unsigned long long tick_t;
930 #define PRItick "llu"
931 static inline tick_t
932 tick(void)
933 {
934  unsigned long long int x;
935  __asm__ __volatile__ ("rdtsc" : "=A" (x));
936  return x;
937 }
938 
939 #elif defined(__GNUC__) && defined(__x86_64__)
940 typedef unsigned long long tick_t;
941 #define PRItick "llu"
942 
943 static __inline__ tick_t
944 tick(void)
945 {
946  unsigned long hi, lo;
947  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
948  return ((unsigned long long)lo)|( ((unsigned long long)hi)<<32);
949 }
950 
951 #elif defined(__powerpc64__) && GCC_VERSION_SINCE(4,8,0)
952 typedef unsigned long long tick_t;
953 #define PRItick "llu"
954 
955 static __inline__ tick_t
956 tick(void)
957 {
958  unsigned long long val = __builtin_ppc_get_timebase();
959  return val;
960 }
961 
962 #elif defined(_WIN32) && defined(_MSC_VER)
963 #include <intrin.h>
964 typedef unsigned __int64 tick_t;
965 #define PRItick "llu"
966 
967 static inline tick_t
968 tick(void)
969 {
970  return __rdtsc();
971 }
972 
973 #else /* use clock */
974 typedef clock_t tick_t;
975 #define PRItick "llu"
976 
977 static inline tick_t
978 tick(void)
979 {
980  return clock();
981 }
982 #endif /* TSC */
983 
984 #elif TICK_TYPE == 2
985 typedef double tick_t;
986 #define PRItick "4.9f"
987 
988 static inline tick_t
989 tick(void)
990 {
991  return getrusage_time();
992 }
993 #else /* TICK_TYPE */
994 #error "choose tick type"
995 #endif /* TICK_TYPE */
996 
997 #define MEASURE_LINE(expr) do { \
998  volatile tick_t start_time = tick(); \
999  volatile tick_t end_time; \
1000  expr; \
1001  end_time = tick(); \
1002  fprintf(stderr, "0\t%"PRItick"\t%s\n", end_time - start_time, #expr); \
1003 } while (0)
1004 
1005 #else /* USE_TICK_T */
1006 #define MEASURE_LINE(expr) expr
1007 #endif /* USE_TICK_T */
1008 
1009 #define FL_TEST2(x,f) ((RGENGC_CHECK_MODE && SPECIAL_CONST_P(x)) ? (rb_bug("FL_TEST2: SPECIAL_CONST (%p)", (void *)(x)), 0) : FL_TEST_RAW((x),(f)) != 0)
1010 #define FL_SET2(x,f) do {if (RGENGC_CHECK_MODE && SPECIAL_CONST_P(x)) rb_bug("FL_SET2: SPECIAL_CONST"); RBASIC(x)->flags |= (f);} while (0)
1011 #define FL_UNSET2(x,f) do {if (RGENGC_CHECK_MODE && SPECIAL_CONST_P(x)) rb_bug("FL_UNSET2: SPECIAL_CONST"); RBASIC(x)->flags &= ~(f);} while (0)
1012 
1013 #define RVALUE_MARK_BITMAP(obj) MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(obj), (obj))
1014 #define RVALUE_PAGE_MARKED(page, obj) MARKED_IN_BITMAP((page)->mark_bits, (obj))
1015 
1016 #if USE_RGENGC
1017 #define RVALUE_WB_UNPROTECTED_BITMAP(obj) MARKED_IN_BITMAP(GET_HEAP_WB_UNPROTECTED_BITS(obj), (obj))
1018 #define RVALUE_UNCOLLECTIBLE_BITMAP(obj) MARKED_IN_BITMAP(GET_HEAP_UNCOLLECTIBLE_BITS(obj), (obj))
1019 #define RVALUE_MARKING_BITMAP(obj) MARKED_IN_BITMAP(GET_HEAP_MARKING_BITS(obj), (obj))
1020 
1021 #define RVALUE_PAGE_WB_UNPROTECTED(page, obj) MARKED_IN_BITMAP((page)->wb_unprotected_bits, (obj))
1022 #define RVALUE_PAGE_UNCOLLECTIBLE(page, obj) MARKED_IN_BITMAP((page)->uncollectible_bits, (obj))
1023 #define RVALUE_PAGE_MARKING(page, obj) MARKED_IN_BITMAP((page)->marking_bits, (obj))
1024 
1025 #define RVALUE_OLD_AGE 3
1026 #define RVALUE_AGE_SHIFT 5 /* FL_PROMOTED0 bit */
1027 
1028 static int rgengc_remembered(rb_objspace_t *objspace, VALUE obj);
1029 static int rgengc_remember(rb_objspace_t *objspace, VALUE obj);
1030 static void rgengc_mark_and_rememberset_clear(rb_objspace_t *objspace, rb_heap_t *heap);
1031 static void rgengc_rememberset_mark(rb_objspace_t *objspace, rb_heap_t *heap);
1032 
1033 static inline int
1035 {
1036  return (int)((flags & (FL_PROMOTED0 | FL_PROMOTED1)) >> RVALUE_AGE_SHIFT);
1037 }
1038 
1039 #endif /* USE_RGENGC */
1040 
1041 
1042 #if RGENGC_CHECK_MODE == 0
1043 static inline VALUE
1045 {
1046  return obj;
1047 }
1048 #else
1049 static VALUE
1051 {
1052  rb_objspace_t *objspace = &rb_objspace;
1053 
1054  if (SPECIAL_CONST_P(obj)) {
1055  rb_bug("check_rvalue_consistency: %p is a special const.", (void *)obj);
1056  }
1057  else if (!is_pointer_to_heap(objspace, (void *)obj)) {
1058  rb_bug("check_rvalue_consistency: %p is not a Ruby object.", (void *)obj);
1059  }
1060  else {
1061  const int wb_unprotected_bit = RVALUE_WB_UNPROTECTED_BITMAP(obj) != 0;
1062  const int uncollectible_bit = RVALUE_UNCOLLECTIBLE_BITMAP(obj) != 0;
1063  const int mark_bit = RVALUE_MARK_BITMAP(obj) != 0;
1064  const int marking_bit = RVALUE_MARKING_BITMAP(obj) != 0, remembered_bit = marking_bit;
1065  const int age = RVALUE_FLAGS_AGE(RBASIC(obj)->flags);
1066 
1067  if (BUILTIN_TYPE(obj) == T_NONE) rb_bug("check_rvalue_consistency: %s is T_NONE", obj_info(obj));
1068  if (BUILTIN_TYPE(obj) == T_ZOMBIE) rb_bug("check_rvalue_consistency: %s is T_ZOMBIE", obj_info(obj));
1069  obj_memsize_of((VALUE)obj, FALSE);
1070 
1071  /* check generation
1072  *
1073  * OLD == age == 3 && old-bitmap && mark-bit (except incremental marking)
1074  */
1075  if (age > 0 && wb_unprotected_bit) {
1076  rb_bug("check_rvalue_consistency: %s is not WB protected, but age is %d > 0.", obj_info(obj), age);
1077  }
1078 
1079  if (!is_marking(objspace) && uncollectible_bit && !mark_bit) {
1080  rb_bug("check_rvalue_consistency: %s is uncollectible, but is not marked while !gc.", obj_info(obj));
1081  }
1082 
1083  if (!is_full_marking(objspace)) {
1084  if (uncollectible_bit && age != RVALUE_OLD_AGE && !wb_unprotected_bit) {
1085  rb_bug("check_rvalue_consistency: %s is uncollectible, but not old (age: %d) and not WB unprotected.", obj_info(obj), age);
1086  }
1087  if (remembered_bit && age != RVALUE_OLD_AGE) {
1088  rb_bug("check_rvalue_consistency: %s is rememberd, but not old (age: %d).", obj_info(obj), age);
1089  }
1090  }
1091 
1092  /*
1093  * check coloring
1094  *
1095  * marking:false marking:true
1096  * marked:false white *invalid*
1097  * marked:true black grey
1098  */
1099  if (is_incremental_marking(objspace) && marking_bit) {
1100  if (!is_marking(objspace) && !mark_bit) rb_bug("check_rvalue_consistency: %s is marking, but not marked.", obj_info(obj));
1101  }
1102  }
1103  return obj;
1104 }
1105 #endif
1106 
1107 static inline int
1109 {
1111  return RVALUE_MARK_BITMAP(obj) != 0;
1112 }
1113 
1114 #if USE_RGENGC
1115 static inline int
1117 {
1119  return RVALUE_WB_UNPROTECTED_BITMAP(obj) != 0;
1120 }
1121 
1122 static inline int
1124 {
1126  return RVALUE_MARKING_BITMAP(obj) != 0;
1127 }
1128 
1129 static inline int
1131 {
1133  return RVALUE_MARKING_BITMAP(obj) != 0;
1134 }
1135 
1136 static inline int
1138 {
1140  return RVALUE_UNCOLLECTIBLE_BITMAP(obj) != 0;
1141 }
1142 
1143 static inline int
1145 {
1146  const VALUE promoted = FL_PROMOTED0 | FL_PROMOTED1;
1147  return (RBASIC(obj)->flags & promoted) == promoted;
1148 }
1149 
1150 static inline int
1152 {
1154  return RVALUE_OLD_P_RAW(obj);
1155 }
1156 
1157 #if RGENGC_CHECK_MODE || GC_DEBUG
1158 static inline int
1159 RVALUE_AGE(VALUE obj)
1160 {
1162  return RVALUE_FLAGS_AGE(RBASIC(obj)->flags);
1163 }
1164 #endif
1165 
1166 static inline void
1168 {
1169  MARK_IN_BITMAP(&page->uncollectible_bits[0], obj);
1170  objspace->rgengc.old_objects++;
1171 
1172 #if RGENGC_PROFILE >= 2
1173  objspace->profile.total_promoted_count++;
1174  objspace->profile.promoted_types[BUILTIN_TYPE(obj)]++;
1175 #endif
1176 }
1177 
1178 static inline void
1180 {
1181  RVALUE_PAGE_OLD_UNCOLLECTIBLE_SET(objspace, GET_HEAP_PAGE(obj), obj);
1182 }
1183 
1184 static inline VALUE
1186 {
1187  flags &= ~(FL_PROMOTED0 | FL_PROMOTED1);
1188  flags |= (age << RVALUE_AGE_SHIFT);
1189  return flags;
1190 }
1191 
1192 /* set age to age+1 */
1193 static inline void
1195 {
1196  VALUE flags = RBASIC(obj)->flags;
1197  int age = RVALUE_FLAGS_AGE(flags);
1198 
1199  if (RGENGC_CHECK_MODE && age == RVALUE_OLD_AGE) {
1200  rb_bug("RVALUE_AGE_INC: can not increment age of OLD object %s.", obj_info(obj));
1201  }
1202 
1203  age++;
1204  RBASIC(obj)->flags = RVALUE_FLAGS_AGE_SET(flags, age);
1205 
1206  if (age == RVALUE_OLD_AGE) {
1207  RVALUE_OLD_UNCOLLECTIBLE_SET(objspace, obj);
1208  }
1210 }
1211 
1212 /* set age to RVALUE_OLD_AGE */
1213 static inline void
1215 {
1218 
1219  RBASIC(obj)->flags = RVALUE_FLAGS_AGE_SET(RBASIC(obj)->flags, RVALUE_OLD_AGE);
1220  RVALUE_OLD_UNCOLLECTIBLE_SET(objspace, obj);
1221 
1223 }
1224 
1225 /* set age to RVALUE_OLD_AGE - 1 */
1226 static inline void
1228 {
1231 
1232  RBASIC(obj)->flags = RVALUE_FLAGS_AGE_SET(RBASIC(obj)->flags, RVALUE_OLD_AGE - 1);
1233 
1235 }
1236 
1237 static inline void
1239 {
1240  RBASIC(obj)->flags = RVALUE_FLAGS_AGE_SET(RBASIC(obj)->flags, 0);
1242 }
1243 
1244 static inline void
1246 {
1249 
1250  if (!is_incremental_marking(objspace) && RVALUE_REMEMBERED(obj)) {
1252  }
1253 
1254  RVALUE_DEMOTE_RAW(objspace, obj);
1255 
1256  if (RVALUE_MARKED(obj)) {
1257  objspace->rgengc.old_objects--;
1258  }
1259 
1261 }
1262 
1263 static inline void
1265 {
1266  RBASIC(obj)->flags = RVALUE_FLAGS_AGE_SET(RBASIC(obj)->flags, 0);
1267 }
1268 
1269 static inline void
1271 {
1274  RVALUE_AGE_RESET_RAW(obj);
1276 }
1277 
1278 static inline int
1280 {
1281  return RVALUE_MARKED(obj) && !RVALUE_MARKING(obj);
1282 }
1283 
1284 #if 0
1285 static inline int
1286 RVALUE_GREY_P(VALUE obj)
1287 {
1288  return RVALUE_MARKED(obj) && RVALUE_MARKING(obj);
1289 }
1290 #endif
1291 
1292 static inline int
1294 {
1295  return RVALUE_MARKED(obj) == FALSE;
1296 }
1297 
1298 #endif /* USE_RGENGC */
1299 
1300 /*
1301  --------------------------- ObjectSpace -----------------------------
1302 */
1303 
1304 rb_objspace_t *
1306 {
1307 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
1308  rb_objspace_t *objspace = calloc(1, sizeof(rb_objspace_t));
1309 #else
1310  rb_objspace_t *objspace = &rb_objspace;
1311 #endif
1312  malloc_limit = gc_params.malloc_limit_min;
1313 
1314  return objspace;
1315 }
1316 
1317 static void free_stack_chunks(mark_stack_t *);
1318 static void heap_page_free(rb_objspace_t *objspace, struct heap_page *page);
1319 
1320 void
1322 {
1324  rb_bug("lazy sweeping underway when freeing object space");
1325 
1326  if (objspace->profile.records) {
1327  free(objspace->profile.records);
1328  objspace->profile.records = 0;
1329  }
1330 
1331  if (global_list) {
1332  struct gc_list *list, *next;
1333  for (list = global_list; list; list = next) {
1334  next = list->next;
1335  xfree(list);
1336  }
1337  }
1338  if (heap_pages_sorted) {
1339  size_t i;
1340  for (i = 0; i < heap_allocated_pages; ++i) {
1341  heap_page_free(objspace, heap_pages_sorted[i]);
1342  }
1344  heap_allocated_pages = 0;
1346  heap_pages_lomem = 0;
1347  heap_pages_himem = 0;
1348 
1349  objspace->eden_heap.total_pages = 0;
1350  objspace->eden_heap.total_slots = 0;
1351  objspace->eden_heap.pages = NULL;
1352  }
1353  free_stack_chunks(&objspace->mark_stack);
1354 #if !(defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE)
1355  if (objspace == &rb_objspace) return;
1356 #endif
1357  free(objspace);
1358 }
1359 
1360 static void
1361 heap_pages_expand_sorted_to(rb_objspace_t *objspace, size_t next_length)
1362 {
1363  struct heap_page **sorted;
1364  size_t size = next_length * sizeof(struct heap_page *);
1365 
1366  gc_report(3, objspace, "heap_pages_expand_sorted: next_length: %d, size: %d\n", (int)next_length, (int)size);
1367 
1368  if (heap_pages_sorted_length > 0) {
1369  sorted = (struct heap_page **)realloc(heap_pages_sorted, size);
1370  if (sorted) heap_pages_sorted = sorted;
1371  }
1372  else {
1373  sorted = heap_pages_sorted = (struct heap_page **)malloc(size);
1374  }
1375 
1376  if (sorted == 0) {
1377  rb_memerror();
1378  }
1379 
1380  heap_pages_sorted_length = next_length;
1381 }
1382 
1383 static void
1385 {
1386  size_t next_length = heap_allocatable_pages;
1387  next_length += heap_eden->total_pages;
1388  next_length += heap_tomb->total_pages;
1389 
1390  if (next_length > heap_pages_sorted_length) {
1391  heap_pages_expand_sorted_to(objspace, next_length);
1392  }
1393 }
1394 
1395 static inline void
1397 {
1398  RVALUE *p = (RVALUE *)obj;
1399  p->as.free.flags = 0;
1400  p->as.free.next = page->freelist;
1401  page->freelist = p;
1402 
1403  if (RGENGC_CHECK_MODE && !is_pointer_to_heap(objspace, p)) {
1404  rb_bug("heap_page_add_freeobj: %p is not rvalue.", p);
1405  }
1406 
1407  gc_report(3, objspace, "heap_page_add_freeobj: add %p to freelist\n", (void *)obj);
1408 }
1409 
1410 static inline void
1411 heap_add_freepage(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
1412 {
1413  if (page->freelist) {
1414  page->free_next = heap->free_pages;
1415  heap->free_pages = page;
1416  }
1417 }
1418 
1419 #if GC_ENABLE_INCREMENTAL_MARK
1420 static inline int
1421 heap_add_poolpage(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
1422 {
1423  if (page->freelist) {
1424  page->free_next = heap->pooled_pages;
1425  heap->pooled_pages = page;
1426  objspace->rincgc.pooled_slots += page->free_slots;
1427  return TRUE;
1428  }
1429  else {
1430  return FALSE;
1431  }
1432 }
1433 #endif
1434 
1435 static void
1436 heap_unlink_page(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
1437 {
1438  if (page->prev) page->prev->next = page->next;
1439  if (page->next) page->next->prev = page->prev;
1440  if (heap->pages == page) heap->pages = page->next;
1441  page->prev = NULL;
1442  page->next = NULL;
1443  heap->total_pages--;
1444  heap->total_slots -= page->total_slots;
1445 }
1446 
1447 static void
1448 heap_page_free(rb_objspace_t *objspace, struct heap_page *page)
1449 {
1451  objspace->profile.total_freed_pages++;
1453  free(page);
1454 }
1455 
1456 static void
1458 {
1459  size_t i, j;
1460 
1461  if (heap_tomb->pages) {
1462  for (i = j = 1; j < heap_allocated_pages; i++) {
1463  struct heap_page *page = heap_pages_sorted[i];
1464 
1465  if (page->flags.in_tomb && page->free_slots == page->total_slots) {
1466  heap_unlink_page(objspace, heap_tomb, page);
1467  heap_page_free(objspace, page);
1468  }
1469  else {
1470  if (i != j) {
1471  heap_pages_sorted[j] = page;
1472  }
1473  j++;
1474  }
1475  }
1476  if (RGENGC_CHECK_MODE) assert(j == heap_allocated_pages);
1477  }
1478 }
1479 
1480 static struct heap_page *
1482 {
1483  RVALUE *start, *end, *p;
1484  struct heap_page *page;
1485  struct heap_page_body *page_body = 0;
1486  size_t hi, lo, mid;
1487  int limit = HEAP_PAGE_OBJ_LIMIT;
1488 
1489  /* assign heap_page body (contains heap_page_header and RVALUEs) */
1491  if (page_body == 0) {
1492  rb_memerror();
1493  }
1494 
1495  /* assign heap_page entry */
1496  page = (struct heap_page *)calloc(1, sizeof(struct heap_page));
1497  if (page == 0) {
1498  aligned_free(page_body);
1499  rb_memerror();
1500  }
1501 
1502  /* adjust obj_limit (object number available in this page) */
1503  start = (RVALUE*)((VALUE)page_body + sizeof(struct heap_page_header));
1504  if ((VALUE)start % sizeof(RVALUE) != 0) {
1505  int delta = (int)(sizeof(RVALUE) - ((VALUE)start % sizeof(RVALUE)));
1506  start = (RVALUE*)((VALUE)start + delta);
1507  limit = (HEAP_PAGE_SIZE - (int)((VALUE)start - (VALUE)page_body))/(int)sizeof(RVALUE);
1508  }
1509  end = start + limit;
1510 
1511  /* setup heap_pages_sorted */
1512  lo = 0;
1513  hi = heap_allocated_pages;
1514  while (lo < hi) {
1515  struct heap_page *mid_page;
1516 
1517  mid = (lo + hi) / 2;
1518  mid_page = heap_pages_sorted[mid];
1519  if (mid_page->start < start) {
1520  lo = mid + 1;
1521  }
1522  else if (mid_page->start > start) {
1523  hi = mid;
1524  }
1525  else {
1526  rb_bug("same heap page is allocated: %p at %"PRIuVALUE, (void *)page_body, (VALUE)mid);
1527  }
1528  }
1531  }
1532  if (hi < heap_allocated_pages) {
1534  }
1535 
1536  heap_pages_sorted[hi] = page;
1537 
1539  objspace->profile.total_allocated_pages++;
1540 
1542  rb_bug("heap_page_allocate: allocated(%"PRIdSIZE") > sorted(%"PRIdSIZE")",
1544  }
1545 
1547  if (heap_pages_himem < end) heap_pages_himem = end;
1548 
1549  page->start = start;
1550  page->total_slots = limit;
1551  page_body->header.page = page;
1552 
1553  for (p = start; p != end; p++) {
1554  gc_report(3, objspace, "assign_heap_page: %p is added to freelist\n", p);
1555  heap_page_add_freeobj(objspace, page, (VALUE)p);
1556  }
1557  page->free_slots = limit;
1558 
1559  return page;
1560 }
1561 
1562 static struct heap_page *
1564 {
1565  struct heap_page *page = heap_tomb->pages;
1566 
1567  while (page) {
1568  if (page->freelist != NULL) {
1569  heap_unlink_page(objspace, heap_tomb, page);
1570  return page;
1571  }
1572  page = page->next;
1573  }
1574 
1575  return NULL;
1576 }
1577 
1578 static struct heap_page *
1580 {
1581  struct heap_page *page = heap_page_resurrect(objspace);
1582  const char *method = "recycle";
1583  if (page == NULL) {
1584  page = heap_page_allocate(objspace);
1585  method = "allocate";
1586  }
1587  if (0) fprintf(stderr, "heap_page_create: %s - %p, heap_allocated_pages: %d, heap_allocated_pages: %d, tomb->total_pages: %d\n",
1588  method, page, (int)heap_pages_sorted_length, (int)heap_allocated_pages, (int)heap_tomb->total_pages);
1589  return page;
1590 }
1591 
1592 static void
1593 heap_add_page(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
1594 {
1595  page->flags.in_tomb = (heap == heap_tomb);
1596  page->next = heap->pages;
1597  if (heap->pages) heap->pages->prev = page;
1598  heap->pages = page;
1599  heap->total_pages++;
1600  heap->total_slots += page->total_slots;
1601 }
1602 
1603 static void
1605 {
1606  struct heap_page *page = heap_page_create(objspace);
1607  heap_add_page(objspace, heap, page);
1608  heap_add_freepage(objspace, heap, page);
1609 }
1610 
1611 static void
1612 heap_add_pages(rb_objspace_t *objspace, rb_heap_t *heap, size_t add)
1613 {
1614  size_t i;
1615 
1617  heap_pages_expand_sorted(objspace);
1618  for (i = 0; i < add; i++) {
1619  heap_assign_page(objspace, heap);
1620  }
1622 }
1623 
1624 static size_t
1626 {
1627  double goal_ratio = gc_params.heap_free_slots_goal_ratio;
1629  size_t next_used;
1630 
1631  if (goal_ratio == 0.0) {
1632  next_used = (size_t)(used * gc_params.growth_factor);
1633  }
1634  else {
1635  /* Find `f' where free_slots = f * total_slots * goal_ratio
1636  * => f = (total_slots - free_slots) / ((1 - goal_ratio) * total_slots)
1637  */
1638  double f = (double)(total_slots - free_slots) / ((1 - goal_ratio) * total_slots);
1639 
1640  if (f > gc_params.growth_factor) f = gc_params.growth_factor;
1641  if (f < 1.0) f = 1.1;
1642 
1643  next_used = (size_t)(f * used);
1644 
1645  if (0) {
1646  fprintf(stderr,
1647  "free_slots(%8"PRIuSIZE")/total_slots(%8"PRIuSIZE")=%1.2f,"
1648  " G(%1.2f), f(%1.2f),"
1649  " used(%8"PRIuSIZE") => next_used(%8"PRIuSIZE")\n",
1650  free_slots, total_slots, free_slots/(double)total_slots,
1651  goal_ratio, f, used, next_used);
1652  }
1653  }
1654 
1655  if (gc_params.growth_max_slots > 0) {
1656  size_t max_used = (size_t)(used + gc_params.growth_max_slots/HEAP_PAGE_OBJ_LIMIT);
1657  if (next_used > max_used) next_used = max_used;
1658  }
1659 
1660  return next_used - used;
1661 }
1662 
1663 static void
1664 heap_set_increment(rb_objspace_t *objspace, size_t additional_pages)
1665 {
1666  size_t used = heap_eden->total_pages;
1667  size_t next_used_limit = used + additional_pages;
1668 
1669  if (next_used_limit == heap_allocated_pages) next_used_limit++;
1670 
1671  heap_allocatable_pages = next_used_limit - used;
1672  heap_pages_expand_sorted(objspace);
1673 
1674  gc_report(1, objspace, "heap_set_increment: heap_allocatable_pages is %d\n", (int)heap_allocatable_pages);
1675 }
1676 
1677 static int
1679 {
1680  if (heap_allocatable_pages > 0) {
1681  gc_report(1, objspace, "heap_increment: heap_pages_sorted_length: %d, heap_pages_inc: %d, heap->total_pages: %d\n",
1683  heap_allocatable_pages--;
1684  heap_assign_page(objspace, heap);
1685  return TRUE;
1686  }
1687  return FALSE;
1688 }
1689 
1690 static void
1692 {
1693  if (RGENGC_CHECK_MODE) assert(heap->free_pages == NULL);
1694 
1695 #if GC_ENABLE_LAZY_SWEEP
1696  if (is_lazy_sweeping(heap)) {
1697  gc_sweep_continue(objspace, heap);
1698  }
1699 #endif
1700 #if GC_ENABLE_INCREMENTAL_MARK
1701  else if (is_incremental_marking(objspace)) {
1702  gc_marks_continue(objspace, heap);
1703  }
1704 #endif
1705 
1706  if (heap->free_pages == NULL &&
1707  (will_be_incremental_marking(objspace) || heap_increment(objspace, heap) == FALSE) &&
1708  gc_start(objspace, FALSE, FALSE, FALSE, GPR_FLAG_NEWOBJ) == FALSE) {
1709  rb_memerror();
1710  }
1711 }
1712 
1713 static RVALUE *
1715 {
1716  struct heap_page *page;
1717  RVALUE *p;
1718 
1719  while (heap->free_pages == NULL) {
1720  heap_prepare(objspace, heap);
1721  }
1722  page = heap->free_pages;
1723  heap->free_pages = page->free_next;
1724  heap->using_page = page;
1725 
1726  if (RGENGC_CHECK_MODE) assert(page->free_slots != 0);
1727  p = page->freelist;
1728  page->freelist = NULL;
1729  page->free_slots = 0;
1730  return p;
1731 }
1732 
1733 static inline VALUE
1735 {
1736  RVALUE *p = heap->freelist;
1737  if (LIKELY(p != NULL)) {
1738  heap->freelist = p->as.free.next;
1739  }
1740  return (VALUE)p;
1741 }
1742 
1743 static inline VALUE
1745 {
1746  RVALUE *p = heap->freelist;
1747 
1748  while (1) {
1749  if (LIKELY(p != NULL)) {
1750  heap->freelist = p->as.free.next;
1751  return (VALUE)p;
1752  }
1753  else {
1754  p = heap_get_freeobj_from_next_freepage(objspace, heap);
1755  }
1756  }
1757 }
1758 
1759 void
1761 {
1762  rb_objspace_t *objspace = &rb_objspace;
1763  objspace->hook_events = event & RUBY_INTERNAL_EVENT_OBJSPACE_MASK;
1764  objspace->flags.has_hook = (objspace->hook_events != 0);
1765 }
1766 
1767 static void
1769 {
1770  EXEC_EVENT_HOOK(th, event, th->cfp->self, 0, 0, 0, data);
1771 }
1772 
1773 #define gc_event_hook_available_p(objspace) ((objspace)->flags.has_hook)
1774 #define gc_event_hook_needed_p(objspace, event) ((objspace)->hook_events & (event))
1775 
1776 #define gc_event_hook(objspace, event, data) do { \
1777  if (UNLIKELY(gc_event_hook_needed_p(objspace, event))) { \
1778  gc_event_hook_body(GET_THREAD(), (objspace), (event), (data)); \
1779  } \
1780 } while (0)
1781 
1782 static inline VALUE
1783 newobj_init(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, int wb_protected, rb_objspace_t *objspace, VALUE obj)
1784 {
1785  if (RGENGC_CHECK_MODE > 0) {
1786  assert(BUILTIN_TYPE(obj) == T_NONE);
1787  assert((flags & FL_WB_PROTECTED) == 0);
1788  }
1789 
1790  /* OBJSETUP */
1791  RBASIC(obj)->flags = flags;
1792  RBASIC_SET_CLASS_RAW(obj, klass);
1793  RANY(obj)->as.values.v1 = v1;
1794  RANY(obj)->as.values.v2 = v2;
1795  RANY(obj)->as.values.v3 = v3;
1796 
1797 #if RGENGC_CHECK_MODE
1798  assert(RVALUE_MARKED(obj) == FALSE);
1799  assert(RVALUE_MARKING(obj) == FALSE);
1800  assert(RVALUE_OLD_P(obj) == FALSE);
1802 
1803  if (flags & FL_PROMOTED1) {
1804  if (RVALUE_AGE(obj) != 2) rb_bug("newobj: %s of age (%d) != 2.", obj_info(obj), RVALUE_AGE(obj));
1805  }
1806  else {
1807  if (RVALUE_AGE(obj) > 0) rb_bug("newobj: %s of age (%d) > 0.", obj_info(obj), RVALUE_AGE(obj));
1808  }
1809  if (rgengc_remembered(objspace, (VALUE)obj)) rb_bug("newobj: %s is remembered.", obj_info(obj));
1810 #endif
1811 
1812 #if USE_RGENGC
1813  if (UNLIKELY(wb_protected == FALSE)) {
1815  }
1816 #endif
1817 
1818 #if RGENGC_PROFILE
1819  if (wb_protected) {
1820  objspace->profile.total_generated_normal_object_count++;
1821 #if RGENGC_PROFILE >= 2
1822  objspace->profile.generated_normal_object_count_types[BUILTIN_TYPE(obj)]++;
1823 #endif
1824  }
1825  else {
1826  objspace->profile.total_generated_shady_object_count++;
1827 #if RGENGC_PROFILE >= 2
1828  objspace->profile.generated_shady_object_count_types[BUILTIN_TYPE(obj)]++;
1829 #endif
1830  }
1831 #endif
1832 
1833 #if GC_DEBUG
1834  RANY(obj)->file = rb_source_loc(&RANY(obj)->line);
1835  assert(!SPECIAL_CONST_P(obj)); /* check alignment */
1836 #endif
1837 
1838  objspace->total_allocated_objects++;
1839 
1840  gc_report(5, objspace, "newobj: %s\n", obj_info(obj));
1841 
1842 #if RGENGC_OLD_NEWOBJ_CHECK > 0
1843  {
1844  static int newobj_cnt = RGENGC_OLD_NEWOBJ_CHECK;
1845 
1846  if (!is_incremental_marking(objspace) &&
1847  flags & FL_WB_PROTECTED && /* do not promote WB unprotected objects */
1848  ! RB_TYPE_P(obj, T_ARRAY)) { /* array.c assumes that allocated objects are new */
1849  if (--newobj_cnt == 0) {
1850  newobj_cnt = RGENGC_OLD_NEWOBJ_CHECK;
1851 
1852  gc_mark_set(objspace, obj);
1853  RVALUE_AGE_SET_OLD(objspace, obj);
1854 
1856  }
1857  }
1858  }
1859 #endif
1861  return obj;
1862 }
1863 
1864 static inline VALUE
1865 newobj_slowpath(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace, int wb_protected)
1866 {
1867  VALUE obj;
1868 
1870  if (during_gc) {
1871  dont_gc = 1;
1872  during_gc = 0;
1873  rb_bug("object allocation during garbage collection phase");
1874  }
1875 
1876  if (ruby_gc_stressful) {
1877  if (!garbage_collect(objspace, FALSE, FALSE, FALSE, GPR_FLAG_NEWOBJ)) {
1878  rb_memerror();
1879  }
1880  }
1881  }
1882 
1883  obj = heap_get_freeobj(objspace, heap_eden);
1884  newobj_init(klass, flags, v1, v2, v3, wb_protected, objspace, obj);
1885  gc_event_hook(objspace, RUBY_INTERNAL_EVENT_NEWOBJ, obj);
1886  return obj;
1887 }
1888 
1889 NOINLINE(static VALUE newobj_slowpath_wb_protected(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace));
1890 NOINLINE(static VALUE newobj_slowpath_wb_unprotected(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace));
1891 
1892 static VALUE
1894 {
1895  return newobj_slowpath(klass, flags, v1, v2, v3, objspace, TRUE);
1896 }
1897 
1898 static VALUE
1900 {
1901  return newobj_slowpath(klass, flags, v1, v2, v3, objspace, FALSE);
1902 }
1903 
1904 static inline VALUE
1905 newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, int wb_protected)
1906 {
1907  rb_objspace_t *objspace = &rb_objspace;
1908  VALUE obj;
1909 
1910 #if GC_DEBUG_STRESS_TO_CLASS
1911  if (UNLIKELY(stress_to_class)) {
1912  long i, cnt = RARRAY_LEN(stress_to_class);
1913  const VALUE *ptr = RARRAY_CONST_PTR(stress_to_class);
1914  for (i = 0; i < cnt; ++i) {
1915  if (klass == ptr[i]) rb_memerror();
1916  }
1917  }
1918 #endif
1919  if (!(during_gc ||
1921  gc_event_hook_available_p(objspace)) &&
1922  (obj = heap_get_freeobj_head(objspace, heap_eden)) != Qfalse) {
1923  return newobj_init(klass, flags, v1, v2, v3, wb_protected, objspace, obj);
1924  }
1925  else {
1926  return wb_protected ?
1927  newobj_slowpath_wb_protected(klass, flags, v1, v2, v3, objspace) :
1928  newobj_slowpath_wb_unprotected(klass, flags, v1, v2, v3, objspace);
1929  }
1930 }
1931 
1932 VALUE
1934 {
1935  if (RGENGC_CHECK_MODE > 0) assert((flags & FL_WB_PROTECTED) == 0);
1936  return newobj_of(klass, flags, 0, 0, 0, FALSE);
1937 }
1938 
1939 VALUE
1941 {
1942  if (RGENGC_CHECK_MODE > 0) assert((flags & FL_WB_PROTECTED) == 0);
1943  return newobj_of(klass, flags, 0, 0, 0, TRUE);
1944 }
1945 
1946 /* for compatibility */
1947 
1948 VALUE
1950 {
1951  return newobj_of(0, T_NONE, 0, 0, 0, FALSE);
1952 }
1953 
1954 VALUE
1955 rb_newobj_of(VALUE klass, VALUE flags)
1956 {
1957  return newobj_of(klass, flags & ~FL_WB_PROTECTED, 0, 0, 0, flags & FL_WB_PROTECTED);
1958 }
1959 
1960 NODE*
1962 {
1963  NODE *n = (NODE *)newobj_of(0, T_NODE, a0, a1, a2, FALSE); /* TODO: node also should be wb protected */
1964  nd_set_type(n, type);
1965  return n;
1966 }
1967 
1968 #undef rb_imemo_new
1969 
1970 VALUE
1971 rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
1972 {
1973  VALUE flags = T_IMEMO | (type << FL_USHIFT);
1974  return newobj_of(v0, flags, v1, v2, v3, TRUE);
1975 }
1976 
1977 #if IMEMO_DEBUG
1978 VALUE
1979 rb_imemo_new_debug(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0, const char *file, int line)
1980 {
1981  VALUE memo = rb_imemo_new(type, v1, v2, v3, v0);
1982  fprintf(stderr, "memo %p (type: %d) @ %s:%d\n", memo, imemo_type(memo), file, line);
1983  return memo;
1984 }
1985 #endif
1986 
1987 VALUE
1988 rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
1989 {
1990  if (klass) Check_Type(klass, T_CLASS);
1991  return newobj_of(klass, T_DATA, (VALUE)dmark, (VALUE)dfree, (VALUE)datap, FALSE);
1992 }
1993 
1994 #undef rb_data_object_alloc
1996  RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree),
1997  rb_data_object_wrap, (klass, datap, dmark, dfree))
1998 
1999 
2000 VALUE
2001 rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
2002 {
2003  VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
2004  DATA_PTR(obj) = xcalloc(1, size);
2005  return obj;
2006 }
2007 
2008 VALUE
2009 rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
2010 {
2011  if (klass) Check_Type(klass, T_CLASS);
2012  return newobj_of(klass, T_DATA, (VALUE)type, (VALUE)1, (VALUE)datap, type->flags & RUBY_FL_WB_PROTECTED);
2013 }
2014 
2015 #undef rb_data_typed_object_alloc
2017  const rb_data_type_t *type),
2018  rb_data_typed_object_wrap, (klass, datap, type))
2019 
2020 VALUE
2021 rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
2022 {
2023  VALUE obj = rb_data_typed_object_wrap(klass, 0, type);
2024  DATA_PTR(obj) = xcalloc(1, size);
2025  return obj;
2026 }
2027 
2028 size_t
2030 {
2031  if (RTYPEDDATA_P(obj)) {
2032  const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
2033  const void *ptr = RTYPEDDATA_DATA(obj);
2034  if (ptr && type->function.dsize) {
2035  return type->function.dsize(ptr);
2036  }
2037  }
2038  return 0;
2039 }
2040 
2041 const char *
2043 {
2044  if (RTYPEDDATA_P(obj)) {
2045  return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
2046  }
2047  else {
2048  return 0;
2049  }
2050 }
2051 
2052 PUREFUNC(static inline int is_pointer_to_heap(rb_objspace_t *objspace, void *ptr);)
2053 static inline int
2054 is_pointer_to_heap(rb_objspace_t *objspace, void *ptr)
2055 {
2056  register RVALUE *p = RANY(ptr);
2057  register struct heap_page *page;
2058  register size_t hi, lo, mid;
2059 
2060  if (p < heap_pages_lomem || p > heap_pages_himem) return FALSE;
2061  if ((VALUE)p % sizeof(RVALUE) != 0) return FALSE;
2062 
2063  /* check if p looks like a pointer using bsearch*/
2064  lo = 0;
2065  hi = heap_allocated_pages;
2066  while (lo < hi) {
2067  mid = (lo + hi) / 2;
2068  page = heap_pages_sorted[mid];
2069  if (page->start <= p) {
2070  if (p < page->start + page->total_slots) {
2071  return TRUE;
2072  }
2073  lo = mid + 1;
2074  }
2075  else {
2076  hi = mid;
2077  }
2078  }
2079  return FALSE;
2080 }
2081 
2082 static enum rb_id_table_iterator_result
2083 free_const_entry_i(VALUE value, void *data)
2084 {
2085  rb_const_entry_t *ce = (rb_const_entry_t *)value;
2086  xfree(ce);
2087  return ID_TABLE_CONTINUE;
2088 }
2089 
2090 void
2091 rb_free_const_table(struct rb_id_table *tbl)
2092 {
2094  rb_id_table_free(tbl);
2095 }
2096 
2097 static inline void
2098 make_zombie(rb_objspace_t *objspace, VALUE obj, void (*dfree)(void *), void *data)
2099 {
2100  struct RZombie *zombie = RZOMBIE(obj);
2101  zombie->basic.flags = T_ZOMBIE;
2102  zombie->dfree = dfree;
2103  zombie->data = data;
2104  zombie->next = heap_pages_deferred_final;
2105  heap_pages_deferred_final = (VALUE)zombie;
2106 }
2107 
2108 static inline void
2110 {
2111  rb_io_t *fptr = RANY(obj)->as.file.fptr;
2112  make_zombie(objspace, obj, (void (*)(void*))rb_io_fptr_finalize, fptr);
2113 }
2114 
2115 static int
2117 {
2119 
2120  switch (BUILTIN_TYPE(obj)) {
2121  case T_NIL:
2122  case T_FIXNUM:
2123  case T_TRUE:
2124  case T_FALSE:
2125  rb_bug("obj_free() called for broken object");
2126  break;
2127  }
2128 
2129  if (FL_TEST(obj, FL_EXIVAR)) {
2131  FL_UNSET(obj, FL_EXIVAR);
2132  }
2133 
2134 #if USE_RGENGC
2136 
2137 #if RGENGC_CHECK_MODE
2138 #define CHECK(x) if (x(obj) != FALSE) rb_bug("obj_free: " #x "(%s) != FALSE", obj_info(obj))
2143 #undef CHECK
2144 #endif
2145 #endif
2146 
2147  switch (BUILTIN_TYPE(obj)) {
2148  case T_OBJECT:
2149  if (!(RANY(obj)->as.basic.flags & ROBJECT_EMBED) &&
2150  RANY(obj)->as.object.as.heap.ivptr) {
2151  xfree(RANY(obj)->as.object.as.heap.ivptr);
2152  }
2153  break;
2154  case T_MODULE:
2155  case T_CLASS:
2157  if (RCLASS_IV_TBL(obj)) {
2159  }
2160  if (RCLASS_CONST_TBL(obj)) {
2162  }
2163  if (RCLASS_IV_INDEX_TBL(obj)) {
2165  }
2166  if (RCLASS_EXT(obj)->subclasses) {
2167  if (BUILTIN_TYPE(obj) == T_MODULE) {
2169  }
2170  else {
2172  }
2173  RCLASS_EXT(obj)->subclasses = NULL;
2174  }
2177  if (RANY(obj)->as.klass.ptr)
2178  xfree(RANY(obj)->as.klass.ptr);
2179  RANY(obj)->as.klass.ptr = NULL;
2180  break;
2181  case T_STRING:
2182  rb_str_free(obj);
2183  break;
2184  case T_ARRAY:
2185  rb_ary_free(obj);
2186  break;
2187  case T_HASH:
2188  if (RANY(obj)->as.hash.ntbl) {
2189  st_free_table(RANY(obj)->as.hash.ntbl);
2190  }
2191  break;
2192  case T_REGEXP:
2193  if (RANY(obj)->as.regexp.ptr) {
2194  onig_free(RANY(obj)->as.regexp.ptr);
2195  }
2196  break;
2197  case T_DATA:
2198  if (DATA_PTR(obj)) {
2199  int free_immediately = FALSE;
2200  void (*dfree)(void *);
2201  void *data = DATA_PTR(obj);
2202 
2203  if (RTYPEDDATA_P(obj)) {
2204  free_immediately = (RANY(obj)->as.typeddata.type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
2205  dfree = RANY(obj)->as.typeddata.type->function.dfree;
2206  if (0 && free_immediately == 0) {
2207  /* to expose non-free-immediate T_DATA */
2208  fprintf(stderr, "not immediate -> %s\n", RANY(obj)->as.typeddata.type->wrap_struct_name);
2209  }
2210  }
2211  else {
2212  dfree = RANY(obj)->as.data.dfree;
2213  }
2214 
2215  if (dfree) {
2216  if (dfree == RUBY_DEFAULT_FREE) {
2217  xfree(data);
2218  }
2219  else if (free_immediately) {
2220  (*dfree)(data);
2221  }
2222  else {
2223  make_zombie(objspace, obj, dfree, data);
2224  return 1;
2225  }
2226  }
2227  }
2228  break;
2229  case T_MATCH:
2230  if (RANY(obj)->as.match.rmatch) {
2231  struct rmatch *rm = RANY(obj)->as.match.rmatch;
2232  onig_region_free(&rm->regs, 0);
2233  if (rm->char_offset)
2234  xfree(rm->char_offset);
2235  xfree(rm);
2236  }
2237  break;
2238  case T_FILE:
2239  if (RANY(obj)->as.file.fptr) {
2240  make_io_zombie(objspace, obj);
2241  return 1;
2242  }
2243  break;
2244  case T_RATIONAL:
2245  case T_COMPLEX:
2246  break;
2247  case T_ICLASS:
2248  /* Basically , T_ICLASS shares table with the module */
2249  if (FL_TEST(obj, RICLASS_IS_ORIGIN)) {
2251  }
2252  if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
2254  }
2255  if (RCLASS_EXT(obj)->subclasses) {
2257  RCLASS_EXT(obj)->subclasses = NULL;
2258  }
2261  xfree(RANY(obj)->as.klass.ptr);
2262  RANY(obj)->as.klass.ptr = NULL;
2263  break;
2264 
2265  case T_FLOAT:
2266  break;
2267 
2268  case T_BIGNUM:
2269  if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2270  xfree(BIGNUM_DIGITS(obj));
2271  }
2272  break;
2273 
2274  case T_NODE:
2275  rb_gc_free_node(obj);
2276  break; /* no need to free iv_tbl */
2277 
2278  case T_STRUCT:
2279  if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2280  RANY(obj)->as.rstruct.as.heap.ptr) {
2281  xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
2282  }
2283  break;
2284 
2285  case T_SYMBOL:
2286  {
2287  rb_gc_free_dsymbol(obj);
2288  }
2289  break;
2290 
2291  case T_IMEMO:
2292  switch (imemo_type(obj)) {
2293  case imemo_ment:
2294  rb_free_method_entry(&RANY(obj)->as.imemo.ment);
2295  break;
2296  case imemo_iseq:
2297  rb_iseq_free(&RANY(obj)->as.imemo.iseq);
2298  break;
2299  case imemo_env:
2300  VM_ASSERT(VM_ENV_ESCAPED_P(RANY(obj)->as.imemo.env.ep));
2301  xfree((VALUE *)RANY(obj)->as.imemo.env.env);
2302  break;
2303  default:
2304  break;
2305  }
2306  return 0;
2307 
2308  default:
2309  rb_bug("gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
2310  BUILTIN_TYPE(obj), (void*)obj, RBASIC(obj)->flags);
2311  }
2312 
2313  if (FL_TEST(obj, FL_FINALIZE)) {
2314  make_zombie(objspace, obj, 0, 0);
2315  return 1;
2316  }
2317  else {
2318  return 0;
2319  }
2320 }
2321 
2322 void
2324 {
2325  rb_objspace_t *objspace = &rb_objspace;
2326 
2328 
2329 #if RGENGC_ESTIMATE_OLDMALLOC
2330  objspace->rgengc.oldmalloc_increase_limit = gc_params.oldmalloc_limit_min;
2331 #endif
2332 
2334  init_mark_stack(&objspace->mark_stack);
2335 
2336 #ifdef USE_SIGALTSTACK
2337  {
2338  /* altstack of another threads are allocated in another place */
2339  rb_thread_t *th = GET_THREAD();
2340  void *tmp = th->altstack;
2341  th->altstack = malloc(rb_sigaltstack_size());
2342  free(tmp); /* free previously allocated area */
2343  }
2344 #endif
2345 
2346  objspace->profile.invoke_time = getrusage_time();
2348 }
2349 
2350 typedef int each_obj_callback(void *, void *, size_t, void *);
2351 
2354  void *data;
2355 };
2356 
2357 static VALUE
2359 {
2360  size_t i;
2361  struct heap_page *page;
2362  RVALUE *pstart = NULL, *pend;
2363  rb_objspace_t *objspace = &rb_objspace;
2364  struct each_obj_args *args = (struct each_obj_args *)arg;
2365 
2366  i = 0;
2367  while (i < heap_allocated_pages) {
2368  while (0 < i && pstart < heap_pages_sorted[i-1]->start) i--;
2369  while (i < heap_allocated_pages && heap_pages_sorted[i]->start <= pstart) i++;
2370  if (heap_allocated_pages <= i) break;
2371 
2372  page = heap_pages_sorted[i];
2373 
2374  pstart = page->start;
2375  pend = pstart + page->total_slots;
2376 
2377  if ((*args->callback)(pstart, pend, sizeof(RVALUE), args->data)) {
2378  break;
2379  }
2380  }
2381 
2382  return Qnil;
2383 }
2384 
2385 static VALUE
2387 {
2388  rb_objspace_t *objspace = &rb_objspace;
2389 
2390  objspace->flags.dont_incremental = FALSE;
2391  return Qnil;
2392 }
2393 
2394 /*
2395  * rb_objspace_each_objects() is special C API to walk through
2396  * Ruby object space. This C API is too difficult to use it.
2397  * To be frank, you should not use it. Or you need to read the
2398  * source code of this function and understand what this function does.
2399  *
2400  * 'callback' will be called several times (the number of heap page,
2401  * at current implementation) with:
2402  * vstart: a pointer to the first living object of the heap_page.
2403  * vend: a pointer to next to the valid heap_page area.
2404  * stride: a distance to next VALUE.
2405  *
2406  * If callback() returns non-zero, the iteration will be stopped.
2407  *
2408  * This is a sample callback code to iterate liveness objects:
2409  *
2410  * int
2411  * sample_callback(void *vstart, void *vend, int stride, void *data) {
2412  * VALUE v = (VALUE)vstart;
2413  * for (; v != (VALUE)vend; v += stride) {
2414  * if (RBASIC(v)->flags) { // liveness check
2415  * // do something with live object 'v'
2416  * }
2417  * return 0; // continue to iteration
2418  * }
2419  *
2420  * Note: 'vstart' is not a top of heap_page. This point the first
2421  * living object to grasp at least one object to avoid GC issue.
2422  * This means that you can not walk through all Ruby object page
2423  * including freed object page.
2424  *
2425  * Note: On this implementation, 'stride' is same as sizeof(RVALUE).
2426  * However, there are possibilities to pass variable values with
2427  * 'stride' with some reasons. You must use stride instead of
2428  * use some constant value in the iteration.
2429  */
2430 void
2432 {
2433  struct each_obj_args args;
2434  rb_objspace_t *objspace = &rb_objspace;
2435  int prev_dont_incremental = objspace->flags.dont_incremental;
2436 
2437  gc_rest(objspace);
2438  objspace->flags.dont_incremental = TRUE;
2439 
2440  args.callback = callback;
2441  args.data = data;
2442 
2443  if (prev_dont_incremental) {
2444  objspace_each_objects((VALUE)&args);
2445  }
2446  else {
2448  }
2449 }
2450 
2451 void
2453 {
2454  struct each_obj_args args;
2455  args.callback = callback;
2456  args.data = data;
2457 
2458  objspace_each_objects((VALUE)&args);
2459 }
2460 
2462  size_t num;
2464 };
2465 
2466 static int
2468 {
2469  RVALUE *p = (RVALUE *)obj;
2470 
2471  if (p->as.basic.flags) {
2472  switch (BUILTIN_TYPE(p)) {
2473  case T_NONE:
2474  case T_IMEMO:
2475  case T_ICLASS:
2476  case T_NODE:
2477  case T_ZOMBIE:
2478  break;
2479  case T_CLASS:
2480  if (!p->as.basic.klass) break;
2481  if (FL_TEST(obj, FL_SINGLETON)) {
2482  return rb_singleton_class_internal_p(obj);
2483  }
2484  return 0;
2485  default:
2486  if (!p->as.basic.klass) break;
2487  return 0;
2488  }
2489  }
2490  return 1;
2491 }
2492 
2493 int
2495 {
2496  return internal_object_p(obj);
2497 }
2498 
2499 static int
2500 os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
2501 {
2502  struct os_each_struct *oes = (struct os_each_struct *)data;
2503  RVALUE *p = (RVALUE *)vstart, *pend = (RVALUE *)vend;
2504 
2505  for (; p != pend; p++) {
2506  volatile VALUE v = (VALUE)p;
2507  if (!internal_object_p(v)) {
2508  if (!oes->of || rb_obj_is_kind_of(v, oes->of)) {
2509  rb_yield(v);
2510  oes->num++;
2511  }
2512  }
2513  }
2514 
2515  return 0;
2516 }
2517 
2518 static VALUE
2520 {
2521  struct os_each_struct oes;
2522 
2523  oes.num = 0;
2524  oes.of = of;
2526  return SIZET2NUM(oes.num);
2527 }
2528 
2529 /*
2530  * call-seq:
2531  * ObjectSpace.each_object([module]) {|obj| ... } -> integer
2532  * ObjectSpace.each_object([module]) -> an_enumerator
2533  *
2534  * Calls the block once for each living, nonimmediate object in this
2535  * Ruby process. If <i>module</i> is specified, calls the block
2536  * for only those classes or modules that match (or are a subclass of)
2537  * <i>module</i>. Returns the number of objects found. Immediate
2538  * objects (<code>Fixnum</code>s, <code>Symbol</code>s
2539  * <code>true</code>, <code>false</code>, and <code>nil</code>) are
2540  * never returned. In the example below, <code>each_object</code>
2541  * returns both the numbers we defined and several constants defined in
2542  * the <code>Math</code> module.
2543  *
2544  * If no block is given, an enumerator is returned instead.
2545  *
2546  * a = 102.7
2547  * b = 95 # Won't be returned
2548  * c = 12345678987654321
2549  * count = ObjectSpace.each_object(Numeric) {|x| p x }
2550  * puts "Total count: #{count}"
2551  *
2552  * <em>produces:</em>
2553  *
2554  * 12345678987654321
2555  * 102.7
2556  * 2.71828182845905
2557  * 3.14159265358979
2558  * 2.22044604925031e-16
2559  * 1.7976931348623157e+308
2560  * 2.2250738585072e-308
2561  * Total count: 7
2562  *
2563  */
2564 
2565 static VALUE
2567 {
2568  VALUE of;
2569 
2570  if (argc == 0) {
2571  of = 0;
2572  }
2573  else {
2574  rb_scan_args(argc, argv, "01", &of);
2575  }
2576  RETURN_ENUMERATOR(os, 1, &of);
2577  return os_obj_of(of);
2578 }
2579 
2580 /*
2581  * call-seq:
2582  * ObjectSpace.undefine_finalizer(obj)
2583  *
2584  * Removes all finalizers for <i>obj</i>.
2585  *
2586  */
2587 
2588 static VALUE
2590 {
2591  return rb_undefine_finalizer(obj);
2592 }
2593 
2594 VALUE
2596 {
2597  rb_objspace_t *objspace = &rb_objspace;
2598  st_data_t data = obj;
2599  rb_check_frozen(obj);
2600  st_delete(finalizer_table, &data, 0);
2601  FL_UNSET(obj, FL_FINALIZE);
2602  return obj;
2603 }
2604 
2605 static void
2607 {
2608  if (!rb_obj_respond_to(block, rb_intern("call"), TRUE)) {
2609  rb_raise(rb_eArgError, "wrong type argument %"PRIsVALUE" (should be callable)",
2610  rb_obj_class(block));
2611  }
2612 }
2613 static void
2615 {
2616  if (!FL_ABLE(obj)) {
2617  rb_raise(rb_eArgError, "cannot define finalizer for %s",
2618  rb_obj_classname(obj));
2619  }
2620  rb_check_frozen(obj);
2621 }
2622 
2623 /*
2624  * call-seq:
2625  * ObjectSpace.define_finalizer(obj, aProc=proc())
2626  *
2627  * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
2628  * was destroyed. The object ID of the <i>obj</i> will be passed
2629  * as an argument to <i>aProc</i>. If <i>aProc</i> is a lambda or
2630  * method, make sure it can be called with a single argument.
2631  *
2632  */
2633 
2634 static VALUE
2636 {
2637  VALUE obj, block;
2638 
2639  rb_scan_args(argc, argv, "11", &obj, &block);
2640  should_be_finalizable(obj);
2641  if (argc == 1) {
2642  block = rb_block_proc();
2643  }
2644  else {
2645  should_be_callable(block);
2646  }
2647 
2648  return define_final0(obj, block);
2649 }
2650 
2651 static VALUE
2653 {
2654  rb_objspace_t *objspace = &rb_objspace;
2655  VALUE table;
2656  st_data_t data;
2657 
2658  RBASIC(obj)->flags |= FL_FINALIZE;
2659 
2660  block = rb_ary_new3(2, INT2FIX(rb_safe_level()), block);
2661  OBJ_FREEZE(block);
2662 
2663  if (st_lookup(finalizer_table, obj, &data)) {
2664  table = (VALUE)data;
2665 
2666  /* avoid duplicate block, table is usually small */
2667  {
2668  const VALUE *ptr = RARRAY_CONST_PTR(table);
2669  long len = RARRAY_LEN(table);
2670  long i;
2671 
2672  for (i = 0; i < len; i++, ptr++) {
2673  if (rb_funcall(*ptr, idEq, 1, block)) {
2674  return *ptr;
2675  }
2676  }
2677  }
2678 
2679  rb_ary_push(table, block);
2680  }
2681  else {
2682  table = rb_ary_new3(1, block);
2683  RBASIC_CLEAR_CLASS(table);
2684  st_add_direct(finalizer_table, obj, table);
2685  }
2686  return block;
2687 }
2688 
2689 VALUE
2691 {
2692  should_be_finalizable(obj);
2693  should_be_callable(block);
2694  return define_final0(obj, block);
2695 }
2696 
2697 void
2699 {
2700  rb_objspace_t *objspace = &rb_objspace;
2701  VALUE table;
2702  st_data_t data;
2703 
2704  if (!FL_TEST(obj, FL_FINALIZE)) return;
2705  if (st_lookup(finalizer_table, obj, &data)) {
2706  table = (VALUE)data;
2707  st_insert(finalizer_table, dest, table);
2708  }
2709  FL_SET(dest, FL_FINALIZE);
2710 }
2711 
2712 static VALUE
2714 {
2715  const VALUE cmd = RARRAY_AREF(final, 1);
2716  const int level = OBJ_TAINTED(cmd) ?
2718 
2719  rb_set_safe_level_force(level);
2720  return rb_check_funcall(cmd, idCall, 1, &objid);
2721 }
2722 
2723 static void
2725 {
2726  long i;
2727  int status;
2728  volatile struct {
2729  VALUE errinfo;
2730  VALUE objid;
2731  rb_control_frame_t *cfp;
2732  long finished;
2733  int safe;
2734  } saved;
2735  rb_thread_t *const th = GET_THREAD();
2736 #define RESTORE_FINALIZER() (\
2737  th->cfp = saved.cfp, \
2738  rb_set_safe_level_force(saved.safe), \
2739  rb_set_errinfo(saved.errinfo))
2740 
2741  saved.safe = rb_safe_level();
2742  saved.errinfo = rb_errinfo();
2743  saved.objid = nonspecial_obj_id(obj);
2744  saved.cfp = th->cfp;
2745  saved.finished = 0;
2746 
2747  TH_PUSH_TAG(th);
2748  status = TH_EXEC_TAG();
2749  if (status) {
2750  ++saved.finished; /* skip failed finalizer */
2751  }
2752  for (i = saved.finished;
2753  RESTORE_FINALIZER(), i<RARRAY_LEN(table);
2754  saved.finished = ++i) {
2755  run_single_final(RARRAY_AREF(table, i), saved.objid);
2756  }
2757  TH_POP_TAG();
2758 #undef RESTORE_FINALIZER
2759 }
2760 
2761 static void
2762 run_final(rb_objspace_t *objspace, VALUE zombie)
2763 {
2764  st_data_t key, table;
2765 
2766  if (RZOMBIE(zombie)->dfree) {
2767  RZOMBIE(zombie)->dfree(RZOMBIE(zombie)->data);
2768  }
2769 
2770  key = (st_data_t)zombie;
2771  if (st_delete(finalizer_table, &key, &table)) {
2772  run_finalizer(objspace, zombie, (VALUE)table);
2773  }
2774 }
2775 
2776 static void
2778 {
2779  while (zombie) {
2780  VALUE next_zombie = RZOMBIE(zombie)->next;
2781  struct heap_page *page = GET_HEAP_PAGE(zombie);
2782 
2783  run_final(objspace, zombie);
2784 
2785  RZOMBIE(zombie)->basic.flags = 0;
2787  page->final_slots--;
2788  page->free_slots++;
2789  heap_page_add_freeobj(objspace, GET_HEAP_PAGE(zombie), zombie);
2790 
2791  objspace->profile.total_freed_objects++;
2792 
2793  zombie = next_zombie;
2794  }
2795 }
2796 
2797 static void
2799 {
2800  VALUE zombie;
2801 
2802  while ((zombie = ATOMIC_VALUE_EXCHANGE(heap_pages_deferred_final, 0)) != 0) {
2803  finalize_list(objspace, zombie);
2804  }
2805 }
2806 
2807 static void
2809 {
2810  rb_objspace_t *objspace = dmy;
2811  if (ATOMIC_EXCHANGE(finalizing, 1)) return;
2812  finalize_deferred(objspace);
2813  ATOMIC_SET(finalizing, 0);
2814 }
2815 
2816 /* TODO: to keep compatibility, maybe unused. */
2817 void
2819 {
2821 }
2822 
2823 static void
2825 {
2826  if (rb_postponed_job_register_one(0, gc_finalize_deferred, objspace) == 0) {
2827  rb_bug("gc_finalize_deferred_register: can't register finalizer.");
2828  }
2829 }
2830 
2835 };
2836 
2837 static int
2839 {
2840  struct force_finalize_list **prev = (struct force_finalize_list **)arg;
2841  struct force_finalize_list *curr = ALLOC(struct force_finalize_list);
2842  curr->obj = key;
2843  curr->table = val;
2844  curr->next = *prev;
2845  *prev = curr;
2846  return ST_CONTINUE;
2847 }
2848 
2849 void
2851 {
2852 #if RGENGC_CHECK_MODE >= 2
2854 #endif
2855  rb_objspace_call_finalizer(&rb_objspace);
2856 }
2857 
2858 static void
2860 {
2861  RVALUE *p, *pend;
2862  size_t i;
2863 
2864  gc_rest(objspace);
2865 
2866  if (ATOMIC_EXCHANGE(finalizing, 1)) return;
2867 
2868  /* run finalizers */
2869  finalize_deferred(objspace);
2871 
2872  gc_rest(objspace);
2873  /* prohibit incremental GC */
2874  objspace->flags.dont_incremental = 1;
2875 
2876  /* force to run finalizer */
2877  while (finalizer_table->num_entries) {
2878  struct force_finalize_list *list = 0;
2880  while (list) {
2881  struct force_finalize_list *curr = list;
2882  st_data_t obj = (st_data_t)curr->obj;
2883  run_finalizer(objspace, curr->obj, curr->table);
2884  st_delete(finalizer_table, &obj, 0);
2885  list = curr->next;
2886  xfree(curr);
2887  }
2888  }
2889 
2890  /* prohibit GC because force T_DATA finalizers can break an object graph consistency */
2891  dont_gc = 1;
2892 
2893  /* running data/file finalizers are part of garbage collection */
2894  gc_enter(objspace, "rb_objspace_call_finalizer");
2895 
2896  /* run data/file object's finalizers */
2897  for (i = 0; i < heap_allocated_pages; i++) {
2898  p = heap_pages_sorted[i]->start; pend = p + heap_pages_sorted[i]->total_slots;
2899  while (p < pend) {
2900  switch (BUILTIN_TYPE(p)) {
2901  case T_DATA:
2902  if (!DATA_PTR(p) || !RANY(p)->as.data.dfree) break;
2903  if (rb_obj_is_thread((VALUE)p)) break;
2904  if (rb_obj_is_mutex((VALUE)p)) break;
2905  if (rb_obj_is_fiber((VALUE)p)) break;
2906  p->as.free.flags = 0;
2907  if (RTYPEDDATA_P(p)) {
2908  RDATA(p)->dfree = RANY(p)->as.typeddata.type->function.dfree;
2909  }
2910  if (RANY(p)->as.data.dfree == (RUBY_DATA_FUNC)-1) {
2911  xfree(DATA_PTR(p));
2912  }
2913  else if (RANY(p)->as.data.dfree) {
2914  make_zombie(objspace, (VALUE)p, RANY(p)->as.data.dfree, RANY(p)->as.data.data);
2915  }
2916  break;
2917  case T_FILE:
2918  if (RANY(p)->as.file.fptr) {
2919  make_io_zombie(objspace, (VALUE)p);
2920  }
2921  break;
2922  }
2923  p++;
2924  }
2925  }
2926 
2927  gc_exit(objspace, "rb_objspace_call_finalizer");
2928 
2931  }
2932 
2934  finalizer_table = 0;
2935  ATOMIC_SET(finalizing, 0);
2936 }
2937 
2938 PUREFUNC(static inline int is_id_value(rb_objspace_t *objspace, VALUE ptr));
2939 static inline int
2941 {
2942  if (!is_pointer_to_heap(objspace, (void *)ptr)) return FALSE;
2943  if (BUILTIN_TYPE(ptr) > T_FIXNUM) return FALSE;
2944  if (BUILTIN_TYPE(ptr) == T_ICLASS) return FALSE;
2945  return TRUE;
2946 }
2947 
2948 static inline int
2950 {
2951  struct heap_page *page = GET_HEAP_PAGE(ptr);
2952  return page->flags.before_sweep ? FALSE : TRUE;
2953 }
2954 
2955 static inline int
2957 {
2958  if (heap_is_swept_object(objspace, heap_eden, ptr)) {
2959  return TRUE;
2960  }
2961  else {
2962  return FALSE;
2963  }
2964 }
2965 
2966 /* garbage objects will be collected soon. */
2967 static inline int
2969 {
2970  if (!is_lazy_sweeping(heap_eden) ||
2971  is_swept_object(objspace, ptr) ||
2972  MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(ptr), ptr)) {
2973 
2974  return FALSE;
2975  }
2976  else {
2977  return TRUE;
2978  }
2979 }
2980 
2981 static inline int
2983 {
2984  switch (BUILTIN_TYPE(ptr)) {
2985  case T_NONE:
2986  case T_ZOMBIE:
2987  return FALSE;
2988  }
2989 
2990  if (!is_garbage_object(objspace, ptr)) {
2991  return TRUE;
2992  }
2993  else {
2994  return FALSE;
2995  }
2996 }
2997 
2998 static inline int
3000 {
3001  if (rb_special_const_p(obj)) return FALSE; /* special const is not markable */
3003  return TRUE;
3004 }
3005 
3006 int
3008 {
3009  rb_objspace_t *objspace = &rb_objspace;
3010  return is_markable_object(objspace, obj) && is_live_object(objspace, obj);
3011 }
3012 
3013 int
3015 {
3016  rb_objspace_t *objspace = &rb_objspace;
3017  return is_garbage_object(objspace, obj);
3018 }
3019 
3020 /*
3021  * call-seq:
3022  * ObjectSpace._id2ref(object_id) -> an_object
3023  *
3024  * Converts an object id to a reference to the object. May not be
3025  * called on an object id passed as a parameter to a finalizer.
3026  *
3027  * s = "I am a string" #=> "I am a string"
3028  * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
3029  * r == s #=> true
3030  *
3031  */
3032 
3033 static VALUE
3034 id2ref(VALUE obj, VALUE objid)
3035 {
3036 #if SIZEOF_LONG == SIZEOF_VOIDP
3037 #define NUM2PTR(x) NUM2ULONG(x)
3038 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
3039 #define NUM2PTR(x) NUM2ULL(x)
3040 #endif
3041  rb_objspace_t *objspace = &rb_objspace;
3042  VALUE ptr;
3043  void *p0;
3044 
3045  ptr = NUM2PTR(objid);
3046  p0 = (void *)ptr;
3047 
3048  if (ptr == Qtrue) return Qtrue;
3049  if (ptr == Qfalse) return Qfalse;
3050  if (ptr == Qnil) return Qnil;
3051  if (FIXNUM_P(ptr)) return (VALUE)ptr;
3052  if (FLONUM_P(ptr)) return (VALUE)ptr;
3053  ptr = obj_id_to_ref(objid);
3054 
3055  if ((ptr % sizeof(RVALUE)) == (4 << 2)) {
3056  ID symid = ptr / sizeof(RVALUE);
3057  if (rb_id2str(symid) == 0)
3058  rb_raise(rb_eRangeError, "%p is not symbol id value", p0);
3059  return ID2SYM(symid);
3060  }
3061 
3062  if (!is_id_value(objspace, ptr)) {
3063  rb_raise(rb_eRangeError, "%p is not id value", p0);
3064  }
3065  if (!is_live_object(objspace, ptr)) {
3066  rb_raise(rb_eRangeError, "%p is recycled object", p0);
3067  }
3068  if (RBASIC(ptr)->klass == 0) {
3069  rb_raise(rb_eRangeError, "%p is internal object", p0);
3070  }
3071  return (VALUE)ptr;
3072 }
3073 
3074 /*
3075  * Document-method: __id__
3076  * Document-method: object_id
3077  *
3078  * call-seq:
3079  * obj.__id__ -> integer
3080  * obj.object_id -> integer
3081  *
3082  * Returns an integer identifier for +obj+.
3083  *
3084  * The same number will be returned on all calls to +object_id+ for a given
3085  * object, and no two active objects will share an id.
3086  *
3087  * Note: that some objects of builtin classes are reused for optimization.
3088  * This is the case for immediate values and frozen string literals.
3089  *
3090  * Immediate values are not passed by reference but are passed by value:
3091  * +nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
3092  *
3093  * Object.new.object_id == Object.new.object_id # => false
3094  * (21 * 2).object_id == (21 * 2).object_id # => true
3095  * "hello".object_id == "hello".object_id # => false
3096  * "hi".freeze.object_id == "hi".freeze.object_id # => true
3097  */
3098 
3099 VALUE
3101 {
3102  /*
3103  * 32-bit VALUE space
3104  * MSB ------------------------ LSB
3105  * false 00000000000000000000000000000000
3106  * true 00000000000000000000000000000010
3107  * nil 00000000000000000000000000000100
3108  * undef 00000000000000000000000000000110
3109  * symbol ssssssssssssssssssssssss00001110
3110  * object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
3111  * fixnum fffffffffffffffffffffffffffffff1
3112  *
3113  * object_id space
3114  * LSB
3115  * false 00000000000000000000000000000000
3116  * true 00000000000000000000000000000010
3117  * nil 00000000000000000000000000000100
3118  * undef 00000000000000000000000000000110
3119  * symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
3120  * object oooooooooooooooooooooooooooooo0 o...o % A = 0
3121  * fixnum fffffffffffffffffffffffffffffff1 bignum if required
3122  *
3123  * where A = sizeof(RVALUE)/4
3124  *
3125  * sizeof(RVALUE) is
3126  * 20 if 32-bit, double is 4-byte aligned
3127  * 24 if 32-bit, double is 8-byte aligned
3128  * 40 if 64-bit
3129  */
3130  if (STATIC_SYM_P(obj)) {
3131  return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
3132  }
3133  else if (FLONUM_P(obj)) {
3134 #if SIZEOF_LONG == SIZEOF_VOIDP
3135  return LONG2NUM((SIGNED_VALUE)obj);
3136 #else
3137  return LL2NUM((SIGNED_VALUE)obj);
3138 #endif
3139  }
3140  else if (SPECIAL_CONST_P(obj)) {
3141  return LONG2NUM((SIGNED_VALUE)obj);
3142  }
3143  return nonspecial_obj_id(obj);
3144 }
3145 
3146 #include "regint.h"
3147 
3148 static size_t
3149 obj_memsize_of(VALUE obj, int use_all_types)
3150 {
3151  size_t size = 0;
3152 
3153  if (SPECIAL_CONST_P(obj)) {
3154  return 0;
3155  }
3156 
3157  if (FL_TEST(obj, FL_EXIVAR)) {
3158  size += rb_generic_ivar_memsize(obj);
3159  }
3160 
3161  switch (BUILTIN_TYPE(obj)) {
3162  case T_OBJECT:
3163  if (!(RBASIC(obj)->flags & ROBJECT_EMBED) &&
3164  ROBJECT(obj)->as.heap.ivptr) {
3165  size += ROBJECT(obj)->as.heap.numiv * sizeof(VALUE);
3166  }
3167  break;
3168  case T_MODULE:
3169  case T_CLASS:
3170  if (RCLASS_M_TBL(obj)) {
3171  size += rb_id_table_memsize(RCLASS_M_TBL(obj));
3172  }
3173  if (RCLASS_EXT(obj)) {
3174  if (RCLASS_IV_TBL(obj)) {
3175  size += st_memsize(RCLASS_IV_TBL(obj));
3176  }
3177  if (RCLASS_IV_INDEX_TBL(obj)) {
3178  size += st_memsize(RCLASS_IV_INDEX_TBL(obj));
3179  }
3180  if (RCLASS(obj)->ptr->iv_tbl) {
3181  size += st_memsize(RCLASS(obj)->ptr->iv_tbl);
3182  }
3183  if (RCLASS(obj)->ptr->const_tbl) {
3184  size += rb_id_table_memsize(RCLASS(obj)->ptr->const_tbl);
3185  }
3186  size += sizeof(rb_classext_t);
3187  }
3188  break;
3189  case T_ICLASS:
3190  if (FL_TEST(obj, RICLASS_IS_ORIGIN)) {
3191  if (RCLASS_M_TBL(obj)) {
3192  size += rb_id_table_memsize(RCLASS_M_TBL(obj));
3193  }
3194  }
3195  break;
3196  case T_STRING:
3197  size += rb_str_memsize(obj);
3198  break;
3199  case T_ARRAY:
3200  size += rb_ary_memsize(obj);
3201  break;
3202  case T_HASH:
3203  if (RHASH(obj)->ntbl) {
3204  size += st_memsize(RHASH(obj)->ntbl);
3205  }
3206  break;
3207  case T_REGEXP:
3208  if (RREGEXP_PTR(obj)) {
3209  size += onig_memsize(RREGEXP_PTR(obj));
3210  }
3211  break;
3212  case T_DATA:
3213  if (use_all_types) size += rb_objspace_data_type_memsize(obj);
3214  break;
3215  case T_MATCH:
3216  if (RMATCH(obj)->rmatch) {
3217  struct rmatch *rm = RMATCH(obj)->rmatch;
3218  size += onig_region_memsize(&rm->regs);
3219  size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated;
3220  size += sizeof(struct rmatch);
3221  }
3222  break;
3223  case T_FILE:
3224  if (RFILE(obj)->fptr) {
3225  size += rb_io_memsize(RFILE(obj)->fptr);
3226  }
3227  break;
3228  case T_RATIONAL:
3229  case T_COMPLEX:
3230  case T_IMEMO:
3231  break;
3232 
3233  case T_FLOAT:
3234  case T_SYMBOL:
3235  break;
3236 
3237  case T_BIGNUM:
3238  if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
3239  size += BIGNUM_LEN(obj) * sizeof(BDIGIT);
3240  }
3241  break;
3242 
3243  case T_NODE:
3244  if (use_all_types) size += rb_node_memsize(obj);
3245  break;
3246 
3247  case T_STRUCT:
3248  if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
3249  RSTRUCT(obj)->as.heap.ptr) {
3250  size += sizeof(VALUE) * RSTRUCT_LEN(obj);
3251  }
3252  break;
3253 
3254  case T_ZOMBIE:
3255  break;
3256 
3257  default:
3258  rb_bug("objspace/memsize_of(): unknown data type 0x%x(%p)",
3259  BUILTIN_TYPE(obj), (void*)obj);
3260  }
3261 
3262  return size + sizeof(RVALUE);
3263 }
3264 
3265 size_t
3267 {
3268  return obj_memsize_of(obj, TRUE);
3269 }
3270 
3271 static int
3273 {
3274  VALUE k = (VALUE)key;
3275  VALUE hash = (VALUE)arg;
3276  rb_hash_aset(hash, k, INT2FIX(0));
3277  return ST_CONTINUE;
3278 }
3279 
3280 /*
3281  * call-seq:
3282  * ObjectSpace.count_objects([result_hash]) -> hash
3283  *
3284  * Counts all objects grouped by type.
3285  *
3286  * It returns a hash, such as:
3287  * {
3288  * :TOTAL=>10000,
3289  * :FREE=>3011,
3290  * :T_OBJECT=>6,
3291  * :T_CLASS=>404,
3292  * # ...
3293  * }
3294  *
3295  * The contents of the returned hash are implementation specific.
3296  * It may be changed in future.
3297  *
3298  * The keys starting with +:T_+ means live objects.
3299  * For example, +:T_ARRAY+ is the number of arrays.
3300  * +:FREE+ means object slots which is not used now.
3301  * +:TOTAL+ means sum of above.
3302  *
3303  * If the optional argument +result_hash+ is given,
3304  * it is overwritten and returned. This is intended to avoid probe effect.
3305  *
3306  * h = {}
3307  * ObjectSpace.count_objects(h)
3308  * puts h
3309  * # => { :TOTAL=>10000, :T_CLASS=>158280, :T_MODULE=>20672, :T_STRING=>527249 }
3310  *
3311  * This method is only expected to work on C Ruby.
3312  *
3313  */
3314 
3315 static VALUE
3317 {
3318  rb_objspace_t *objspace = &rb_objspace;
3319  size_t counts[T_MASK+1];
3320  size_t freed = 0;
3321  size_t total = 0;
3322  size_t i;
3323  VALUE hash;
3324 
3325  if (rb_scan_args(argc, argv, "01", &hash) == 1) {
3326  if (!RB_TYPE_P(hash, T_HASH))
3327  rb_raise(rb_eTypeError, "non-hash given");
3328  }
3329 
3330  for (i = 0; i <= T_MASK; i++) {
3331  counts[i] = 0;
3332  }
3333 
3334  for (i = 0; i < heap_allocated_pages; i++) {
3335  struct heap_page *page = heap_pages_sorted[i];
3336  RVALUE *p, *pend;
3337 
3338  p = page->start; pend = p + page->total_slots;
3339  for (;p < pend; p++) {
3340  if (p->as.basic.flags) {
3341  counts[BUILTIN_TYPE(p)]++;
3342  }
3343  else {
3344  freed++;
3345  }
3346  }
3347  total += page->total_slots;
3348  }
3349 
3350  if (hash == Qnil) {
3351  hash = rb_hash_new();
3352  }
3353  else if (!RHASH_EMPTY_P(hash)) {
3354  st_foreach(RHASH_TBL_RAW(hash), set_zero, hash);
3355  }
3356  rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(total));
3357  rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), SIZET2NUM(freed));
3358 
3359  for (i = 0; i <= T_MASK; i++) {
3360  VALUE type;
3361  switch (i) {
3362 #define COUNT_TYPE(t) case (t): type = ID2SYM(rb_intern(#t)); break;
3363  COUNT_TYPE(T_NONE);
3371  COUNT_TYPE(T_HASH);
3374  COUNT_TYPE(T_FILE);
3375  COUNT_TYPE(T_DATA);
3379  COUNT_TYPE(T_NIL);
3380  COUNT_TYPE(T_TRUE);
3386  COUNT_TYPE(T_NODE);
3389 #undef COUNT_TYPE
3390  default: type = INT2NUM(i); break;
3391  }
3392  if (counts[i])
3393  rb_hash_aset(hash, type, SIZET2NUM(counts[i]));
3394  }
3395 
3396  return hash;
3397 }
3398 
3399 /*
3400  ------------------------ Garbage Collection ------------------------
3401 */
3402 
3403 /* Sweeping */
3404 
3405 static size_t
3407 {
3408  return heap_eden->total_slots + heap_tomb->total_slots;
3409 }
3410 
3411 static size_t
3413 {
3415 }
3416 
3417 static size_t
3419 {
3421 }
3422 
3423 static void
3425 {
3426 #if USE_RGENGC
3427  /* copy oldgen bitmap to mark bitmap */
3429 #else
3430  /* clear mark bitmap */
3431  memset(&page->mark_bits[0], 0, HEAP_PAGE_BITMAP_SIZE);
3432 #endif
3433 }
3434 
3435 static inline int
3436 gc_page_sweep(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *sweep_page)
3437 {
3438  int i;
3439  int empty_slots = 0, freed_slots = 0, final_slots = 0;
3440  RVALUE *p, *pend,*offset;
3441  bits_t *bits, bitset;
3442 
3443  gc_report(2, objspace, "page_sweep: start.\n");
3444 
3445  sweep_page->flags.before_sweep = FALSE;
3446 
3447  p = sweep_page->start; pend = p + sweep_page->total_slots;
3448  offset = p - NUM_IN_PAGE(p);
3449  bits = sweep_page->mark_bits;
3450 
3451  /* create guard : fill 1 out-of-range */
3452  bits[BITMAP_INDEX(p)] |= BITMAP_BIT(p)-1;
3453  bits[BITMAP_INDEX(pend)] |= ~(BITMAP_BIT(pend) - 1);
3454 
3455  for (i=0; i < HEAP_PAGE_BITMAP_LIMIT; i++) {
3456  bitset = ~bits[i];
3457  if (bitset) {
3458  p = offset + i * BITS_BITLENGTH;
3459  do {
3460  if (bitset & 1) {
3461  switch (BUILTIN_TYPE(p)) {
3462  default: { /* majority case */
3463  gc_report(2, objspace, "page_sweep: free %s\n", obj_info((VALUE)p));
3464 #if USE_RGENGC && RGENGC_CHECK_MODE
3465  if (!is_full_marking(objspace)) {
3466  if (RVALUE_OLD_P((VALUE)p)) rb_bug("page_sweep: %s - old while minor GC.", obj_info((VALUE)p));
3467  if (rgengc_remembered(objspace, (VALUE)p)) rb_bug("page_sweep: %s - remembered.", obj_info((VALUE)p));
3468  }
3469 #endif
3470  if (obj_free(objspace, (VALUE)p)) {
3471  final_slots++;
3472  }
3473  else {
3474  (void)VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
3475  heap_page_add_freeobj(objspace, sweep_page, (VALUE)p);
3476  gc_report(3, objspace, "page_sweep: %s is added to freelist\n", obj_info((VALUE)p));
3477  freed_slots++;
3478  }
3479  break;
3480  }
3481 
3482  /* minor cases */
3483  case T_ZOMBIE:
3484  /* already counted */
3485  break;
3486  case T_NONE:
3487  empty_slots++; /* already freed */
3488  break;
3489  }
3490  }
3491  p++;
3492  bitset >>= 1;
3493  } while (bitset);
3494  }
3495  }
3496 
3497  gc_setup_mark_bits(sweep_page);
3498 
3499 #if GC_PROFILE_MORE_DETAIL
3500  if (gc_prof_enabled(objspace)) {
3501  gc_profile_record *record = gc_prof_record(objspace);
3502  record->removing_objects += final_slots + freed_slots;
3503  record->empty_objects += empty_slots;
3504  }
3505 #endif
3506  if (0) fprintf(stderr, "gc_page_sweep(%d): total_slots: %d, freed_slots: %d, empty_slots: %d, final_slots: %d\n",
3507  (int)rb_gc_count(),
3508  (int)sweep_page->total_slots,
3509  freed_slots, empty_slots, final_slots);
3510 
3511  sweep_page->free_slots = freed_slots + empty_slots;
3512  objspace->profile.total_freed_objects += freed_slots;
3514  sweep_page->final_slots += final_slots;
3515 
3517  rb_thread_t *th = GET_THREAD();
3518  if (th) {
3520  }
3521  }
3522 
3523  gc_report(2, objspace, "page_sweep: end.\n");
3524 
3525  return freed_slots + empty_slots;
3526 }
3527 
3528 /* allocate additional minimum page to work */
3529 static void
3531 {
3532  if (!heap->free_pages && heap_increment(objspace, heap) == FALSE) {
3533  /* there is no free after page_sweep() */
3534  heap_set_increment(objspace, 1);
3535  if (!heap_increment(objspace, heap)) { /* can't allocate additional free objects */
3536  rb_memerror();
3537  }
3538  }
3539 }
3540 
3541 static const char *
3543 {
3544  switch (mode) {
3545  case gc_mode_none: return "none";
3546  case gc_mode_marking: return "marking";
3547  case gc_mode_sweeping: return "sweeping";
3548  default: rb_bug("gc_mode_name: unknown mode: %d", (int)mode);
3549  }
3550 }
3551 
3552 static void
3554 {
3555 #if RGENGC_CHECK_MODE
3556  enum gc_mode prev_mode = gc_mode(objspace);
3557  switch (prev_mode) {
3558  case gc_mode_none: assert(mode == gc_mode_marking); break;
3559  case gc_mode_marking: assert(mode == gc_mode_sweeping); break;
3560  case gc_mode_sweeping: assert(mode == gc_mode_none); break;
3561  }
3562 #endif
3563  if (0) fprintf(stderr, "gc_mode_transition: %s->%s\n", gc_mode_name(gc_mode(objspace)), gc_mode_name(mode));
3564  gc_mode_set(objspace, mode);
3565 }
3566 
3567 static void
3569 {
3570  heap->sweep_pages = heap->pages;
3571  heap->free_pages = NULL;
3572 #if GC_ENABLE_INCREMENTAL_MARK
3573  heap->pooled_pages = NULL;
3574  objspace->rincgc.pooled_slots = 0;
3575 #endif
3576  if (heap->using_page) {
3577  RVALUE **p = &heap->using_page->freelist;
3578  while (*p) {
3579  p = &(*p)->as.free.next;
3580  }
3581  *p = heap->freelist;
3582  heap->using_page = NULL;
3583  }
3584  heap->freelist = NULL;
3585 }
3586 
3587 #if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 4
3588 __attribute__((noinline))
3589 #endif
3590 static void
3592 {
3594  gc_sweep_start_heap(objspace, heap_eden);
3595 }
3596 
3597 static void
3599 {
3600  gc_report(1, objspace, "gc_sweep_finish");
3601 
3602  gc_prof_set_heap_info(objspace);
3603  heap_pages_free_unused_pages(objspace);
3604 
3605  /* if heap_pages has unused pages, then assign them to increment */
3606  if (heap_allocatable_pages < heap_tomb->total_pages) {
3607  heap_allocatable_pages = heap_tomb->total_pages;
3608  }
3609 
3611  gc_mode_transition(objspace, gc_mode_none);
3612 
3613 #if RGENGC_CHECK_MODE >= 2
3615 #endif
3616 }
3617 
3618 static int
3620 {
3621  struct heap_page *sweep_page = heap->sweep_pages;
3622  int unlink_limit = 3;
3623 #if GC_ENABLE_INCREMENTAL_MARK
3624  int need_pool = will_be_incremental_marking(objspace) ? TRUE : FALSE;
3625 
3626  gc_report(2, objspace, "gc_sweep_step (need_pool: %d)\n", need_pool);
3627 #else
3628  gc_report(2, objspace, "gc_sweep_step\n");
3629 #endif
3630 
3631  if (sweep_page == NULL) return FALSE;
3632 
3633 #if GC_ENABLE_LAZY_SWEEP
3634  gc_prof_sweep_timer_start(objspace);
3635 #endif
3636 
3637  while (sweep_page) {
3638  struct heap_page *next_sweep_page = heap->sweep_pages = sweep_page->next;
3639  int free_slots = gc_page_sweep(objspace, heap, sweep_page);
3640 
3641  if (sweep_page->final_slots + free_slots == sweep_page->total_slots &&
3643  unlink_limit > 0) {
3645  unlink_limit--;
3646  /* there are no living objects -> move this page to tomb heap */
3647  heap_unlink_page(objspace, heap, sweep_page);
3648  heap_add_page(objspace, heap_tomb, sweep_page);
3649  }
3650  else if (free_slots > 0) {
3651 #if GC_ENABLE_INCREMENTAL_MARK
3652  if (need_pool) {
3653  if (heap_add_poolpage(objspace, heap, sweep_page)) {
3654  need_pool = FALSE;
3655  }
3656  }
3657  else {
3658  heap_add_freepage(objspace, heap, sweep_page);
3659  break;
3660  }
3661 #else
3662  heap_add_freepage(objspace, heap, sweep_page);
3663  break;
3664 #endif
3665  }
3666  else {
3667  sweep_page->free_next = NULL;
3668  }
3669 
3670  sweep_page = next_sweep_page;
3671  }
3672 
3673  if (heap->sweep_pages == NULL) {
3674  gc_sweep_finish(objspace);
3675  }
3676 
3677 #if GC_ENABLE_LAZY_SWEEP
3678  gc_prof_sweep_timer_stop(objspace);
3679 #endif
3680 
3681  return heap->free_pages != NULL;
3682 }
3683 
3684 static void
3686 {
3687  rb_heap_t *heap = heap_eden; /* lazy sweep only for eden */
3688 
3689  while (has_sweeping_pages(heap)) {
3690  gc_sweep_step(objspace, heap);
3691  }
3692 }
3693 
3694 #if GC_ENABLE_LAZY_SWEEP
3695 static void
3697 {
3699 
3700  gc_enter(objspace, "sweep_continue");
3701 #if USE_RGENGC
3702  if (objspace->rgengc.need_major_gc == GPR_FLAG_NONE && heap_increment(objspace, heap)) {
3703  gc_report(3, objspace, "gc_sweep_continue: success heap_increment().\n");
3704  }
3705 #endif
3706  gc_sweep_step(objspace, heap);
3707  gc_exit(objspace, "sweep_continue");
3708 }
3709 #endif
3710 
3711 static void
3713 {
3714  const unsigned int immediate_sweep = objspace->flags.immediate_sweep;
3715 
3716  gc_report(1, objspace, "gc_sweep: immediate: %d\n", immediate_sweep);
3717 
3718  if (immediate_sweep) {
3719 #if !GC_ENABLE_LAZY_SWEEP
3720  gc_prof_sweep_timer_start(objspace);
3721 #endif
3722  gc_sweep_start(objspace);
3723  gc_sweep_rest(objspace);
3724 #if !GC_ENABLE_LAZY_SWEEP
3725  gc_prof_sweep_timer_stop(objspace);
3726 #endif
3727  }
3728  else {
3729  struct heap_page *page;
3730  gc_sweep_start(objspace);
3731  page = heap_eden->sweep_pages;
3732  while (page) {
3733  page->flags.before_sweep = TRUE;
3734  page = page->next;
3735  }
3736  gc_sweep_step(objspace, heap_eden);
3737  }
3738 
3740 }
3741 
3742 /* Marking - Marking stack */
3743 
3744 static stack_chunk_t *
3746 {
3747  stack_chunk_t *res;
3748 
3749  res = malloc(sizeof(stack_chunk_t));
3750  if (!res)
3751  rb_memerror();
3752 
3753  return res;
3754 }
3755 
3756 static inline int
3758 {
3759  return stack->chunk == NULL;
3760 }
3761 
3762 static size_t
3764 {
3765  size_t size = stack->index;
3766  stack_chunk_t *chunk = stack->chunk ? stack->chunk->next : NULL;
3767 
3768  while (chunk) {
3769  size += stack->limit;
3770  chunk = chunk->next;
3771  }
3772  return size;
3773 }
3774 
3775 static void
3777 {
3778  chunk->next = stack->cache;
3779  stack->cache = chunk;
3780  stack->cache_size++;
3781 }
3782 
3783 static void
3785 {
3786  stack_chunk_t *chunk;
3787 
3788  if (stack->unused_cache_size > (stack->cache_size/2)) {
3789  chunk = stack->cache;
3790  stack->cache = stack->cache->next;
3791  stack->cache_size--;
3792  free(chunk);
3793  }
3794  stack->unused_cache_size = stack->cache_size;
3795 }
3796 
3797 static void
3799 {
3801 
3802  if (RGENGC_CHECK_MODE) assert(stack->index == stack->limit);
3803 
3804  if (stack->cache_size > 0) {
3805  next = stack->cache;
3806  stack->cache = stack->cache->next;
3807  stack->cache_size--;
3808  if (stack->unused_cache_size > stack->cache_size)
3809  stack->unused_cache_size = stack->cache_size;
3810  }
3811  else {
3812  next = stack_chunk_alloc();
3813  }
3814  next->next = stack->chunk;
3815  stack->chunk = next;
3816  stack->index = 0;
3817 }
3818 
3819 static void
3821 {
3823 
3824  prev = stack->chunk->next;
3825  if (RGENGC_CHECK_MODE) assert(stack->index == 0);
3826  add_stack_chunk_cache(stack, stack->chunk);
3827  stack->chunk = prev;
3828  stack->index = stack->limit;
3829 }
3830 
3831 static void
3833 {
3834  stack_chunk_t *chunk = stack->chunk;
3835  stack_chunk_t *next = NULL;
3836 
3837  while (chunk != NULL) {
3838  next = chunk->next;
3839  free(chunk);
3840  chunk = next;
3841  }
3842 }
3843 
3844 static void
3846 {
3847  if (stack->index == stack->limit) {
3848  push_mark_stack_chunk(stack);
3849  }
3850  stack->chunk->data[stack->index++] = data;
3851 }
3852 
3853 static int
3855 {
3856  if (is_mark_stack_empty(stack)) {
3857  return FALSE;
3858  }
3859  if (stack->index == 1) {
3860  *data = stack->chunk->data[--stack->index];
3861  pop_mark_stack_chunk(stack);
3862  }
3863  else {
3864  *data = stack->chunk->data[--stack->index];
3865  }
3866  return TRUE;
3867 }
3868 
3869 #if GC_ENABLE_INCREMENTAL_MARK
3870 static int
3872 {
3873  int i;
3874  for (i=0; i<limit; i++) {
3875  if (chunk->data[i] == obj) {
3876  chunk->data[i] = Qundef;
3877  return TRUE;
3878  }
3879  }
3880  return FALSE;
3881 }
3882 
3883 static void
3885 {
3886  stack_chunk_t *chunk = stack->chunk;
3887  int limit = stack->index;
3888 
3889  while (chunk) {
3890  if (invalidate_mark_stack_chunk(chunk, limit, obj)) return;
3891  chunk = chunk->next;
3892  limit = stack->limit;
3893  }
3894  rb_bug("invalid_mark_stack: unreachable");
3895 }
3896 #endif
3897 
3898 static void
3900 {
3901  int i;
3902 
3903  MEMZERO(stack, mark_stack_t, 1);
3904  stack->index = stack->limit = STACK_CHUNK_SIZE;
3905  stack->cache_size = 0;
3906 
3907  for (i=0; i < 4; i++) {
3909  }
3910  stack->unused_cache_size = stack->cache_size;
3911 }
3912 
3913 /* Marking */
3914 
3915 #ifdef __ia64
3916 #define SET_STACK_END (SET_MACHINE_STACK_END(&th->machine.stack_end), th->machine.register_stack_end = rb_ia64_bsp())
3917 #else
3918 #define SET_STACK_END SET_MACHINE_STACK_END(&th->machine.stack_end)
3919 #endif
3920 
3921 #define STACK_START (th->machine.stack_start)
3922 #define STACK_END (th->machine.stack_end)
3923 #define STACK_LEVEL_MAX (th->machine.stack_maxsize/sizeof(VALUE))
3924 
3925 #if STACK_GROW_DIRECTION < 0
3926 # define STACK_LENGTH (size_t)(STACK_START - STACK_END)
3927 #elif STACK_GROW_DIRECTION > 0
3928 # define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
3929 #else
3930 # define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
3931  : (size_t)(STACK_END - STACK_START + 1))
3932 #endif
3933 #if !STACK_GROW_DIRECTION
3935 int
3937 {
3938  VALUE *end;
3939  SET_MACHINE_STACK_END(&end);
3940 
3941  if (end > addr) return ruby_stack_grow_direction = 1;
3942  return ruby_stack_grow_direction = -1;
3943 }
3944 #endif
3945 
3946 size_t
3948 {
3949  rb_thread_t *th = GET_THREAD();
3950  SET_STACK_END;
3951  if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
3952  return STACK_LENGTH;
3953 }
3954 
3955 #if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
3956 static int
3957 stack_check(int water_mark)
3958 {
3959  int ret;
3960  rb_thread_t *th = GET_THREAD();
3961  SET_STACK_END;
3962  ret = STACK_LENGTH > STACK_LEVEL_MAX - water_mark;
3963 #ifdef __ia64
3964  if (!ret) {
3965  ret = (VALUE*)rb_ia64_bsp() - th->machine.register_stack_start >
3966  th->machine.register_stack_maxsize/sizeof(VALUE) - water_mark;
3967  }
3968 #endif
3969  return ret;
3970 }
3971 #endif
3972 
3973 #define STACKFRAME_FOR_CALL_CFUNC 512
3974 
3975 int
3977 {
3978 #if defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK)
3979  return 0;
3980 #else
3982 #endif
3983 }
3984 
3986 static void
3987 mark_locations_array(rb_objspace_t *objspace, register const VALUE *x, register long n)
3988 {
3989  VALUE v;
3990  while (n--) {
3991  v = *x;
3992  gc_mark_maybe(objspace, v);
3993  x++;
3994  }
3995 }
3996 
3997 static void
3998 gc_mark_locations(rb_objspace_t *objspace, const VALUE *start, const VALUE *end)
3999 {
4000  long n;
4001 
4002  if (end <= start) return;
4003  n = end - start;
4004  mark_locations_array(objspace, start, n);
4005 }
4006 
4007 void
4009 {
4010  gc_mark_locations(&rb_objspace, start, end);
4011 }
4012 
4013 static void
4014 gc_mark_values(rb_objspace_t *objspace, long n, const VALUE *values)
4015 {
4016  long i;
4017 
4018  for (i=0; i<n; i++) {
4019  gc_mark(objspace, values[i]);
4020  }
4021 }
4022 
4023 void
4024 rb_gc_mark_values(long n, const VALUE *values)
4025 {
4026  rb_objspace_t *objspace = &rb_objspace;
4027  gc_mark_values(objspace, n, values);
4028 }
4029 
4030 static int
4032 {
4033  rb_objspace_t *objspace = (rb_objspace_t *)data;
4034  gc_mark(objspace, (VALUE)value);
4035  return ST_CONTINUE;
4036 }
4037 
4038 static void
4040 {
4041  if (!tbl || tbl->num_entries == 0) return;
4042  st_foreach(tbl, mark_entry, (st_data_t)objspace);
4043 }
4044 
4045 static int
4047 {
4048  rb_objspace_t *objspace = (rb_objspace_t *)data;
4049  gc_mark(objspace, (VALUE)key);
4050  return ST_CONTINUE;
4051 }
4052 
4053 static void
4055 {
4056  if (!tbl) return;
4057  st_foreach(tbl, mark_key, (st_data_t)objspace);
4058 }
4059 
4060 void
4062 {
4063  mark_set(&rb_objspace, tbl);
4064 }
4065 
4066 static int
4068 {
4069  rb_objspace_t *objspace = (rb_objspace_t *)data;
4070 
4071  gc_mark(objspace, (VALUE)key);
4072  gc_mark(objspace, (VALUE)value);
4073  return ST_CONTINUE;
4074 }
4075 
4076 static void
4078 {
4079  if (!tbl) return;
4080  st_foreach(tbl, mark_keyvalue, (st_data_t)objspace);
4081 }
4082 
4083 void
4085 {
4086  mark_hash(&rb_objspace, tbl);
4087 }
4088 
4089 static void
4091 {
4092  const rb_method_definition_t *def = me->def;
4093 
4094  gc_mark(objspace, me->owner);
4095  gc_mark(objspace, me->defined_class);
4096 
4097  if (def) {
4098  switch (def->type) {
4099  case VM_METHOD_TYPE_ISEQ:
4100  if (def->body.iseq.iseqptr) gc_mark(objspace, (VALUE)def->body.iseq.iseqptr);
4101  gc_mark(objspace, (VALUE)def->body.iseq.cref);
4102  break;
4104  case VM_METHOD_TYPE_IVAR:
4105  gc_mark(objspace, def->body.attr.location);
4106  break;
4108  gc_mark(objspace, def->body.proc);
4109  break;
4110  case VM_METHOD_TYPE_ALIAS:
4111  gc_mark(objspace, (VALUE)def->body.alias.original_me);
4112  return;
4114  gc_mark(objspace, (VALUE)def->body.refined.orig_me);
4115  gc_mark(objspace, (VALUE)def->body.refined.owner);
4116  break;
4117  case VM_METHOD_TYPE_CFUNC:
4118  case VM_METHOD_TYPE_ZSUPER:
4121  case VM_METHOD_TYPE_UNDEF:
4123  break;
4124  }
4125  }
4126 }
4127 
4128 static enum rb_id_table_iterator_result
4130 {
4131  rb_objspace_t *objspace = (rb_objspace_t *)data;
4132 
4133  gc_mark(objspace, me);
4134  return ID_TABLE_CONTINUE;
4135 }
4136 
4137 static void
4138 mark_m_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
4139 {
4140  if (tbl) {
4142  }
4143 }
4144 
4145 static enum rb_id_table_iterator_result
4146 mark_const_entry_i(VALUE value, void *data)
4147 {
4148  const rb_const_entry_t *ce = (const rb_const_entry_t *)value;
4149  rb_objspace_t *objspace = data;
4150 
4151  gc_mark(objspace, ce->value);
4152  gc_mark(objspace, ce->file);
4153  return ID_TABLE_CONTINUE;
4154 }
4155 
4156 static void
4157 mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
4158 {
4159  if (!tbl) return;
4161 }
4162 
4163 #if STACK_GROW_DIRECTION < 0
4164 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
4165 #elif STACK_GROW_DIRECTION > 0
4166 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
4167 #else
4168 #define GET_STACK_BOUNDS(start, end, appendix) \
4169  ((STACK_END < STACK_START) ? \
4170  ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
4171 #endif
4172 
4173 static void mark_stack_locations(rb_objspace_t *objspace, rb_thread_t *th,
4174  const VALUE *stack_start, const VALUE *stack_end);
4175 
4176 static void
4178 {
4179  union {
4180  rb_jmp_buf j;
4181  VALUE v[sizeof(rb_jmp_buf) / sizeof(VALUE)];
4182  } save_regs_gc_mark;
4183  VALUE *stack_start, *stack_end;
4184 
4186  /* This assumes that all registers are saved into the jmp_buf (and stack) */
4187  rb_setjmp(save_regs_gc_mark.j);
4188 
4189  /* SET_STACK_END must be called in this function because
4190  * the stack frame of this function may contain
4191  * callee save registers and they should be marked. */
4192  SET_STACK_END;
4193  GET_STACK_BOUNDS(stack_start, stack_end, 1);
4194 
4195  mark_locations_array(objspace, save_regs_gc_mark.v, numberof(save_regs_gc_mark.v));
4196 
4197  mark_stack_locations(objspace, th, stack_start, stack_end);
4198 }
4199 
4200 void
4202 {
4203  rb_objspace_t *objspace = rb_objspace_of(th->vm);
4204  VALUE *stack_start, *stack_end;
4205 
4206  GET_STACK_BOUNDS(stack_start, stack_end, 0);
4207  mark_stack_locations(objspace, th, stack_start, stack_end);
4208 }
4209 
4210 static void
4212  const VALUE *stack_start, const VALUE *stack_end)
4213 {
4214 
4215  gc_mark_locations(objspace, stack_start, stack_end);
4216 #ifdef __ia64
4217  gc_mark_locations(objspace,
4218  th->machine.register_stack_start,
4219  th->machine.register_stack_end);
4220 #endif
4221 #if defined(__mc68000__)
4222  gc_mark_locations(objspace,
4223  (VALUE*)((char*)stack_start + 2),
4224  (VALUE*)((char*)stack_end - 2));
4225 #endif
4226 }
4227 
4228 void
4230 {
4231  mark_tbl(&rb_objspace, tbl);
4232 }
4233 
4234 static void
4236 {
4237  (void)VALGRIND_MAKE_MEM_DEFINED(&obj, sizeof(obj));
4238  if (is_pointer_to_heap(objspace, (void *)obj)) {
4239  int type = BUILTIN_TYPE(obj);
4240  if (type != T_ZOMBIE && type != T_NONE) {
4241  gc_mark_ptr(objspace, obj);
4242  }
4243  }
4244 }
4245 
4246 void
4248 {
4249  gc_mark_maybe(&rb_objspace, obj);
4250 }
4251 
4252 static inline int
4254 {
4255  if (RVALUE_MARKED(obj)) return 0;
4257  return 1;
4258 }
4259 
4260 #if USE_RGENGC
4261 static int
4263 {
4264  struct heap_page *page = GET_HEAP_PAGE(obj);
4266 
4267  if (!MARKED_IN_BITMAP(uncollectible_bits, obj)) {
4269  MARK_IN_BITMAP(uncollectible_bits, obj);
4271 
4272 #if RGENGC_PROFILE > 0
4273  objspace->profile.total_remembered_shady_object_count++;
4274 #if RGENGC_PROFILE >= 2
4275  objspace->profile.remembered_shady_object_count_types[BUILTIN_TYPE(obj)]++;
4276 #endif
4277 #endif
4278  return TRUE;
4279  }
4280  else {
4281  return FALSE;
4282  }
4283 }
4284 #endif
4285 
4286 static void
4288 {
4289 #if USE_RGENGC
4290  const VALUE old_parent = objspace->rgengc.parent_object;
4291 
4292  if (old_parent) { /* parent object is old */
4293  if (RVALUE_WB_UNPROTECTED(obj)) {
4294  if (gc_remember_unprotected(objspace, obj)) {
4295  gc_report(2, objspace, "relation: (O->S) %s -> %s\n", obj_info(old_parent), obj_info(obj));
4296  }
4297  }
4298  else {
4299  if (!RVALUE_OLD_P(obj)) {
4300  if (RVALUE_MARKED(obj)) {
4301  /* An object pointed from an OLD object should be OLD. */
4302  gc_report(2, objspace, "relation: (O->unmarked Y) %s -> %s\n", obj_info(old_parent), obj_info(obj));
4303  RVALUE_AGE_SET_OLD(objspace, obj);
4304  if (is_incremental_marking(objspace)) {
4305  if (!RVALUE_MARKING(obj)) {
4306  gc_grey(objspace, obj);
4307  }
4308  }
4309  else {
4310  rgengc_remember(objspace, obj);
4311  }
4312  }
4313  else {
4314  gc_report(2, objspace, "relation: (O->Y) %s -> %s\n", obj_info(old_parent), obj_info(obj));
4315  RVALUE_AGE_SET_CANDIDATE(objspace, obj);
4316  }
4317  }
4318  }
4319  }
4320 
4321  if (RGENGC_CHECK_MODE) assert(old_parent == objspace->rgengc.parent_object);
4322 #endif
4323 }
4324 
4325 static void
4326 gc_grey(rb_objspace_t *objspace, VALUE obj)
4327 {
4328 #if RGENGC_CHECK_MODE
4329  if (RVALUE_MARKED(obj) == FALSE) rb_bug("gc_grey: %s is not marked.", obj_info(obj));
4330  if (RVALUE_MARKING(obj) == TRUE) rb_bug("gc_grey: %s is marking/remembered.", obj_info(obj));
4331 #endif
4332 
4333 #if GC_ENABLE_INCREMENTAL_MARK
4334  if (is_incremental_marking(objspace)) {
4336  }
4337 #endif
4338 
4339  push_mark_stack(&objspace->mark_stack, obj);
4340 }
4341 
4342 static void
4344 {
4345 #if USE_RGENGC
4346  struct heap_page *page = GET_HEAP_PAGE(obj);
4347 
4348 #if RGENGC_CHECK_MODE
4349  assert(RVALUE_MARKING(obj) == FALSE);
4350 #endif
4351 
4353 
4354  if (!RVALUE_PAGE_WB_UNPROTECTED(page, obj)) {
4355  if (!RVALUE_OLD_P(obj)) {
4356  gc_report(3, objspace, "gc_aging: YOUNG: %s\n", obj_info(obj));
4357  RVALUE_AGE_INC(objspace, obj);
4358  }
4359  else if (is_full_marking(objspace)) {
4361  RVALUE_PAGE_OLD_UNCOLLECTIBLE_SET(objspace, page, obj);
4362  }
4363  }
4365 #endif /* USE_RGENGC */
4366 
4367  objspace->marked_slots++;
4368 }
4369 
4370 NOINLINE(static void gc_mark_ptr(rb_objspace_t *objspace, VALUE obj));
4371 
4372 static void
4374 {
4375  if (LIKELY(objspace->mark_func_data == NULL)) {
4376  rgengc_check_relation(objspace, obj);
4377  if (!gc_mark_set(objspace, obj)) return; /* already marked */
4378  gc_aging(objspace, obj);
4379  gc_grey(objspace, obj);
4380  }
4381  else {
4382  objspace->mark_func_data->mark_func(obj, objspace->mark_func_data->data);
4383  }
4384 }
4385 
4386 static inline void
4387 gc_mark(rb_objspace_t *objspace, VALUE obj)
4388 {
4389  if (!is_markable_object(objspace, obj)) return;
4390  gc_mark_ptr(objspace, obj);
4391 }
4392 
4393 void
4395 {
4396  gc_mark(&rb_objspace, ptr);
4397 }
4398 
4399 /* CAUTION: THIS FUNCTION ENABLE *ONLY BEFORE* SWEEPING.
4400  * This function is only for GC_END_MARK timing.
4401  */
4402 
4403 int
4405 {
4406  return RVALUE_MARKED(obj) ? TRUE : FALSE;
4407 }
4408 
4409 static inline void
4411 {
4412 #if USE_RGENGC
4413  if (RVALUE_OLD_P(obj)) {
4414  objspace->rgengc.parent_object = obj;
4415  }
4416  else {
4417  objspace->rgengc.parent_object = Qfalse;
4418  }
4419 #endif
4420 }
4421 
4422 static void
4424 {
4425  switch (imemo_type(obj)) {
4426  case imemo_env:
4427  {
4428  const rb_env_t *env = (const rb_env_t *)obj;
4429  VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
4430  gc_mark_values(objspace, (long)env->env_size, env->env);
4432  gc_mark(objspace, (VALUE)rb_vm_env_prev_env(env));
4433  gc_mark(objspace, (VALUE)env->iseq);
4434  }
4435  return;
4436  case imemo_cref:
4437  gc_mark(objspace, RANY(obj)->as.imemo.cref.klass);
4438  gc_mark(objspace, (VALUE)RANY(obj)->as.imemo.cref.next);
4439  gc_mark(objspace, RANY(obj)->as.imemo.cref.refinements);
4440  return;
4441  case imemo_svar:
4442  gc_mark(objspace, RANY(obj)->as.imemo.svar.cref_or_me);
4443  gc_mark(objspace, RANY(obj)->as.imemo.svar.lastline);
4444  gc_mark(objspace, RANY(obj)->as.imemo.svar.backref);
4445  gc_mark(objspace, RANY(obj)->as.imemo.svar.others);
4446  return;
4447  case imemo_throw_data:
4448  gc_mark(objspace, RANY(obj)->as.imemo.throw_data.throw_obj);
4449  return;
4450  case imemo_ifunc:
4451  gc_mark_maybe(objspace, (VALUE)RANY(obj)->as.imemo.ifunc.data);
4452  return;
4453  case imemo_memo:
4454  gc_mark(objspace, RANY(obj)->as.imemo.memo.v1);
4455  gc_mark(objspace, RANY(obj)->as.imemo.memo.v2);
4456  gc_mark_maybe(objspace, RANY(obj)->as.imemo.memo.u3.value);
4457  return;
4458  case imemo_ment:
4459  mark_method_entry(objspace, &RANY(obj)->as.imemo.ment);
4460  return;
4461  case imemo_iseq:
4462  rb_iseq_mark((rb_iseq_t *)obj);
4463  return;
4464 #if VM_CHECK_MODE > 0
4465  default:
4467 #endif
4468  }
4469 }
4470 
4471 static void
4473 {
4474  register RVALUE *any = RANY(obj);
4475  gc_mark_set_parent(objspace, obj);
4476 
4477  if (FL_TEST(obj, FL_EXIVAR)) {
4478  rb_mark_generic_ivar(obj);
4479  }
4480 
4481  switch (BUILTIN_TYPE(obj)) {
4482  case T_NIL:
4483  case T_FIXNUM:
4484  rb_bug("rb_gc_mark() called for broken object");
4485  break;
4486 
4487  case T_NODE:
4488  obj = rb_gc_mark_node(&any->as.node);
4489  if (obj) gc_mark(objspace, obj);
4490  return; /* no need to mark class. */
4491 
4492  case T_IMEMO:
4493  gc_mark_imemo(objspace, obj);
4494  return;
4495  }
4496 
4497  gc_mark(objspace, any->as.basic.klass);
4498 
4499  switch (BUILTIN_TYPE(obj)) {
4500  case T_CLASS:
4501  case T_MODULE:
4502  mark_m_tbl(objspace, RCLASS_M_TBL(obj));
4503  if (!RCLASS_EXT(obj)) break;
4504  mark_tbl(objspace, RCLASS_IV_TBL(obj));
4505  mark_const_tbl(objspace, RCLASS_CONST_TBL(obj));
4506  gc_mark(objspace, RCLASS_SUPER((VALUE)obj));
4507  break;
4508 
4509  case T_ICLASS:
4510  if (FL_TEST(obj, RICLASS_IS_ORIGIN)) {
4511  mark_m_tbl(objspace, RCLASS_M_TBL(obj));
4512  }
4513  if (!RCLASS_EXT(obj)) break;
4514  mark_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
4515  gc_mark(objspace, RCLASS_SUPER((VALUE)obj));
4516  break;
4517 
4518  case T_ARRAY:
4519  if (FL_TEST(obj, ELTS_SHARED)) {
4520  gc_mark(objspace, any->as.array.as.heap.aux.shared);
4521  }
4522  else {
4523  long i, len = RARRAY_LEN(obj);
4524  const VALUE *ptr = RARRAY_CONST_PTR(obj);
4525  for (i=0; i < len; i++) {
4526  gc_mark(objspace, *ptr++);
4527  }
4528  }
4529  break;
4530 
4531  case T_HASH:
4532  mark_hash(objspace, any->as.hash.ntbl);
4533  gc_mark(objspace, any->as.hash.ifnone);
4534  break;
4535 
4536  case T_STRING:
4537  if (STR_SHARED_P(obj)) {
4538  gc_mark(objspace, any->as.string.as.heap.aux.shared);
4539  }
4540  break;
4541 
4542  case T_DATA:
4543  {
4544  void *const ptr = DATA_PTR(obj);
4545  if (ptr) {
4546  RUBY_DATA_FUNC mark_func = RTYPEDDATA_P(obj) ?
4547  any->as.typeddata.type->function.dmark :
4548  any->as.data.dmark;
4549  if (mark_func) (*mark_func)(ptr);
4550  }
4551  }
4552  break;
4553 
4554  case T_OBJECT:
4555  {
4556  uint32_t i, len = ROBJECT_NUMIV(obj);
4557  VALUE *ptr = ROBJECT_IVPTR(obj);
4558  for (i = 0; i < len; i++) {
4559  gc_mark(objspace, *ptr++);
4560  }
4561  }
4562  break;
4563 
4564  case T_FILE:
4565  if (any->as.file.fptr) {
4566  gc_mark(objspace, any->as.file.fptr->pathv);
4567  gc_mark(objspace, any->as.file.fptr->tied_io_for_writing);
4568  gc_mark(objspace, any->as.file.fptr->writeconv_asciicompat);
4569  gc_mark(objspace, any->as.file.fptr->writeconv_pre_ecopts);
4570  gc_mark(objspace, any->as.file.fptr->encs.ecopts);
4571  gc_mark(objspace, any->as.file.fptr->write_lock);
4572  }
4573  break;
4574 
4575  case T_REGEXP:
4576  gc_mark(objspace, any->as.regexp.src);
4577  break;
4578 
4579  case T_FLOAT:
4580  case T_BIGNUM:
4581  case T_SYMBOL:
4582  break;
4583 
4584  case T_MATCH:
4585  gc_mark(objspace, any->as.match.regexp);
4586  if (any->as.match.str) {
4587  gc_mark(objspace, any->as.match.str);
4588  }
4589  break;
4590 
4591  case T_RATIONAL:
4592  gc_mark(objspace, any->as.rational.num);
4593  gc_mark(objspace, any->as.rational.den);
4594  break;
4595 
4596  case T_COMPLEX:
4597  gc_mark(objspace, any->as.complex.real);
4598  gc_mark(objspace, any->as.complex.imag);
4599  break;
4600 
4601  case T_STRUCT:
4602  {
4603  long len = RSTRUCT_LEN(obj);
4604  const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
4605 
4606  while (len--) {
4607  gc_mark(objspace, *ptr++);
4608  }
4609  }
4610  break;
4611 
4612  default:
4613 #if GC_DEBUG
4615 #endif
4616  if (BUILTIN_TYPE(obj) == T_NONE) rb_bug("rb_gc_mark(): %p is T_NONE", (void *)obj);
4617  if (BUILTIN_TYPE(obj) == T_ZOMBIE) rb_bug("rb_gc_mark(): %p is T_ZOMBIE", (void *)obj);
4618  rb_bug("rb_gc_mark(): unknown data type 0x%x(%p) %s",
4619  BUILTIN_TYPE(obj), any,
4620  is_pointer_to_heap(objspace, any) ? "corrupted object" : "non object");
4621  }
4622 }
4623 
4628 static inline int
4629 gc_mark_stacked_objects(rb_objspace_t *objspace, int incremental, size_t count)
4630 {
4631  mark_stack_t *mstack = &objspace->mark_stack;
4632  VALUE obj;
4633 #if GC_ENABLE_INCREMENTAL_MARK
4634  size_t marked_slots_at_the_beginning = objspace->marked_slots;
4635  size_t popped_count = 0;
4636 #endif
4637 
4638  while (pop_mark_stack(mstack, &obj)) {
4639  if (obj == Qundef) continue; /* skip */
4640 
4641  if (RGENGC_CHECK_MODE && !RVALUE_MARKED(obj)) {
4642  rb_bug("gc_mark_stacked_objects: %s is not marked.", obj_info(obj));
4643  }
4644  gc_mark_children(objspace, obj);
4645 
4646 #if GC_ENABLE_INCREMENTAL_MARK
4647  if (incremental) {
4648  if (RGENGC_CHECK_MODE && !RVALUE_MARKING(obj)) {
4649  rb_bug("gc_mark_stacked_objects: incremental, but marking bit is 0");
4650  }
4652  popped_count++;
4653 
4654  if (popped_count + (objspace->marked_slots - marked_slots_at_the_beginning) > count) {
4655  break;
4656  }
4657  }
4658  else {
4659  /* just ignore marking bits */
4660  }
4661 #endif
4662  }
4663 
4665 
4666  if (is_mark_stack_empty(mstack)) {
4667  shrink_stack_chunk_cache(mstack);
4668  return TRUE;
4669  }
4670  else {
4671  return FALSE;
4672  }
4673 }
4674 
4675 static int
4677 {
4678  return gc_mark_stacked_objects(objspace, TRUE, count);
4679 }
4680 
4681 static int
4683 {
4684  return gc_mark_stacked_objects(objspace, FALSE, 0);
4685 }
4686 
4687 #if PRINT_ROOT_TICKS
4688 #define MAX_TICKS 0x100
4689 static tick_t mark_ticks[MAX_TICKS];
4690 static const char *mark_ticks_categories[MAX_TICKS];
4691 
4692 static void
4693 show_mark_ticks(void)
4694 {
4695  int i;
4696  fprintf(stderr, "mark ticks result:\n");
4697  for (i=0; i<MAX_TICKS; i++) {
4698  const char *category = mark_ticks_categories[i];
4699  if (category) {
4700  fprintf(stderr, "%s\t%8lu\n", category, (unsigned long)mark_ticks[i]);
4701  }
4702  else {
4703  break;
4704  }
4705  }
4706 }
4707 
4708 #endif /* PRITNT_ROOT_TICKS */
4709 
4710 static void
4711 gc_mark_roots(rb_objspace_t *objspace, const char **categoryp)
4712 {
4713  struct gc_list *list;
4714  rb_thread_t *th = GET_THREAD();
4715 
4716 #if PRINT_ROOT_TICKS
4717  tick_t start_tick = tick();
4718  int tick_count = 0;
4719  const char *prev_category = 0;
4720 
4721  if (mark_ticks_categories[0] == 0) {
4722  atexit(show_mark_ticks);
4723  }
4724 #endif
4725 
4726  if (categoryp) *categoryp = "xxx";
4727 
4728 #if USE_RGENGC
4729  objspace->rgengc.parent_object = Qfalse;
4730 #endif
4731 
4732 #if PRINT_ROOT_TICKS
4733 #define MARK_CHECKPOINT_PRINT_TICK(category) do { \
4734  if (prev_category) { \
4735  tick_t t = tick(); \
4736  mark_ticks[tick_count] = t - start_tick; \
4737  mark_ticks_categories[tick_count] = prev_category; \
4738  tick_count++; \
4739  } \
4740  prev_category = category; \
4741  start_tick = tick(); \
4742 } while (0)
4743 #else /* PRITNT_ROOT_TICKS */
4744 #define MARK_CHECKPOINT_PRINT_TICK(category)
4745 #endif
4746 
4747 #define MARK_CHECKPOINT(category) do { \
4748  if (categoryp) *categoryp = category; \
4749  MARK_CHECKPOINT_PRINT_TICK(category); \
4750 } while (0)
4751 
4752  MARK_CHECKPOINT("vm");
4753  SET_STACK_END;
4754  rb_vm_mark(th->vm);
4755  if (th->vm->self) gc_mark(objspace, th->vm->self);
4756 
4757  MARK_CHECKPOINT("finalizers");
4758  mark_tbl(objspace, finalizer_table);
4759 
4760  MARK_CHECKPOINT("machine_context");
4761  mark_current_machine_context(objspace, th);
4762 
4763  MARK_CHECKPOINT("encodings");
4765 
4766  /* mark protected global variables */
4767  MARK_CHECKPOINT("global_list");
4768  for (list = global_list; list; list = list->next) {
4769  rb_gc_mark_maybe(*list->varptr);
4770  }
4771 
4772  MARK_CHECKPOINT("end_proc");
4773  rb_mark_end_proc();
4774 
4775  MARK_CHECKPOINT("global_tbl");
4777 
4779 
4780  MARK_CHECKPOINT("finish");
4781 #undef MARK_CHECKPOINT
4782 }
4783 
4784 #if RGENGC_CHECK_MODE >= 4
4785 
4786 #define MAKE_ROOTSIG(obj) (((VALUE)(obj) << 1) | 0x01)
4787 #define IS_ROOTSIG(obj) ((VALUE)(obj) & 0x01)
4788 #define GET_ROOTSIG(obj) ((const char *)((VALUE)(obj) >> 1))
4789 
4790 struct reflist {
4791  VALUE *list;
4792  int pos;
4793  int size;
4794 };
4795 
4796 static struct reflist *
4797 reflist_create(VALUE obj)
4798 {
4799  struct reflist *refs = xmalloc(sizeof(struct reflist));
4800  refs->size = 1;
4801  refs->list = ALLOC_N(VALUE, refs->size);
4802  refs->list[0] = obj;
4803  refs->pos = 1;
4804  return refs;
4805 }
4806 
4807 static void
4808 reflist_destruct(struct reflist *refs)
4809 {
4810  xfree(refs->list);
4811  xfree(refs);
4812 }
4813 
4814 static void
4815 reflist_add(struct reflist *refs, VALUE obj)
4816 {
4817  if (refs->pos == refs->size) {
4818  refs->size *= 2;
4819  SIZED_REALLOC_N(refs->list, VALUE, refs->size, refs->size/2);
4820  }
4821 
4822  refs->list[refs->pos++] = obj;
4823 }
4824 
4825 static void
4826 reflist_dump(struct reflist *refs)
4827 {
4828  int i;
4829  for (i=0; i<refs->pos; i++) {
4830  VALUE obj = refs->list[i];
4831  if (IS_ROOTSIG(obj)) { /* root */
4832  fprintf(stderr, "<root@%s>", GET_ROOTSIG(obj));
4833  }
4834  else {
4835  fprintf(stderr, "<%s>", obj_info(obj));
4836  }
4837  if (i+1 < refs->pos) fprintf(stderr, ", ");
4838  }
4839 }
4840 
4841 static int
4842 reflist_refered_from_machine_context(struct reflist *refs)
4843 {
4844  int i;
4845  for (i=0; i<refs->pos; i++) {
4846  VALUE obj = refs->list[i];
4847  if (IS_ROOTSIG(obj) && strcmp(GET_ROOTSIG(obj), "machine_context") == 0) return 1;
4848  }
4849  return 0;
4850 }
4851 
4852 struct allrefs {
4853  rb_objspace_t *objspace;
4854  /* a -> obj1
4855  * b -> obj1
4856  * c -> obj1
4857  * c -> obj2
4858  * d -> obj3
4859  * #=> {obj1 => [a, b, c], obj2 => [c, d]}
4860  */
4861  struct st_table *references;
4862  const char *category;
4863  VALUE root_obj;
4865 };
4866 
4867 static int
4868 allrefs_add(struct allrefs *data, VALUE obj)
4869 {
4870  struct reflist *refs;
4871 
4872  if (st_lookup(data->references, obj, (st_data_t *)&refs)) {
4873  reflist_add(refs, data->root_obj);
4874  return 0;
4875  }
4876  else {
4877  refs = reflist_create(data->root_obj);
4878  st_insert(data->references, obj, (st_data_t)refs);
4879  return 1;
4880  }
4881 }
4882 
4883 static void
4884 allrefs_i(VALUE obj, void *ptr)
4885 {
4886  struct allrefs *data = (struct allrefs *)ptr;
4887 
4888  if (allrefs_add(data, obj)) {
4889  push_mark_stack(&data->mark_stack, obj);
4890  }
4891 }
4892 
4893 static void
4894 allrefs_roots_i(VALUE obj, void *ptr)
4895 {
4896  struct allrefs *data = (struct allrefs *)ptr;
4897  if (strlen(data->category) == 0) rb_bug("!!!");
4898  data->root_obj = MAKE_ROOTSIG(data->category);
4899 
4900  if (allrefs_add(data, obj)) {
4901  push_mark_stack(&data->mark_stack, obj);
4902  }
4903 }
4904 
4905 static st_table *
4906 objspace_allrefs(rb_objspace_t *objspace)
4907 {
4908  struct allrefs data;
4909  struct mark_func_data_struct mfd;
4910  VALUE obj;
4911  int prev_dont_gc = dont_gc;
4912  dont_gc = TRUE;
4913 
4914  data.objspace = objspace;
4915  data.references = st_init_numtable();
4916  init_mark_stack(&data.mark_stack);
4917 
4918  mfd.mark_func = allrefs_roots_i;
4919  mfd.data = &data;
4920 
4921  /* traverse root objects */
4922  PUSH_MARK_FUNC_DATA(&mfd);
4923  objspace->mark_func_data = &mfd;
4924  gc_mark_roots(objspace, &data.category);
4926 
4927  /* traverse rest objects reachable from root objects */
4928  while (pop_mark_stack(&data.mark_stack, &obj)) {
4929  rb_objspace_reachable_objects_from(data.root_obj = obj, allrefs_i, &data);
4930  }
4931  free_stack_chunks(&data.mark_stack);
4932 
4933  dont_gc = prev_dont_gc;
4934  return data.references;
4935 }
4936 
4937 static int
4938 objspace_allrefs_destruct_i(st_data_t key, st_data_t value, void *ptr)
4939 {
4940  struct reflist *refs = (struct reflist *)value;
4941  reflist_destruct(refs);
4942  return ST_CONTINUE;
4943 }
4944 
4945 static void
4946 objspace_allrefs_destruct(struct st_table *refs)
4947 {
4948  st_foreach(refs, objspace_allrefs_destruct_i, 0);
4949  st_free_table(refs);
4950 }
4951 
4952 #if RGENGC_CHECK_MODE >= 5
4953 static int
4954 allrefs_dump_i(st_data_t k, st_data_t v, st_data_t ptr)
4955 {
4956  VALUE obj = (VALUE)k;
4957  struct reflist *refs = (struct reflist *)v;
4958  fprintf(stderr, "[allrefs_dump_i] %s <- ", obj_info(obj));
4959  reflist_dump(refs);
4960  fprintf(stderr, "\n");
4961  return ST_CONTINUE;
4962 }
4963 
4964 static void
4965 allrefs_dump(rb_objspace_t *objspace)
4966 {
4967  fprintf(stderr, "[all refs] (size: %d)\n", (int)objspace->rgengc.allrefs_table->num_entries);
4968  st_foreach(objspace->rgengc.allrefs_table, allrefs_dump_i, 0);
4969 }
4970 #endif
4971 
4972 static int
4973 gc_check_after_marks_i(st_data_t k, st_data_t v, void *ptr)
4974 {
4975  VALUE obj = k;
4976  struct reflist *refs = (struct reflist *)v;
4977  rb_objspace_t *objspace = (rb_objspace_t *)ptr;
4978 
4979  /* object should be marked or oldgen */
4980  if (!MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(obj), obj)) {
4981  fprintf(stderr, "gc_check_after_marks_i: %s is not marked and not oldgen.\n", obj_info(obj));
4982  fprintf(stderr, "gc_check_after_marks_i: %p is referred from ", (void *)obj);
4983  reflist_dump(refs);
4984 
4985  if (reflist_refered_from_machine_context(refs)) {
4986  fprintf(stderr, " (marked from machine stack).\n");
4987  /* marked from machine context can be false positive */
4988  }
4989  else {
4990  objspace->rgengc.error_count++;
4991  fprintf(stderr, "\n");
4992  }
4993  }
4994  return ST_CONTINUE;
4995 }
4996 
4997 static void
4998 gc_marks_check(rb_objspace_t *objspace, int (*checker_func)(ANYARGS), const char *checker_name)
4999 {
5000  size_t saved_malloc_increase = objspace->malloc_params.increase;
5001 #if RGENGC_ESTIMATE_OLDMALLOC
5002  size_t saved_oldmalloc_increase = objspace->rgengc.oldmalloc_increase;
5003 #endif
5004  VALUE already_disabled = rb_gc_disable();
5005 
5006  objspace->rgengc.allrefs_table = objspace_allrefs(objspace);
5007 
5008  if (checker_func) {
5009  st_foreach(objspace->rgengc.allrefs_table, checker_func, (st_data_t)objspace);
5010  }
5011 
5012  if (objspace->rgengc.error_count > 0) {
5013 #if RGENGC_CHECK_MODE >= 5
5014  allrefs_dump(objspace);
5015 #endif
5016  if (checker_name) rb_bug("%s: GC has problem.", checker_name);
5017  }
5018 
5019  objspace_allrefs_destruct(objspace->rgengc.allrefs_table);
5020  objspace->rgengc.allrefs_table = 0;
5021 
5022  if (already_disabled == Qfalse) rb_gc_enable();
5023  objspace->malloc_params.increase = saved_malloc_increase;
5024 #if RGENGC_ESTIMATE_OLDMALLOC
5025  objspace->rgengc.oldmalloc_increase = saved_oldmalloc_increase;
5026 #endif
5027 }
5028 #endif /* RGENGC_CHECK_MODE >= 4 */
5029 
5035 
5036 #if USE_RGENGC
5040 #endif
5041 };
5042 
5043 #if USE_RGENGC
5044 static void
5045 check_generation_i(const VALUE child, void *ptr)
5046 {
5048  const VALUE parent = data->parent;
5049 
5050  if (RGENGC_CHECK_MODE) assert(RVALUE_OLD_P(parent));
5051 
5052  if (!RVALUE_OLD_P(child)) {
5053  if (!RVALUE_REMEMBERED(parent) &&
5054  !RVALUE_REMEMBERED(child) &&
5055  !RVALUE_UNCOLLECTIBLE(child)) {
5056  fprintf(stderr, "verify_internal_consistency_reachable_i: WB miss (O->Y) %s -> %s\n", obj_info(parent), obj_info(child));
5057  data->err_count++;
5058  }
5059  }
5060 }
5061 
5062 static void
5063 check_color_i(const VALUE child, void *ptr)
5064 {
5066  const VALUE parent = data->parent;
5067 
5068  if (!RVALUE_WB_UNPROTECTED(parent) && RVALUE_WHITE_P(child)) {
5069  fprintf(stderr, "verify_internal_consistency_reachable_i: WB miss (B->W) - %s -> %s\n",
5070  obj_info(parent), obj_info(child));
5071  data->err_count++;
5072  }
5073 }
5074 #endif
5075 
5076 static void
5077 check_children_i(const VALUE child, void *ptr)
5078 {
5079  check_rvalue_consistency(child);
5080 }
5081 
5082 static int
5083 verify_internal_consistency_i(void *page_start, void *page_end, size_t stride, void *ptr)
5084 {
5086  VALUE obj;
5087  rb_objspace_t *objspace = data->objspace;
5088 
5089  for (obj = (VALUE)page_start; obj != (VALUE)page_end; obj += stride) {
5090  if (is_live_object(objspace, obj)) {
5091  /* count objects */
5092  data->live_object_count++;
5093 
5095 
5096 #if USE_RGENGC
5097  /* check health of children */
5098  data->parent = obj;
5099 
5100  if (RVALUE_OLD_P(obj)) data->old_object_count++;
5101  if (RVALUE_WB_UNPROTECTED(obj) && RVALUE_UNCOLLECTIBLE(obj)) data->remembered_shady_count++;
5102 
5103  if (!is_marking(objspace) && RVALUE_OLD_P(obj)) {
5104  /* reachable objects from an oldgen object should be old or (young with remember) */
5105  data->parent = obj;
5107  }
5108 
5109  if (is_incremental_marking(objspace)) {
5110  if (RVALUE_BLACK_P(obj)) {
5111  /* reachable objects from black objects should be black or grey objects */
5112  data->parent = obj;
5114  }
5115  }
5116 #endif
5117  }
5118  else {
5119  if (BUILTIN_TYPE(obj) == T_ZOMBIE) {
5120  if (RGENGC_CHECK_MODE) assert(RBASIC(obj)->flags == T_ZOMBIE);
5121  data->zombie_object_count++;
5122  }
5123  }
5124  }
5125 
5126  return 0;
5127 }
5128 
5129 static int
5130 gc_verify_heap_page(rb_objspace_t *objspace, struct heap_page *page, VALUE obj)
5131 {
5132 #if USE_RGENGC
5133  int i;
5134  unsigned int has_remembered_shady = FALSE;
5135  unsigned int has_remembered_old = FALSE;
5136  int rememberd_old_objects = 0;
5137  int free_objects = 0;
5138  int zombie_objects = 0;
5139 
5140  for (i=0; i<page->total_slots; i++) {
5141  VALUE obj = (VALUE)&page->start[i];
5142  if (RBASIC(obj) == 0) free_objects++;
5143  if (BUILTIN_TYPE(obj) == T_ZOMBIE) zombie_objects++;
5144  if (RVALUE_PAGE_UNCOLLECTIBLE(page, obj) && RVALUE_PAGE_WB_UNPROTECTED(page, obj)) has_remembered_shady = TRUE;
5145  if (RVALUE_PAGE_MARKING(page, obj)) {
5146  has_remembered_old = TRUE;
5147  rememberd_old_objects++;
5148  }
5149  }
5150 
5151  if (!is_incremental_marking(objspace) &&
5152  page->flags.has_remembered_objects == FALSE && has_remembered_old == TRUE) {
5153 
5154  for (i=0; i<page->total_slots; i++) {
5155  VALUE obj = (VALUE)&page->start[i];
5156  if (RVALUE_PAGE_MARKING(page, obj)) {
5157  fprintf(stderr, "marking -> %s\n", obj_info(obj));
5158  }
5159  }
5160  rb_bug("page %p's has_remembered_objects should be false, but there are remembered old objects (%d). %s",
5161  page, rememberd_old_objects, obj ? obj_info(obj) : "");
5162  }
5163 
5164  if (page->flags.has_uncollectible_shady_objects == FALSE && has_remembered_shady == TRUE) {
5165  rb_bug("page %p's has_remembered_shady should be false, but there are remembered shady objects. %s",
5166  page, obj ? obj_info(obj) : "");
5167  }
5168 
5169  if (0) {
5170  /* free_slots may not equal to free_objects */
5171  if (page->free_slots != free_objects) {
5172  rb_bug("page %p's free_slots should be %d, but %d\n", page, (int)page->free_slots, free_objects);
5173  }
5174  }
5175  if (page->final_slots != zombie_objects) {
5176  rb_bug("page %p's final_slots should be %d, but %d\n", page, (int)page->final_slots, zombie_objects);
5177  }
5178 
5179  return rememberd_old_objects;
5180 #else
5181  return 0;
5182 #endif
5183 }
5184 
5185 static int
5187 {
5188  int rememberd_old_objects = 0;
5189 
5190  while (page) {
5191  if (page->flags.has_remembered_objects == FALSE) {
5192  rememberd_old_objects += gc_verify_heap_page(objspace, page, Qfalse);
5193  }
5194  page = page->next;
5195  }
5196 
5197  return rememberd_old_objects;
5198 }
5199 
5200 static int
5202 {
5203  int rememberd_old_objects = 0;
5204  rememberd_old_objects = gc_verify_heap_pages_(objspace, heap_eden->pages);
5205  rememberd_old_objects = gc_verify_heap_pages_(objspace, heap_tomb->pages);
5206  return rememberd_old_objects;
5207 }
5208 
5209 /*
5210  * call-seq:
5211  * GC.verify_internal_consistency -> nil
5212  *
5213  * Verify internal consistency.
5214  *
5215  * This method is implementation specific.
5216  * Now this method checks generational consistency
5217  * if RGenGC is supported.
5218  */
5219 static VALUE
5221 {
5222  rb_objspace_t *objspace = &rb_objspace;
5223  struct verify_internal_consistency_struct data = {0};
5224  struct each_obj_args eo_args;
5225 
5226  data.objspace = objspace;
5227  gc_report(5, objspace, "gc_verify_internal_consistency: start\n");
5228 
5229  /* check relations */
5230 
5232  eo_args.data = (void *)&data;
5233  objspace_each_objects((VALUE)&eo_args);
5234 
5235  if (data.err_count != 0) {
5236 #if RGENGC_CHECK_MODE >= 5
5237  objspace->rgengc.error_count = data.err_count;
5238  gc_marks_check(objspace, NULL, NULL);
5239  allrefs_dump(objspace);
5240 #endif
5241  rb_bug("gc_verify_internal_consistency: found internal inconsistency.");
5242  }
5243 
5244  /* check heap_page status */
5245  gc_verify_heap_pages(objspace);
5246 
5247  /* check counters */
5248 
5250  if (objspace_live_slots(objspace) != data.live_object_count) {
5251  fprintf(stderr, "heap_pages_final_slots: %d, objspace->profile.total_freed_objects: %d\n",
5252  (int)heap_pages_final_slots, (int)objspace->profile.total_freed_objects);
5253  rb_bug("inconsistent live slot nubmer: expect %"PRIuSIZE", but %"PRIuSIZE".", objspace_live_slots(objspace), data.live_object_count);
5254  }
5255  }
5256 
5257 #if USE_RGENGC
5258  if (!is_marking(objspace)) {
5259  if (objspace->rgengc.old_objects != data.old_object_count) {
5260  rb_bug("inconsistent old slot nubmer: expect %"PRIuSIZE", but %"PRIuSIZE".", objspace->rgengc.old_objects, data.old_object_count);
5261  }
5263  rb_bug("inconsistent old slot nubmer: expect %"PRIuSIZE", but %"PRIuSIZE".", objspace->rgengc.uncollectible_wb_unprotected_objects, data.remembered_shady_count);
5264  }
5265  }
5266 #endif
5267 
5268  if (!finalizing) {
5269  size_t list_count = 0;
5270 
5271  {
5273  while (z) {
5274  list_count++;
5275  z = RZOMBIE(z)->next;
5276  }
5277  }
5278 
5280  heap_pages_final_slots != list_count) {
5281 
5282  rb_bug("inconsistent finalizing object count:\n"
5283  " expect %"PRIuSIZE"\n"
5284  " but %"PRIuSIZE" zombies\n"
5285  " heap_pages_deferred_final list has %"PRIuSIZE" items.",
5287  data.zombie_object_count,
5288  list_count);
5289  }
5290  }
5291 
5292  gc_report(5, objspace, "gc_verify_internal_consistency: OK\n");
5293 
5294  return Qnil;
5295 }
5296 
5297 void
5299 {
5301 }
5302 
5303 /* marks */
5304 
5305 static void
5306 gc_marks_start(rb_objspace_t *objspace, int full_mark)
5307 {
5308  /* start marking */
5309  gc_report(1, objspace, "gc_marks_start: (%s)\n", full_mark ? "full" : "minor");
5311 
5312 #if USE_RGENGC
5313  if (full_mark) {
5314 #if GC_ENABLE_INCREMENTAL_MARK
5315  objspace->rincgc.step_slots = (objspace->marked_slots * 2) / ((objspace->rincgc.pooled_slots / HEAP_PAGE_OBJ_LIMIT) + 1);
5316 
5317  if (0) fprintf(stderr, "objspace->marked_slots: %d, objspace->rincgc.pooled_page_num: %d, objspace->rincgc.step_slots: %d, \n",
5318  (int)objspace->marked_slots, (int)objspace->rincgc.pooled_slots, (int)objspace->rincgc.step_slots);
5319 #endif
5320  objspace->flags.during_minor_gc = FALSE;
5321  objspace->profile.major_gc_count++;
5323  objspace->rgengc.old_objects = 0;
5324  objspace->rgengc.last_major_gc = objspace->profile.count;
5325  objspace->marked_slots = 0;
5327  }
5328  else {
5329  objspace->flags.during_minor_gc = TRUE;
5330  objspace->marked_slots =
5331  objspace->rgengc.old_objects + objspace->rgengc.uncollectible_wb_unprotected_objects; /* uncollectible objects are marked already */
5332  objspace->profile.minor_gc_count++;
5334  }
5335 #endif
5336 
5337  gc_mark_roots(objspace, NULL);
5338 
5339  gc_report(1, objspace, "gc_marks_start: (%s) end, stack in %d\n", full_mark ? "full" : "minor", (int)mark_stack_size(&objspace->mark_stack));
5340 }
5341 
5342 #if GC_ENABLE_INCREMENTAL_MARK
5343 static void
5345 {
5346  struct heap_page *page = heap_eden->pages;
5347 
5348  while (page) {
5349  bits_t *mark_bits = page->mark_bits;
5350  bits_t *wbun_bits = page->wb_unprotected_bits;
5351  RVALUE *p = page->start;
5352  RVALUE *offset = p - NUM_IN_PAGE(p);
5353  size_t j;
5354 
5355  for (j=0; j<HEAP_PAGE_BITMAP_LIMIT; j++) {
5356  bits_t bits = mark_bits[j] & wbun_bits[j];
5357 
5358  if (bits) {
5359  p = offset + j * BITS_BITLENGTH;
5360 
5361  do {
5362  if (bits & 1) {
5363  gc_report(2, objspace, "gc_marks_wb_unprotected_objects: marked shady: %s\n", obj_info((VALUE)p));
5364  if (RGENGC_CHECK_MODE > 0) {
5366  assert(RVALUE_MARKED((VALUE)p));
5367  }
5368  gc_mark_children(objspace, (VALUE)p);
5369  }
5370  p++;
5371  bits >>= 1;
5372  } while (bits);
5373  }
5374  }
5375 
5376  page = page->next;
5377  }
5378 
5379  gc_mark_stacked_objects_all(objspace);
5380 }
5381 
5382 static struct heap_page *
5384 {
5385  struct heap_page *page = heap->pooled_pages;
5386 
5387  if (page) {
5388  heap->pooled_pages = page->free_next;
5389  page->free_next = heap->free_pages;
5390  heap->free_pages = page;
5391  }
5392 
5393  return page;
5394 }
5395 #endif
5396 
5397 static int
5399 {
5400 #if GC_ENABLE_INCREMENTAL_MARK
5401  /* finish incremental GC */
5402  if (is_incremental_marking(objspace)) {
5403  if (heap_eden->pooled_pages) {
5405  gc_report(1, objspace, "gc_marks_finish: pooled pages are exists. retry.\n");
5406  return FALSE; /* continue marking phase */
5407  }
5408 
5409  if (RGENGC_CHECK_MODE && is_mark_stack_empty(&objspace->mark_stack) == 0) {
5410  rb_bug("gc_marks_finish: mark stack is not empty (%d).", (int)mark_stack_size(&objspace->mark_stack));
5411  }
5412 
5413  gc_mark_roots(objspace, 0);
5414 
5415  if (is_mark_stack_empty(&objspace->mark_stack) == FALSE) {
5416  gc_report(1, objspace, "gc_marks_finish: not empty (%d). retry.\n", (int)mark_stack_size(&objspace->mark_stack));
5417  return FALSE;
5418  }
5419 
5420 #if RGENGC_CHECK_MODE >= 2
5421  if (gc_verify_heap_pages(objspace) != 0) {
5422  rb_bug("gc_marks_finish (incremental): there are remembered old objects.");
5423  }
5424 #endif
5425 
5427  /* check children of all marked wb-unprotected objects */
5429  }
5430 #endif /* GC_ENABLE_INCREMENTAL_MARK */
5431 
5432 #if RGENGC_CHECK_MODE >= 2
5434 #endif
5435 
5436 #if USE_RGENGC
5437  if (is_full_marking(objspace)) {
5438  /* See the comment about RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR */
5439  const double r = gc_params.oldobject_limit_factor;
5441  objspace->rgengc.old_objects_limit = (size_t)(objspace->rgengc.old_objects * r);
5442  }
5443 #endif
5444 
5445 #if RGENGC_CHECK_MODE >= 4
5446  gc_marks_check(objspace, gc_check_after_marks_i, "after_marks");
5447 #endif
5448 
5449  {
5450  /* decide full GC is needed or not */
5451  rb_heap_t *heap = heap_eden;
5453  size_t sweep_slots = total_slots - objspace->marked_slots; /* will be swept slots */
5454  size_t max_free_slots = (size_t)(total_slots * gc_params.heap_free_slots_max_ratio);
5455  size_t min_free_slots = (size_t)(total_slots * gc_params.heap_free_slots_min_ratio);
5456  int full_marking = is_full_marking(objspace);
5457 
5458 #if RGENGC_CHECK_MODE
5459  assert(heap->total_slots >= objspace->marked_slots);
5460 #endif
5461 
5462  /* setup free-able page counts */
5463  if (max_free_slots < gc_params.heap_init_slots) max_free_slots = gc_params.heap_init_slots;
5464 
5465  if (sweep_slots > max_free_slots) {
5466  heap_pages_freeable_pages = (sweep_slots - max_free_slots) / HEAP_PAGE_OBJ_LIMIT;
5467  }
5468  else {
5470  }
5471 
5472  /* check free_min */
5473  if (min_free_slots < gc_params.heap_free_slots) min_free_slots = gc_params.heap_free_slots;
5474 
5475 #if USE_RGENGC
5476  if (sweep_slots < min_free_slots) {
5477  if (!full_marking) {
5478  if (objspace->profile.count - objspace->rgengc.last_major_gc < RVALUE_OLD_AGE) {
5479  full_marking = TRUE;
5480  /* do not update last_major_gc, because full marking is not done. */
5481  goto increment;
5482  }
5483  else {
5484  gc_report(1, objspace, "gc_marks_finish: next is full GC!!)\n");
5486  }
5487  }
5488  else {
5489  increment:
5490  gc_report(1, objspace, "gc_marks_finish: heap_set_increment!!\n");
5491  heap_set_increment(objspace, heap_extend_pages(objspace, sweep_slots, total_slots));
5492  heap_increment(objspace, heap);
5493  }
5494  }
5495 
5496  if (full_marking) {
5497  /* See the comment about RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR */
5498  const double r = gc_params.oldobject_limit_factor;
5500  objspace->rgengc.old_objects_limit = (size_t)(objspace->rgengc.old_objects * r);
5501  }
5502 
5505  }
5506  if (objspace->rgengc.old_objects > objspace->rgengc.old_objects_limit) {
5508  }
5509  if (RGENGC_FORCE_MAJOR_GC) {
5511  }
5512 
5513  gc_report(1, objspace, "gc_marks_finish (marks %d objects, old %d objects, total %d slots, sweep %d slots, increment: %d, next GC: %s)\n",
5514  (int)objspace->marked_slots, (int)objspace->rgengc.old_objects, (int)heap->total_slots, (int)sweep_slots, (int)heap_allocatable_pages,
5515  objspace->rgengc.need_major_gc ? "major" : "minor");
5516 #else /* USE_RGENGC */
5517  if (sweep_slots < min_free_slots) {
5518  gc_report(1, objspace, "gc_marks_finish: heap_set_increment!!\n");
5519  heap_set_increment(objspace, heap_extend_pages(objspace, sweep_slot, total_slot));
5520  heap_increment(objspace, heap);
5521  }
5522 #endif
5523  }
5524 
5526 
5527  return TRUE;
5528 }
5529 
5530 #if GC_ENABLE_INCREMENTAL_MARK
5531 static void
5532 gc_marks_step(rb_objspace_t *objspace, int slots)
5533 {
5534  if (RGENGC_CHECK_MODE) assert(is_marking(objspace));
5535 
5536  if (gc_mark_stacked_objects_incremental(objspace, slots)) {
5537  if (gc_marks_finish(objspace)) {
5538  /* finish */
5539  gc_sweep(objspace);
5540  }
5541  }
5542  if (0) fprintf(stderr, "objspace->marked_slots: %d\n", (int)objspace->marked_slots);
5543 }
5544 #endif
5545 
5546 static void
5548 {
5549  gc_report(1, objspace, "gc_marks_rest\n");
5550 
5551 #if GC_ENABLE_INCREMENTAL_MARK
5552  heap_eden->pooled_pages = NULL;
5553 #endif
5554 
5555  if (is_incremental_marking(objspace)) {
5556  do {
5557  while (gc_mark_stacked_objects_incremental(objspace, INT_MAX) == FALSE);
5558  } while (gc_marks_finish(objspace) == FALSE);
5559  }
5560  else {
5561  gc_mark_stacked_objects_all(objspace);
5562  gc_marks_finish(objspace);
5563  }
5564 
5565  /* move to sweep */
5566  gc_sweep(objspace);
5567 }
5568 
5569 #if GC_ENABLE_INCREMENTAL_MARK
5570 static void
5572 {
5573  int slots = 0;
5574  const char *from;
5575 
5577 
5578  gc_enter(objspace, "marks_continue");
5579 
5581  {
5582  if (heap->pooled_pages) {
5583  while (heap->pooled_pages && slots < HEAP_PAGE_OBJ_LIMIT) {
5584  struct heap_page *page = heap_move_pooled_pages_to_free_pages(heap);
5585  slots += page->free_slots;
5586  }
5587  from = "pooled-pages";
5588  }
5589  else if (heap_increment(objspace, heap)) {
5590  slots = heap->free_pages->free_slots;
5591  from = "incremented-pages";
5592  }
5593 
5594  if (slots > 0) {
5595  gc_report(2, objspace, "gc_marks_continue: provide %d slots from %s.\n", slots, from);
5596  gc_marks_step(objspace, (int)objspace->rincgc.step_slots);
5597  }
5598  else {
5599  gc_report(2, objspace, "gc_marks_continue: no more pooled pages (stack depth: %d).\n", (int)mark_stack_size(&objspace->mark_stack));
5600  gc_marks_rest(objspace);
5601  }
5602  }
5604 
5605  gc_exit(objspace, "marks_continue");
5606 }
5607 #endif
5608 
5609 static void
5610 gc_marks(rb_objspace_t *objspace, int full_mark)
5611 {
5612  gc_prof_mark_timer_start(objspace);
5613 
5615  {
5616  /* setup marking */
5617 
5618 #if USE_RGENGC
5619  gc_marks_start(objspace, full_mark);
5620  if (!is_incremental_marking(objspace)) {
5621  gc_marks_rest(objspace);
5622  }
5623 
5624 #if RGENGC_PROFILE > 0
5625  if (gc_prof_record(objspace)) {
5626  gc_profile_record *record = gc_prof_record(objspace);
5627  record->old_objects = objspace->rgengc.old_objects;
5628  }
5629 #endif
5630 
5631 #else /* USE_RGENGC */
5632  gc_marks_start(objspace, TRUE);
5633  gc_marks_rest(objspace);
5634 #endif
5635  }
5637  gc_prof_mark_timer_stop(objspace);
5638 }
5639 
5640 /* RGENGC */
5641 
5642 static void
5643 gc_report_body(int level, rb_objspace_t *objspace, const char *fmt, ...)
5644 {
5645  if (level <= RGENGC_DEBUG) {
5646  char buf[1024];
5647  FILE *out = stderr;
5648  va_list args;
5649  const char *status = " ";
5650 
5651 #if USE_RGENGC
5652  if (during_gc) {
5653  status = is_full_marking(objspace) ? "+" : "-";
5654  }
5655  else {
5656  if (is_lazy_sweeping(heap_eden)) {
5657  status = "S";
5658  }
5659  if (is_incremental_marking(objspace)) {
5660  status = "M";
5661  }
5662  }
5663 #endif
5664 
5665  va_start(args, fmt);
5666  vsnprintf(buf, 1024, fmt, args);
5667  va_end(args);
5668 
5669  fprintf(out, "%s|", status);
5670  fputs(buf, out);
5671  }
5672 }
5673 
5674 #if USE_RGENGC
5675 
5676 /* bit operations */
5677 
5678 static int
5680 {
5681  return RVALUE_REMEMBERED(obj);
5682 }
5683 
5684 static int
5686 {
5687  struct heap_page *page = GET_HEAP_PAGE(obj);
5688  bits_t *bits = &page->marking_bits[0];
5689 
5691 
5692  if (MARKED_IN_BITMAP(bits, obj)) {
5693  return FALSE;
5694  }
5695  else {
5697  MARK_IN_BITMAP(bits, obj);
5698  return TRUE;
5699  }
5700 }
5701 
5702 /* wb, etc */
5703 
5704 /* return FALSE if already remembered */
5705 static int
5707 {
5708  gc_report(6, objspace, "rgengc_remember: %s %s\n", obj_info(obj),
5709  rgengc_remembersetbits_get(objspace, obj) ? "was already remembered" : "is remembered now");
5710 
5712 
5713  if (RGENGC_CHECK_MODE) {
5714  if (RVALUE_WB_UNPROTECTED(obj)) rb_bug("rgengc_remember: %s is not wb protected.", obj_info(obj));
5715  }
5716 
5717 #if RGENGC_PROFILE > 0
5718  if (!rgengc_remembered(objspace, obj)) {
5719  if (RVALUE_WB_UNPROTECTED(obj) == 0) {
5720  objspace->profile.total_remembered_normal_object_count++;
5721 #if RGENGC_PROFILE >= 2
5722  objspace->profile.remembered_normal_object_count_types[BUILTIN_TYPE(obj)]++;
5723 #endif
5724  }
5725  }
5726 #endif /* RGENGC_PROFILE > 0 */
5727 
5728  return rgengc_remembersetbits_set(objspace, obj);
5729 }
5730 
5731 static int
5733 {
5734  int result = rgengc_remembersetbits_get(objspace, obj);
5736  gc_report(6, objspace, "rgengc_remembered: %s\n", obj_info(obj));
5737  return result;
5738 }
5739 
5740 #ifndef PROFILE_REMEMBERSET_MARK
5741 #define PROFILE_REMEMBERSET_MARK 0
5742 #endif
5743 
5744 static void
5746 {
5747  size_t j;
5748  struct heap_page *page = heap->pages;
5749 #if PROFILE_REMEMBERSET_MARK
5750  int has_old = 0, has_shady = 0, has_both = 0, skip = 0;
5751 #endif
5752  gc_report(1, objspace, "rgengc_rememberset_mark: start\n");
5753 
5754  while (page) {
5756  RVALUE *p = page->start;
5757  RVALUE *offset = p - NUM_IN_PAGE(p);
5758  bits_t bitset, bits[HEAP_PAGE_BITMAP_LIMIT];
5759  bits_t *marking_bits = page->marking_bits;
5762 #if PROFILE_REMEMBERSET_MARK
5763  if (page->flags.has_remembered_objects && page->flags.has_uncollectible_shady_objects) has_both++;
5764  else if (page->flags.has_remembered_objects) has_old++;
5765  else if (page->flags.has_uncollectible_shady_objects) has_shady++;
5766 #endif
5767  for (j=0; j<HEAP_PAGE_BITMAP_LIMIT; j++) {
5768  bits[j] = marking_bits[j] | (uncollectible_bits[j] & wb_unprotected_bits[j]);
5769  marking_bits[j] = 0;
5770  }
5772 
5773  for (j=0; j < HEAP_PAGE_BITMAP_LIMIT; j++) {
5774  bitset = bits[j];
5775 
5776  if (bitset) {
5777  p = offset + j * BITS_BITLENGTH;
5778 
5779  do {
5780  if (bitset & 1) {
5781  VALUE obj = (VALUE)p;
5782  gc_report(2, objspace, "rgengc_rememberset_mark: mark %s\n", obj_info(obj));
5783 
5784  if (RGENGC_CHECK_MODE) {
5787  }
5788 
5789  gc_mark_children(objspace, obj);
5790  }
5791  p++;
5792  bitset >>= 1;
5793  } while (bitset);
5794  }
5795  }
5796  }
5797 #if PROFILE_REMEMBERSET_MARK
5798  else {
5799  skip++;
5800  }
5801 #endif
5802 
5803  page = page->next;
5804  }
5805 
5806 #if PROFILE_REMEMBERSET_MARK
5807  fprintf(stderr, "%d\t%d\t%d\t%d\n", has_both, has_old, has_shady, skip);
5808 #endif
5809  gc_report(1, objspace, "rgengc_rememberset_mark: finished\n");
5810 }
5811 
5812 static void
5814 {
5815  struct heap_page *page = heap->pages;
5816 
5817  while (page) {
5818  memset(&page->mark_bits[0], 0, HEAP_PAGE_BITMAP_SIZE);
5819  memset(&page->marking_bits[0], 0, HEAP_PAGE_BITMAP_SIZE);
5820  memset(&page->uncollectible_bits[0], 0, HEAP_PAGE_BITMAP_SIZE);
5823  page = page->next;
5824  }
5825 }
5826 
5827 /* RGENGC: APIs */
5828 
5829 NOINLINE(static void gc_writebarrier_generational(VALUE a, VALUE b, rb_objspace_t *objspace));
5830 
5831 static void
5833 {
5834  if (RGENGC_CHECK_MODE) {
5835  if (!RVALUE_OLD_P(a)) rb_bug("gc_writebarrier_generational: %s is not an old object.", obj_info(a));
5836  if ( RVALUE_OLD_P(b)) rb_bug("gc_writebarrier_generational: %s is an old object.", obj_info(b));
5837  if (is_incremental_marking(objspace)) rb_bug("gc_writebarrier_generational: called while incremental marking: %s -> %s", obj_info(a), obj_info(b));
5838  }
5839 
5840 #if 1
5841  /* mark `a' and remember (default behavior) */
5842  if (!rgengc_remembered(objspace, a)) {
5843  rgengc_remember(objspace, a);
5844  gc_report(1, objspace, "gc_writebarrier_generational: %s (remembered) -> %s\n", obj_info(a), obj_info(b));
5845  }
5846 #else
5847  /* mark `b' and remember */
5849  if (RVALUE_WB_UNPROTECTED(b)) {
5850  gc_remember_unprotected(objspace, b);
5851  }
5852  else {
5853  RVALUE_AGE_SET_OLD(objspace, b);
5854  rgengc_remember(objspace, b);
5855  }
5856 
5857  gc_report(1, objspace, "gc_writebarrier_generational: %s -> %s (remembered)\n", obj_info(a), obj_info(b));
5858 #endif
5859 
5862 }
5863 
5864 #if GC_ENABLE_INCREMENTAL_MARK
5865 static void
5866 gc_mark_from(rb_objspace_t *objspace, VALUE obj, VALUE parent)
5867 {
5868  gc_mark_set_parent(objspace, parent);
5869  rgengc_check_relation(objspace, obj);
5870  if (gc_mark_set(objspace, obj) == FALSE) return;
5871  gc_aging(objspace, obj);
5872  gc_grey(objspace, obj);
5873 }
5874 
5875 NOINLINE(static void gc_writebarrier_incremental(VALUE a, VALUE b, rb_objspace_t *objspace));
5876 
5877 static void
5879 {
5880  gc_report(2, objspace, "gc_writebarrier_incremental: [LG] %s -> %s\n", obj_info(a), obj_info(b));
5881 
5882  if (RVALUE_BLACK_P(a)) {
5883  if (RVALUE_WHITE_P(b)) {
5884  if (!RVALUE_WB_UNPROTECTED(a)) {
5885  gc_report(2, objspace, "gc_writebarrier_incremental: [IN] %s -> %s\n", obj_info(a), obj_info(b));
5886  gc_mark_from(objspace, b, a);
5887  }
5888  }
5889  else if (RVALUE_OLD_P(a) && !RVALUE_OLD_P(b)) {
5890  if (!RVALUE_WB_UNPROTECTED(b)) {
5891  gc_report(1, objspace, "gc_writebarrier_incremental: [GN] %s -> %s\n", obj_info(a), obj_info(b));
5892  RVALUE_AGE_SET_OLD(objspace, b);
5893 
5894  if (RVALUE_BLACK_P(b)) {
5895  gc_grey(objspace, b);
5896  }
5897  }
5898  else {
5899  gc_report(1, objspace, "gc_writebarrier_incremental: [LL] %s -> %s\n", obj_info(a), obj_info(b));
5900  gc_remember_unprotected(objspace, b);
5901  }
5902  }
5903  }
5904 }
5905 #else
5906 #define gc_writebarrier_incremental(a, b, objspace)
5907 #endif
5908 
5909 void
5911 {
5912  rb_objspace_t *objspace = &rb_objspace;
5913 
5914  if (RGENGC_CHECK_MODE && SPECIAL_CONST_P(a)) rb_bug("rb_gc_writebarrier: a is special const");
5915  if (RGENGC_CHECK_MODE && SPECIAL_CONST_P(b)) rb_bug("rb_gc_writebarrier: b is special const");
5916 
5917  if (!is_incremental_marking(objspace)) {
5918  if (!RVALUE_OLD_P(a) || RVALUE_OLD_P(b)) {
5919  return;
5920  }
5921  else {
5922  gc_writebarrier_generational(a, b, objspace);
5923  }
5924  }
5925  else { /* slow path */
5926  gc_writebarrier_incremental(a, b, objspace);
5927  }
5928 }
5929 
5930 void
5932 {
5933  if (RVALUE_WB_UNPROTECTED(obj)) {
5934  return;
5935  }
5936  else {
5937  rb_objspace_t *objspace = &rb_objspace;
5938 
5939  gc_report(2, objspace, "rb_gc_writebarrier_unprotect: %s %s\n", obj_info(obj),
5940  rgengc_remembered(objspace, obj) ? " (already remembered)" : "");
5941 
5942  if (RVALUE_OLD_P(obj)) {
5943  gc_report(1, objspace, "rb_gc_writebarrier_unprotect: %s\n", obj_info(obj));
5944  RVALUE_DEMOTE(objspace, obj);
5945  gc_mark_set(objspace, obj);
5946  gc_remember_unprotected(objspace, obj);
5947 
5948 #if RGENGC_PROFILE
5949  objspace->profile.total_shade_operation_count++;
5950 #if RGENGC_PROFILE >= 2
5951  objspace->profile.shade_operation_count_types[BUILTIN_TYPE(obj)]++;
5952 #endif /* RGENGC_PROFILE >= 2 */
5953 #endif /* RGENGC_PROFILE */
5954  }
5955  else {
5956  RVALUE_AGE_RESET(obj);
5957  }
5958 
5960  }
5961 }
5962 
5963 /*
5964  * remember `obj' if needed.
5965  */
5966 void
5968 {
5969  rb_objspace_t *objspace = &rb_objspace;
5970 
5971  gc_report(1, objspace, "rb_gc_writebarrier_remember: %s\n", obj_info(obj));
5972 
5973  if (is_incremental_marking(objspace)) {
5974  if (RVALUE_BLACK_P(obj)) {
5975  gc_grey(objspace, obj);
5976  }
5977  }
5978  else {
5979  if (RVALUE_OLD_P(obj)) {
5980  rgengc_remember(objspace, obj);
5981  }
5982  }
5983 }
5984 
5986 
5987 static int
5989 {
5990  fprintf(stderr, "%s\t%d\n", (char *)key, (int)val);
5991  return ST_CONTINUE;
5992 }
5993 
5994 static void
5996 {
5997  st_foreach(rgengc_unprotect_logging_table, rgengc_unprotect_logging_exit_func_i, 0);
5998 }
5999 
6000 void
6001 rb_gc_unprotect_logging(void *objptr, const char *filename, int line)
6002 {
6003  VALUE obj = (VALUE)objptr;
6004 
6005  if (rgengc_unprotect_logging_table == 0) {
6006  rgengc_unprotect_logging_table = st_init_strtable();
6008  }
6009 
6010  if (RVALUE_WB_UNPROTECTED(obj) == 0) {
6011  char buff[0x100];
6012  st_data_t cnt = 1;
6013  char *ptr = buff;
6014 
6015  snprintf(ptr, 0x100 - 1, "%s|%s:%d", obj_info(obj), filename, line);
6016 
6017  if (st_lookup(rgengc_unprotect_logging_table, (st_data_t)ptr, &cnt)) {
6018  cnt++;
6019  }
6020  else {
6021  ptr = (char *)malloc(strlen(buff) + 1);
6022  if (!ptr) rb_memerror();
6023  strcpy(ptr, buff);
6024  }
6025  st_insert(rgengc_unprotect_logging_table, (st_data_t)ptr, cnt);
6026  }
6027 }
6028 #endif /* USE_RGENGC */
6029 
6030 void
6032 {
6033 #if USE_RGENGC
6034  rb_objspace_t *objspace = &rb_objspace;
6035 
6036  if (RVALUE_WB_UNPROTECTED(obj) && !RVALUE_WB_UNPROTECTED(dest)) {
6037  if (!RVALUE_OLD_P(dest)) {
6039  RVALUE_AGE_RESET_RAW(dest);
6040  }
6041  else {
6042  RVALUE_DEMOTE(objspace, dest);
6043  }
6044  }
6045 
6047 #endif
6048 }
6049 
6050 /* RGENGC analysis information */
6051 
6052 VALUE
6054 {
6055 #if USE_RGENGC
6056  return RVALUE_WB_UNPROTECTED(obj) ? Qfalse : Qtrue;
6057 #else
6058  return Qfalse;
6059 #endif
6060 }
6061 
6062 VALUE
6064 {
6065  return OBJ_PROMOTED(obj) ? Qtrue : Qfalse;
6066 }
6067 
6068 size_t
6069 rb_obj_gc_flags(VALUE obj, ID* flags, size_t max)
6070 {
6071  size_t n = 0;
6072  static ID ID_marked;
6073 #if USE_RGENGC
6074  static ID ID_wb_protected, ID_old, ID_marking, ID_uncollectible;
6075 #endif
6076 
6077  if (!ID_marked) {
6078 #define I(s) ID_##s = rb_intern(#s);
6079  I(marked);
6080 #if USE_RGENGC
6081  I(wb_protected);
6082  I(old);
6083  I(marking);
6084  I(uncollectible);
6085 #endif
6086 #undef I
6087  }
6088 
6089 #if USE_RGENGC
6090  if (RVALUE_WB_UNPROTECTED(obj) == 0 && n<max) flags[n++] = ID_wb_protected;
6091  if (RVALUE_OLD_P(obj) && n<max) flags[n++] = ID_old;
6092  if (RVALUE_UNCOLLECTIBLE(obj) && n<max) flags[n++] = ID_uncollectible;
6093  if (MARKED_IN_BITMAP(GET_HEAP_MARKING_BITS(obj), obj) && n<max) flags[n++] = ID_marking;
6094 #endif
6095  if (MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(obj), obj) && n<max) flags[n++] = ID_marked;
6096  return n;
6097 }
6098 
6099 /* GC */
6100 
6101 void
6103 {
6104  rb_objspace_t *objspace = &rb_objspace;
6105 
6106 #if USE_RGENGC
6107  int is_old = RVALUE_OLD_P(obj);
6108 
6109  gc_report(2, objspace, "rb_gc_force_recycle: %s\n", obj_info(obj));
6110 
6111  if (is_old) {
6112  if (RVALUE_MARKED(obj)) {
6113  objspace->rgengc.old_objects--;
6114  }
6115  }
6118 
6119 #if GC_ENABLE_INCREMENTAL_MARK
6120  if (is_incremental_marking(objspace)) {
6121  if (MARKED_IN_BITMAP(GET_HEAP_MARKING_BITS(obj), obj)) {
6122  invalidate_mark_stack(&objspace->mark_stack, obj);
6124  }
6126  }
6127  else {
6128 #endif
6129  if (is_old || !GET_HEAP_PAGE(obj)->flags.before_sweep) {
6131  }
6133 #if GC_ENABLE_INCREMENTAL_MARK
6134  }
6135 #endif
6136 #endif
6137 
6138  objspace->profile.total_freed_objects++;
6139 
6140  heap_page_add_freeobj(objspace, GET_HEAP_PAGE(obj), obj);
6141 
6142  /* Disable counting swept_slots because there are no meaning.
6143  * if (!MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(p), p)) {
6144  * objspace->heap.swept_slots++;
6145  * }
6146  */
6147 }
6148 
6149 #ifndef MARK_OBJECT_ARY_BUCKET_SIZE
6150 #define MARK_OBJECT_ARY_BUCKET_SIZE 1024
6151 #endif
6152 
6153 void
6155 {
6156  VALUE ary_ary = GET_THREAD()->vm->mark_object_ary;
6157  VALUE ary = rb_ary_last(0, 0, ary_ary);
6158 
6159  if (ary == Qnil || RARRAY_LEN(ary) >= MARK_OBJECT_ARY_BUCKET_SIZE) {
6161  rb_ary_push(ary_ary, ary);
6162  }
6163 
6164  rb_ary_push(ary, obj);
6165 }
6166 
6167 void
6169 {
6170  rb_objspace_t *objspace = &rb_objspace;
6171  struct gc_list *tmp;
6172 
6173  tmp = ALLOC(struct gc_list);
6174  tmp->next = global_list;
6175  tmp->varptr = addr;
6176  global_list = tmp;
6177 }
6178 
6179 void
6181 {
6182  rb_objspace_t *objspace = &rb_objspace;
6183  struct gc_list *tmp = global_list;
6184 
6185  if (tmp->varptr == addr) {
6186  global_list = tmp->next;
6187  xfree(tmp);
6188  return;
6189  }
6190  while (tmp->next) {
6191  if (tmp->next->varptr == addr) {
6192  struct gc_list *t = tmp->next;
6193 
6194  tmp->next = tmp->next->next;
6195  xfree(t);
6196  break;
6197  }
6198  tmp = tmp->next;
6199  }
6200 }
6201 
6202 void
6204 {
6206 }
6207 
6208 #define GC_NOTIFY 0
6209 
6210 enum {
6215 };
6216 
6217 #define gc_stress_full_mark_after_malloc_p() \
6218  (FIXNUM_P(ruby_gc_stress_mode) && (FIX2LONG(ruby_gc_stress_mode) & (1<<gc_stress_full_mark_after_malloc)))
6219 
6220 static void
6222 {
6223  if (!heap->freelist && !heap->free_pages) {
6224  if (!heap_increment(objspace, heap)) {
6225  heap_set_increment(objspace, 1);
6226  heap_increment(objspace, heap);
6227  }
6228  }
6229 }
6230 
6231 static int
6233 {
6234  if (dont_gc || during_gc || ruby_disable_gc) {
6235  heap_ready_to_gc(objspace, heap_eden);
6236  return FALSE;
6237  }
6238  else {
6239  return TRUE;
6240  }
6241 }
6242 
6243 static void
6245 {
6246  gc_prof_set_malloc_info(objspace);
6247  {
6248  size_t inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0);
6249  size_t old_limit = malloc_limit;
6250 
6251  if (inc > malloc_limit) {
6252  malloc_limit = (size_t)(inc * gc_params.malloc_limit_growth_factor);
6253  if (gc_params.malloc_limit_max > 0 && /* ignore max-check if 0 */
6254  malloc_limit > gc_params.malloc_limit_max) {
6255  malloc_limit = gc_params.malloc_limit_max;
6256  }
6257  }
6258  else {
6259  malloc_limit = (size_t)(malloc_limit * 0.98); /* magic number */
6260  if (malloc_limit < gc_params.malloc_limit_min) {
6261  malloc_limit = gc_params.malloc_limit_min;
6262  }
6263  }
6264 
6265  if (0) {
6266  if (old_limit != malloc_limit) {
6267  fprintf(stderr, "[%"PRIuSIZE"] malloc_limit: %"PRIuSIZE" -> %"PRIuSIZE"\n",
6268  rb_gc_count(), old_limit, malloc_limit);
6269  }
6270  else {
6271  fprintf(stderr, "[%"PRIuSIZE"] malloc_limit: not changed (%"PRIuSIZE")\n",
6273  }
6274  }
6275  }
6276 
6277  /* reset oldmalloc info */
6278 #if RGENGC_ESTIMATE_OLDMALLOC
6279  if (!is_full_marking(objspace)) {
6280  if (objspace->rgengc.oldmalloc_increase > objspace->rgengc.oldmalloc_increase_limit) {
6282  objspace->rgengc.oldmalloc_increase_limit =
6283  (size_t)(objspace->rgengc.oldmalloc_increase_limit * gc_params.oldmalloc_limit_growth_factor);
6284 
6285  if (objspace->rgengc.oldmalloc_increase_limit > gc_params.oldmalloc_limit_max) {
6286  objspace->rgengc.oldmalloc_increase_limit = gc_params.oldmalloc_limit_max;
6287  }
6288  }
6289 
6290  if (0) fprintf(stderr, "%d\t%d\t%u\t%u\t%d\n",
6291  (int)rb_gc_count(),
6292  (int)objspace->rgengc.need_major_gc,
6293  (unsigned int)objspace->rgengc.oldmalloc_increase,
6294  (unsigned int)objspace->rgengc.oldmalloc_increase_limit,
6295  (unsigned int)gc_params.oldmalloc_limit_max);
6296  }
6297  else {
6298  /* major GC */
6299  objspace->rgengc.oldmalloc_increase = 0;
6300 
6301  if ((objspace->profile.latest_gc_info & GPR_FLAG_MAJOR_BY_OLDMALLOC) == 0) {
6302  objspace->rgengc.oldmalloc_increase_limit =
6303  (size_t)(objspace->rgengc.oldmalloc_increase_limit / ((gc_params.oldmalloc_limit_growth_factor - 1)/10 + 1));
6304  if (objspace->rgengc.oldmalloc_increase_limit < gc_params.oldmalloc_limit_min) {
6305  objspace->rgengc.oldmalloc_increase_limit = gc_params.oldmalloc_limit_min;
6306  }
6307  }
6308  }
6309 #endif
6310 }
6311 
6312 static int
6313 garbage_collect(rb_objspace_t *objspace, int full_mark, int immediate_mark, int immediate_sweep, int reason)
6314 {
6315 #if GC_PROFILE_MORE_DETAIL
6316  objspace->profile.prepare_time = getrusage_time();
6317 #endif
6318 
6319  gc_rest(objspace);
6320 
6321 #if GC_PROFILE_MORE_DETAIL
6322  objspace->profile.prepare_time = getrusage_time() - objspace->profile.prepare_time;
6323 #endif
6324 
6325  return gc_start(objspace, full_mark, immediate_mark, immediate_sweep, reason);
6326 }
6327 
6328 static int
6329 gc_start(rb_objspace_t *objspace, const int full_mark, const int immediate_mark, const unsigned int immediate_sweep, int reason)
6330 {
6331  int do_full_mark = full_mark;
6332  objspace->flags.immediate_sweep = immediate_sweep;
6333 
6334  if (!heap_allocated_pages) return FALSE; /* heap is not ready */
6335  if (reason != GPR_FLAG_METHOD && !ready_to_gc(objspace)) return TRUE; /* GC is not allowed */
6336 
6337  if (RGENGC_CHECK_MODE) {
6338  assert(gc_mode(objspace) == gc_mode_none);
6340  assert(!is_incremental_marking(objspace));
6341 #if RGENGC_CHECK_MODE >= 2
6343 #endif
6344  }
6345 
6346  gc_enter(objspace, "gc_start");
6347 
6348  if (ruby_gc_stressful) {
6350 
6351  if ((flag & (1<<gc_stress_no_major)) == 0) {
6352  do_full_mark = TRUE;
6353  }
6354 
6355  objspace->flags.immediate_sweep = !(flag & (1<<gc_stress_no_immediate_sweep));
6356  }
6357  else {
6358 #if USE_RGENGC
6359  if (objspace->rgengc.need_major_gc) {
6360  reason |= objspace->rgengc.need_major_gc;
6361  do_full_mark = TRUE;
6362  }
6363  else if (RGENGC_FORCE_MAJOR_GC) {
6364  reason = GPR_FLAG_MAJOR_BY_FORCE;
6365  do_full_mark = TRUE;
6366  }
6367 
6368  objspace->rgengc.need_major_gc = GPR_FLAG_NONE;
6369 #endif
6370  }
6371 
6372  if (do_full_mark && (reason & GPR_FLAG_MAJOR_MASK) == 0) {
6373  reason |= GPR_FLAG_MAJOR_BY_FORCE; /* GC by CAPI, METHOD, and so on. */
6374  }
6375 
6376 #if GC_ENABLE_INCREMENTAL_MARK
6377  if (!GC_ENABLE_INCREMENTAL_MARK || objspace->flags.dont_incremental || immediate_mark) {
6379  }
6380  else {
6381  objspace->flags.during_incremental_marking = do_full_mark;
6382  }
6383 #endif
6384 
6385  if (!GC_ENABLE_LAZY_SWEEP || objspace->flags.dont_incremental) {
6386  objspace->flags.immediate_sweep = TRUE;
6387  }
6388 
6389  if (objspace->flags.immediate_sweep) reason |= GPR_FLAG_IMMEDIATE_SWEEP;
6390 
6391  gc_report(1, objspace, "gc_start(%d, %d, %d, reason: %d) => %d, %d, %d\n",
6392  full_mark, immediate_mark, immediate_sweep, reason,
6393  do_full_mark, !is_incremental_marking(objspace), objspace->flags.immediate_sweep);
6394 
6395  objspace->profile.count++;
6396  objspace->profile.latest_gc_info = reason;
6399  gc_prof_setup_new_record(objspace, reason);
6400  gc_reset_malloc_info(objspace);
6401 
6402  gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_START, 0 /* TODO: pass minor/immediate flag? */);
6404 
6405  gc_prof_timer_start(objspace);
6406  {
6407  gc_marks(objspace, do_full_mark);
6408  }
6409  gc_prof_timer_stop(objspace);
6410 
6411  gc_exit(objspace, "gc_start");
6412  return TRUE;
6413 }
6414 
6415 static void
6417 {
6418  int marking = is_incremental_marking(objspace);
6419  int sweeping = is_lazy_sweeping(heap_eden);
6420 
6421  if (marking || sweeping) {
6422  gc_enter(objspace, "gc_rest");
6423 
6425 
6426  if (is_incremental_marking(objspace)) {
6428  gc_marks_rest(objspace);
6430  }
6431  if (is_lazy_sweeping(heap_eden)) {
6432  gc_sweep_rest(objspace);
6433  }
6434  gc_exit(objspace, "gc_rest");
6435  }
6436 }
6437 
6440  int reason;
6444 };
6445 
6446 static void
6448 {
6449  int i = 0;
6450  if (is_marking(objspace)) {
6451  buff[i++] = 'M';
6452 #if USE_RGENGC
6453  if (is_full_marking(objspace)) buff[i++] = 'F';
6454 #if GC_ENABLE_INCREMENTAL_MARK
6455  if (is_incremental_marking(objspace)) buff[i++] = 'I';
6456 #endif
6457 #endif
6458  }
6459  else if (is_sweeping(objspace)) {
6460  buff[i++] = 'S';
6461  if (is_lazy_sweeping(heap_eden)) buff[i++] = 'L';
6462  }
6463  else {
6464  buff[i++] = 'N';
6465  }
6466  buff[i] = '\0';
6467 }
6468 
6469 static const char *
6471 {
6472  static char buff[0x10];
6473  gc_current_status_fill(objspace, buff);
6474  return buff;
6475 }
6476 
6477 #if PRINT_ENTER_EXIT_TICK
6478 
6479 static tick_t last_exit_tick;
6480 static tick_t enter_tick;
6481 static int enter_count = 0;
6482 static char last_gc_status[0x10];
6483 
6484 static inline void
6485 gc_record(rb_objspace_t *objspace, int direction, const char *event)
6486 {
6487  if (direction == 0) { /* enter */
6488  enter_count++;
6489  enter_tick = tick();
6490  gc_current_status_fill(objspace, last_gc_status);
6491  }
6492  else { /* exit */
6493  tick_t exit_tick = tick();
6494  char current_gc_status[0x10];
6495  gc_current_status_fill(objspace, current_gc_status);
6496 #if 1
6497  /* [last mutator time] [gc time] [event] */
6498  fprintf(stderr, "%"PRItick"\t%"PRItick"\t%s\t[%s->%s|%c]\n",
6499  enter_tick - last_exit_tick,
6500  exit_tick - enter_tick,
6501  event,
6502  last_gc_status, current_gc_status,
6503  (objspace->profile.latest_gc_info & GPR_FLAG_MAJOR_MASK) ? '+' : '-');
6504  last_exit_tick = exit_tick;
6505 #else
6506  /* [enter_tick] [gc time] [event] */
6507  fprintf(stderr, "%"PRItick"\t%"PRItick"\t%s\t[%s->%s|%c]\n",
6508  enter_tick,
6509  exit_tick - enter_tick,
6510  event,
6511  last_gc_status, current_gc_status,
6512  (objspace->profile.latest_gc_info & GPR_FLAG_MAJOR_MASK) ? '+' : '-');
6513 #endif
6514  }
6515 }
6516 #else /* PRINT_ENTER_EXIT_TICK */
6517 static inline void
6518 gc_record(rb_objspace_t *objspace, int direction, const char *event)
6519 {
6520  /* null */
6521 }
6522 #endif /* PRINT_ENTER_EXIT_TICK */
6523 
6524 static inline void
6525 gc_enter(rb_objspace_t *objspace, const char *event)
6526 {
6527  if (RGENGC_CHECK_MODE) assert(during_gc == 0);
6529 
6530  during_gc = TRUE;
6531  gc_report(1, objspace, "gc_entr: %s [%s]\n", event, gc_current_status(objspace));
6532  gc_record(objspace, 0, event);
6533  gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_ENTER, 0); /* TODO: which parameter should be passed? */
6534 }
6535 
6536 static inline void
6537 gc_exit(rb_objspace_t *objspace, const char *event)
6538 {
6539  if (RGENGC_CHECK_MODE) assert(during_gc != 0);
6540 
6541  gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_EXIT, 0); /* TODO: which parameter should be passsed? */
6542  gc_record(objspace, 1, event);
6543  gc_report(1, objspace, "gc_exit: %s [%s]\n", event, gc_current_status(objspace));
6544  during_gc = FALSE;
6545 }
6546 
6547 static void *
6548 gc_with_gvl(void *ptr)
6549 {
6550  struct objspace_and_reason *oar = (struct objspace_and_reason *)ptr;
6551  return (void *)(VALUE)garbage_collect(oar->objspace, oar->full_mark, oar->immediate_mark, oar->immediate_sweep, oar->reason);
6552 }
6553 
6554 static int
6556 {
6557  if (dont_gc) return TRUE;
6558  if (ruby_thread_has_gvl_p()) {
6559  return garbage_collect(objspace, full_mark, immediate_mark, immediate_sweep, reason);
6560  }
6561  else {
6562  if (ruby_native_thread_p()) {
6563  struct objspace_and_reason oar;
6564  oar.objspace = objspace;
6565  oar.reason = reason;
6566  oar.full_mark = full_mark;
6569  return (int)(VALUE)rb_thread_call_with_gvl(gc_with_gvl, (void *)&oar);
6570  }
6571  else {
6572  /* no ruby thread */
6573  fprintf(stderr, "[FATAL] failed to allocate memory\n");
6574  exit(EXIT_FAILURE);
6575  }
6576  }
6577 }
6578 
6579 int
6581 {
6582  return garbage_collect(&rb_objspace, TRUE, TRUE, TRUE, GPR_FLAG_CAPI);
6583 }
6584 
6585 #undef Init_stack
6586 
6587 void
6588 Init_stack(volatile VALUE *addr)
6589 {
6590  ruby_init_stack(addr);
6591 }
6592 
6593 /*
6594  * call-seq:
6595  * GC.start -> nil
6596  * ObjectSpace.garbage_collect -> nil
6597  * include GC; garbage_collect -> nil
6598  * GC.start(full_mark: true, immediate_sweep: true) -> nil
6599  * ObjectSpace.garbage_collect(full_mark: true, immediate_sweep: true) -> nil
6600  * include GC; garbage_collect(full_mark: true, immediate_sweep: true) -> nil
6601  *
6602  * Initiates garbage collection, unless manually disabled.
6603  *
6604  * This method is defined with keyword arguments that default to true:
6605  *
6606  * def GC.start(full_mark: true, immediate_sweep: true); end
6607  *
6608  * Use full_mark: false to perform a minor GC.
6609  * Use immediate_sweep: false to defer sweeping (use lazy sweep).
6610  *
6611  * Note: These keyword arguments are implementation and version dependent. They
6612  * are not guaranteed to be future-compatible, and may be ignored if the
6613  * underlying implementation does not support them.
6614  */
6615 
6616 static VALUE
6618 {
6619  rb_objspace_t *objspace = &rb_objspace;
6621  VALUE opt = Qnil;
6622  static ID keyword_ids[3];
6623 
6624  rb_scan_args(argc, argv, "0:", &opt);
6625 
6626  if (!NIL_P(opt)) {
6627  VALUE kwvals[3];
6628 
6629  if (!keyword_ids[0]) {
6630  keyword_ids[0] = rb_intern("full_mark");
6631  keyword_ids[1] = rb_intern("immediate_mark");
6632  keyword_ids[2] = rb_intern("immediate_sweep");
6633  }
6634 
6635  rb_get_kwargs(opt, keyword_ids, 0, 3, kwvals);
6636 
6637  if (kwvals[0] != Qundef) full_mark = RTEST(kwvals[0]);
6638  if (kwvals[1] != Qundef) immediate_mark = RTEST(kwvals[1]);
6639  if (kwvals[2] != Qundef) immediate_sweep = RTEST(kwvals[2]);
6640  }
6641 
6642  garbage_collect(objspace, full_mark, immediate_mark, immediate_sweep, GPR_FLAG_METHOD);
6643  gc_finalize_deferred(objspace);
6644 
6645  return Qnil;
6646 }
6647 
6648 VALUE
6650 {
6651  rb_gc();
6652  return Qnil;
6653 }
6654 
6655 void
6656 rb_gc(void)
6657 {
6658  rb_objspace_t *objspace = &rb_objspace;
6659  garbage_collect(objspace, TRUE, TRUE, TRUE, GPR_FLAG_CAPI);
6660  gc_finalize_deferred(objspace);
6661 }
6662 
6663 int
6665 {
6666  rb_objspace_t *objspace = &rb_objspace;
6667  return during_gc;
6668 }
6669 
6670 #if RGENGC_PROFILE >= 2
6671 
6672 static const char *type_name(int type, VALUE obj);
6673 
6674 static void
6675 gc_count_add_each_types(VALUE hash, const char *name, const size_t *types)
6676 {
6677  VALUE result = rb_hash_new();
6678  int i;
6679  for (i=0; i<T_MASK; i++) {
6680  const char *type = type_name(i, 0);
6681  rb_hash_aset(result, ID2SYM(rb_intern(type)), SIZET2NUM(types[i]));
6682  }
6683  rb_hash_aset(hash, ID2SYM(rb_intern(name)), result);
6684 }
6685 #endif
6686 
6687 size_t
6689 {
6690  return rb_objspace.profile.count;
6691 }
6692 
6693 /*
6694  * call-seq:
6695  * GC.count -> Integer
6696  *
6697  * The number of times GC occurred.
6698  *
6699  * It returns the number of times GC occurred since the process started.
6700  *
6701  */
6702 
6703 static VALUE
6705 {
6706  return SIZET2NUM(rb_gc_count());
6707 }
6708 
6709 static VALUE
6710 gc_info_decode(rb_objspace_t *objspace, const VALUE hash_or_key, const int orig_flags)
6711 {
6712  static VALUE sym_major_by = Qnil, sym_gc_by, sym_immediate_sweep, sym_have_finalizer, sym_state;
6713  static VALUE sym_nofree, sym_oldgen, sym_shady, sym_force, sym_stress;
6714 #if RGENGC_ESTIMATE_OLDMALLOC
6715  static VALUE sym_oldmalloc;
6716 #endif
6717  static VALUE sym_newobj, sym_malloc, sym_method, sym_capi;
6718  static VALUE sym_none, sym_marking, sym_sweeping;
6719  VALUE hash = Qnil, key = Qnil;
6720  VALUE major_by;
6721  VALUE flags = orig_flags ? orig_flags : objspace->profile.latest_gc_info;
6722 
6723  if (SYMBOL_P(hash_or_key)) {
6724  key = hash_or_key;
6725  }
6726  else if (RB_TYPE_P(hash_or_key, T_HASH)) {
6727  hash = hash_or_key;
6728  }
6729  else {
6730  rb_raise(rb_eTypeError, "non-hash or symbol given");
6731  }
6732 
6733  if (sym_major_by == Qnil) {
6734 #define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
6735  S(major_by);
6736  S(gc_by);
6737  S(immediate_sweep);
6738  S(have_finalizer);
6739  S(state);
6740 
6741  S(stress);
6742  S(nofree);
6743  S(oldgen);
6744  S(shady);
6745  S(force);
6746 #if RGENGC_ESTIMATE_OLDMALLOC
6747  S(oldmalloc);
6748 #endif
6749  S(newobj);
6750  S(malloc);
6751  S(method);
6752  S(capi);
6753 
6754  S(none);
6755  S(marking);
6756  S(sweeping);
6757 #undef S
6758  }
6759 
6760 #define SET(name, attr) \
6761  if (key == sym_##name) \
6762  return (attr); \
6763  else if (hash != Qnil) \
6764  rb_hash_aset(hash, sym_##name, (attr));
6765 
6766  major_by =
6767  (flags & GPR_FLAG_MAJOR_BY_NOFREE) ? sym_nofree :
6768  (flags & GPR_FLAG_MAJOR_BY_OLDGEN) ? sym_oldgen :
6769  (flags & GPR_FLAG_MAJOR_BY_SHADY) ? sym_shady :
6770  (flags & GPR_FLAG_MAJOR_BY_FORCE) ? sym_force :
6771 #if RGENGC_ESTIMATE_OLDMALLOC
6772  (flags & GPR_FLAG_MAJOR_BY_OLDMALLOC) ? sym_oldmalloc :
6773 #endif
6774  Qnil;
6775  SET(major_by, major_by);
6776 
6777  SET(gc_by,
6778  (flags & GPR_FLAG_NEWOBJ) ? sym_newobj :
6779  (flags & GPR_FLAG_MALLOC) ? sym_malloc :
6780  (flags & GPR_FLAG_METHOD) ? sym_method :
6781  (flags & GPR_FLAG_CAPI) ? sym_capi :
6782  (flags & GPR_FLAG_STRESS) ? sym_stress :
6783  Qnil
6784  );
6785 
6786  SET(have_finalizer, (flags & GPR_FLAG_HAVE_FINALIZE) ? Qtrue : Qfalse);
6787  SET(immediate_sweep, (flags & GPR_FLAG_IMMEDIATE_SWEEP) ? Qtrue : Qfalse);
6788 
6789  if (orig_flags == 0) {
6790  SET(state, gc_mode(objspace) == gc_mode_none ? sym_none :
6791  gc_mode(objspace) == gc_mode_marking ? sym_marking : sym_sweeping);
6792  }
6793 #undef SET
6794 
6795  if (!NIL_P(key)) {/* matched key should return above */
6796  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
6797  }
6798 
6799  return hash;
6800 }
6801 
6802 VALUE
6804 {
6805  rb_objspace_t *objspace = &rb_objspace;
6806  return gc_info_decode(objspace, key, 0);
6807 }
6808 
6809 /*
6810  * call-seq:
6811  * GC.latest_gc_info -> {:gc_by=>:newobj}
6812  * GC.latest_gc_info(hash) -> hash
6813  * GC.latest_gc_info(:major_by) -> :malloc
6814  *
6815  * Returns information about the most recent garbage collection.
6816  */
6817 
6818 static VALUE
6820 {
6821  rb_objspace_t *objspace = &rb_objspace;
6822  VALUE arg = Qnil;
6823 
6824  if (rb_scan_args(argc, argv, "01", &arg) == 1) {
6825  if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
6826  rb_raise(rb_eTypeError, "non-hash or symbol given");
6827  }
6828  }
6829 
6830  if (arg == Qnil) {
6831  arg = rb_hash_new();
6832  }
6833 
6834  return gc_info_decode(objspace, arg, 0);
6835 }
6836 
6855 #if USE_RGENGC
6862 #if RGENGC_ESTIMATE_OLDMALLOC
6865 #endif
6866 #if RGENGC_PROFILE
6867  gc_stat_sym_total_generated_normal_object_count,
6868  gc_stat_sym_total_generated_shady_object_count,
6869  gc_stat_sym_total_shade_operation_count,
6870  gc_stat_sym_total_promoted_count,
6871  gc_stat_sym_total_remembered_normal_object_count,
6872  gc_stat_sym_total_remembered_shady_object_count,
6873 #endif
6874 #endif
6876 };
6877 
6888 #if USE_RGENGC
6893 #endif
6898 #if RGENGC_ESTIMATE_OLDMALLOC
6901 #endif
6903 };
6904 
6908 
6909 static void
6911 {
6912  if (gc_stat_symbols[0] == 0) {
6913 #define S(s) gc_stat_symbols[gc_stat_sym_##s] = ID2SYM(rb_intern_const(#s))
6914  S(count);
6916  S(heap_sorted_length);
6918  S(heap_available_slots);
6919  S(heap_live_slots);
6920  S(heap_free_slots);
6921  S(heap_final_slots);
6922  S(heap_marked_slots);
6923  S(heap_eden_pages);
6924  S(heap_tomb_pages);
6925  S(total_allocated_pages);
6926  S(total_freed_pages);
6927  S(total_allocated_objects);
6928  S(total_freed_objects);
6929  S(malloc_increase_bytes);
6930  S(malloc_increase_bytes_limit);
6931 #if USE_RGENGC
6932  S(minor_gc_count);
6933  S(major_gc_count);
6934  S(remembered_wb_unprotected_objects);
6935  S(remembered_wb_unprotected_objects_limit);
6936  S(old_objects);
6937  S(old_objects_limit);
6938 #if RGENGC_ESTIMATE_OLDMALLOC
6939  S(oldmalloc_increase_bytes);
6940  S(oldmalloc_increase_bytes_limit);
6941 #endif
6942 #if RGENGC_PROFILE
6943  S(total_generated_normal_object_count);
6944  S(total_generated_shady_object_count);
6945  S(total_shade_operation_count);
6946  S(total_promoted_count);
6947  S(total_remembered_normal_object_count);
6948  S(total_remembered_shady_object_count);
6949 #endif /* RGENGC_PROFILE */
6950 #endif /* USE_RGENGC */
6951 #undef S
6952 #define S(s) gc_stat_compat_symbols[gc_stat_compat_sym_##s] = ID2SYM(rb_intern_const(#s))
6953  S(gc_stat_heap_used);
6954  S(heap_eden_page_length);
6955  S(heap_tomb_page_length);
6956  S(heap_increment);
6957  S(heap_length);
6958  S(heap_live_slot);
6959  S(heap_free_slot);
6960  S(heap_final_slot);
6961  S(heap_swept_slot);
6962 #if USE_RGEGC
6963  S(remembered_shady_object);
6964  S(remembered_shady_object_limit);
6965  S(old_object);
6966  S(old_object_limit);
6967 #endif
6968  S(total_allocated_object);
6969  S(total_freed_object);
6970  S(malloc_increase);
6971  S(malloc_limit);
6972 #if RGENGC_ESTIMATE_OLDMALLOC
6973  S(oldmalloc_increase);
6974  S(oldmalloc_limit);
6975 #endif
6976 #undef S
6977 
6978  {
6979  VALUE table = gc_stat_compat_table = rb_hash_new();
6980  rb_obj_hide(table);
6982 
6983  /* compatibility layer for Ruby 2.1 */
6984 #define OLD_SYM(s) gc_stat_compat_symbols[gc_stat_compat_sym_##s]
6985 #define NEW_SYM(s) gc_stat_symbols[gc_stat_sym_##s]
6986  rb_hash_aset(table, OLD_SYM(gc_stat_heap_used), NEW_SYM(heap_allocated_pages));
6987  rb_hash_aset(table, OLD_SYM(heap_eden_page_length), NEW_SYM(heap_eden_pages));
6988  rb_hash_aset(table, OLD_SYM(heap_tomb_page_length), NEW_SYM(heap_tomb_pages));
6990  rb_hash_aset(table, OLD_SYM(heap_length), NEW_SYM(heap_sorted_length));
6991  rb_hash_aset(table, OLD_SYM(heap_live_slot), NEW_SYM(heap_live_slots));
6992  rb_hash_aset(table, OLD_SYM(heap_free_slot), NEW_SYM(heap_free_slots));
6993  rb_hash_aset(table, OLD_SYM(heap_final_slot), NEW_SYM(heap_final_slots));
6994 #if USE_RGEGC
6995  rb_hash_aset(table, OLD_SYM(remembered_shady_object), NEW_SYM(remembered_wb_unprotected_objects));
6996  rb_hash_aset(table, OLD_SYM(remembered_shady_object_limit), NEW_SYM(remembered_wb_unprotected_objects_limit));
6997  rb_hash_aset(table, OLD_SYM(old_object), NEW_SYM(old_objects));
6998  rb_hash_aset(table, OLD_SYM(old_object_limit), NEW_SYM(old_objects_limit));
6999 #endif
7000  rb_hash_aset(table, OLD_SYM(total_allocated_object), NEW_SYM(total_allocated_objects));
7001  rb_hash_aset(table, OLD_SYM(total_freed_object), NEW_SYM(total_freed_objects));
7002  rb_hash_aset(table, OLD_SYM(malloc_increase), NEW_SYM(malloc_increase_bytes));
7003  rb_hash_aset(table, OLD_SYM(malloc_limit), NEW_SYM(malloc_increase_bytes_limit));
7004 #if RGENGC_ESTIMATE_OLDMALLOC
7005  rb_hash_aset(table, OLD_SYM(oldmalloc_increase), NEW_SYM(oldmalloc_increase_bytes));
7006  rb_hash_aset(table, OLD_SYM(oldmalloc_limit), NEW_SYM(oldmalloc_increase_bytes_limit));
7007 #endif
7008 #undef OLD_SYM
7009 #undef NEW_SYM
7010  rb_obj_freeze(table);
7011  }
7012  }
7013 }
7014 
7015 static VALUE
7017 {
7018  VALUE new_key = rb_hash_lookup(gc_stat_compat_table, key);
7019 
7020  if (!NIL_P(new_key)) {
7021  static int warned = 0;
7022  if (warned == 0) {
7023  rb_warn("GC.stat keys were changed from Ruby 2.1. "
7024  "In this case, you refer to obsolete `%"PRIsVALUE"' (new key is `%"PRIsVALUE"'). "
7025  "Please check <https://bugs.ruby-lang.org/issues/9924> for more information.",
7026  key, new_key);
7027  warned = 1;
7028  }
7029  }
7030 
7031  return new_key;
7032 }
7033 
7034 static VALUE
7036 {
7037  VALUE key, new_key;
7038 
7039  Check_Type(hash, T_HASH);
7040  rb_check_arity(argc, 2, 2);
7041  key = argv[1];
7042 
7043  if ((new_key = compat_key(key)) != Qnil) {
7044  return rb_hash_lookup(hash, new_key);
7045  }
7046 
7047  return Qnil;
7048 }
7049 
7050 static size_t
7052 {
7053  rb_objspace_t *objspace = &rb_objspace;
7054  VALUE hash = Qnil, key = Qnil;
7055 
7057 
7058  if (RB_TYPE_P(hash_or_sym, T_HASH)) {
7059  hash = hash_or_sym;
7060 
7061  if (NIL_P(RHASH_IFNONE(hash))) {
7062  static VALUE default_proc_for_compat = 0;
7063  if (default_proc_for_compat == 0) { /* TODO: it should be */
7064  default_proc_for_compat = rb_proc_new(default_proc_for_compat_func, Qnil);
7065  rb_gc_register_mark_object(default_proc_for_compat);
7066  }
7067  rb_hash_set_default_proc(hash, default_proc_for_compat);
7068  }
7069  }
7070  else if (SYMBOL_P(hash_or_sym)) {
7071  key = hash_or_sym;
7072  }
7073  else {
7074  rb_raise(rb_eTypeError, "non-hash or symbol argument");
7075  }
7076 
7077 #define SET(name, attr) \
7078  if (key == gc_stat_symbols[gc_stat_sym_##name]) \
7079  return attr; \
7080  else if (hash != Qnil) \
7081  rb_hash_aset(hash, gc_stat_symbols[gc_stat_sym_##name], SIZET2NUM(attr));
7082 
7083  again:
7084  SET(count, objspace->profile.count);
7085 
7086  /* implementation dependent counters */
7088  SET(heap_sorted_length, heap_pages_sorted_length);
7090  SET(heap_available_slots, objspace_available_slots(objspace));
7091  SET(heap_live_slots, objspace_live_slots(objspace));
7092  SET(heap_free_slots, objspace_free_slots(objspace));
7093  SET(heap_final_slots, heap_pages_final_slots);
7094  SET(heap_marked_slots, objspace->marked_slots);
7095  SET(heap_eden_pages, heap_eden->total_pages);
7096  SET(heap_tomb_pages, heap_tomb->total_pages);
7097  SET(total_allocated_pages, objspace->profile.total_allocated_pages);
7098  SET(total_freed_pages, objspace->profile.total_freed_pages);
7099  SET(total_allocated_objects, objspace->total_allocated_objects);
7100  SET(total_freed_objects, objspace->profile.total_freed_objects);
7101  SET(malloc_increase_bytes, malloc_increase);
7102  SET(malloc_increase_bytes_limit, malloc_limit);
7103 #if USE_RGENGC
7104  SET(minor_gc_count, objspace->profile.minor_gc_count);
7105  SET(major_gc_count, objspace->profile.major_gc_count);
7106  SET(remembered_wb_unprotected_objects, objspace->rgengc.uncollectible_wb_unprotected_objects);
7107  SET(remembered_wb_unprotected_objects_limit, objspace->rgengc.uncollectible_wb_unprotected_objects_limit);
7108  SET(old_objects, objspace->rgengc.old_objects);
7109  SET(old_objects_limit, objspace->rgengc.old_objects_limit);
7110 #if RGENGC_ESTIMATE_OLDMALLOC
7111  SET(oldmalloc_increase_bytes, objspace->rgengc.oldmalloc_increase);
7112  SET(oldmalloc_increase_bytes_limit, objspace->rgengc.oldmalloc_increase_limit);
7113 #endif
7114 
7115 #if RGENGC_PROFILE
7116  SET(total_generated_normal_object_count, objspace->profile.total_generated_normal_object_count);
7117  SET(total_generated_shady_object_count, objspace->profile.total_generated_shady_object_count);
7118  SET(total_shade_operation_count, objspace->profile.total_shade_operation_count);
7119  SET(total_promoted_count, objspace->profile.total_promoted_count);
7120  SET(total_remembered_normal_object_count, objspace->profile.total_remembered_normal_object_count);
7121  SET(total_remembered_shady_object_count, objspace->profile.total_remembered_shady_object_count);
7122 #endif /* RGENGC_PROFILE */
7123 #endif /* USE_RGENGC */
7124 #undef SET
7125 
7126  if (!NIL_P(key)) { /* matched key should return above */
7127  VALUE new_key;
7128  if ((new_key = compat_key(key)) != Qnil) {
7129  key = new_key;
7130  goto again;
7131  }
7132  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
7133  }
7134 
7135 #if defined(RGENGC_PROFILE) && RGENGC_PROFILE >= 2
7136  if (hash != Qnil) {
7137  gc_count_add_each_types(hash, "generated_normal_object_count_types", objspace->profile.generated_normal_object_count_types);
7138  gc_count_add_each_types(hash, "generated_shady_object_count_types", objspace->profile.generated_shady_object_count_types);
7139  gc_count_add_each_types(hash, "shade_operation_count_types", objspace->profile.shade_operation_count_types);
7140  gc_count_add_each_types(hash, "promoted_types", objspace->profile.promoted_types);
7141  gc_count_add_each_types(hash, "remembered_normal_object_count_types", objspace->profile.remembered_normal_object_count_types);
7142  gc_count_add_each_types(hash, "remembered_shady_object_count_types", objspace->profile.remembered_shady_object_count_types);
7143  }
7144 #endif
7145 
7146  return 0;
7147 }
7148 
7149 /*
7150  * call-seq:
7151  * GC.stat -> Hash
7152  * GC.stat(hash) -> hash
7153  * GC.stat(:key) -> Numeric
7154  *
7155  * Returns a Hash containing information about the GC.
7156  *
7157  * The hash includes information about internal statistics about GC such as:
7158  *
7159  * {
7160  * :count=>0,
7161  * :heap_allocated_pages=>24,
7162  * :heap_sorted_length=>24,
7163  * :heap_allocatable_pages=>0,
7164  * :heap_available_slots=>9783,
7165  * :heap_live_slots=>7713,
7166  * :heap_free_slots=>2070,
7167  * :heap_final_slots=>0,
7168  * :heap_marked_slots=>0,
7169  * :heap_eden_pages=>24,
7170  * :heap_tomb_pages=>0,
7171  * :total_allocated_pages=>24,
7172  * :total_freed_pages=>0,
7173  * :total_allocated_objects=>7796,
7174  * :total_freed_objects=>83,
7175  * :malloc_increase_bytes=>2389312,
7176  * :malloc_increase_bytes_limit=>16777216,
7177  * :minor_gc_count=>0,
7178  * :major_gc_count=>0,
7179  * :remembered_wb_unprotected_objects=>0,
7180  * :remembered_wb_unprotected_objects_limit=>0,
7181  * :old_objects=>0,
7182  * :old_objects_limit=>0,
7183  * :oldmalloc_increase_bytes=>2389760,
7184  * :oldmalloc_increase_bytes_limit=>16777216
7185  * }
7186  *
7187  * The contents of the hash are implementation specific and may be changed in
7188  * the future.
7189  *
7190  * This method is only expected to work on C Ruby.
7191  *
7192  */
7193 
7194 static VALUE
7196 {
7197  VALUE arg = Qnil;
7198 
7199  if (rb_scan_args(argc, argv, "01", &arg) == 1) {
7200  if (SYMBOL_P(arg)) {
7201  size_t value = gc_stat_internal(arg);
7202  return SIZET2NUM(value);
7203  }
7204  else if (!RB_TYPE_P(arg, T_HASH)) {
7205  rb_raise(rb_eTypeError, "non-hash or symbol given");
7206  }
7207  }
7208 
7209  if (arg == Qnil) {
7210  arg = rb_hash_new();
7211  }
7212  gc_stat_internal(arg);
7213  return arg;
7214 }
7215 
7216 size_t
7218 {
7219  if (SYMBOL_P(key)) {
7220  size_t value = gc_stat_internal(key);
7221  return value;
7222  }
7223  else {
7224  gc_stat_internal(key);
7225  return 0;
7226  }
7227 }
7228 
7229 /*
7230  * call-seq:
7231  * GC.stress -> integer, true or false
7232  *
7233  * Returns current status of GC stress mode.
7234  */
7235 
7236 static VALUE
7238 {
7239  rb_objspace_t *objspace = &rb_objspace;
7240  return ruby_gc_stress_mode;
7241 }
7242 
7243 static void
7245 {
7246  objspace->flags.gc_stressful = RTEST(flag);
7247  objspace->gc_stress_mode = flag;
7248 }
7249 
7250 /*
7251  * call-seq:
7252  * GC.stress = flag -> flag
7253  *
7254  * Updates the GC stress mode.
7255  *
7256  * When stress mode is enabled, the GC is invoked at every GC opportunity:
7257  * all memory and object allocations.
7258  *
7259  * Enabling stress mode will degrade performance, it is only for debugging.
7260  *
7261  * flag can be true, false, or an integer bit-ORed following flags.
7262  * 0x01:: no major GC
7263  * 0x02:: no immediate sweep
7264  * 0x04:: full mark after malloc/calloc/realloc
7265  */
7266 
7267 static VALUE
7269 {
7270  rb_objspace_t *objspace = &rb_objspace;
7271  gc_stress_set(objspace, flag);
7272  return flag;
7273 }
7274 
7275 /*
7276  * call-seq:
7277  * GC.enable -> true or false
7278  *
7279  * Enables garbage collection, returning +true+ if garbage
7280  * collection was previously disabled.
7281  *
7282  * GC.disable #=> false
7283  * GC.enable #=> true
7284  * GC.enable #=> false
7285  *
7286  */
7287 
7288 VALUE
7290 {
7291  rb_objspace_t *objspace = &rb_objspace;
7292  int old = dont_gc;
7293 
7294  dont_gc = FALSE;
7295  return old ? Qtrue : Qfalse;
7296 }
7297 
7298 /*
7299  * call-seq:
7300  * GC.disable -> true or false
7301  *
7302  * Disables garbage collection, returning +true+ if garbage
7303  * collection was already disabled.
7304  *
7305  * GC.disable #=> false
7306  * GC.disable #=> true
7307  *
7308  */
7309 
7310 VALUE
7312 {
7313  rb_objspace_t *objspace = &rb_objspace;
7314  int old = dont_gc;
7315 
7316  gc_rest(objspace);
7317 
7318  dont_gc = TRUE;
7319  return old ? Qtrue : Qfalse;
7320 }
7321 
7322 static int
7323 get_envparam_size(const char *name, size_t *default_value, size_t lower_bound)
7324 {
7325  char *ptr = getenv(name);
7326  ssize_t val;
7327 
7328  if (ptr != NULL && *ptr) {
7329  size_t unit = 0;
7330  char *end;
7331 #if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
7332  val = strtoll(ptr, &end, 0);
7333 #else
7334  val = strtol(ptr, &end, 0);
7335 #endif
7336  switch (*end) {
7337  case 'k': case 'K':
7338  unit = 1024;
7339  ++end;
7340  break;
7341  case 'm': case 'M':
7342  unit = 1024*1024;
7343  ++end;
7344  break;
7345  case 'g': case 'G':
7346  unit = 1024*1024*1024;
7347  ++end;
7348  break;
7349  }
7350  while (*end && isspace((unsigned char)*end)) end++;
7351  if (*end) {
7352  if (RTEST(ruby_verbose)) fprintf(stderr, "invalid string for %s: %s\n", name, ptr);
7353  return 0;
7354  }
7355  if (unit > 0) {
7356  if (val < -(ssize_t)(SIZE_MAX / 2 / unit) || (ssize_t)(SIZE_MAX / 2 / unit) < val) {
7357  if (RTEST(ruby_verbose)) fprintf(stderr, "%s=%s is ignored because it overflows\n", name, ptr);
7358  return 0;
7359  }
7360  val *= unit;
7361  }
7362  if (val > 0 && (size_t)val > lower_bound) {
7363  if (RTEST(ruby_verbose)) {
7364  fprintf(stderr, "%s=%"PRIdSIZE" (default value: %"PRIuSIZE")\n", name, val, *default_value);
7365  }
7366  *default_value = (size_t)val;
7367  return 1;
7368  }
7369  else {
7370  if (RTEST(ruby_verbose)) {
7371  fprintf(stderr, "%s=%"PRIdSIZE" (default value: %"PRIuSIZE") is ignored because it must be greater than %"PRIuSIZE".\n",
7372  name, val, *default_value, lower_bound);
7373  }
7374  return 0;
7375  }
7376  }
7377  return 0;
7378 }
7379 
7380 static int
7381 get_envparam_double(const char *name, double *default_value, double lower_bound, double upper_bound, int accept_zero)
7382 {
7383  char *ptr = getenv(name);
7384  double val;
7385 
7386  if (ptr != NULL && *ptr) {
7387  char *end;
7388  val = strtod(ptr, &end);
7389  if (!*ptr || *end) {
7390  if (RTEST(ruby_verbose)) fprintf(stderr, "invalid string for %s: %s\n", name, ptr);
7391  return 0;
7392  }
7393 
7394  if (accept_zero && val == 0.0) {
7395  goto accept;
7396  }
7397  else if (val <= lower_bound) {
7398  if (RTEST(ruby_verbose)) {
7399  fprintf(stderr, "%s=%f (default value: %f) is ignored because it must be greater than %f.\n",
7400  name, val, *default_value, lower_bound);
7401  }
7402  }
7403  else if (upper_bound != 0.0 && /* ignore upper_bound if it is 0.0 */
7404  val > upper_bound) {
7405  if (RTEST(ruby_verbose)) {
7406  fprintf(stderr, "%s=%f (default value: %f) is ignored because it must be lower than %f.\n",
7407  name, val, *default_value, upper_bound);
7408  }
7409  }
7410  else {
7411  accept:
7412  if (RTEST(ruby_verbose)) fprintf(stderr, "%s=%f (default value: %f)\n", name, val, *default_value);
7413  *default_value = val;
7414  return 1;
7415  }
7416  }
7417  return 0;
7418 }
7419 
7420 static void
7422 {
7423  size_t min_pages;
7424  rb_objspace_t *objspace = &rb_objspace;
7425 
7426  min_pages = gc_params.heap_init_slots / HEAP_PAGE_OBJ_LIMIT;
7427  if (min_pages > heap_eden->total_pages) {
7428  heap_add_pages(objspace, heap_eden, min_pages - heap_eden->total_pages);
7429  }
7430 }
7431 
7432 /*
7433  * GC tuning environment variables
7434  *
7435  * * RUBY_GC_HEAP_INIT_SLOTS
7436  * - Initial allocation slots.
7437  * * RUBY_GC_HEAP_FREE_SLOTS
7438  * - Prepare at least this amount of slots after GC.
7439  * - Allocate slots if there are not enough slots.
7440  * * RUBY_GC_HEAP_GROWTH_FACTOR (new from 2.1)
7441  * - Allocate slots by this factor.
7442  * - (next slots number) = (current slots number) * (this factor)
7443  * * RUBY_GC_HEAP_GROWTH_MAX_SLOTS (new from 2.1)
7444  * - Allocation rate is limited to this number of slots.
7445  * * RUBY_GC_HEAP_FREE_SLOTS_MIN_RATIO (new from 2.4)
7446  * - Allocate additional pages when the number of free slots is
7447  * lower than the value (total_slots * (this ratio)).
7448  * * RUBY_GC_HEAP_FREE_SLOTS_GOAL_RATIO (new from 2.4)
7449  * - Allocate slots to satisfy this formula:
7450  * free_slots = total_slots * goal_ratio
7451  * - In other words, prepare (total_slots * goal_ratio) free slots.
7452  * - if this value is 0.0, then use RUBY_GC_HEAP_GROWTH_FACTOR directly.
7453  * * RUBY_GC_HEAP_FREE_SLOTS_MAX_RATIO (new from 2.4)
7454  * - Allow to free pages when the number of free slots is
7455  * greater than the value (total_slots * (this ratio)).
7456  * * RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR (new from 2.1.1)
7457  * - Do full GC when the number of old objects is more than R * N
7458  * where R is this factor and
7459  * N is the number of old objects just after last full GC.
7460  *
7461  * * obsolete
7462  * * RUBY_FREE_MIN -> RUBY_GC_HEAP_FREE_SLOTS (from 2.1)
7463  * * RUBY_HEAP_MIN_SLOTS -> RUBY_GC_HEAP_INIT_SLOTS (from 2.1)
7464  *
7465  * * RUBY_GC_MALLOC_LIMIT
7466  * * RUBY_GC_MALLOC_LIMIT_MAX (new from 2.1)
7467  * * RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR (new from 2.1)
7468  *
7469  * * RUBY_GC_OLDMALLOC_LIMIT (new from 2.1)
7470  * * RUBY_GC_OLDMALLOC_LIMIT_MAX (new from 2.1)
7471  * * RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR (new from 2.1)
7472  */
7473 
7474 void
7475 ruby_gc_set_params(int safe_level)
7476 {
7477  if (safe_level > 0) return;
7478 
7479  /* RUBY_GC_HEAP_FREE_SLOTS */
7480  if (get_envparam_size("RUBY_GC_HEAP_FREE_SLOTS", &gc_params.heap_free_slots, 0)) {
7481  /* ok */
7482  }
7483  else if (get_envparam_size("RUBY_FREE_MIN", &gc_params.heap_free_slots, 0)) {
7484  rb_warn("RUBY_FREE_MIN is obsolete. Use RUBY_GC_HEAP_FREE_SLOTS instead.");
7485  }
7486 
7487  /* RUBY_GC_HEAP_INIT_SLOTS */
7488  if (get_envparam_size("RUBY_GC_HEAP_INIT_SLOTS", &gc_params.heap_init_slots, 0)) {
7490  }
7491  else if (get_envparam_size("RUBY_HEAP_MIN_SLOTS", &gc_params.heap_init_slots, 0)) {
7492  rb_warn("RUBY_HEAP_MIN_SLOTS is obsolete. Use RUBY_GC_HEAP_INIT_SLOTS instead.");
7494  }
7495 
7496  get_envparam_double("RUBY_GC_HEAP_GROWTH_FACTOR", &gc_params.growth_factor, 1.0, 0.0, FALSE);
7497  get_envparam_size ("RUBY_GC_HEAP_GROWTH_MAX_SLOTS", &gc_params.growth_max_slots, 0);
7498  get_envparam_double("RUBY_GC_HEAP_FREE_SLOTS_MIN_RATIO", &gc_params.heap_free_slots_min_ratio,
7499  0.0, 1.0, FALSE);
7500  get_envparam_double("RUBY_GC_HEAP_FREE_SLOTS_MAX_RATIO", &gc_params.heap_free_slots_max_ratio,
7501  gc_params.heap_free_slots_min_ratio, 1.0, FALSE);
7502  get_envparam_double("RUBY_GC_HEAP_FREE_SLOTS_GOAL_RATIO", &gc_params.heap_free_slots_goal_ratio,
7504  get_envparam_double("RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR", &gc_params.oldobject_limit_factor, 0.0, 0.0, TRUE);
7505 
7506  get_envparam_size ("RUBY_GC_MALLOC_LIMIT", &gc_params.malloc_limit_min, 0);
7507  get_envparam_size ("RUBY_GC_MALLOC_LIMIT_MAX", &gc_params.malloc_limit_max, 0);
7508  get_envparam_double("RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR", &gc_params.malloc_limit_growth_factor, 1.0, 0.0, FALSE);
7509 
7510 #if RGENGC_ESTIMATE_OLDMALLOC
7511  if (get_envparam_size("RUBY_GC_OLDMALLOC_LIMIT", &gc_params.oldmalloc_limit_min, 0)) {
7512  rb_objspace_t *objspace = &rb_objspace;
7513  objspace->rgengc.oldmalloc_increase_limit = gc_params.oldmalloc_limit_min;
7514  }
7515  get_envparam_size ("RUBY_GC_OLDMALLOC_LIMIT_MAX", &gc_params.oldmalloc_limit_max, 0);
7516  get_envparam_double("RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR", &gc_params.oldmalloc_limit_growth_factor, 1.0, 0.0, FALSE);
7517 #endif
7518 }
7519 
7520 void
7521 rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data)
7522 {
7523  rb_objspace_t *objspace = &rb_objspace;
7524 
7525  if (is_markable_object(objspace, obj)) {
7526  struct mark_func_data_struct mfd;
7527  mfd.mark_func = func;
7528  mfd.data = data;
7529  PUSH_MARK_FUNC_DATA(&mfd);
7530  gc_mark_children(objspace, obj);
7532  }
7533 }
7534 
7536  const char *category;
7537  void (*func)(const char *category, VALUE, void *);
7538  void *data;
7539 };
7540 
7541 static void
7542 root_objects_from(VALUE obj, void *ptr)
7543 {
7544  const struct root_objects_data *data = (struct root_objects_data *)ptr;
7545  (*data->func)(data->category, obj, data->data);
7546 }
7547 
7548 void
7549 rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data)
7550 {
7551  rb_objspace_t *objspace = &rb_objspace;
7552  struct root_objects_data data;
7553  struct mark_func_data_struct mfd;
7554 
7555  data.func = func;
7556  data.data = passing_data;
7557 
7558  mfd.mark_func = root_objects_from;
7559  mfd.data = &data;
7560 
7561  PUSH_MARK_FUNC_DATA(&mfd);
7562  gc_mark_roots(objspace, &data.category);
7564 }
7565 
7566 /*
7567  ------------------------ Extended allocator ------------------------
7568 */
7569 
7570 static void objspace_xfree(rb_objspace_t *objspace, void *ptr, size_t size);
7571 
7572 static void *
7574 {
7575  rb_raise(rb_eNoMemError, "%s", (const char *)ptr);
7576  return 0; /* should not be reached */
7577 }
7578 
7579 static void
7581 {
7582  if (ruby_thread_has_gvl_p()) {
7583  rb_raise(rb_eNoMemError, "%s", msg);
7584  }
7585  else {
7586  if (ruby_native_thread_p()) {
7588  }
7589  else {
7590  fprintf(stderr, "[FATAL] %s\n", msg);
7591  exit(EXIT_FAILURE);
7592  }
7593  }
7594 }
7595 
7596 static void *
7598 {
7599  rb_memerror();
7600  return 0;
7601 }
7602 
7603 static void
7605 {
7606  if (ruby_thread_has_gvl_p()) {
7607  rb_memerror();
7608  }
7609  else {
7610  if (ruby_native_thread_p()) {
7612  }
7613  else {
7614  /* no ruby thread */
7615  fprintf(stderr, "[FATAL] failed to allocate memory\n");
7616  exit(EXIT_FAILURE);
7617  }
7618  }
7619 }
7620 
7621 void
7623 {
7624  rb_thread_t *th = GET_THREAD();
7625  rb_objspace_t *objspace = rb_objspace_of(th->vm);
7626 
7627  if (during_gc) gc_exit(objspace, "rb_memerror");
7628 
7629  if (!nomem_error ||
7631  fprintf(stderr, "[FATAL] failed to allocate memory\n");
7632  exit(EXIT_FAILURE);
7633  }
7636  GET_THREAD()->errinfo = nomem_error;
7637  TH_JUMP_TAG(th, TAG_RAISE);
7638  }
7641 }
7642 
7643 static void *
7644 aligned_malloc(size_t alignment, size_t size)
7645 {
7646  void *res;
7647 
7648 #if defined __MINGW32__
7649  res = __mingw_aligned_malloc(size, alignment);
7650 #elif defined _WIN32
7651  void *_aligned_malloc(size_t, size_t);
7652  res = _aligned_malloc(size, alignment);
7653 #elif defined(HAVE_POSIX_MEMALIGN)
7654  if (posix_memalign(&res, alignment, size) == 0) {
7655  return res;
7656  }
7657  else {
7658  return NULL;
7659  }
7660 #elif defined(HAVE_MEMALIGN)
7661  res = memalign(alignment, size);
7662 #else
7663  char* aligned;
7664  res = malloc(alignment + size + sizeof(void*));
7665  aligned = (char*)res + alignment + sizeof(void*);
7666  aligned -= ((VALUE)aligned & (alignment - 1));
7667  ((void**)aligned)[-1] = res;
7668  res = (void*)aligned;
7669 #endif
7670 
7671 #if defined(_DEBUG) || GC_DEBUG
7672  /* alignment must be a power of 2 */
7673  assert(((alignment - 1) & alignment) == 0);
7674  assert(alignment % sizeof(void*) == 0);
7675 #endif
7676  return res;
7677 }
7678 
7679 static void
7680 aligned_free(void *ptr)
7681 {
7682 #if defined __MINGW32__
7683  __mingw_aligned_free(ptr);
7684 #elif defined _WIN32
7685  _aligned_free(ptr);
7686 #elif defined(HAVE_MEMALIGN) || defined(HAVE_POSIX_MEMALIGN)
7687  free(ptr);
7688 #else
7689  free(((void**)ptr)[-1]);
7690 #endif
7691 }
7692 
7693 static inline size_t
7694 objspace_malloc_size(rb_objspace_t *objspace, void *ptr, size_t hint)
7695 {
7696 #ifdef HAVE_MALLOC_USABLE_SIZE
7697  return malloc_usable_size(ptr);
7698 #else
7699  return hint;
7700 #endif
7701 }
7702 
7707 };
7708 
7709 static inline void
7710 atomic_sub_nounderflow(size_t *var, size_t sub)
7711 {
7712  if (sub == 0) return;
7713 
7714  while (1) {
7715  size_t val = *var;
7716  if (val < sub) sub = val;
7717  if (ATOMIC_SIZE_CAS(*var, val, val-sub) == val) break;
7718  }
7719 }
7720 
7721 static void
7723 {
7726  }
7727 }
7728 
7729 static void
7730 objspace_malloc_increase(rb_objspace_t *objspace, void *mem, size_t new_size, size_t old_size, enum memop_type type)
7731 {
7732  if (new_size > old_size) {
7733  ATOMIC_SIZE_ADD(malloc_increase, new_size - old_size);
7734 #if RGENGC_ESTIMATE_OLDMALLOC
7735  ATOMIC_SIZE_ADD(objspace->rgengc.oldmalloc_increase, new_size - old_size);
7736 #endif
7737  }
7738  else {
7739  atomic_sub_nounderflow(&malloc_increase, old_size - new_size);
7740 #if RGENGC_ESTIMATE_OLDMALLOC
7741  atomic_sub_nounderflow(&objspace->rgengc.oldmalloc_increase, old_size - new_size);
7742 #endif
7743  }
7744 
7745  if (type == MEMOP_TYPE_MALLOC) {
7746  retry:
7749  gc_rest(objspace); /* gc_rest can reduce malloc_increase */
7750  goto retry;
7751  }
7753  }
7754  }
7755 
7756 #if MALLOC_ALLOCATED_SIZE
7757  if (new_size >= old_size) {
7758  ATOMIC_SIZE_ADD(objspace->malloc_params.allocated_size, new_size - old_size);
7759  }
7760  else {
7761  size_t dec_size = old_size - new_size;
7762  size_t allocated_size = objspace->malloc_params.allocated_size;
7763 
7764 #if MALLOC_ALLOCATED_SIZE_CHECK
7765  if (allocated_size < dec_size) {
7766  rb_bug("objspace_malloc_increase: underflow malloc_params.allocated_size.");
7767  }
7768 #endif
7769  atomic_sub_nounderflow(&objspace->malloc_params.allocated_size, dec_size);
7770  }
7771 
7772  if (0) fprintf(stderr, "increase - ptr: %p, type: %s, new_size: %d, old_size: %d\n",
7773  mem,
7774  type == MEMOP_TYPE_MALLOC ? "malloc" :
7775  type == MEMOP_TYPE_FREE ? "free " :
7776  type == MEMOP_TYPE_REALLOC ? "realloc": "error",
7777  (int)new_size, (int)old_size);
7778 
7779  switch (type) {
7780  case MEMOP_TYPE_MALLOC:
7781  ATOMIC_SIZE_INC(objspace->malloc_params.allocations);
7782  break;
7783  case MEMOP_TYPE_FREE:
7784  {
7785  size_t allocations = objspace->malloc_params.allocations;
7786  if (allocations > 0) {
7787  atomic_sub_nounderflow(&objspace->malloc_params.allocations, 1);
7788  }
7789 #if MALLOC_ALLOCATED_SIZE_CHECK
7790  else {
7791  if (RGENGC_CHECK_MODE) assert(objspace->malloc_params.allocations > 0);
7792  }
7793 #endif
7794  }
7795  break;
7796  case MEMOP_TYPE_REALLOC: /* ignore */ break;
7797  }
7798 #endif
7799 }
7800 
7801 static inline size_t
7803 {
7804  if (size == 0) size = 1;
7805 
7806 #if CALC_EXACT_MALLOC_SIZE
7807  size += sizeof(size_t);
7808 #endif
7809 
7810  return size;
7811 }
7812 
7813 static inline void *
7814 objspace_malloc_fixup(rb_objspace_t *objspace, void *mem, size_t size)
7815 {
7816 #if CALC_EXACT_MALLOC_SIZE
7817  ((size_t *)mem)[0] = size;
7818  mem = (size_t *)mem + 1;
7819 #endif
7820 
7821  return mem;
7822 }
7823 
7824 #define TRY_WITH_GC(alloc) do { \
7825  objspace_malloc_gc_stress(objspace); \
7826  if (!(alloc) && \
7827  (!garbage_collect_with_gvl(objspace, TRUE, TRUE, TRUE, GPR_FLAG_MALLOC) || /* full/immediate mark && immediate sweep */ \
7828  !(alloc))) { \
7829  ruby_memerror(); \
7830  } \
7831  } while (0)
7832 
7833 /* this shouldn't be called directly.
7834  * objspace_xmalloc and objspace_xmalloc2 checks allocation size.
7835  */
7836 static void *
7837 objspace_xmalloc0(rb_objspace_t *objspace, size_t size)
7838 {
7839  void *mem;
7840 
7841  size = objspace_malloc_prepare(objspace, size);
7842  TRY_WITH_GC(mem = malloc(size));
7843  size = objspace_malloc_size(objspace, mem, size);
7844  objspace_malloc_increase(objspace, mem, size, 0, MEMOP_TYPE_MALLOC);
7845  return objspace_malloc_fixup(objspace, mem, size);
7846 }
7847 
7848 static void *
7849 objspace_xmalloc(rb_objspace_t *objspace, size_t size)
7850 {
7851  if ((ssize_t)size < 0) {
7852  negative_size_allocation_error("too large allocation size");
7853  }
7854  return objspace_xmalloc0(objspace, size);
7855 }
7856 
7857 static inline size_t
7858 xmalloc2_size(const size_t count, const size_t elsize)
7859 {
7860  size_t ret;
7861  if (rb_mul_size_overflow(count, elsize, SSIZE_MAX, &ret)) {
7862  ruby_malloc_size_overflow(count, elsize);
7863  }
7864  return ret;
7865 }
7866 
7867 static void *
7868 objspace_xmalloc2(rb_objspace_t *objspace, size_t n, size_t size)
7869 {
7870  return objspace_xmalloc0(&rb_objspace, xmalloc2_size(n, size));
7871 }
7872 
7873 static void *
7874 objspace_xrealloc(rb_objspace_t *objspace, void *ptr, size_t new_size, size_t old_size)
7875 {
7876  void *mem;
7877 
7878  if (!ptr) return objspace_xmalloc(objspace, new_size);
7879 
7880  /*
7881  * The behavior of realloc(ptr, 0) is implementation defined.
7882  * Therefore we don't use realloc(ptr, 0) for portability reason.
7883  * see http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_400.htm
7884  */
7885  if (new_size == 0) {
7886  objspace_xfree(objspace, ptr, old_size);
7887  return 0;
7888  }
7889 
7890 #if CALC_EXACT_MALLOC_SIZE
7891  new_size += sizeof(size_t);
7892  ptr = (size_t *)ptr - 1;
7893  old_size = ((size_t *)ptr)[0];
7894 #endif
7895 
7896  old_size = objspace_malloc_size(objspace, ptr, old_size);
7897  TRY_WITH_GC(mem = realloc(ptr, new_size));
7898  new_size = objspace_malloc_size(objspace, mem, new_size);
7899 
7900 #if CALC_EXACT_MALLOC_SIZE
7901  ((size_t *)mem)[0] = new_size;
7902  mem = (size_t *)mem + 1;
7903 #endif
7904 
7905  objspace_malloc_increase(objspace, mem, new_size, old_size, MEMOP_TYPE_REALLOC);
7906 
7907  return mem;
7908 }
7909 
7910 static void
7911 objspace_xfree(rb_objspace_t *objspace, void *ptr, size_t old_size)
7912 {
7913 #if CALC_EXACT_MALLOC_SIZE
7914  ptr = ((size_t *)ptr) - 1;
7915  old_size = ((size_t*)ptr)[0];
7916 #endif
7917  old_size = objspace_malloc_size(objspace, ptr, old_size);
7918 
7919  free(ptr);
7920 
7921  objspace_malloc_increase(objspace, ptr, 0, old_size, MEMOP_TYPE_FREE);
7922 }
7923 
7924 static void *
7925 ruby_xmalloc0(size_t size)
7926 {
7927  return objspace_xmalloc0(&rb_objspace, size);
7928 }
7929 
7930 void *
7931 ruby_xmalloc(size_t size)
7932 {
7933  return objspace_xmalloc(&rb_objspace, size);
7934 }
7935 
7936 void
7937 ruby_malloc_size_overflow(size_t count, size_t elsize)
7938 {
7940  "malloc: possible integer overflow (%"PRIuSIZE"*%"PRIuSIZE")",
7941  count, elsize);
7942 }
7943 
7944 void *
7945 ruby_xmalloc2(size_t n, size_t size)
7946 {
7947  return objspace_xmalloc2(&rb_objspace, n, size);
7948 }
7949 
7950 static void *
7951 objspace_xcalloc(rb_objspace_t *objspace, size_t count, size_t elsize)
7952 {
7953  void *mem;
7954  size_t size;
7955 
7956  size = xmalloc2_size(count, elsize);
7957  size = objspace_malloc_prepare(objspace, size);
7958 
7959  TRY_WITH_GC(mem = calloc(1, size));
7960  size = objspace_malloc_size(objspace, mem, size);
7961  objspace_malloc_increase(objspace, mem, size, 0, MEMOP_TYPE_MALLOC);
7962  return objspace_malloc_fixup(objspace, mem, size);
7963 }
7964 
7965 void *
7966 ruby_xcalloc(size_t n, size_t size)
7967 {
7968  return objspace_xcalloc(&rb_objspace, n, size);
7969 }
7970 
7971 #ifdef ruby_sized_xrealloc
7972 #undef ruby_sized_xrealloc
7973 #endif
7974 void *
7975 ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size)
7976 {
7977  return objspace_xrealloc(&rb_objspace, ptr, new_size, old_size);
7978 }
7979 
7980 void *
7981 ruby_xrealloc(void *ptr, size_t new_size)
7982 {
7983  return ruby_sized_xrealloc(ptr, new_size, 0);
7984 }
7985 
7986 #ifdef ruby_sized_xrealloc2
7987 #undef ruby_sized_xrealloc2
7988 #endif
7989 void *
7990 ruby_sized_xrealloc2(void *ptr, size_t n, size_t size, size_t old_n)
7991 {
7992  size_t len = size * n;
7993  if (n != 0 && size != len / n) {
7994  rb_raise(rb_eArgError, "realloc: possible integer overflow");
7995  }
7996  return objspace_xrealloc(&rb_objspace, ptr, len, old_n * size);
7997 }
7998 
7999 void *
8000 ruby_xrealloc2(void *ptr, size_t n, size_t size)
8001 {
8002  return ruby_sized_xrealloc2(ptr, n, size, 0);
8003 }
8004 
8005 #ifdef ruby_sized_xfree
8006 #undef ruby_sized_xfree
8007 #endif
8008 void
8009 ruby_sized_xfree(void *x, size_t size)
8010 {
8011  if (x) {
8012  objspace_xfree(&rb_objspace, x, size);
8013  }
8014 }
8015 
8016 void
8017 ruby_xfree(void *x)
8018 {
8019  ruby_sized_xfree(x, 0);
8020 }
8021 
8022 /* Mimic ruby_xmalloc, but need not rb_objspace.
8023  * should return pointer suitable for ruby_xfree
8024  */
8025 void *
8026 ruby_mimmalloc(size_t size)
8027 {
8028  void *mem;
8029 #if CALC_EXACT_MALLOC_SIZE
8030  size += sizeof(size_t);
8031 #endif
8032  mem = malloc(size);
8033 #if CALC_EXACT_MALLOC_SIZE
8034  /* set 0 for consistency of allocated_size/allocations */
8035  ((size_t *)mem)[0] = 0;
8036  mem = (size_t *)mem + 1;
8037 #endif
8038  return mem;
8039 }
8040 
8041 void
8042 ruby_mimfree(void *ptr)
8043 {
8044  size_t *mem = (size_t *)ptr;
8045 #if CALC_EXACT_MALLOC_SIZE
8046  mem = mem - 1;
8047 #endif
8048  free(mem);
8049 }
8050 
8051 void *
8052 rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t size, size_t cnt)
8053 {
8054  NODE *s;
8055  void *ptr;
8056 
8057  s = rb_node_newnode(NODE_ALLOCA, 0, 0, 0);
8058  ptr = ruby_xmalloc0(size);
8059  s->u1.value = (VALUE)ptr;
8060  s->u3.cnt = cnt;
8061  *store = (VALUE)s;
8062  return ptr;
8063 }
8064 
8065 void *
8066 rb_alloc_tmp_buffer(volatile VALUE *store, long len)
8067 {
8068  long cnt;
8069 
8070  if (len < 0 || (cnt = (long)roomof(len, sizeof(VALUE))) < 0) {
8071  rb_raise(rb_eArgError, "negative buffer size (or size too big)");
8072  }
8073 
8074  return rb_alloc_tmp_buffer_with_count(store, len, cnt);
8075 }
8076 
8077 void
8078 rb_free_tmp_buffer(volatile VALUE *store)
8079 {
8080  VALUE s = ATOMIC_VALUE_EXCHANGE(*store, 0);
8081  if (s) {
8082  void *ptr = ATOMIC_PTR_EXCHANGE(RNODE(s)->u1.node, 0);
8083  RNODE(s)->u3.cnt = 0;
8084  ruby_xfree(ptr);
8085  }
8086 }
8087 
8088 #if MALLOC_ALLOCATED_SIZE
8089 /*
8090  * call-seq:
8091  * GC.malloc_allocated_size -> Integer
8092  *
8093  * Returns the size of memory allocated by malloc().
8094  *
8095  * Only available if ruby was built with +CALC_EXACT_MALLOC_SIZE+.
8096  */
8097 
8098 static VALUE
8099 gc_malloc_allocated_size(VALUE self)
8100 {
8101  return UINT2NUM(rb_objspace.malloc_params.allocated_size);
8102 }
8103 
8104 /*
8105  * call-seq:
8106  * GC.malloc_allocations -> Integer
8107  *
8108  * Returns the number of malloc() allocations.
8109  *
8110  * Only available if ruby was built with +CALC_EXACT_MALLOC_SIZE+.
8111  */
8112 
8113 static VALUE
8114 gc_malloc_allocations(VALUE self)
8115 {
8116  return UINT2NUM(rb_objspace.malloc_params.allocations);
8117 }
8118 #endif
8119 
8120 void
8122 {
8123  rb_objspace_t *objspace = &rb_objspace;
8124  if (diff > 0) {
8125  objspace_malloc_increase(objspace, 0, diff, 0, MEMOP_TYPE_REALLOC);
8126  }
8127  else if (diff < 0) {
8128  objspace_malloc_increase(objspace, 0, 0, -diff, MEMOP_TYPE_REALLOC);
8129  }
8130 }
8131 
8132 /*
8133  ------------------------------ WeakMap ------------------------------
8134 */
8135 
8136 struct weakmap {
8137  st_table *obj2wmap; /* obj -> [ref,...] */
8138  st_table *wmap2obj; /* ref -> obj */
8139  VALUE final;
8140 };
8141 
8142 #define WMAP_DELETE_DEAD_OBJECT_IN_MARK 0
8143 
8144 #if WMAP_DELETE_DEAD_OBJECT_IN_MARK
8145 static int
8146 wmap_mark_map(st_data_t key, st_data_t val, st_data_t arg)
8147 {
8148  rb_objspace_t *objspace = (rb_objspace_t *)arg;
8149  VALUE obj = (VALUE)val;
8150  if (!is_live_object(objspace, obj)) return ST_DELETE;
8151  return ST_CONTINUE;
8152 }
8153 #endif
8154 
8155 static void
8156 wmap_mark(void *ptr)
8157 {
8158  struct weakmap *w = ptr;
8159 #if WMAP_DELETE_DEAD_OBJECT_IN_MARK
8160  if (w->obj2wmap) st_foreach(w->obj2wmap, wmap_mark_map, (st_data_t)&rb_objspace);
8161 #endif
8162  rb_gc_mark(w->final);
8163 }
8164 
8165 static int
8167 {
8168  VALUE *ptr = (VALUE *)val;
8169  ruby_sized_xfree(ptr, (ptr[0] + 1) * sizeof(VALUE));
8170  return ST_CONTINUE;
8171 }
8172 
8173 static void
8174 wmap_free(void *ptr)
8175 {
8176  struct weakmap *w = ptr;
8178  st_free_table(w->obj2wmap);
8179  st_free_table(w->wmap2obj);
8180 }
8181 
8182 static int
8184 {
8185  VALUE *ptr = (VALUE *)val;
8186  *(size_t *)arg += (ptr[0] + 1) * sizeof(VALUE);
8187  return ST_CONTINUE;
8188 }
8189 
8190 static size_t
8191 wmap_memsize(const void *ptr)
8192 {
8193  size_t size;
8194  const struct weakmap *w = ptr;
8195  size = sizeof(*w);
8196  size += st_memsize(w->obj2wmap);
8197  size += st_memsize(w->wmap2obj);
8199  return size;
8200 }
8201 
8203  "weakmap",
8204  {
8205  wmap_mark,
8206  wmap_free,
8207  wmap_memsize,
8208  },
8210 };
8211 
8212 static VALUE
8214 {
8215  struct weakmap *w;
8216  VALUE obj = TypedData_Make_Struct(klass, struct weakmap, &weakmap_type, w);
8217  w->obj2wmap = st_init_numtable();
8218  w->wmap2obj = st_init_numtable();
8219  w->final = rb_obj_method(obj, ID2SYM(rb_intern("finalize")));
8220  return obj;
8221 }
8222 
8223 static int
8224 wmap_final_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
8225 {
8226  VALUE wmap, *ptr, size, i, j;
8227  if (!existing) return ST_STOP;
8228  wmap = (VALUE)arg, ptr = (VALUE *)*value;
8229  for (i = j = 1, size = ptr[0]; i <= size; ++i) {
8230  if (ptr[i] != wmap) {
8231  ptr[j++] = ptr[i];
8232  }
8233  }
8234  if (j == 1) {
8235  ruby_sized_xfree(ptr, i * sizeof(VALUE));
8236  return ST_DELETE;
8237  }
8238  if (j < i) {
8239  ptr = ruby_sized_xrealloc2(ptr, j + 1, sizeof(VALUE), i);
8240  ptr[0] = j;
8241  *value = (st_data_t)ptr;
8242  }
8243  return ST_CONTINUE;
8244 }
8245 
8246 static VALUE
8248 {
8249  st_data_t orig, wmap, data;
8250  VALUE obj, *rids, i, size;
8251  struct weakmap *w;
8252 
8253  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8254  /* Get reference from object id. */
8255  obj = obj_id_to_ref(objid);
8256 
8257  /* obj is original referenced object and/or weak reference. */
8258  orig = (st_data_t)obj;
8259  if (st_delete(w->obj2wmap, &orig, &data)) {
8260  rids = (VALUE *)data;
8261  size = *rids++;
8262  for (i = 0; i < size; ++i) {
8263  wmap = (st_data_t)rids[i];
8264  st_delete(w->wmap2obj, &wmap, NULL);
8265  }
8266  ruby_sized_xfree((VALUE *)data, (size + 1) * sizeof(VALUE));
8267  }
8268 
8269  wmap = (st_data_t)obj;
8270  if (st_delete(w->wmap2obj, &wmap, &orig)) {
8271  wmap = (st_data_t)obj;
8272  st_update(w->obj2wmap, orig, wmap_final_func, wmap);
8273  }
8274  return self;
8275 }
8276 
8280 };
8281 
8282 static int
8284 {
8285  VALUE str = (VALUE)arg;
8286  VALUE k = (VALUE)key, v = (VALUE)val;
8287 
8288  if (RSTRING_PTR(str)[0] == '#') {
8289  rb_str_cat2(str, ", ");
8290  }
8291  else {
8292  rb_str_cat2(str, ": ");
8293  RSTRING_PTR(str)[0] = '#';
8294  }
8295  k = SPECIAL_CONST_P(k) ? rb_inspect(k) : rb_any_to_s(k);
8296  rb_str_append(str, k);
8297  rb_str_cat2(str, " => ");
8298  v = SPECIAL_CONST_P(v) ? rb_inspect(v) : rb_any_to_s(v);
8299  rb_str_append(str, v);
8300  OBJ_INFECT(str, k);
8301  OBJ_INFECT(str, v);
8302 
8303  return ST_CONTINUE;
8304 }
8305 
8306 static VALUE
8308 {
8309  VALUE str;
8310  VALUE c = rb_class_name(CLASS_OF(self));
8311  struct weakmap *w;
8312 
8313  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8314  str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
8315  if (w->wmap2obj) {
8317  }
8318  RSTRING_PTR(str)[0] = '#';
8319  rb_str_cat2(str, ">");
8320  return str;
8321 }
8322 
8323 static int
8325 {
8326  rb_objspace_t *objspace = (rb_objspace_t *)arg;
8327  VALUE obj = (VALUE)val;
8328  if (is_id_value(objspace, obj) && is_live_object(objspace, obj)) {
8329  rb_yield_values(2, (VALUE)key, obj);
8330  }
8331  return ST_CONTINUE;
8332 }
8333 
8334 /* Iterates over keys and objects in a weakly referenced object */
8335 static VALUE
8337 {
8338  struct weakmap *w;
8339  rb_objspace_t *objspace = &rb_objspace;
8340 
8341  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8342  st_foreach(w->wmap2obj, wmap_each_i, (st_data_t)objspace);
8343  return self;
8344 }
8345 
8346 static int
8348 {
8349  rb_objspace_t *objspace = (rb_objspace_t *)arg;
8350  VALUE obj = (VALUE)val;
8351  if (is_id_value(objspace, obj) && is_live_object(objspace, obj)) {
8352  rb_yield((VALUE)key);
8353  }
8354  return ST_CONTINUE;
8355 }
8356 
8357 /* Iterates over keys and objects in a weakly referenced object */
8358 static VALUE
8360 {
8361  struct weakmap *w;
8362  rb_objspace_t *objspace = &rb_objspace;
8363 
8364  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8365  st_foreach(w->wmap2obj, wmap_each_key_i, (st_data_t)objspace);
8366  return self;
8367 }
8368 
8369 static int
8371 {
8372  rb_objspace_t *objspace = (rb_objspace_t *)arg;
8373  VALUE obj = (VALUE)val;
8374  if (is_id_value(objspace, obj) && is_live_object(objspace, obj)) {
8375  rb_yield(obj);
8376  }
8377  return ST_CONTINUE;
8378 }
8379 
8380 /* Iterates over keys and objects in a weakly referenced object */
8381 static VALUE
8383 {
8384  struct weakmap *w;
8385  rb_objspace_t *objspace = &rb_objspace;
8386 
8387  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8389  return self;
8390 }
8391 
8392 static int
8394 {
8395  struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg;
8396  rb_objspace_t *objspace = argp->objspace;
8397  VALUE ary = argp->value;
8398  VALUE obj = (VALUE)val;
8399  if (is_id_value(objspace, obj) && is_live_object(objspace, obj)) {
8400  rb_ary_push(ary, (VALUE)key);
8401  }
8402  return ST_CONTINUE;
8403 }
8404 
8405 /* Iterates over keys and objects in a weakly referenced object */
8406 static VALUE
8408 {
8409  struct weakmap *w;
8410  struct wmap_iter_arg args;
8411 
8412  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8413  args.objspace = &rb_objspace;
8414  args.value = rb_ary_new();
8416  return args.value;
8417 }
8418 
8419 static int
8421 {
8422  struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg;
8423  rb_objspace_t *objspace = argp->objspace;
8424  VALUE ary = argp->value;
8425  VALUE obj = (VALUE)val;
8426  if (is_id_value(objspace, obj) && is_live_object(objspace, obj)) {
8427  rb_ary_push(ary, obj);
8428  }
8429  return ST_CONTINUE;
8430 }
8431 
8432 /* Iterates over values and objects in a weakly referenced object */
8433 static VALUE
8435 {
8436  struct weakmap *w;
8437  struct wmap_iter_arg args;
8438 
8439  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8440  args.objspace = &rb_objspace;
8441  args.value = rb_ary_new();
8443  return args.value;
8444 }
8445 
8446 static int
8448 {
8449  VALUE size, *ptr, *optr;
8450  if (existing) {
8451  size = (ptr = optr = (VALUE *)*val)[0];
8452  ++size;
8453  ptr = ruby_sized_xrealloc2(ptr, size + 1, sizeof(VALUE), size);
8454  }
8455  else {
8456  optr = 0;
8457  size = 1;
8458  ptr = ruby_xmalloc2(2, sizeof(VALUE));
8459  }
8460  ptr[0] = size;
8461  ptr[size] = (VALUE)arg;
8462  if (ptr == optr) return ST_STOP;
8463  *val = (st_data_t)ptr;
8464  return ST_CONTINUE;
8465 }
8466 
8467 /* Creates a weak reference from the given key to the given value */
8468 static VALUE
8469 wmap_aset(VALUE self, VALUE wmap, VALUE orig)
8470 {
8471  struct weakmap *w;
8472 
8473  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8474  should_be_finalizable(orig);
8475  should_be_finalizable(wmap);
8476  define_final0(orig, w->final);
8477  define_final0(wmap, w->final);
8478  st_update(w->obj2wmap, (st_data_t)orig, wmap_aset_update, wmap);
8479  st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig);
8480  return nonspecial_obj_id(orig);
8481 }
8482 
8483 /* Retrieves a weakly referenced object with the given key */
8484 static VALUE
8485 wmap_aref(VALUE self, VALUE wmap)
8486 {
8487  st_data_t data;
8488  VALUE obj;
8489  struct weakmap *w;
8490  rb_objspace_t *objspace = &rb_objspace;
8491 
8492  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8493  if (!st_lookup(w->wmap2obj, (st_data_t)wmap, &data)) return Qnil;
8494  obj = (VALUE)data;
8495  if (!is_id_value(objspace, obj)) return Qnil;
8496  if (!is_live_object(objspace, obj)) return Qnil;
8497  return obj;
8498 }
8499 
8500 /* Returns +true+ if +key+ is registered */
8501 static VALUE
8503 {
8504  return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
8505 }
8506 
8507 static VALUE
8509 {
8510  struct weakmap *w;
8511  st_index_t n;
8512 
8513  TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
8514  n = w->wmap2obj->num_entries;
8515 #if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
8516  return ULONG2NUM(n);
8517 #else
8518  return ULL2NUM(n);
8519 #endif
8520 }
8521 
8522 /*
8523  ------------------------------ GC profiler ------------------------------
8524 */
8525 
8526 #define GC_PROFILE_RECORD_DEFAULT_SIZE 100
8527 
8528 /* return sec in user time */
8529 static double
8531 {
8532 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
8533  {
8534  static int try_clock_gettime = 1;
8535  struct timespec ts;
8536  if (try_clock_gettime && clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
8537  return ts.tv_sec + ts.tv_nsec * 1e-9;
8538  }
8539  else {
8540  try_clock_gettime = 0;
8541  }
8542  }
8543 #endif
8544 
8545 #ifdef RUSAGE_SELF
8546  {
8547  struct rusage usage;
8548  struct timeval time;
8549  if (getrusage(RUSAGE_SELF, &usage) == 0) {
8550  time = usage.ru_utime;
8551  return time.tv_sec + time.tv_usec * 1e-6;
8552  }
8553  }
8554 #endif
8555 
8556 #ifdef _WIN32
8557  {
8558  FILETIME creation_time, exit_time, kernel_time, user_time;
8559  ULARGE_INTEGER ui;
8560  LONG_LONG q;
8561  double t;
8562 
8563  if (GetProcessTimes(GetCurrentProcess(),
8564  &creation_time, &exit_time, &kernel_time, &user_time) != 0) {
8565  memcpy(&ui, &user_time, sizeof(FILETIME));
8566  q = ui.QuadPart / 10L;
8567  t = (DWORD)(q % 1000000L) * 1e-6;
8568  q /= 1000000L;
8569 #ifdef __GNUC__
8570  t += q;
8571 #else
8572  t += (double)(DWORD)(q >> 16) * (1 << 16);
8573  t += (DWORD)q & ~(~0 << 16);
8574 #endif
8575  return t;
8576  }
8577  }
8578 #endif
8579 
8580  return 0.0;
8581 }
8582 
8583 static inline void
8585 {
8586  if (objspace->profile.run) {
8587  size_t index = objspace->profile.next_index;
8588  gc_profile_record *record;
8589 
8590  /* create new record */
8591  objspace->profile.next_index++;
8592 
8593  if (!objspace->profile.records) {
8595  objspace->profile.records = malloc(sizeof(gc_profile_record) * objspace->profile.size);
8596  }
8597  if (index >= objspace->profile.size) {
8598  void *ptr;
8599  objspace->profile.size += 1000;
8600  ptr = realloc(objspace->profile.records, sizeof(gc_profile_record) * objspace->profile.size);
8601  if (!ptr) rb_memerror();
8602  objspace->profile.records = ptr;
8603  }
8604  if (!objspace->profile.records) {
8605  rb_bug("gc_profile malloc or realloc miss");
8606  }
8607  record = objspace->profile.current_record = &objspace->profile.records[objspace->profile.next_index - 1];
8608  MEMZERO(record, gc_profile_record, 1);
8609 
8610  /* setup before-GC parameter */
8611  record->flags = reason | (ruby_gc_stressful ? GPR_FLAG_STRESS : 0);
8612 #if MALLOC_ALLOCATED_SIZE
8613  record->allocated_size = malloc_allocated_size;
8614 #endif
8615 #if GC_PROFILE_MORE_DETAIL && GC_PROFILE_DETAIL_MEMORY
8616 #ifdef RUSAGE_SELF
8617  {
8618  struct rusage usage;
8619  if (getrusage(RUSAGE_SELF, &usage) == 0) {
8620  record->maxrss = usage.ru_maxrss;
8621  record->minflt = usage.ru_minflt;
8622  record->majflt = usage.ru_majflt;
8623  }
8624  }
8625 #endif
8626 #endif
8627  }
8628 }
8629 
8630 static inline void
8632 {
8633  if (gc_prof_enabled(objspace)) {
8634  gc_profile_record *record = gc_prof_record(objspace);
8635 #if GC_PROFILE_MORE_DETAIL
8636  record->prepare_time = objspace->profile.prepare_time;
8637 #endif
8638  record->gc_time = 0;
8639  record->gc_invoke_time = getrusage_time();
8640  }
8641 }
8642 
8643 static double
8644 elapsed_time_from(double time)
8645 {
8646  double now = getrusage_time();
8647  if (now > time) {
8648  return now - time;
8649  }
8650  else {
8651  return 0;
8652  }
8653 }
8654 
8655 static inline void
8657 {
8658  if (gc_prof_enabled(objspace)) {
8659  gc_profile_record *record = gc_prof_record(objspace);
8660  record->gc_time = elapsed_time_from(record->gc_invoke_time);
8661  record->gc_invoke_time -= objspace->profile.invoke_time;
8662  }
8663 }
8664 
8665 #define RUBY_DTRACE_GC_HOOK(name) \
8666  do {if (RUBY_DTRACE_GC_##name##_ENABLED()) RUBY_DTRACE_GC_##name();} while (0)
8667 static inline void
8669 {
8670  RUBY_DTRACE_GC_HOOK(MARK_BEGIN);
8671 #if GC_PROFILE_MORE_DETAIL
8672  if (gc_prof_enabled(objspace)) {
8673  gc_prof_record(objspace)->gc_mark_time = getrusage_time();
8674  }
8675 #endif
8676 }
8677 
8678 static inline void
8680 {
8681  RUBY_DTRACE_GC_HOOK(MARK_END);
8682 #if GC_PROFILE_MORE_DETAIL
8683  if (gc_prof_enabled(objspace)) {
8684  gc_profile_record *record = gc_prof_record(objspace);
8685  record->gc_mark_time = elapsed_time_from(record->gc_mark_time);
8686  }
8687 #endif
8688 }
8689 
8690 static inline void
8692 {
8693  RUBY_DTRACE_GC_HOOK(SWEEP_BEGIN);
8694  if (gc_prof_enabled(objspace)) {
8695  gc_profile_record *record = gc_prof_record(objspace);
8696 
8697  if (record->gc_time > 0 || GC_PROFILE_MORE_DETAIL) {
8699  }
8700  }
8701 }
8702 
8703 static inline void
8705 {
8706  RUBY_DTRACE_GC_HOOK(SWEEP_END);
8707 
8708  if (gc_prof_enabled(objspace)) {
8709  double sweep_time;
8710  gc_profile_record *record = gc_prof_record(objspace);
8711 
8712  if (record->gc_time > 0) {
8713  sweep_time = elapsed_time_from(objspace->profile.gc_sweep_start_time);
8714  /* need to accumulate GC time for lazy sweep after gc() */
8715  record->gc_time += sweep_time;
8716  }
8717  else if (GC_PROFILE_MORE_DETAIL) {
8718  sweep_time = elapsed_time_from(objspace->profile.gc_sweep_start_time);
8719  }
8720 
8721 #if GC_PROFILE_MORE_DETAIL
8722  record->gc_sweep_time += sweep_time;
8724 #endif
8726  }
8727 }
8728 
8729 static inline void
8731 {
8732 #if GC_PROFILE_MORE_DETAIL
8733  if (gc_prof_enabled(objspace)) {
8734  gc_profile_record *record = gc_prof_record(objspace);
8735  record->allocate_increase = malloc_increase;
8736  record->allocate_limit = malloc_limit;
8737  }
8738 #endif
8739 }
8740 
8741 static inline void
8743 {
8744  if (gc_prof_enabled(objspace)) {
8745  gc_profile_record *record = gc_prof_record(objspace);
8746  size_t live = objspace->profile.total_allocated_objects_at_gc_start - objspace->profile.total_freed_objects;
8747  size_t total = objspace->profile.heap_used_at_gc_start * HEAP_PAGE_OBJ_LIMIT;
8748 
8749 #if GC_PROFILE_MORE_DETAIL
8750  record->heap_use_pages = objspace->profile.heap_used_at_gc_start;
8751  record->heap_live_objects = live;
8752  record->heap_free_objects = total - live;
8753 #endif
8754 
8755  record->heap_total_objects = total;
8756  record->heap_use_size = live * sizeof(RVALUE);
8757  record->heap_total_size = total * sizeof(RVALUE);
8758  }
8759 }
8760 
8761 /*
8762  * call-seq:
8763  * GC::Profiler.clear -> nil
8764  *
8765  * Clears the GC profiler data.
8766  *
8767  */
8768 
8769 static VALUE
8771 {
8772  rb_objspace_t *objspace = &rb_objspace;
8773  if (GC_PROFILE_RECORD_DEFAULT_SIZE * 2 < objspace->profile.size) {
8775  objspace->profile.records = realloc(objspace->profile.records, sizeof(gc_profile_record) * objspace->profile.size);
8776  if (!objspace->profile.records) {
8777  rb_memerror();
8778  }
8779  }
8780  MEMZERO(objspace->profile.records, gc_profile_record, objspace->profile.size);
8781  objspace->profile.next_index = 0;
8782  objspace->profile.current_record = 0;
8783  return Qnil;
8784 }
8785 
8786 /*
8787  * call-seq:
8788  * GC::Profiler.raw_data -> [Hash, ...]
8789  *
8790  * Returns an Array of individual raw profile data Hashes ordered
8791  * from earliest to latest by +:GC_INVOKE_TIME+.
8792  *
8793  * For example:
8794  *
8795  * [
8796  * {
8797  * :GC_TIME=>1.3000000000000858e-05,
8798  * :GC_INVOKE_TIME=>0.010634999999999999,
8799  * :HEAP_USE_SIZE=>289640,
8800  * :HEAP_TOTAL_SIZE=>588960,
8801  * :HEAP_TOTAL_OBJECTS=>14724,
8802  * :GC_IS_MARKED=>false
8803  * },
8804  * # ...
8805  * ]
8806  *
8807  * The keys mean:
8808  *
8809  * +:GC_TIME+::
8810  * Time elapsed in seconds for this GC run
8811  * +:GC_INVOKE_TIME+::
8812  * Time elapsed in seconds from startup to when the GC was invoked
8813  * +:HEAP_USE_SIZE+::
8814  * Total bytes of heap used
8815  * +:HEAP_TOTAL_SIZE+::
8816  * Total size of heap in bytes
8817  * +:HEAP_TOTAL_OBJECTS+::
8818  * Total number of objects
8819  * +:GC_IS_MARKED+::
8820  * Returns +true+ if the GC is in mark phase
8821  *
8822  * If ruby was built with +GC_PROFILE_MORE_DETAIL+, you will also have access
8823  * to the following hash keys:
8824  *
8825  * +:GC_MARK_TIME+::
8826  * +:GC_SWEEP_TIME+::
8827  * +:ALLOCATE_INCREASE+::
8828  * +:ALLOCATE_LIMIT+::
8829  * +:HEAP_USE_PAGES+::
8830  * +:HEAP_LIVE_OBJECTS+::
8831  * +:HEAP_FREE_OBJECTS+::
8832  * +:HAVE_FINALIZE+::
8833  *
8834  */
8835 
8836 static VALUE
8838 {
8839  VALUE prof;
8840  VALUE gc_profile = rb_ary_new();
8841  size_t i;
8842  rb_objspace_t *objspace = (&rb_objspace);
8843 
8844  if (!objspace->profile.run) {
8845  return Qnil;
8846  }
8847 
8848  for (i =0; i < objspace->profile.next_index; i++) {
8849  gc_profile_record *record = &objspace->profile.records[i];
8850 
8851  prof = rb_hash_new();
8852  rb_hash_aset(prof, ID2SYM(rb_intern("GC_FLAGS")), gc_info_decode(0, rb_hash_new(), record->flags));
8853  rb_hash_aset(prof, ID2SYM(rb_intern("GC_TIME")), DBL2NUM(record->gc_time));
8854  rb_hash_aset(prof, ID2SYM(rb_intern("GC_INVOKE_TIME")), DBL2NUM(record->gc_invoke_time));
8855  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_USE_SIZE")), SIZET2NUM(record->heap_use_size));
8856  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_TOTAL_SIZE")), SIZET2NUM(record->heap_total_size));
8857  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_TOTAL_OBJECTS")), SIZET2NUM(record->heap_total_objects));
8858  rb_hash_aset(prof, ID2SYM(rb_intern("GC_IS_MARKED")), Qtrue);
8859 #if GC_PROFILE_MORE_DETAIL
8860  rb_hash_aset(prof, ID2SYM(rb_intern("GC_MARK_TIME")), DBL2NUM(record->gc_mark_time));
8861  rb_hash_aset(prof, ID2SYM(rb_intern("GC_SWEEP_TIME")), DBL2NUM(record->gc_sweep_time));
8862  rb_hash_aset(prof, ID2SYM(rb_intern("ALLOCATE_INCREASE")), SIZET2NUM(record->allocate_increase));
8863  rb_hash_aset(prof, ID2SYM(rb_intern("ALLOCATE_LIMIT")), SIZET2NUM(record->allocate_limit));
8864  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_USE_PAGES")), SIZET2NUM(record->heap_use_pages));
8865  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_LIVE_OBJECTS")), SIZET2NUM(record->heap_live_objects));
8866  rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_FREE_OBJECTS")), SIZET2NUM(record->heap_free_objects));
8867 
8868  rb_hash_aset(prof, ID2SYM(rb_intern("REMOVING_OBJECTS")), SIZET2NUM(record->removing_objects));
8869  rb_hash_aset(prof, ID2SYM(rb_intern("EMPTY_OBJECTS")), SIZET2NUM(record->empty_objects));
8870 
8871  rb_hash_aset(prof, ID2SYM(rb_intern("HAVE_FINALIZE")), (record->flags & GPR_FLAG_HAVE_FINALIZE) ? Qtrue : Qfalse);
8872 #endif
8873 
8874 #if RGENGC_PROFILE > 0
8875  rb_hash_aset(prof, ID2SYM(rb_intern("OLD_OBJECTS")), SIZET2NUM(record->old_objects));
8876  rb_hash_aset(prof, ID2SYM(rb_intern("REMEMBERED_NORMAL_OBJECTS")), SIZET2NUM(record->remembered_normal_objects));
8877  rb_hash_aset(prof, ID2SYM(rb_intern("REMEMBERED_SHADY_OBJECTS")), SIZET2NUM(record->remembered_shady_objects));
8878 #endif
8879  rb_ary_push(gc_profile, prof);
8880  }
8881 
8882  return gc_profile;
8883 }
8884 
8885 #if GC_PROFILE_MORE_DETAIL
8886 #define MAJOR_REASON_MAX 0x10
8887 
8888 static char *
8889 gc_profile_dump_major_reason(int flags, char *buff)
8890 {
8891  int reason = flags & GPR_FLAG_MAJOR_MASK;
8892  int i = 0;
8893 
8894  if (reason == GPR_FLAG_NONE) {
8895  buff[0] = '-';
8896  buff[1] = 0;
8897  }
8898  else {
8899 #define C(x, s) \
8900  if (reason & GPR_FLAG_MAJOR_BY_##x) { \
8901  buff[i++] = #x[0]; \
8902  if (i >= MAJOR_REASON_MAX) rb_bug("gc_profile_dump_major_reason: overflow"); \
8903  buff[i] = 0; \
8904  }
8905  C(NOFREE, N);
8906  C(OLDGEN, O);
8907  C(SHADY, S);
8908 #if RGENGC_ESTIMATE_OLDMALLOC
8909  C(OLDMALLOC, M);
8910 #endif
8911 #undef C
8912  }
8913  return buff;
8914 }
8915 #endif
8916 
8917 static void
8919 {
8920  rb_objspace_t *objspace = &rb_objspace;
8921  size_t count = objspace->profile.next_index;
8922 #ifdef MAJOR_REASON_MAX
8923  char reason_str[MAJOR_REASON_MAX];
8924 #endif
8925 
8926  if (objspace->profile.run && count /* > 1 */) {
8927  size_t i;
8928  const gc_profile_record *record;
8929 
8930  append(out, rb_sprintf("GC %"PRIuSIZE" invokes.\n", objspace->profile.count));
8931  append(out, rb_str_new_cstr("Index Invoke Time(sec) Use Size(byte) Total Size(byte) Total Object GC Time(ms)\n"));
8932 
8933  for (i = 0; i < count; i++) {
8934  record = &objspace->profile.records[i];
8935  append(out, rb_sprintf("%5"PRIuSIZE" %19.3f %20"PRIuSIZE" %20"PRIuSIZE" %20"PRIuSIZE" %30.20f\n",
8936  i+1, record->gc_invoke_time, record->heap_use_size,
8937  record->heap_total_size, record->heap_total_objects, record->gc_time*1000));
8938  }
8939 
8940 #if GC_PROFILE_MORE_DETAIL
8941  append(out, rb_str_new_cstr("\n\n" \
8942  "More detail.\n" \
8943  "Prepare Time = Previously GC's rest sweep time\n"
8944  "Index Flags Allocate Inc. Allocate Limit"
8946  " Allocated Size"
8947 #endif
8948  " Use Page Mark Time(ms) Sweep Time(ms) Prepare Time(ms) LivingObj FreeObj RemovedObj EmptyObj"
8949 #if RGENGC_PROFILE
8950  " OldgenObj RemNormObj RemShadObj"
8951 #endif
8953  " MaxRSS(KB) MinorFLT MajorFLT"
8954 #endif
8955  "\n"));
8956 
8957  for (i = 0; i < count; i++) {
8958  record = &objspace->profile.records[i];
8959  append(out, rb_sprintf("%5"PRIuSIZE" %4s/%c/%6s%c %13"PRIuSIZE" %15"PRIuSIZE
8961  " %15"PRIuSIZE
8962 #endif
8963  " %9"PRIuSIZE" %17.12f %17.12f %17.12f %10"PRIuSIZE" %10"PRIuSIZE" %10"PRIuSIZE" %10"PRIuSIZE
8964 #if RGENGC_PROFILE
8965  "%10"PRIuSIZE" %10"PRIuSIZE" %10"PRIuSIZE
8966 #endif
8968  "%11ld %8ld %8ld"
8969 #endif
8970 
8971  "\n",
8972  i+1,
8973  gc_profile_dump_major_reason(record->flags, reason_str),
8974  (record->flags & GPR_FLAG_HAVE_FINALIZE) ? 'F' : '.',
8975  (record->flags & GPR_FLAG_NEWOBJ) ? "NEWOBJ" :
8976  (record->flags & GPR_FLAG_MALLOC) ? "MALLOC" :
8977  (record->flags & GPR_FLAG_METHOD) ? "METHOD" :
8978  (record->flags & GPR_FLAG_CAPI) ? "CAPI__" : "??????",
8979  (record->flags & GPR_FLAG_STRESS) ? '!' : ' ',
8980  record->allocate_increase, record->allocate_limit,
8982  record->allocated_size,
8983 #endif
8984  record->heap_use_pages,
8985  record->gc_mark_time*1000,
8986  record->gc_sweep_time*1000,
8987  record->prepare_time*1000,
8988 
8989  record->heap_live_objects,
8990  record->heap_free_objects,
8991  record->removing_objects,
8992  record->empty_objects
8993 #if RGENGC_PROFILE
8994  ,
8995  record->old_objects,
8996  record->remembered_normal_objects,
8997  record->remembered_shady_objects
8998 #endif
9000  ,
9001  record->maxrss / 1024,
9002  record->minflt,
9003  record->majflt
9004 #endif
9005 
9006  ));
9007  }
9008 #endif
9009  }
9010 }
9011 
9012 /*
9013  * call-seq:
9014  * GC::Profiler.result -> String
9015  *
9016  * Returns a profile data report such as:
9017  *
9018  * GC 1 invokes.
9019  * Index Invoke Time(sec) Use Size(byte) Total Size(byte) Total Object GC time(ms)
9020  * 1 0.012 159240 212940 10647 0.00000000000001530000
9021  */
9022 
9023 static VALUE
9025 {
9026  VALUE str = rb_str_buf_new(0);
9028  return str;
9029 }
9030 
9031 /*
9032  * call-seq:
9033  * GC::Profiler.report
9034  * GC::Profiler.report(io)
9035  *
9036  * Writes the GC::Profiler.result to <tt>$stdout</tt> or the given IO object.
9037  *
9038  */
9039 
9040 static VALUE
9042 {
9043  VALUE out;
9044 
9045  if (argc == 0) {
9046  out = rb_stdout;
9047  }
9048  else {
9049  rb_scan_args(argc, argv, "01", &out);
9050  }
9052 
9053  return Qnil;
9054 }
9055 
9056 /*
9057  * call-seq:
9058  * GC::Profiler.total_time -> float
9059  *
9060  * The total time used for garbage collection in seconds
9061  */
9062 
9063 static VALUE
9065 {
9066  double time = 0;
9067  rb_objspace_t *objspace = &rb_objspace;
9068 
9069  if (objspace->profile.run && objspace->profile.next_index > 0) {
9070  size_t i;
9071  size_t count = objspace->profile.next_index;
9072 
9073  for (i = 0; i < count; i++) {
9074  time += objspace->profile.records[i].gc_time;
9075  }
9076  }
9077  return DBL2NUM(time);
9078 }
9079 
9080 /*
9081  * call-seq:
9082  * GC::Profiler.enabled? -> true or false
9083  *
9084  * The current status of GC profile mode.
9085  */
9086 
9087 static VALUE
9089 {
9090  rb_objspace_t *objspace = &rb_objspace;
9091  return objspace->profile.run ? Qtrue : Qfalse;
9092 }
9093 
9094 /*
9095  * call-seq:
9096  * GC::Profiler.enable -> nil
9097  *
9098  * Starts the GC profiler.
9099  *
9100  */
9101 
9102 static VALUE
9104 {
9105  rb_objspace_t *objspace = &rb_objspace;
9106  objspace->profile.run = TRUE;
9107  objspace->profile.current_record = 0;
9108  return Qnil;
9109 }
9110 
9111 /*
9112  * call-seq:
9113  * GC::Profiler.disable -> nil
9114  *
9115  * Stops the GC profiler.
9116  *
9117  */
9118 
9119 static VALUE
9121 {
9122  rb_objspace_t *objspace = &rb_objspace;
9123 
9124  objspace->profile.run = FALSE;
9125  objspace->profile.current_record = 0;
9126  return Qnil;
9127 }
9128 
9129 /*
9130  ------------------------------ DEBUG ------------------------------
9131 */
9132 
9133 static const char *
9134 type_name(int type, VALUE obj)
9135 {
9136  switch (type) {
9137 #define TYPE_NAME(t) case (t): return #t;
9138  TYPE_NAME(T_NONE);
9140  TYPE_NAME(T_CLASS);
9142  TYPE_NAME(T_FLOAT);
9145  TYPE_NAME(T_ARRAY);
9146  TYPE_NAME(T_HASH);
9149  TYPE_NAME(T_FILE);
9150  TYPE_NAME(T_MATCH);
9153  TYPE_NAME(T_NIL);
9154  TYPE_NAME(T_TRUE);
9155  TYPE_NAME(T_FALSE);
9158  TYPE_NAME(T_UNDEF);
9159  TYPE_NAME(T_IMEMO);
9160  TYPE_NAME(T_NODE);
9163  case T_DATA:
9164  if (obj && rb_objspace_data_type_name(obj)) {
9165  return rb_objspace_data_type_name(obj);
9166  }
9167  return "T_DATA";
9168 #undef TYPE_NAME
9169  }
9170  return "unknown";
9171 }
9172 
9173 static const char *
9175 {
9176  return type_name(TYPE(obj), obj);
9177 }
9178 
9179 static const char *
9181 {
9182  switch (type) {
9183  case VM_METHOD_TYPE_ISEQ: return "iseq";
9184  case VM_METHOD_TYPE_ATTRSET: return "attrest";
9185  case VM_METHOD_TYPE_IVAR: return "ivar";
9186  case VM_METHOD_TYPE_BMETHOD: return "bmethod";
9187  case VM_METHOD_TYPE_ALIAS: return "alias";
9188  case VM_METHOD_TYPE_REFINED: return "refined";
9189  case VM_METHOD_TYPE_CFUNC: return "cfunc";
9190  case VM_METHOD_TYPE_ZSUPER: return "zsuper";
9191  case VM_METHOD_TYPE_MISSING: return "missing";
9192  case VM_METHOD_TYPE_OPTIMIZED: return "optimized";
9193  case VM_METHOD_TYPE_UNDEF: return "undef";
9194  case VM_METHOD_TYPE_NOTIMPLEMENTED: return "notimplemented";
9195  }
9196  rb_bug("method_type_name: unreachable (type: %d)", type);
9197 }
9198 
9199 /* from array.c */
9200 # define ARY_SHARED_P(ary) \
9201  (assert(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \
9202  FL_TEST((ary),ELTS_SHARED)!=0)
9203 # define ARY_EMBED_P(ary) \
9204  (assert(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \
9205  FL_TEST((ary), RARRAY_EMBED_FLAG)!=0)
9206 
9207 static void
9208 rb_raw_iseq_info(char *buff, const int buff_size, const rb_iseq_t *iseq)
9209 {
9210  if (iseq->body->location.label) {
9211  snprintf(buff, buff_size, "%s %s@%s:%d", buff,
9212  RSTRING_PTR(iseq->body->location.label),
9213  RSTRING_PTR(iseq->body->location.path),
9214  FIX2INT(iseq->body->location.first_lineno));
9215  }
9216 }
9217 
9218 const char *
9219 rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
9220 {
9221  if (SPECIAL_CONST_P(obj)) {
9222  snprintf(buff, buff_size, "%s", obj_type_name(obj));
9223  }
9224  else {
9225 #define TF(c) ((c) != 0 ? "true" : "false")
9226 #define C(c, s) ((c) != 0 ? (s) : " ")
9227  const int type = BUILTIN_TYPE(obj);
9228 #if USE_RGENGC
9229  const int age = RVALUE_FLAGS_AGE(RBASIC(obj)->flags);
9230 
9231  snprintf(buff, buff_size, "%p [%d%s%s%s%s] %s",
9232  (void *)obj, age,
9233  C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
9234  C(RVALUE_MARK_BITMAP(obj), "M"),
9235  C(RVALUE_MARKING_BITMAP(obj), "R"),
9236  C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
9237  obj_type_name(obj));
9238 #else
9239  snprintf(buff, buff_size, "%p [%s] %s",
9240  (void *)obj,
9241  C(RVALUE_MARK_BITMAP(obj), "M"),
9242  obj_type_name(obj));
9243 #endif
9244 
9245  if (internal_object_p(obj)) {
9246  /* ignore */
9247  }
9248  else if (RBASIC(obj)->klass == 0) {
9249  snprintf(buff, buff_size, "%s (temporary internal)", buff);
9250  }
9251  else {
9252  VALUE class_path = rb_class_path_cached(RBASIC(obj)->klass);
9253  if (!NIL_P(class_path)) {
9254  snprintf(buff, buff_size, "%s (%s)", buff, RSTRING_PTR(class_path));
9255  }
9256  }
9257 
9258 #if GC_DEBUG
9259  snprintf(buff, buff_size, "%s @%s:%d", buff, RANY(obj)->file, RANY(obj)->line);
9260 #endif
9261 
9262  switch (type) {
9263  case T_NODE:
9264  snprintf(buff, buff_size, "%s (%s)", buff,
9265  ruby_node_name(nd_type(obj)));
9266  break;
9267  case T_ARRAY:
9268  snprintf(buff, buff_size, "%s [%s%s] len: %d", buff,
9269  C(ARY_EMBED_P(obj), "E"),
9270  C(ARY_SHARED_P(obj), "S"),
9271  (int)RARRAY_LEN(obj));
9272  break;
9273  case T_STRING: {
9274  snprintf(buff, buff_size, "%s %s", buff, RSTRING_PTR(obj));
9275  break;
9276  }
9277  case T_CLASS: {
9278  VALUE class_path = rb_class_path_cached(obj);
9279  if (!NIL_P(class_path)) {
9280  snprintf(buff, buff_size, "%s %s", buff, RSTRING_PTR(class_path));
9281  }
9282  break;
9283  }
9284  case T_DATA: {
9285  const rb_iseq_t *iseq;
9286  if (rb_obj_is_proc(obj) && (iseq = vm_proc_iseq(obj)) != NULL) {
9287  rb_raw_iseq_info(buff, buff_size, iseq);
9288  }
9289  else {
9290  const char * const type_name = rb_objspace_data_type_name(obj);
9291  if (type_name) {
9292  snprintf(buff, buff_size, "%s %s", buff, type_name);
9293  }
9294  }
9295  break;
9296  }
9297  case T_IMEMO: {
9298  const char *imemo_name;
9299  switch (imemo_type(obj)) {
9300 #define IMEMO_NAME(x) case imemo_##x: imemo_name = #x; break;
9301  IMEMO_NAME(env);
9302  IMEMO_NAME(cref);
9303  IMEMO_NAME(svar);
9304  IMEMO_NAME(throw_data);
9305  IMEMO_NAME(ifunc);
9306  IMEMO_NAME(memo);
9307  IMEMO_NAME(ment);
9308  IMEMO_NAME(iseq);
9309 #undef IMEMO_NAME
9310  }
9311  snprintf(buff, buff_size, "%s %s", buff, imemo_name);
9312 
9313  switch (imemo_type(obj)) {
9314  case imemo_ment: {
9315  const rb_method_entry_t *me = &RANY(obj)->as.imemo.ment;
9316  snprintf(buff, buff_size, "%s (called_id: %s, type: %s, alias: %d, owner: %s, defined_class: %s)", buff,
9317  rb_id2name(me->called_id),
9318  method_type_name(me->def->type),
9319  me->def->alias_count,
9320  obj_info(me->owner),
9321  obj_info(me->defined_class));
9322  break;
9323  }
9324  case imemo_iseq: {
9325  const rb_iseq_t *iseq = (const rb_iseq_t *)obj;
9326  rb_raw_iseq_info(buff, buff_size, iseq);
9327  break;
9328  }
9329  default:
9330  break;
9331  }
9332  }
9333  default:
9334  break;
9335  }
9336 #undef TF
9337 #undef C
9338  }
9339  return buff;
9340 }
9341 
9342 #if RGENGC_OBJ_INFO
9343 #define OBJ_INFO_BUFFERS_NUM 10
9344 #define OBJ_INFO_BUFFERS_SIZE 0x100
9345 static int obj_info_buffers_index = 0;
9346 static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
9347 
9348 static const char *
9349 obj_info(VALUE obj)
9350 {
9351  const int index = obj_info_buffers_index++;
9352  char *const buff = &obj_info_buffers[index][0];
9353 
9354  if (obj_info_buffers_index >= OBJ_INFO_BUFFERS_NUM) {
9355  obj_info_buffers_index = 0;
9356  }
9357 
9358  return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
9359 }
9360 #else
9361 static const char *
9363 {
9364  return obj_type_name(obj);
9365 }
9366 #endif
9367 
9368 const char *
9370 {
9371  if (!rb_special_const_p(obj)) {
9372  return obj_info(obj);
9373  }
9374  else {
9375  return obj_type_name(obj);
9376  }
9377 }
9378 
9379 void
9381 {
9382  char buff[0x100];
9383  fprintf(stderr, "rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
9384 }
9385 
9386 #if GC_DEBUG
9387 
9388 void
9390 {
9391  rb_objspace_t *objspace = &rb_objspace;
9392 
9393  fprintf(stderr, "created at: %s:%d\n", RANY(obj)->file, RANY(obj)->line);
9394 
9395  if (is_pointer_to_heap(objspace, (void *)obj)) {
9396  fprintf(stderr, "pointer to heap?: true\n");
9397  }
9398  else {
9399  fprintf(stderr, "pointer to heap?: false\n");
9400  return;
9401  }
9402 
9403  fprintf(stderr, "marked? : %s\n", MARKED_IN_BITMAP(GET_HEAP_MARK_BITS(obj), obj) ? "true" : "false");
9404 #if USE_RGENGC
9405  fprintf(stderr, "age? : %d\n", RVALUE_AGE(obj));
9406  fprintf(stderr, "old? : %s\n", RVALUE_OLD_P(obj) ? "true" : "false");
9407  fprintf(stderr, "WB-protected?: %s\n", RVALUE_WB_UNPROTECTED(obj) ? "false" : "true");
9408  fprintf(stderr, "remembered? : %s\n", RVALUE_REMEMBERED(obj) ? "true" : "false");
9409 #endif
9410 
9411  if (is_lazy_sweeping(heap_eden)) {
9412  fprintf(stderr, "lazy sweeping?: true\n");
9413  fprintf(stderr, "swept?: %s\n", is_swept_object(objspace, obj) ? "done" : "not yet");
9414  }
9415  else {
9416  fprintf(stderr, "lazy sweeping?: false\n");
9417  }
9418 }
9419 
9420 static VALUE
9421 gcdebug_sentinel(VALUE obj, VALUE name)
9422 {
9423  fprintf(stderr, "WARNING: object %s(%p) is inadvertently collected\n", (char *)name, (void *)obj);
9424  return Qnil;
9425 }
9426 
9427 void
9428 rb_gcdebug_sentinel(VALUE obj, const char *name)
9429 {
9430  rb_define_finalizer(obj, rb_proc_new(gcdebug_sentinel, (VALUE)name));
9431 }
9432 
9433 #endif /* GC_DEBUG */
9434 
9435 #if GC_DEBUG_STRESS_TO_CLASS
9436 static VALUE
9437 rb_gcdebug_add_stress_to_class(int argc, VALUE *argv, VALUE self)
9438 {
9439  rb_objspace_t *objspace = &rb_objspace;
9440 
9441  if (!stress_to_class) {
9443  }
9444  rb_ary_cat(stress_to_class, argv, argc);
9445  return self;
9446 }
9447 
9448 static VALUE
9449 rb_gcdebug_remove_stress_to_class(int argc, VALUE *argv, VALUE self)
9450 {
9451  rb_objspace_t *objspace = &rb_objspace;
9452  int i;
9453 
9454  if (stress_to_class) {
9455  for (i = 0; i < argc; ++i) {
9457  }
9458  if (RARRAY_LEN(stress_to_class) == 0) {
9459  stress_to_class = 0;
9460  }
9461  }
9462  return Qnil;
9463 }
9464 #endif
9465 
9466 /*
9467  * Document-module: ObjectSpace
9468  *
9469  * The ObjectSpace module contains a number of routines
9470  * that interact with the garbage collection facility and allow you to
9471  * traverse all living objects with an iterator.
9472  *
9473  * ObjectSpace also provides support for object finalizers, procs that will be
9474  * called when a specific object is about to be destroyed by garbage
9475  * collection.
9476  *
9477  * require 'objspace'
9478  *
9479  * a = "A"
9480  * b = "B"
9481  *
9482  * ObjectSpace.define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
9483  * ObjectSpace.define_finalizer(b, proc {|id| puts "Finalizer two on #{id}" })
9484  *
9485  * _produces:_
9486  *
9487  * Finalizer two on 537763470
9488  * Finalizer one on 537763480
9489  */
9490 
9491 /*
9492  * Document-class: ObjectSpace::WeakMap
9493  *
9494  * An ObjectSpace::WeakMap object holds references to
9495  * any objects, but those objects can get garbage collected.
9496  *
9497  * This class is mostly used internally by WeakRef, please use
9498  * +lib/weakref.rb+ for the public interface.
9499  */
9500 
9501 /* Document-class: GC::Profiler
9502  *
9503  * The GC profiler provides access to information on GC runs including time,
9504  * length and object space size.
9505  *
9506  * Example:
9507  *
9508  * GC::Profiler.enable
9509  *
9510  * require 'rdoc/rdoc'
9511  *
9512  * GC::Profiler.report
9513  *
9514  * GC::Profiler.disable
9515  *
9516  * See also GC.count, GC.malloc_allocated_size and GC.malloc_allocations
9517  */
9518 
9519 /*
9520  * The GC module provides an interface to Ruby's mark and
9521  * sweep garbage collection mechanism.
9522  *
9523  * Some of the underlying methods are also available via the ObjectSpace
9524  * module.
9525  *
9526  * You may obtain information about the operation of the GC through
9527  * GC::Profiler.
9528  */
9529 
9530 void
9531 Init_GC(void)
9532 {
9533 #undef rb_intern
9534  VALUE rb_mObjSpace;
9535  VALUE rb_mProfiler;
9536  VALUE gc_constants;
9537 
9538  rb_mGC = rb_define_module("GC");
9539  rb_define_singleton_method(rb_mGC, "start", gc_start_internal, -1);
9540  rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
9541  rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
9542  rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
9543  rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set_m, 1);
9544  rb_define_singleton_method(rb_mGC, "count", gc_count, 0);
9545  rb_define_singleton_method(rb_mGC, "stat", gc_stat, -1);
9546  rb_define_singleton_method(rb_mGC, "latest_gc_info", gc_latest_gc_info, -1);
9547  rb_define_method(rb_mGC, "garbage_collect", gc_start_internal, -1);
9548 
9549  gc_constants = rb_hash_new();
9550  rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_SIZE")), SIZET2NUM(sizeof(RVALUE)));
9551  rb_hash_aset(gc_constants, ID2SYM(rb_intern("HEAP_PAGE_OBJ_LIMIT")), SIZET2NUM(HEAP_PAGE_OBJ_LIMIT));
9552  rb_hash_aset(gc_constants, ID2SYM(rb_intern("HEAP_PAGE_BITMAP_SIZE")), SIZET2NUM(HEAP_PAGE_BITMAP_SIZE));
9553  rb_hash_aset(gc_constants, ID2SYM(rb_intern("HEAP_PAGE_BITMAP_PLANES")), SIZET2NUM(HEAP_PAGE_BITMAP_PLANES));
9554  OBJ_FREEZE(gc_constants);
9555  rb_define_const(rb_mGC, "INTERNAL_CONSTANTS", gc_constants);
9556 
9557  rb_mProfiler = rb_define_module_under(rb_mGC, "Profiler");
9558  rb_define_singleton_method(rb_mProfiler, "enabled?", gc_profile_enable_get, 0);
9559  rb_define_singleton_method(rb_mProfiler, "enable", gc_profile_enable, 0);
9560  rb_define_singleton_method(rb_mProfiler, "raw_data", gc_profile_record_get, 0);
9561  rb_define_singleton_method(rb_mProfiler, "disable", gc_profile_disable, 0);
9562  rb_define_singleton_method(rb_mProfiler, "clear", gc_profile_clear, 0);
9563  rb_define_singleton_method(rb_mProfiler, "result", gc_profile_result, 0);
9564  rb_define_singleton_method(rb_mProfiler, "report", gc_profile_report, -1);
9565  rb_define_singleton_method(rb_mProfiler, "total_time", gc_profile_total_time, 0);
9566 
9567  rb_mObjSpace = rb_define_module("ObjectSpace");
9568  rb_define_module_function(rb_mObjSpace, "each_object", os_each_obj, -1);
9569  rb_define_module_function(rb_mObjSpace, "garbage_collect", gc_start_internal, -1);
9570 
9571  rb_define_module_function(rb_mObjSpace, "define_finalizer", define_final, -1);
9572  rb_define_module_function(rb_mObjSpace, "undefine_finalizer", undefine_final, 1);
9573 
9574  rb_define_module_function(rb_mObjSpace, "_id2ref", id2ref, 1);
9575 
9577 
9579  rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
9580 
9581  rb_define_module_function(rb_mObjSpace, "count_objects", count_objects, -1);
9582 
9583  {
9584  VALUE rb_cWeakMap = rb_define_class_under(rb_mObjSpace, "WeakMap", rb_cObject);
9585  rb_define_alloc_func(rb_cWeakMap, wmap_allocate);
9586  rb_define_method(rb_cWeakMap, "[]=", wmap_aset, 2);
9587  rb_define_method(rb_cWeakMap, "[]", wmap_aref, 1);
9588  rb_define_method(rb_cWeakMap, "include?", wmap_has_key, 1);
9589  rb_define_method(rb_cWeakMap, "member?", wmap_has_key, 1);
9590  rb_define_method(rb_cWeakMap, "key?", wmap_has_key, 1);
9591  rb_define_method(rb_cWeakMap, "inspect", wmap_inspect, 0);
9592  rb_define_method(rb_cWeakMap, "each", wmap_each, 0);
9593  rb_define_method(rb_cWeakMap, "each_pair", wmap_each, 0);
9594  rb_define_method(rb_cWeakMap, "each_key", wmap_each_key, 0);
9595  rb_define_method(rb_cWeakMap, "each_value", wmap_each_value, 0);
9596  rb_define_method(rb_cWeakMap, "keys", wmap_keys, 0);
9597  rb_define_method(rb_cWeakMap, "values", wmap_values, 0);
9598  rb_define_method(rb_cWeakMap, "size", wmap_size, 0);
9599  rb_define_method(rb_cWeakMap, "length", wmap_size, 0);
9600  rb_define_private_method(rb_cWeakMap, "finalize", wmap_finalize, 1);
9601  rb_include_module(rb_cWeakMap, rb_mEnumerable);
9602  }
9603 
9604  /* internal methods */
9605  rb_define_singleton_method(rb_mGC, "verify_internal_consistency", gc_verify_internal_consistency, 0);
9606 #if MALLOC_ALLOCATED_SIZE
9607  rb_define_singleton_method(rb_mGC, "malloc_allocated_size", gc_malloc_allocated_size, 0);
9608  rb_define_singleton_method(rb_mGC, "malloc_allocations", gc_malloc_allocations, 0);
9609 #endif
9610 
9611 #if GC_DEBUG_STRESS_TO_CLASS
9612  rb_define_singleton_method(rb_mGC, "add_stress_to_class", rb_gcdebug_add_stress_to_class, -1);
9613  rb_define_singleton_method(rb_mGC, "remove_stress_to_class", rb_gcdebug_remove_stress_to_class, -1);
9614 #endif
9615 
9616  /* ::GC::OPTS, which shows GC build options */
9617  {
9618  VALUE opts;
9619  rb_define_const(rb_mGC, "OPTS", opts = rb_ary_new());
9620 #define OPT(o) if (o) rb_ary_push(opts, rb_fstring_lit(#o))
9621  OPT(GC_DEBUG);
9622  OPT(USE_RGENGC);
9623  OPT(RGENGC_DEBUG);
9633 #undef OPT
9634  OBJ_FREEZE(opts);
9635  }
9636 }
#define ELTS_SHARED
Definition: ruby.h:944
VALUE of
Definition: gc.c:2463
rb_event_flag_t hook_events
Definition: gc.c:526
#define rb_objspace
Definition: gc.c:711
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:1312
VALUE value
Definition: gc.c:8279
static void * objspace_malloc_fixup(rb_objspace_t *objspace, void *mem, size_t size)
Definition: gc.c:7814
int rb_objspace_marked_object_p(VALUE obj)
Definition: gc.c:4404
void rb_gc(void)
Definition: gc.c:6656
union RString::@125 as
static const char * type_name(int type, VALUE obj)
Definition: gc.c:9134
static void mark_hash(rb_objspace_t *objspace, st_table *tbl)
Definition: gc.c:4077
rb_control_frame_t * cfp
Definition: vm_core.h:708
void rb_gc_finalize_deferred(void)
Definition: gc.c:2818
void rb_class_remove_from_super_subclasses(VALUE klass)
Definition: class.c:76
static int VM_ENV_ESCAPED_P(const VALUE *ep)
Definition: vm_core.h:1097
struct RNode node
Definition: gc.c:412
#define T_SYMBOL
Definition: ruby.h:508
size_t heap_total_objects
Definition: gc.c:354
#define T_OBJECT
Definition: ruby.h:491
static void gc_marks(rb_objspace_t *objspace, int full_mark)
Definition: gc.c:5610
size_t step_slots
Definition: gc.c:632
size_t marked_slots
Definition: gc.c:542
void(* RUBY_DATA_FUNC)(void *)
Definition: ruby.h:1122
const VALUE num
Definition: internal.h:525
Definition: re.h:44
Definition: st.h:99
VALUE rb_ary_last(int argc, const VALUE *argv, VALUE ary)
Definition: array.c:1361
rb_vm_t * vm
Definition: vm_core.h:703
void rb_class_detach_subclasses(VALUE klass)
Definition: class.c:133
void rb_free_const_table(struct rb_id_table *tbl)
Definition: gc.c:2091
static enum gc_mode gc_mode_verify(enum gc_mode mode)
Definition: gc.c:750
void * data
Definition: gc.c:805
const VALUE owner
Definition: method.h:144
static void gc_heap_prepare_minimum_pages(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:3530
struct rb_objspace::@114 rincgc
static int RVALUE_FLAGS_AGE(VALUE flags)
Definition: gc.c:1034
#define FL_EXIVAR
Definition: ruby.h:1222
VALUE regexp
Definition: re.h:48
struct heap_page * pooled_pages
Definition: gc.c:488
static void heap_set_increment(rb_objspace_t *objspace, size_t additional_pages)
Definition: gc.c:1664
void rb_gc_writebarrier(VALUE a, VALUE b)
Definition: gc.c:5910
static void gc_marks_start(rb_objspace_t *objspace, int full)
Definition: gc.c:5306
static void root_objects_from(VALUE obj, void *ptr)
Definition: gc.c:7542
#define RARRAY_LEN(a)
Definition: ruby.h:1026
static void RVALUE_DEMOTE(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1245
void rb_bug(const char *fmt,...)
Definition: error.c:482
rb_method_type_t type
Definition: method.h:148
struct heap_page::@115 flags
static void gc_grey(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:4326
#define heap_pages_final_slots
Definition: gc.c:732
size_t rb_id_table_memsize(const struct rb_id_table *tbl)
static VALUE gc_profile_disable(void)
Definition: gc.c:9120
static void objspace_malloc_increase(rb_objspace_t *objspace, void *mem, size_t new_size, size_t old_size, enum memop_type type)
Definition: gc.c:7730
static void gc_sweep_continue(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:3696
#define FALSE
Definition: nkf.h:174
static int set_zero(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:3272
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
rb_method_attr_t attr
Definition: method.h:155
static void gc_aging(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4343
#define GC_PROFILE_MORE_DETAIL
Definition: gc.c:291
double heap_free_slots_min_ratio
Definition: gc.c:169
static VALUE gc_stress_set_m(VALUE self, VALUE flag)
Definition: gc.c:7268
VALUE deferred_final
Definition: gc.c:554
#define STACK_START
Definition: gc.c:3921
VALUE rb_obj_id(VALUE obj)
Definition: gc.c:3100
#define SSIZE_MAX
Definition: ruby.h:292
size_t strlen(const char *)
gc_profile_record_flag
Definition: gc.c:324
#define roomof(x, y)
Definition: internal.h:838
#define INT2NUM(x)
Definition: ruby.h:1538
#define has_sweeping_pages(heap)
Definition: gc.c:785
RVALUE * start
Definition: gc.c:672
void rb_objspace_free(rb_objspace_t *objspace)
Definition: gc.c:1321
int need_major_gc
Definition: gc.c:612
static void gc_prof_set_malloc_info(rb_objspace_t *)
Definition: gc.c:8730
static enum rb_id_table_iterator_result free_const_entry_i(VALUE value, void *data)
Definition: gc.c:2083
#define RCLASS_CONST_TBL(c)
Definition: internal.h:689
Definition: constant.h:31
#define is_marking(objspace)
Definition: gc.c:768
size_t uncollectible_wb_unprotected_objects_limit
Definition: gc.c:615
static VALUE wmap_each(VALUE self)
Definition: gc.c:8336
#define T_FIXNUM
Definition: ruby.h:503
Definition: st.h:79
#define RUBY_DEFAULT_FREE
Definition: ruby.h:1139
void rb_gc_free_dsymbol(VALUE)
Definition: symbol.c:629
#define IMEMO_NAME(x)
static size_t xmalloc2_size(const size_t count, const size_t elsize)
Definition: gc.c:7858
static void pop_mark_stack_chunk(mark_stack_t *stack)
Definition: gc.c:3820
size_t num
Definition: gc.c:2462
Definition: st.h:99
size_t total_pages
Definition: gc.c:490
static VALUE wmap_each_value(VALUE self)
Definition: gc.c:8382
#define BIGNUM_DIGITS(b)
Definition: internal.h:515
#define T_MATCH
Definition: ruby.h:507
double gc_sweep_start_time
Definition: gc.c:595
size_t unused_cache_size
Definition: gc.c:477
static void run_final(rb_objspace_t *objspace, VALUE zombie)
Definition: gc.c:2762
static void gc_record(rb_objspace_t *objspace, int direction, const char *event)
Definition: gc.c:6518
static void gc_prof_timer_stop(rb_objspace_t *)
Definition: gc.c:8656
static int rgengc_remembered(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:5732
#define RSTRUCT_CONST_PTR(st)
Definition: internal.h:609
unsigned int UINT8 __attribute__((__mode__(__QI__)))
Definition: ffi_common.h:110
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:1031
struct RBasic basic
Definition: gc.c:802
struct rb_thread_struct::@204 machine
VALUE rb_errinfo(void)
Definition: eval.c:1623
static int max(int a, int b)
Definition: strftime.c:142
static VALUE id2ref(VALUE obj, VALUE objid)
Definition: gc.c:3034
static unsigned int hash(str, len) register const char *str
static void rb_raw_iseq_info(char *buff, const int buff_size, const rb_iseq_t *iseq)
Definition: gc.c:9208
struct RFile file
Definition: gc.c:411
short final_slots
Definition: gc.c:663
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define FIXNUM_FLAG
Definition: ruby.h:441
void * ruby_sized_xrealloc2(void *ptr, size_t n, size_t size, size_t old_n)
Definition: gc.c:7990
static int mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
Definition: gc.c:4067
size_t size
Definition: gc.c:565
#define GC_HEAP_GROWTH_FACTOR
Definition: gc.c:111
#define FL_USHIFT
Definition: ruby.h:1225
static VALUE os_each_obj(int argc, VALUE *argv, VALUE os)
Definition: gc.c:2566
#define RVALUE_AGE_SHIFT
Definition: gc.c:1026
static int is_pointer_to_heap(rb_objspace_t *objspace, void *ptr)
Definition: gc.c:2054
int run
Definition: gc.c:560
const VALUE location
Definition: method.h:135
int immediate_sweep
Definition: gc.c:6443
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
Definition: gc.c:2009
size_t ruby_stack_length(VALUE **p)
Definition: gc.c:3947
#define FLUSH_REGISTER_WINDOWS
Definition: defines.h:287
static const char * obj_type_name(VALUE obj)
Definition: gc.c:9174
size_t old_objects_limit
Definition: gc.c:617
#define malloc_allocated_size
Definition: gc.c:724
#define CLASS_OF(v)
Definition: ruby.h:453
void rb_gc_free_node(VALUE obj)
Definition: node.c:1005
#define GC_HEAP_OLDOBJECT_LIMIT_FACTOR
Definition: gc.c:117
static VALUE default_proc_for_compat_func(VALUE hash, VALUE dmy, int argc, VALUE *argv)
Definition: gc.c:7035
static void heap_page_free(rb_objspace_t *objspace, struct heap_page *page)
Definition: gc.c:1448
#define T_MODULE
Definition: ruby.h:494
static void gc_sweep_rest(rb_objspace_t *objspace)
Definition: gc.c:3685
const VALUE file
Definition: constant.h:35
#define RCLASS_EXT(c)
Definition: classext.h:15
#define N
Definition: lgamma_r.c:20
void rb_class_remove_from_module_subclasses(VALUE klass)
Definition: class.c:94
rb_cref_t *const cref
Definition: method.h:124
static size_t objspace_free_slots(rb_objspace_t *objspace)
Definition: gc.c:3418
#define st_foreach
Definition: regint.h:186
#define ATOMIC_EXCHANGE(var, val)
Definition: ruby_atomic.h:131
static void gc_mark_imemo(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4423
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1858
static int wmap_each_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8324
VALUE rb_obj_is_thread(VALUE obj)
Definition: vm.c:2487
const VALUE * env
Definition: vm_core.h:877
#define Qtrue
Definition: ruby.h:437
static void wmap_mark(void *ptr)
Definition: gc.c:8156
#define rb_data_typed_object_alloc
Definition: gc.c:15
unsigned int during_minor_gc
Definition: gc.c:519
static VALUE gc_verify_internal_consistency(VALUE self)
Definition: gc.c:5220
size_t onig_memsize(const regex_t *reg)
Definition: regcomp.c:5651
VALUE ecopts
Definition: io.h:85
static int RVALUE_REMEMBERED(VALUE obj)
Definition: gc.c:1130
size_t oldmalloc_limit_max
Definition: gc.c:179
#define BIGNUM_LEN(b)
Definition: internal.h:509
Definition: io.h:62
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:1971
unsigned int count
Definition: file.c:20
static void gc_finalize_deferred(void *dmy)
Definition: gc.c:2808
VALUE rb_wb_protected_newobj_of(VALUE klass, VALUE flags)
Definition: gc.c:1940
struct rb_io_t * fptr
Definition: ruby.h:1072
#define rb_id2str(id)
Definition: vm_backtrace.c:29
void rb_iseq_free(const rb_iseq_t *iseq)
Definition: iseq.c:70
static size_t objspace_malloc_prepare(rb_objspace_t *objspace, size_t size)
Definition: gc.c:7802
Definition: st.h:99
#define GC_HEAP_FREE_SLOTS_MAX_RATIO
Definition: gc.c:127
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1190
#define RGENGC_FORCE_MAJOR_GC
Definition: gc.c:271
static void * ruby_xmalloc0(size_t size)
Definition: gc.c:7925
void rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
static void gc_enter(rb_objspace_t *objspace, const char *event)
Definition: gc.c:6525
node_type
Definition: node.h:22
#define GET_HEAP_UNCOLLECTIBLE_BITS(x)
Definition: gc.c:704
static const rb_iseq_t * vm_proc_iseq(VALUE procval)
Definition: vm_core.h:1322
static void invalidate_mark_stack(mark_stack_t *stack, VALUE obj)
Definition: gc.c:3884
void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj)
Definition: gc.c:6031
static int stack_check(int water_mark)
Definition: gc.c:3957
static VALUE newobj_slowpath_wb_unprotected(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace)
Definition: gc.c:1899
static enum rb_id_table_iterator_result mark_const_entry_i(VALUE value, void *data)
Definition: gc.c:4146
void * ruby_xmalloc2(size_t n, size_t size)
Definition: gc.c:7945
#define heap_pages_sorted_length
Definition: gc.c:727
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
void rb_iseq_mark(const rb_iseq_t *iseq)
Definition: iseq.c:106
Definition: gc.c:445
void ruby_mimfree(void *ptr)
Definition: gc.c:8042
#define dont_gc
Definition: gc.c:736
void rb_gcdebug_print_obj_condition(VALUE obj)
#define GC_ENABLE_LAZY_SWEEP
Definition: gc.c:300
static int wmap_keys_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8393
VALUE rb_eTypeError
Definition: error.c:762
void(* dfree)(void *)
Definition: gc.c:804
size_t increase
Definition: gc.c:503
#define TH_JUMP_TAG(th, st)
Definition: eval_intern.h:186
#define finalizer_table
Definition: gc.c:739
#define T_RATIONAL
Definition: ruby.h:509
VALUE rb_newobj(void)
Definition: gc.c:1949
#define rb_check_arity
Definition: intern.h:303
RUBY_ALIAS_FUNCTION(rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree), rb_data_object_wrap,(klass, datap, dmark, dfree))
Definition: gc.c:1995
struct rb_data_type_struct::@131 function
#define ULONG2NUM(x)
Definition: ruby.h:1574
const rb_iseq_t * iseq
Definition: vm_core.h:875
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
#define SIZED_REALLOC_N(var, type, n, old_n)
Definition: internal.h:1093
#define RREGEXP_PTR(r)
Definition: ruby.h:1056
static void gc_prof_mark_timer_start(rb_objspace_t *)
Definition: gc.c:8668
#define heap_pages_freeable_pages
Definition: gc.c:731
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
rb_objspace_t * objspace
Definition: gc.c:5031
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
Definition: zonetab.h:883
#define GET_PAGE_BODY(x)
Definition: gc.c:687
#define RGENGC_DEBUG
Definition: gc.c:223
#define SYM2ID(x)
Definition: ruby.h:384
size_t oldmalloc_increase
Definition: gc.c:620
#define ARY_SHARED_P(ary)
Definition: gc.c:9200
const VALUE owner
Definition: method.h:55
ONIG_EXTERN void onig_region_free(OnigRegion *region, int free_self)
Definition: regexec.c:341
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:532
#define RGENGC_ESTIMATE_OLDMALLOC
Definition: gc.c:264
void * ruby_xrealloc2(void *ptr, size_t n, size_t size)
Definition: gc.c:8000
union RNode::@149 u3
struct rb_iseq_constant_body * body
Definition: vm_core.h:395
#define RGENGC_PROFILE
Definition: gc.c:254
static void RVALUE_AGE_SET_CANDIDATE(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1227
static int gc_page_sweep(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *sweep_page)
Definition: gc.c:3436
static struct heap_page * heap_page_create(rb_objspace_t *objspace)
Definition: gc.c:1579
static VALUE count_objects(int argc, VALUE *argv, VALUE os)
Definition: gc.c:3316
#define OBJ_PROMOTED(x)
Definition: ruby.h:1423
#define RGENGC_CHECK_MODE
Definition: gc.c:235
static VALUE run_single_final(VALUE final, VALUE objid)
Definition: gc.c:2713
void ruby_sized_xfree(void *x, size_t size)
Definition: gc.c:8009
void rb_objspace_reachable_objects_from_root(void(func)(const char *category, VALUE, void *), void *passing_data)
Definition: gc.c:7549
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
size_t next_index
Definition: gc.c:564
#define MARK_OBJECT_ARY_BUCKET_SIZE
Definition: gc.c:6150
#define global_list
Definition: gc.c:740
struct gc_list * next
Definition: gc.c:461
#define STACK_UPPER(x, a, b)
Definition: gc.h:77
#define gc_mode_set(objspace, mode)
Definition: gc.c:766
static double elapsed_time_from(double time)
Definition: gc.c:8644
static void * objspace_xmalloc(rb_objspace_t *objspace, size_t size)
Definition: gc.c:7849
static void gc_prof_sweep_timer_start(rb_objspace_t *)
Definition: gc.c:8691
#define PRIxVALUE
Definition: ruby.h:133
#define GC_HEAP_FREE_SLOTS_MIN_RATIO
Definition: gc.c:121
const VALUE * ep
Definition: vm_core.h:876
#define GC_MALLOC_LIMIT_MAX
Definition: gc.c:134
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define Check_Type(v, t)
Definition: ruby.h:562
static int gc_start(rb_objspace_t *objspace, const int full_mark, const int immediate_mark, const unsigned int immediate_sweep, int reason)
Definition: gc.c:6329
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
int rb_io_fptr_finalize(rb_io_t *)
Definition: io.c:4399
static VALUE newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, int wb_protected)
Definition: gc.c:1905
static void ruby_memerror(void)
Definition: gc.c:7604
struct rb_objspace::@109 flags
struct re_registers regs
Definition: re.h:37
#define heap_allocated_pages
Definition: gc.c:726
static int RVALUE_WHITE_P(VALUE obj)
Definition: gc.c:1293
rb_env_t env
Definition: gc.c:424
ID called_id
Definition: method.h:54
int rb_objspace_garbage_object_p(VALUE obj)
Definition: gc.c:3014
int ruby_get_stack_grow_direction(volatile VALUE *addr)
Definition: gc.c:3936
#define GC_PROFILE_DETAIL_MEMORY
Definition: gc.c:294
void * rb_alloc_tmp_buffer(volatile VALUE *store, long len)
Definition: gc.c:8066
#define TH_EXEC_TAG()
Definition: eval_intern.h:180
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:690
#define T_HASH
Definition: ruby.h:499
static void gc_rest(rb_objspace_t *objspace)
Definition: gc.c:6416
void Init_heap(void)
Definition: gc.c:2323
int ruby_thread_has_gvl_p(void)
Definition: thread.c:1541
ONIG_EXTERN void onig_free(OnigRegex)
#define ruby_gc_stress_mode
Definition: gc.c:742
size_t rb_io_memsize(const rb_io_t *)
Definition: io.c:4414
struct RVALUE::@104::@105 free
static int wmap_memsize_map(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8183
const VALUE value
Definition: constant.h:34
#define ruby_gc_stressful
Definition: gc.c:741
static int wmap_values_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8420
#define ARY_EMBED_P(ary)
Definition: gc.c:9203
static VALUE gc_profile_total_time(VALUE self)
Definition: gc.c:9064
static struct heap_page * heap_page_allocate(rb_objspace_t *objspace)
Definition: gc.c:1481
double oldobject_limit_factor
Definition: gc.c:172
static int obj_free(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:2116
static int mark_key(st_data_t key, st_data_t value, st_data_t data)
Definition: gc.c:4046
static int heap_is_swept_object(rb_objspace_t *objspace, rb_heap_t *heap, VALUE ptr)
Definition: gc.c:2949
#define nd_set_type(n, t)
Definition: node.h:275
#define DATA_PTR(dta)
Definition: ruby.h:1113
static size_t objspace_available_slots(rb_objspace_t *objspace)
Definition: gc.c:3406
void rb_objspace_each_objects(each_obj_callback *callback, void *data)
Definition: gc.c:2431
unsigned int mode
Definition: gc.c:511
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
static const char * gc_current_status(rb_objspace_t *objspace)
Definition: gc.c:6470
#define GC_PROFILE_RECORD_DEFAULT_SIZE
Definition: gc.c:8526
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4394
int ruby_disable_gc
Definition: gc.c:814
size_t rb_gc_count(void)
Definition: gc.c:6688
static int rgengc_remembersetbits_get(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:5679
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:867
int rb_objspace_markable_object_p(VALUE obj)
Definition: gc.c:3007
size_t sorted_length
Definition: gc.c:548
#define T_ARRAY
Definition: ruby.h:498
st_data_t st_index_t
Definition: st.h:50
#define ATOMIC_PTR_EXCHANGE(var, val)
Definition: ruby_atomic.h:177
#define RUBY_INTERNAL_EVENT_GC_START
Definition: ruby.h:2087
#define BITMAP_BIT(p)
Definition: gc.c:694
#define st_delete
Definition: regint.h:182
#define st_lookup
Definition: regint.h:185
void rb_gc_register_address(VALUE *addr)
Definition: gc.c:6168
#define heap_pages_himem
Definition: gc.c:729
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: st.c:1371
VALUE rb_io_write(VALUE, VALUE)
Definition: io.c:1508
#define VALGRIND_MAKE_MEM_UNDEFINED(p, n)
Definition: zlib.c:25
#define RFILE(obj)
Definition: ruby.h:1213
static void RVALUE_AGE_INC(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1194
static void check_generation_i(const VALUE child, void *ptr)
Definition: gc.c:5045
#define ROBJECT_NUMIV(o)
Definition: ruby.h:900
memop_type
Definition: gc.c:7703
#define S(s)
int index
Definition: gc.c:474
#define rb_setjmp(env)
Definition: gc.c:89
time_t tv_sec
Definition: missing.h:54
static int gc_mark_stacked_objects_incremental(rb_objspace_t *, size_t count)
Definition: gc.c:4676
Definition: gc.c:459
#define assert(x)
Definition: dlmalloc.c:1176
const rb_iseq_t iseq
Definition: gc.c:423
size_t heap_used_at_gc_start
Definition: gc.c:597
void rb_gc_force_recycle(VALUE obj)
Definition: gc.c:6102
VALUE writeconv_pre_ecopts
Definition: io.h:95
static VALUE wmap_each_key(VALUE self)
Definition: gc.c:8359
size_t malloc_limit_min
Definition: gc.c:174
int char_offset_num_allocated
Definition: re.h:40
void rb_obj_info_dump(VALUE obj)
Definition: gc.c:9380
struct RNode * node
Definition: node.h:239
#define gc_event_hook(objspace, event, data)
Definition: gc.c:1776
#define FIXNUM_P(f)
Definition: ruby.h:365
static size_t obj_memsize_of(VALUE obj, int use_all_types)
Definition: gc.c:3149
#define ATOMIC_VALUE_EXCHANGE(var, val)
Definition: ruby_atomic.h:206
const char * rb_source_loc(int *pline)
Definition: vm.c:1291
#define st_init_strtable
Definition: regint.h:180
int ruby_native_thread_p(void)
Definition: thread.c:4900
#define T_UNDEF
Definition: ruby.h:512
#define nd_type(n)
Definition: node.h:274
static int RVALUE_UNCOLLECTIBLE(VALUE obj)
Definition: gc.c:1137
VALUE rb_ary_cat(VALUE ary, const VALUE *argv, long len)
Definition: array.c:917
static void gc_reset_malloc_info(rb_objspace_t *objspace)
Definition: gc.c:6244
#define RDATA(obj)
Definition: ruby.h:1211
static size_t heap_extend_pages(rb_objspace_t *objspace, size_t free_slots, size_t total_slots)
Definition: gc.c:1625
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2802
int limit
Definition: gc.c:475
size_t total_allocated_objects_at_gc_start
Definition: gc.c:596
static int heap_increment(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1678
static size_t objspace_malloc_size(rb_objspace_t *objspace, void *ptr, size_t hint)
Definition: gc.c:7694
#define SET(name, attr)
Definition: ruby.h:1078
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Definition: gc.c:4008
#define RSTRUCT(obj)
Definition: internal.h:613
#define is_incremental_marking(objspace)
Definition: gc.c:776
bits_t wb_unprotected_bits[HEAP_PAGE_BITMAP_LIMIT]
Definition: gc.c:677
VALUE gc_stress_mode
Definition: gc.c:607
static int wmap_free_map(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8166
int ruby_stack_grow_direction
Definition: gc.c:3934
gc_stat_sym
Definition: gc.c:6837
int ruby_stack_check(void)
Definition: gc.c:3976
size_t oldmalloc_limit_min
Definition: gc.c:178
#define OBJ_TAINTED(x)
Definition: ruby.h:1298
struct heap_page_header header
Definition: gc.c:454
#define RHASH_IFNONE(h)
Definition: ruby.h:1065
static VALUE wmap_size(VALUE self)
Definition: gc.c:8508
VALUE rb_eRangeError
Definition: error.c:766
const char * rb_obj_classname(VALUE)
Definition: variable.c:458
struct rb_objspace::mark_func_data_struct * mark_func_data
static void push_mark_stack(mark_stack_t *, VALUE)
Definition: gc.c:3845
#define MARK_CHECKPOINT(category)
stack_chunk_t * chunk
Definition: gc.c:472
rb_method_iseq_t iseq
Definition: method.h:153
#define ATOMIC_SIZE_ADD(var, val)
Definition: ruby_atomic.h:134
unsigned int has_hook
Definition: gc.c:517
#define ATOMIC_SIZE_CAS(var, oldval, val)
Definition: ruby_atomic.h:156
static void gc_mode_transition(rb_objspace_t *objspace, enum gc_mode mode)
Definition: gc.c:3553
static VALUE objspace_each_objects(VALUE arg)
Definition: gc.c:2358
static void negative_size_allocation_error(const char *)
Definition: gc.c:7580
static void heap_ready_to_gc(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:6221
gc_profile_record * current_record
Definition: gc.c:563
#define ruby_initial_gc_stress
Definition: gc.c:718
#define RVALUE_MARKING_BITMAP(obj)
Definition: gc.c:1019
#define GET_THREAD()
Definition: vm_core.h:1513
rb_heap_t tomb_heap
Definition: gc.c:530
static void finalize_list(rb_objspace_t *objspace, VALUE zombie)
Definition: gc.c:2777
time_t tv_sec
Definition: missing.h:61
static VALUE define_final(int argc, VALUE *argv, VALUE os)
Definition: gc.c:2635
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
static int force_chain_object(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:2838
#define heap_pages_deferred_final
Definition: gc.c:733
Definition: node.h:235
unsigned int immediate_sweep
Definition: gc.c:512
#define obj_id_to_ref(objid)
Definition: gc.c:790
#define RVALUE_PAGE_WB_UNPROTECTED(page, obj)
Definition: gc.c:1021
void rb_global_variable(VALUE *var)
Definition: gc.c:6203
static VALUE wmap_allocate(VALUE klass)
Definition: gc.c:8213
static void mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th)
Definition: gc.c:4177
#define GC_OLDMALLOC_LIMIT_GROWTH_FACTOR
Definition: gc.c:144
void rb_gc_unprotect_logging(void *objptr, const char *filename, int line)
Definition: gc.c:6001
void rb_objspace_each_objects_without_setup(each_obj_callback *callback, void *data)
Definition: gc.c:2452
VALUE data[STACK_CHUNK_SIZE]
Definition: gc.c:467
unsigned int gc_stressful
Definition: gc.c:516
void rb_gc_mark_values(long n, const VALUE *values)
Definition: gc.c:4024
#define RVALUE_OLD_AGE
Definition: gc.c:1025
void rb_exc_raise(VALUE mesg)
Definition: eval.c:620
void rb_objspace_set_event_hook(const rb_event_flag_t event)
Definition: gc.c:1760
size_t limit
Definition: gc.c:502
const VALUE den
Definition: internal.h:526
#define malloc_increase
Definition: gc.c:723
#define FL_SINGLETON
Definition: ruby.h:1215
struct RVALUE RVALUE
#define RHASH(obj)
Definition: internal.h:562
VALUE rb_define_finalizer(VALUE obj, VALUE block)
Definition: gc.c:2690
static void mark_m_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
Definition: gc.c:4138
size_t total_freed_objects
Definition: gc.c:601
gc_mode
Definition: gc.c:494
#define strtod(s, e)
Definition: util.h:77
#define RBASIC_SET_CLASS_RAW(obj, cls)
Definition: internal.h:1313
static void check_color_i(const VALUE child, void *ptr)
Definition: gc.c:5063
size_t rb_obj_memsize_of(VALUE obj)
Definition: gc.c:3266
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
void * ruby_xcalloc(size_t n, size_t size)
Definition: gc.c:7966
#define GET_STACK_BOUNDS(start, end, appendix)
Definition: gc.c:4168
Definition: gc.c:8136
struct heap_page * next
Definition: gc.c:674
static void gc_prof_timer_start(rb_objspace_t *)
Definition: gc.c:8631
size_t uncollectible_wb_unprotected_objects
Definition: gc.c:614
#define ATOMIC_SET(var, val)
Definition: ruby_atomic.h:127
void Init_GC(void)
Definition: gc.c:9531
#define TH_POP_TAG()
Definition: eval_intern.h:137
#define will_be_incremental_marking(objspace)
Definition: gc.c:781
#define MEMZERO(p, type, n)
Definition: ruby.h:1660
VALUE rb_obj_method(VALUE, VALUE)
Definition: proc.c:1723
Definition: ruby.h:961
const VALUE src
Definition: ruby.h:1053
#define is_sweeping(objspace)
Definition: gc.c:769
static VALUE check_rvalue_consistency(const VALUE obj)
Definition: gc.c:1044
size_t oldmalloc_increase_limit
Definition: gc.c:621
void rb_gc_adjust_memory_usage(ssize_t diff)
Definition: gc.c:8121
#define RUBY_INTERNAL_EVENT_GC_ENTER
Definition: ruby.h:2090
struct rb_objspace::@108 malloc_params
size_t total_allocated_objects
Definition: gc.c:527
void rb_free_generic_ivar(VALUE)
Definition: variable.c:1183
#define during_gc
Definition: gc.c:737
#define FL_TEST(x, f)
Definition: ruby.h:1284
int rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
Definition: vm_trace.c:1581
int latest_gc_info
Definition: gc.c:561
PUREFUNC(static inline int is_id_value(rb_objspace_t *objspace, VALUE ptr))
static VALUE wmap_has_key(VALUE self, VALUE key)
Definition: gc.c:8502
static void * aligned_malloc(size_t, size_t)
Definition: gc.c:7644
static void gc_writebarrier_incremental(VALUE a, VALUE b, rb_objspace_t *objspace)
Definition: gc.c:5878
void * rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t size, size_t cnt)
Definition: gc.c:8052
void rb_ary_free(VALUE ary)
Definition: array.c:547
void rb_mark_end_proc(void)
Definition: eval_jump.c:80
static void gc_mark(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:4387
#define STACK_CHUNK_SIZE
Definition: gc.c:464
#define ROBJECT_IVPTR(o)
Definition: ruby.h:904
#define GC_HEAP_GROWTH_MAX_SLOTS
Definition: gc.c:114
VALUE rb_class_name(VALUE)
Definition: variable.c:443
VALUE rb_undefine_finalizer(VALUE obj)
Definition: gc.c:2595
void rb_id_table_free(struct rb_id_table *tbl)
#define RGENGC_OLD_NEWOBJ_CHECK
Definition: gc.c:245
#define RUBY_SAFE_LEVEL_MAX
Definition: ruby.h:599
#define ALLOC_N(type, n)
Definition: ruby.h:1587
void rb_vm_mark(void *ptr)
Definition: vm.c:2101
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1576
double gc_invoke_time
Definition: gc.c:352
size_t rb_generic_ivar_memsize(VALUE)
Definition: variable.c:1200
static void * gc_with_gvl(void *ptr)
Definition: gc.c:6548
NOINLINE(static VALUE newobj_slowpath_wb_protected(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace))
const rb_env_t * rb_vm_env_prev_env(const rb_env_t *env)
Definition: vm.c:739
void rb_gc_copy_finalizer(VALUE dest, VALUE obj)
Definition: gc.c:2698
#define nomem_error
Definition: gc.c:810
VALUE final
Definition: gc.c:8139
rb_method_alias_t alias
Definition: method.h:156
#define val
size_t allocated_pages
Definition: gc.c:546
short total_slots
Definition: gc.c:661
long tv_usec
Definition: missing.h:55
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
static void finalize_deferred(rb_objspace_t *objspace)
Definition: gc.c:2798
static VALUE define_final0(VALUE obj, VALUE block)
Definition: gc.c:2652
IUnknown DWORD
Definition: win32ole.c:32
static void * negative_size_allocation_error_with_gvl(void *ptr)
Definition: gc.c:7573
void rb_gc_unregister_address(VALUE *addr)
Definition: gc.c:6180
size_t pooled_slots
Definition: gc.c:631
size_t st_memsize(const st_table *tab)
Definition: st.c:674
static int pop_mark_stack(mark_stack_t *, VALUE *)
Definition: gc.c:3854
static VALUE gc_stress_get(VALUE self)
Definition: gc.c:7237
void rb_free_tmp_buffer(volatile VALUE *store)
Definition: gc.c:8078
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
Definition: zlib.c:24
void rb_str_free(VALUE)
Definition: string.c:1281
size_t malloc_limit_max
Definition: gc.c:175
#define I(s)
#define GET_HEAP_MARKING_BITS(x)
Definition: gc.c:706
#define T_NIL
Definition: ruby.h:490
VALUE * varptr
Definition: gc.c:460
static void RVALUE_OLD_UNCOLLECTIBLE_SET(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1179
#define NUM2PTR(x)
const VALUE imag
Definition: internal.h:543
rb_atomic_t finalizing
Definition: gc.c:533
VALUE rb_str_cat2(VALUE, const char *)
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1871
static void gc_event_hook_body(rb_thread_t *th, rb_objspace_t *objspace, const rb_event_flag_t event, VALUE data)
Definition: gc.c:1768
VALUE rb_ary_new(void)
Definition: array.c:493
#define O(member)
Definition: eventids2.c:130
#define T_TRUE
Definition: ruby.h:504
struct gc_profile_record gc_profile_record
#define UINT2NUM(x)
Definition: ruby.h:1539
static void gc_mark_maybe(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:4235
mark_stack_t mark_stack
Definition: gc.c:541
static void RVALUE_AGE_SET_OLD(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1214
Definition: ruby.h:854
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1860
#define RSTRUCT_LEN(st)
Definition: ruby.h:1193
static VALUE compat_key(VALUE key)
Definition: gc.c:7016
static const char * obj_info(VALUE obj)
Definition: gc.c:9362
#define M(n)
#define snprintf
Definition: subst.h:6
unsigned int before_sweep
Definition: gc.c:665
static VALUE heap_get_freeobj(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1744
Definition: ruby.h:1009
Definition: gc.c:393
static void push_mark_stack_chunk(mark_stack_t *stack)
Definition: gc.c:3798
#define nonspecial_obj_id(obj)
Definition: gc.c:789
#define NIL_P(v)
Definition: ruby.h:451
st_table * obj2wmap
Definition: gc.c:8137
long tv_nsec
Definition: missing.h:62
const VALUE real
Definition: internal.h:542
static struct heap_page * heap_page_resurrect(rb_objspace_t *objspace)
Definition: gc.c:1563
VALUE rb_gc_latest_gc_info(VALUE key)
Definition: gc.c:6803
static VALUE os_obj_of(VALUE of)
Definition: gc.c:2519
#define add(x, y)
Definition: date_strftime.c:23
#define CEILDIV(i, mod)
Definition: gc.c:647
unsigned int dont_incremental
Definition: gc.c:514
static char msg[50]
Definition: strerror.c:8
#define calloc
Definition: ripper.c:118
static size_t wmap_memsize(const void *ptr)
Definition: gc.c:8191
static int heap_add_poolpage(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
Definition: gc.c:1421
struct rb_method_definition_struct *const def
Definition: method.h:53
static void gc_sweep_finish(rb_objspace_t *objspace)
Definition: gc.c:3598
void rb_free_method_entry(const rb_method_entry_t *me)
Definition: vm_method.c:174
double gc_time
Definition: gc.c:351
#define RCLASS_IV_TBL(c)
Definition: internal.h:688
#define RUBY_DTRACE_GC_HOOK(name)
Definition: gc.c:8665
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2734
VALUE value
Definition: node.h:241
VALUE rb_obj_is_mutex(VALUE obj)
Definition: thread_sync.c:83
#define MALLOC_ALLOCATED_SIZE
Definition: gc.c:310
Definition: internal.h:818
VALUE rb_eNoMemError
Definition: error.c:773
static void heap_add_freepage(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
Definition: gc.c:1411
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:525
#define FLONUM_P(x)
Definition: ruby.h:399
struct rb_io_t::rb_io_enc_t encs
#define T_FLOAT
Definition: ruby.h:495
VALUE rb_mGC
Definition: gc.c:813
#define rb_data_object_alloc
Definition: gc.c:14
#define TYPE(x)
Definition: ruby.h:521
static void gc_marks_step(rb_objspace_t *objspace, int slots)
Definition: gc.c:5532
int argc
Definition: ruby.c:183
void rb_vm_register_special_exception(enum ruby_special_exceptions sp, VALUE cls, const char *mesg)
Definition: vm.c:2142
VALUE writeconv_asciicompat
Definition: io.h:92
size_t total_freed_pages
Definition: gc.c:603
static int RVALUE_OLD_P_RAW(VALUE obj)
Definition: gc.c:1144
static void callback(ffi_cif *cif, void *resp, void **args, void *ctx)
Definition: closure.c:183
struct heap_page * pages
Definition: gc.c:485
#define Qfalse
Definition: ruby.h:436
static void objspace_malloc_gc_stress(rb_objspace_t *objspace)
Definition: gc.c:7722
static void gc_finalize_deferred_register(rb_objspace_t *objspace)
Definition: gc.c:2824
#define RVALUE_UNCOLLECTIBLE_BITMAP(obj)
Definition: gc.c:1018
#define realloc
Definition: ripper.c:117
static int is_garbage_object(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:2968
size_t onig_region_memsize(const OnigRegion *regs)
Definition: regcomp.c:5666
unsigned int dont_gc
Definition: gc.c:513
Definition: method.h:50
struct force_finalize_list * next
Definition: gc.c:2834
static int gc_sweep_step(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:3619
static const char * method_type_name(rb_method_type_t type)
Definition: gc.c:9180
VALUE rb_gc_mark_node(NODE *obj)
Definition: node.c:1047
#define T_BIGNUM
Definition: ruby.h:501
#define range(low, item, hi)
Definition: date_strftime.c:21
#define heap_eden
Definition: gc.c:734
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:6154
#define ATOMIC_SIZE_EXCHANGE(var, val)
Definition: ruby_atomic.h:136
#define T_NODE
Definition: ruby.h:513
#define GC_MALLOC_LIMIT_GROWTH_FACTOR
Definition: gc.c:137
size_t(* dsize)(const void *)
Definition: ruby.h:1092
#define RNODE(obj)
Definition: node.h:262
static VALUE gc_stat_compat_symbols[gc_stat_compat_sym_last]
Definition: gc.c:6906
#define STACK_END
Definition: gc.c:3922
#define OBJ_FREEZE(x)
Definition: ruby.h:1308
#define EXIT_FAILURE
Definition: eval_intern.h:33
#define is_lazy_sweeping(heap)
Definition: gc.c:786
#define STR_SHARED_P(s)
Definition: internal.h:1489
static void gc_marks_rest(rb_objspace_t *objspace)
Definition: gc.c:5547
#define T_COMPLEX
Definition: ruby.h:510
#define RVALUE_MARK_BITMAP(obj)
Definition: gc.c:1013
static int RVALUE_BLACK_P(VALUE obj)
Definition: gc.c:1279
static void rgengc_rememberset_mark(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:5745
static RVALUE * heap_get_freeobj_from_next_freepage(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1714
long cnt
Definition: node.h:257
size_t rb_objspace_data_type_memsize(VALUE obj)
Definition: gc.c:2029
#define is_full_marking(objspace)
Definition: gc.c:771
NODE * rb_node_newnode(enum node_type type, VALUE a0, VALUE a1, VALUE a2)
Definition: gc.c:1961
static int RVALUE_WB_UNPROTECTED(VALUE obj)
Definition: gc.c:1116
double heap_free_slots_goal_ratio
Definition: gc.c:170
#define numberof(array)
Definition: etc.c:616
void rb_gc_mark_machine_stack(rb_thread_t *th)
Definition: gc.c:4201
#define ALLOC(type)
Definition: ruby.h:1588
static VALUE wmap_values(VALUE self)
Definition: gc.c:8434
rb_method_type_t
Definition: method.h:100
static void run_finalizer(rb_objspace_t *objspace, VALUE obj, VALUE table)
Definition: gc.c:2724
void * ruby_mimmalloc(size_t size)
Definition: gc.c:8026
#define PRIuVALUE
Definition: ruby.h:132
size_t heap_total_size
Definition: gc.c:356
union RNode::@147 u1
static void gc_mark_locations(rb_objspace_t *objspace, const VALUE *start, const VALUE *end)
Definition: gc.c:3998
struct st_table * ntbl
Definition: internal.h:557
st_table * finalizer_table
Definition: gc.c:557
static void heap_pages_expand_sorted_to(rb_objspace_t *objspace, size_t next_length)
Definition: gc.c:1361
#define rb_thread_raised_clear(th)
Definition: eval_intern.h:270
#define RUBY_INTERNAL_EVENT_GC_END_MARK
Definition: ruby.h:2088
static void mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
Definition: gc.c:4157
#define stress_to_class
Definition: gc.c:746
static int internal_object_p(VALUE obj)
Definition: gc.c:2467
#define sub(x, y)
Definition: date_strftime.c:24
#define FL_FINALIZE
Definition: ruby.h:1219
#define FL_PROMOTED1
Definition: ruby.h:1218
#define FL_ABLE(x)
Definition: ruby.h:1282
#define COUNT_TYPE(t)
static void gc_exit(rb_objspace_t *objspace, const char *event)
Definition: gc.c:6537
static void gc_prof_set_heap_info(rb_objspace_t *)
Definition: gc.c:8742
size_t total_slots
Definition: gc.c:491
static const char * gc_mode_name(enum gc_mode mode)
Definition: gc.c:3542
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1731
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1020
static void gc_profile_dump_on(VALUE out, VALUE(*append)(VALUE, VALUE))
Definition: gc.c:8918
volatile VALUE * rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val)
Definition: gc.c:96
#define RCLASS_M_TBL(c)
Definition: internal.h:690
#define heap_tomb
Definition: gc.c:735
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1028
#define FL_PROMOTED0
Definition: ruby.h:1217
struct RRational rational
Definition: gc.c:414
int rb_during_gc(void)
Definition: gc.c:6664
static void rgengc_unprotect_logging_exit_func(void)
Definition: gc.c:5995
#define lo
Definition: siphash.c:21
union rb_method_definition_struct::@144 body
static double getrusage_time(void)
Definition: gc.c:8530
#define TRUE
Definition: nkf.h:175
static VALUE gc_profile_report(int argc, VALUE *argv, VALUE self)
Definition: gc.c:9041
VALUE * ruby_initial_gc_stress_ptr
Definition: gc.c:720
#define T_DATA
Definition: ruby.h:506
double malloc_limit_growth_factor
Definition: gc.c:176
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:117
void ruby_malloc_size_overflow(size_t count, size_t elsize)
Definition: gc.c:7937
static void gc_set_initial_pages(void)
Definition: gc.c:7421
static VALUE RVALUE_FLAGS_AGE_SET(VALUE flags, int age)
Definition: gc.c:1185
size_t cache_size
Definition: gc.c:476
static void gc_prof_sweep_timer_stop(rb_objspace_t *)
Definition: gc.c:8704
VALUE rb_mEnumerable
Definition: enum.c:18
static int rgengc_unprotect_logging_exit_func_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:5988
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
int rb_objspace_internal_object_p(VALUE obj)
Definition: gc.c:2494
static VALUE incremental_enable(void)
Definition: gc.c:2386
#define GET_HEAP_MARK_BITS(x)
Definition: gc.c:702
#define STACKFRAME_FOR_CALL_CFUNC
Definition: gc.c:3973
VALUE parent_object
Definition: gc.c:611
#define RICLASS_IS_ORIGIN
Definition: internal.h:697
int rb_obj_respond_to(VALUE, ID, int)
Definition: vm_method.c:1985
#define VM_ASSERT(expr)
Definition: vm_core.h:54
#define GC_DEBUG
Definition: gc.c:211
#define RESTORE_FINALIZER()
PRINTF_ARGS(static void gc_report_body(int level, rb_objspace_t *objspace, const char *fmt,...), 3, 4)
static void gc_prof_mark_timer_stop(rb_objspace_t *)
Definition: gc.c:8679
static void aligned_free(void *)
Definition: gc.c:7680
unsigned int has_remembered_objects
Definition: gc.c:666
static void gc_sweep(rb_objspace_t *objspace)
Definition: gc.c:3712
const VALUE klass
Definition: ruby.h:856
size_t rb_obj_gc_flags(VALUE obj, ID *flags, size_t max)
Definition: gc.c:6069
static void add_stack_chunk_cache(mark_stack_t *stack, stack_chunk_t *chunk)
Definition: gc.c:3776
#define MARKED_IN_BITMAP(bits, p)
Definition: gc.c:697
#define MEMMOVE(p1, p2, type, n)
Definition: ruby.h:1662
#define malloc
Definition: ripper.c:116
#define RZOMBIE(o)
Definition: gc.c:808
static void gc_stress_set(rb_objspace_t *objspace, VALUE flag)
Definition: gc.c:7244
struct RHash hash
Definition: gc.c:406
VALUE rb_hash_new(void)
Definition: hash.c:441
void ruby_xfree(void *x)
Definition: gc.c:8017
static void gc_writebarrier_generational(VALUE a, VALUE b, rb_objspace_t *objspace)
Definition: gc.c:5832
#define STACK_LENGTH
Definition: gc.c:3930
static VALUE wmap_aset(VALUE self, VALUE wmap, VALUE orig)
Definition: gc.c:8469
size_t rb_str_memsize(VALUE)
Definition: string.c:1294
static int os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
Definition: gc.c:2500
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
RVALUE * freelist
Definition: gc.c:481
const char * category
Definition: gc.c:7536
static int rgengc_remember(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:5706
#define RUBY_INTERNAL_EVENT_GC_EXIT
Definition: ruby.h:2091
#define rb_thread_raised_set(th, f)
Definition: eval_intern.h:267
Definition: ruby.h:1070
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define T_IMEMO
Definition: ruby.h:511
static int wmap_final_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: gc.c:8224
#define PRIsVALUE
Definition: ruby.h:135
static void mark_tbl(rb_objspace_t *objspace, st_table *tbl)
Definition: gc.c:4039
void Init_stack(volatile VALUE *addr)
Definition: gc.c:6588
static st_table * rgengc_unprotect_logging_table
Definition: gc.c:5985
unsigned long ID
Definition: ruby.h:86
size_t final_slots
Definition: gc.c:553
static ruby_gc_params_t gc_params
Definition: gc.c:185
VALUE tied_io_for_writing
Definition: io.h:73
VALUE v2
Definition: gc.c:429
unsigned int env_size
Definition: vm_core.h:878
#define ATOMIC_SIZE_INC(var)
Definition: ruby_atomic.h:147
#define MALLOC_ALLOCATED_SIZE_CHECK
Definition: gc.c:313
static void atomic_sub_nounderflow(size_t *var, size_t sub)
Definition: gc.c:7710
static int gc_mark_stacked_objects_all(rb_objspace_t *)
Definition: gc.c:4682
#define Qnil
Definition: ruby.h:438
#define T_STRUCT
Definition: ruby.h:500
unsigned int uintptr_t
Definition: win32.h:106
VALUE next
Definition: gc.c:803
static void heap_pages_free_unused_pages(rb_objspace_t *objspace)
Definition: gc.c:1457
static int is_id_value(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:2940
#define LIKELY(x)
Definition: ffi_common.h:125
size_t major_gc_count
Definition: gc.c:574
static void rgengc_mark_and_rememberset_clear(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:5813
static void heap_add_pages(rb_objspace_t *objspace, rb_heap_t *heap, size_t add)
Definition: gc.c:1612
#define BITMAP_INDEX(p)
Definition: gc.c:692
VALUE rb_obj_rgengc_writebarrier_protected_p(VALUE obj)
Definition: gc.c:6053
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
unsigned long VALUE
Definition: ruby.h:85
#define NUM_IN_PAGE(p)
Definition: gc.c:691
struct rb_objspace::@112 profile
static VALUE gc_profile_enable_get(VALUE self)
Definition: gc.c:9088
static void rgengc_check_relation(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4287
static VALUE result
Definition: nkf.c:40
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, called_id_, klass_, data_)
Definition: vm_core.h:1628
static VALUE gc_start_internal(int argc, VALUE *argv, VALUE self)
Definition: gc.c:6617
short free_slots
Definition: gc.c:662
static int mark_entry(st_data_t key, st_data_t value, st_data_t data)
Definition: gc.c:4031
#define gc_prof_record(objspace)
Definition: gc.c:892
#define RBASIC(obj)
Definition: ruby.h:1204
const char * rb_objspace_data_type_name(VALUE obj)
Definition: gc.c:2042
const VALUE defined_class
Definition: method.h:52
#define GET_HEAP_WB_UNPROTECTED_BITS(x)
Definition: gc.c:705
VALUE rb_obj_hide(VALUE obj)
Definition: object.c:51
static void gc_current_status_fill(rb_objspace_t *objspace, char *buff)
Definition: gc.c:6447
double heap_free_slots_max_ratio
Definition: gc.c:171
static int is_markable_object(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:2999
#define USE_RGENGC
Definition: ruby.h:760
#define STATIC_SYM_P(x)
Definition: ruby.h:380
#define FIX2INT(x)
Definition: ruby.h:686
static void * ruby_memerror_body(void *dummy)
Definition: gc.c:7597
void rb_mark_tbl(st_table *tbl)
Definition: gc.c:4229
const struct rb_method_entry_struct *const original_me
Definition: method.h:139
static VALUE newobj_slowpath_wb_protected(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace)
Definition: gc.c:1893
Definition: gc.c:659
rb_cref_t cref
Definition: gc.c:417
size_t heap_use_size
Definition: gc.c:355
void(* dmark)(void *)
Definition: ruby.h:1080
RUBY_FUNC_EXPORTED size_t rb_ary_memsize(VALUE ary)
Definition: array.c:555
static ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS void mark_locations_array(rb_objspace_t *objspace, register const VALUE *x, register long n)
Definition: gc.c:3987
#define heap_pages_lomem
Definition: gc.c:728
#define rb_ary_new3
Definition: intern.h:91
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:131
VALUE rb_gc_disable(void)
Definition: gc.c:7311
#define OPT(o)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:439
int clock_gettime(clockid_t, struct timespec *)
Definition: win32.c:4592
static int gc_verify_heap_pages_(rb_objspace_t *objspace, struct heap_page *page)
Definition: gc.c:5186
#define SET_MACHINE_STACK_END(p)
Definition: gc.h:11
void ruby_init_stack(volatile VALUE *)
const char * rb_id2name(ID)
Definition: symbol.c:759
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:923
VALUE flags
Definition: ruby.h:855
static void gc_mark_ptr(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:4373
VALUE rb_str_new_cstr(const char *)
Definition: string.c:770
struct heap_page * free_next
Definition: gc.c:671
int rb_sigaltstack_size(void)
static void make_zombie(rb_objspace_t *objspace, VALUE obj, void(*dfree)(void *), void *data)
Definition: gc.c:2098
void * ruby_xrealloc(void *ptr, size_t new_size)
Definition: gc.c:7981
static void gc_sweep_start(rb_objspace_t *objspace)
Definition: gc.c:3591
size_t growth_max_slots
Definition: gc.c:167
struct heap_page * page
Definition: gc.c:450
static int gc_marks_finish(rb_objspace_t *objspace)
Definition: gc.c:5398
void ruby_gc_set_params(int safe_level)
Definition: gc.c:7475
void rb_class_detach_module_subclasses(VALUE klass)
Definition: class.c:145
static void wmap_free(void *ptr)
Definition: gc.c:8174
static int is_mark_stack_empty(mark_stack_t *stack)
Definition: gc.c:3757
int rb_garbage_collect(void)
Definition: gc.c:6580
static enum rb_id_table_iterator_result mark_method_entry_i(VALUE me, void *data)
Definition: gc.c:4129
void * data
Definition: gc.c:7538
static VALUE gc_latest_gc_info(int argc, VALUE *argv, VALUE self)
Definition: gc.c:6819
#define rb_objspace_of(vm)
Definition: gc.c:712
#define CHAR_BIT
Definition: ruby.h:196
Definition: re.h:36
#define RANY(o)
Definition: gc.c:799
#define FL_UNSET(x, f)
Definition: ruby.h:1292
#define RTYPEDDATA_P(v)
Definition: ruby.h:1115
rb_objspace_t * objspace
Definition: gc.c:6439
#define ROBJECT(obj)
Definition: ruby.h:1205
long strtol(const char *nptr, char **endptr, int base)
Definition: strtol.c:7
#define LONG2NUM(x)
Definition: ruby.h:1573
static const rb_data_type_t weakmap_type
Definition: gc.c:8202
void rb_memerror(void)
Definition: gc.c:7622
static void make_io_zombie(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:2109
unsigned int uint32_t
Definition: sha2.h:101
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
static void heap_assign_page(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1604
#define C(c, s)
static void heap_unlink_page(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
Definition: gc.c:1436
void rb_set_safe_level_force(int)
Definition: safe.c:41
#define getenv(name)
Definition: win32.c:71
static void gc_mark_values(rb_objspace_t *objspace, long n, const VALUE *values)
Definition: gc.c:4014
static size_t gc_stat_internal(VALUE hash_or_sym)
Definition: gc.c:7051
VALUE str
Definition: re.h:46
static int gc_mark_stacked_objects(rb_objspace_t *objspace, int incremental, size_t count)
incremental: 0 -> not incremental (do all) incremental: n -> mark at most `n&#39; objects ...
Definition: gc.c:4629
void * ruby_xmalloc(size_t size)
Definition: gc.c:7931
static VALUE gc_profile_result(void)
Definition: gc.c:9024
size_t rb_node_memsize(VALUE obj)
Definition: node.c:1025
union RVALUE::@104 as
static void RVALUE_AGE_RESET_RAW(VALUE obj)
Definition: gc.c:1264
union RArray::@128 as
static VALUE wmap_aref(VALUE self, VALUE wmap)
Definition: gc.c:8485
#define RSTRING_PTR(str)
Definition: ruby.h:982
static int garbage_collect(rb_objspace_t *, int full_mark, int immediate_mark, int immediate_sweep, int reason)
Definition: gc.c:6313
#define MARK_IN_BITMAP(bits, p)
Definition: gc.c:698
size_t count
Definition: gc.c:600
static size_t objspace_live_slots(rb_objspace_t *objspace)
Definition: gc.c:3412
#define GC_HEAP_FREE_SLOTS_GOAL_RATIO
Definition: gc.c:124
static void gc_marks_wb_unprotected_objects(rb_objspace_t *objspace)
Definition: gc.c:5344
VALUE write_lock
Definition: io.h:97
#define RVALUE_WB_UNPROTECTED_BITMAP(obj)
Definition: gc.c:1017
struct heap_page * prev
Definition: gc.c:660
#define finalizing
Definition: gc.c:738
bits_t mark_bits[HEAP_PAGE_BITMAP_LIMIT]
Definition: gc.c:680
static VALUE wmap_keys(VALUE self)
Definition: gc.c:8407
#define STACK_LEVEL_MAX
Definition: gc.c:3923
void * ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size)
Definition: gc.c:7975
struct RMatch match
Definition: gc.c:413
#define GC_HEAP_FREE_SLOTS
Definition: gc.c:108
static int rgengc_remembersetbits_set(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:5685
static void free_stack_chunks(mark_stack_t *)
Definition: gc.c:3832
static VALUE gc_profile_record_get(void)
Definition: gc.c:8837
int size
Definition: encoding.c:57
#define PUSH_MARK_FUNC_DATA(v)
Definition: gc.c:904
void rb_mark_generic_ivar(VALUE)
Definition: variable.c:1173
#define f
#define INT2FIX(i)
Definition: ruby.h:232
static int RVALUE_MARKED(VALUE obj)
Definition: gc.c:1108
void * data
Definition: gc.c:2354
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Definition: gc.c:1988
static int wmap_aset_update(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: gc.c:8447
#define RCLASS_SUPER(c)
Definition: classext.h:16
static int ready_to_gc(rb_objspace_t *objspace)
Definition: gc.c:6232
int rb_safe_level(void)
Definition: safe.c:35
VALUE v1
Definition: gc.c:428
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
#define GC_ENABLE_INCREMENTAL_MARK
Definition: gc.c:297
static int RVALUE_OLD_P(VALUE obj)
Definition: gc.c:1151
static int get_envparam_double(const char *name, double *default_value, double lower_bound, double upper_bound, int accept_zero)
Definition: gc.c:7381
static void gc_mark_set_parent(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4410
double invoke_time
Definition: gc.c:570
#define RVALUE_PAGE_UNCOLLECTIBLE(page, obj)
Definition: gc.c:1022
static void mark_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me)
Definition: gc.c:4090
VALUE rb_block_proc(void)
Definition: proc.c:787
#define xmalloc
Definition: defines.h:183
#define SIZE_MAX
Definition: ruby.h:276
static void RVALUE_AGE_RESET(VALUE obj)
Definition: gc.c:1270
void rb_objspace_reachable_objects_from(VALUE obj, void(func)(VALUE, void *), void *data)
Definition: gc.c:7521
#define RUBY_INTERNAL_EVENT_NEWOBJ
Definition: ruby.h:2085
#define heap_allocatable_pages
Definition: gc.c:730
#define RUBY_INTERNAL_EVENT_FREEOBJ
Definition: ruby.h:2086
static void RVALUE_PAGE_OLD_UNCOLLECTIBLE_SET(rb_objspace_t *objspace, struct heap_page *page, VALUE obj)
Definition: gc.c:1167
#define st_init_numtable
Definition: regint.h:178
VALUE rb_gc_start(void)
Definition: gc.c:6649
size_t allocatable_pages
Definition: gc.c:547
static int wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8283
#define ANYARGS
Definition: defines.h:173
int getrusage(int who, struct rusage *usage)
VALUE rb_wb_unprotected_newobj_of(VALUE klass, VALUE flags)
Definition: gc.c:1933
size_t rb_gc_stat(VALUE key)
Definition: gc.c:7217
void rb_mark_set(st_table *tbl)
Definition: gc.c:4061
struct RVALUE * next
Definition: gc.c:397
#define gc_stress_full_mark_after_malloc_p()
Definition: gc.c:6217
RUBY_SYMBOL_EXPORT_BEGIN void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
Definition: thread.c:1499
struct RRegexp regexp
Definition: gc.c:405
static int garbage_collect_with_gvl(rb_objspace_t *objspace, int full_mark, int immediate_mark, int immediate_sweep, int reason)
Definition: gc.c:6555
static VALUE gc_count(VALUE self)
Definition: gc.c:6704
st_table * wmap2obj
Definition: gc.c:8138
static int is_swept_object(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:2956
size_t minor_gc_count
Definition: gc.c:573
const char * ruby_node_name(int node)
Definition: iseq.c:1739
VALUE rb_newobj_of(VALUE klass, VALUE flags)
Definition: gc.c:1955
#define RCLASS_IV_INDEX_TBL(c)
Definition: internal.h:692
static int gc_remember_unprotected(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4262
#define RHASH_TBL_RAW(h)
Definition: internal.h:1118
static void heap_pages_expand_sorted(rb_objspace_t *objspace)
Definition: gc.c:1384
VALUE rb_class_path_cached(VALUE)
Definition: variable.c:318
#define FL_WB_PROTECTED
Definition: ruby.h:1216
struct gc_list * global_list
Definition: gc.c:605
static void * objspace_xrealloc(rb_objspace_t *objspace, void *ptr, size_t new_size, size_t old_size)
Definition: gc.c:7874
static void heap_add_page(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *page)
Definition: gc.c:1593
static VALUE wmap_inspect(VALUE self)
Definition: gc.c:8307
static void heap_prepare(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1691
VALUE rb_any_to_s(VALUE)
Definition: object.c:500
VALUE rb_obj_is_fiber(VALUE obj)
Definition: cont.c:351
static stack_chunk_t * stack_chunk_alloc(void)
Definition: gc.c:3745
static int rb_special_const_p(VALUE obj)
Definition: ruby.h:2001
int ruby_gc_debug_indent
Definition: gc.c:812
VALUE rb_data_object_zalloc(VALUE, size_t, RUBY_DATA_FUNC, RUBY_DATA_FUNC)
VALUE pathv
Definition: io.h:68
#define RCLASS_CALLABLE_M_TBL(c)
Definition: internal.h:691
gc_profile_record * records
Definition: gc.c:562
#define RTEST(v)
Definition: ruby.h:450
VALUE rb_proc_new(VALUE(*)(ANYARGS), VALUE)
Definition: proc.c:2661
#define HEAP_PAGE_ALIGN_LOG
Definition: gc.c:645
#define T_STRING
Definition: ruby.h:496
static VALUE gc_stat_compat_table
Definition: gc.c:6907
#define POP_MARK_FUNC_DATA()
Definition: gc.c:908
#define PRIuSIZE
Definition: ruby.h:177
#define OBJ_INFECT(x, s)
Definition: ruby.h:1304
unsigned int in_tomb
Definition: gc.c:668
static void check_children_i(const VALUE child, void *ptr)
Definition: gc.c:5077
static Bigint * diff(Bigint *a, Bigint *b)
Definition: util.c:1507
static void rb_objspace_call_finalizer(rb_objspace_t *objspace)
Definition: gc.c:2859
static VALUE heap_get_freeobj_head(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:1734
VALUE rb_obj_rgengc_promoted_p(VALUE obj)
Definition: gc.c:6063
struct rb_encoding_entry * list
Definition: encoding.c:55
static VALUE gc_stat_symbols[gc_stat_sym_last]
Definition: gc.c:6905
static int wmap_each_value_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8370
#define st_add_direct
Definition: regint.h:187
size_t last_major_gc
Definition: gc.c:613
int each_obj_callback(void *, void *, size_t, void *)
Definition: gc.c:2350
#define T_FALSE
Definition: ruby.h:505
#define T_FILE
Definition: ruby.h:502
struct rb_heap_struct rb_heap_t
int rb_singleton_class_internal_p(VALUE sklass)
Definition: class.c:450
rb_objspace_t * rb_objspace_alloc(void)
Definition: gc.c:1305
struct RArray::@128::@129 heap
static void gc_sweep_start_heap(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:3568
size_t heap_free_slots
Definition: gc.c:165
size_t heap_init_slots
Definition: gc.c:164
static int RVALUE_MARKING(VALUE obj)
Definition: gc.c:1123
#define VM_UNREACHABLE(func)
Definition: vm_core.h:55
USHORT * table
Definition: file.c:19
double growth_factor
Definition: gc.c:166
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1182
const char * rb_obj_info(VALUE obj)
Definition: gc.c:9369
#define CHECK(sub)
Definition: compile.c:408
RVALUE * freelist
Definition: gc.c:673
#define UNLIKELY(x)
Definition: ffi_common.h:126
#define NEW_SYM(s)
struct mark_stack mark_stack_t
#define rb_thread_raised_p(th, f)
Definition: eval_intern.h:269
void rb_gc_writebarrier_remember(VALUE obj)
Definition: gc.c:5967
rb_heap_t eden_heap
Definition: gc.c:529
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:240
static size_t mark_stack_size(mark_stack_t *stack)
Definition: gc.c:3763
unsigned int during_incremental_marking
Definition: gc.c:522
#define st_insert
Definition: regint.h:184
int rb_atomic_t
Definition: ruby_atomic.h:120
static int invalidate_mark_stack_chunk(stack_chunk_t *chunk, int limit, VALUE obj)
Definition: gc.c:3871
#define heap_pages_sorted
Definition: gc.c:725
static int gc_verify_heap_pages(rb_objspace_t *objspace)
Definition: gc.c:5201
#define T_CLASS
Definition: ruby.h:492
size_t total_allocated_pages
Definition: gc.c:602
const VALUE ifnone
Definition: internal.h:559
struct rmatch_offset * char_offset
Definition: re.h:41
#define SET_STACK_END
Definition: gc.c:3918
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:1039
int immediate_mark
Definition: gc.c:6442
volatile VALUE rb_gc_guarded_val
Definition: gc.c:94
#define PRIdSIZE
Definition: ruby.h:174
static void gc_mark_roots(rb_objspace_t *objspace, const char **categoryp)
Definition: gc.c:4711
static void init_mark_stack(mark_stack_t *stack)
Definition: gc.c:3899
void rb_gc_mark_maybe(VALUE obj)
Definition: gc.c:4247
VALUE self
Definition: vm_core.h:485
VALUE gc_stress
Definition: gc.c:182
static void shrink_stack_chunk_cache(mark_stack_t *stack)
Definition: gc.c:3784
const char * name
Definition: nkf.c:208
#define FL_SET(x, f)
Definition: ruby.h:1290
struct RData data
Definition: gc.c:407
#define ID2SYM(x)
Definition: ruby.h:383
#define GC_HEAP_INIT_SLOTS
Definition: gc.c:105
static void gc_report_body(int level, rb_objspace_t *objspace, const char *fmt,...)
Definition: gc.c:5643
#define gc_event_hook_available_p(objspace)
Definition: gc.c:1773
static void gc_prof_setup_new_record(rb_objspace_t *objspace, int reason)
Definition: gc.c:8584
static VALUE gc_info_decode(rb_objspace_t *objspace, const VALUE hash_or_key, const int orig_flags)
Definition: gc.c:6710
void rb_gc_mark_global_tbl(void)
Definition: variable.c:593
Definition: ruby.h:889
static int is_live_object(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:2982
#define GC_OLDMALLOC_LIMIT_MIN
Definition: gc.c:141
imemo_type
Definition: internal.h:737
#define st_free_table
Definition: regint.h:188
gc_stat_compat_sym
Definition: gc.c:6878
const char * rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
Definition: gc.c:9219
VALUE rb_inspect(VALUE)
Definition: object.c:519
uint32_t rb_event_flag_t
Definition: ruby.h:2095
#define hi
Definition: siphash.c:22
#define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
Definition: gc.c:65
#define RMATCH(obj)
Definition: re.h:51
void(* func)(const char *category, VALUE, void *)
Definition: gc.c:7537
const rb_data_type_t * type
Definition: ruby.h:1108
uintptr_t bits_t
Definition: gc.c:443
static int get_envparam_size(const char *name, size_t *default_value, size_t lower_bound)
Definition: gc.c:7323
#define gc_mode(objspace)
Definition: gc.c:765
static void gc_marks_continue(rb_objspace_t *objspace, rb_heap_t *heap)
Definition: gc.c:5571
#define RTYPEDDATA_DATA(v)
Definition: ruby.h:1117
void rb_gc_writebarrier_unprotect(VALUE obj)
Definition: gc.c:5931
void(* mark_func)(VALUE v, void *data)
Definition: gc.c:538
struct heap_page * using_page
Definition: gc.c:484
void(* dmark)(void *)
Definition: ruby.h:1090
#define rb_check_frozen(obj)
Definition: intern.h:276
#define RVALUE_PAGE_MARKING(page, obj)
Definition: gc.c:1023
struct RTypedData typeddata
Definition: gc.c:408
rb_id_table_iterator_result
Definition: id_table.h:8
RUBY_EXTERN VALUE rb_stdout
Definition: ruby.h:1950
void rb_gc_call_finalizer_at_exit(void)
Definition: gc.c:2850
static void gc_mark_from(rb_objspace_t *objspace, VALUE obj, VALUE parent)
Definition: gc.c:5866
VALUE rb_gc_enable(void)
Definition: gc.c:7289
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1111
#define memcpy(d, s, n)
Definition: ffi_common.h:55
Definition: gc.c:801
#define vsnprintf
Definition: subst.h:7
const rb_iseq_t *const iseqptr
Definition: method.h:123
static void RVALUE_DEMOTE_RAW(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1238
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1249
struct RArray array
Definition: gc.c:404
struct rb_objspace rb_objspace_t
static int verify_internal_consistency_i(void *page_start, void *page_end, size_t stride, void *ptr)
Definition: gc.c:5083
#define RUBY_INTERNAL_EVENT_OBJSPACE_MASK
Definition: ruby.h:2092
#define TYPE_NAME(t)
void void xfree(void *)
static void gc_mark_children(rb_objspace_t *objspace, VALUE ptr)
Definition: gc.c:4472
#define RUBY_INTERNAL_EVENT_GC_END_SWEEP
Definition: ruby.h:2089
struct RBasic basic
Definition: gc.c:399
#define RHASH_EMPTY_P(h)
Definition: ruby.h:1067
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define gc_prof_enabled(objspace)
Definition: gc.c:893
bits_t uncollectible_bits[HEAP_PAGE_BITMAP_LIMIT]
Definition: gc.c:682
struct rb_objspace::@113 rgengc
void rb_mark_hash(st_table *tbl)
Definition: gc.c:4084
VALUE v3
Definition: gc.c:430
#define GC_MALLOC_LIMIT_MIN
Definition: gc.c:131
Definition: id.h:88
#define rb_intern(str)
each_obj_callback * callback
Definition: gc.c:2353
void * data
Definition: ruby.h:1082
static VALUE undefine_final(VALUE os, VALUE obj)
Definition: gc.c:2589
VALUE rb_str_buf_new(long)
Definition: string.c:1247
static VALUE gc_profile_clear(void)
Definition: gc.c:8770
#define gc_report
Definition: gc.c:899
#define T_ZOMBIE
Definition: ruby.h:514
void rb_gc_mark_encodings(void)
Definition: encoding.c:263
static void * objspace_xmalloc2(rb_objspace_t *objspace, size_t n, size_t size)
Definition: gc.c:7868
#define SYMBOL_P(x)
Definition: ruby.h:382
size_t old_objects
Definition: gc.c:616
#define RCLASS(obj)
Definition: ruby.h:1206
#define T_NONE
Definition: ruby.h:489
const struct rb_method_entry_struct *const orig_me
Definition: method.h:143
static void * objspace_xcalloc(rb_objspace_t *objspace, size_t count, size_t elsize)
Definition: gc.c:7951
static int wmap_each_key_i(st_data_t key, st_data_t val, st_data_t arg)
Definition: gc.c:8347
struct RString string
Definition: gc.c:403
#define TAG_RAISE
Definition: vm_core.h:168
#define env
static void objspace_xfree(rb_objspace_t *objspace, void *ptr, size_t size)
Definition: gc.c:7911
#define NULL
Definition: _sdbm.c:102
stack_chunk_t * cache
Definition: gc.c:473
#define OLD_SYM(s)
#define RTYPEDDATA_TYPE(v)
Definition: ruby.h:1116
unsigned int during_gc
Definition: gc.c:515
rb_method_refined_t refined
Definition: method.h:157
struct rb_classext_struct rb_classext_t
Definition: internal.h:673
#define Qundef
Definition: ruby.h:439
size_t freeable_pages
Definition: gc.c:550
#define T_ICLASS
Definition: ruby.h:493
#define GC_OLDMALLOC_LIMIT_MAX
Definition: gc.c:147
VALUE flags
Definition: gc.c:396
static VALUE wmap_finalize(VALUE self, VALUE objid)
Definition: gc.c:8247
static VALUE gc_stat(int argc, VALUE *argv, VALUE self)
Definition: gc.c:7195
static void mark_set(rb_objspace_t *objspace, st_table *tbl)
Definition: gc.c:4054
static VALUE newobj_slowpath(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, rb_objspace_t *objspace, int wb_protected)
Definition: gc.c:1865
void rb_gc_verify_internal_consistency(void)
Definition: gc.c:5298
#define GET_HEAP_PAGE(x)
Definition: gc.c:689
st_index_t num_entries
Definition: st.h:86
#define malloc_limit
Definition: gc.c:722
static int match(VALUE str, VALUE pat, VALUE hash, int(*cb)(VALUE, VALUE))
Definition: date_parse.c:280
static int gc_verify_heap_page(rb_objspace_t *objspace, struct heap_page *page, VALUE obj)
Definition: gc.c:5130
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
#define ruby_verbose
Definition: ruby.h:1792
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2818
#define CALC_EXACT_MALLOC_SIZE
Definition: gc.c:303
struct heap_page ** sorted
Definition: gc.c:545
static VALUE newobj_init(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, int wb_protected, rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:1783
double oldmalloc_limit_growth_factor
Definition: gc.c:180
struct RComplex complex
Definition: gc.c:415
void rb_ary_delete_same(VALUE ary, VALUE item)
Definition: array.c:3022
struct stack_chunk stack_chunk_t
void rb_warn(const char *fmt,...)
Definition: error.c:221
free(psz)
static VALUE gc_profile_enable(void)
Definition: gc.c:9103
static void VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
Definition: vm_core.h:1001
#define SIZET2NUM(v)
Definition: ruby.h:264
static int gc_mark_set(rb_objspace_t *objspace, VALUE obj)
Definition: gc.c:4253
VALUE rb_eArgError
Definition: error.c:763
#define T_REGEXP
Definition: ruby.h:497
#define CLEAR_IN_BITMAP(bits, p)
Definition: gc.c:699
#define T_MASK
Definition: md5.c:131
rb_objspace_t * objspace
Definition: gc.c:8278
#define rb_jmp_buf
Definition: gc.c:90
#define BDIGIT
Definition: bigdecimal.h:46
#define BIGNUM_EMBED_FLAG
Definition: internal.h:506
static void setup_gc_stat_symbols(void)
Definition: gc.c:6910
struct RString::@125::@126 heap
unsigned int has_uncollectible_shady_objects
Definition: gc.c:667
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Definition: ruby.h:1605
bits_t marking_bits[HEAP_PAGE_BITMAP_LIMIT]
Definition: gc.c:683
static void should_be_finalizable(VALUE obj)
Definition: gc.c:2614
char ** argv
Definition: ruby.c:184
Definition: ruby.h:1050
static void should_be_callable(VALUE block)
Definition: gc.c:2606
struct heap_page * free_pages
Definition: gc.c:483
#define DBL2NUM(dbl)
Definition: ruby.h:941
static void gc_setup_mark_bits(struct heap_page *page)
Definition: gc.c:3424
#define __asm__
#define L(x)
Definition: asm.h:125
rb_iseq_location_t location
Definition: vm_core.h:358
static void mark_stack_locations(rb_objspace_t *objspace, rb_thread_t *th, const VALUE *stack_start, const VALUE *stack_end)
Definition: gc.c:4211
static void heap_page_add_freeobj(rb_objspace_t *objspace, struct heap_page *page, VALUE obj)
Definition: gc.c:1396
#define rb_sym2str(sym)
Definition: console.c:107
#define TRY_WITH_GC(alloc)
Definition: gc.c:7824
VALUE rb_obj_class(VALUE)
Definition: object.c:229
#define SIGNED_VALUE
Definition: ruby.h:87
#define xcalloc
Definition: defines.h:185
struct stack_chunk * next
Definition: gc.c:468
static void * objspace_xmalloc0(rb_objspace_t *objspace, size_t size)
Definition: gc.c:7837
Definition: gc.c:471
struct heap_page * sweep_pages
Definition: gc.c:486
static struct heap_page * heap_move_pooled_pages_to_free_pages(rb_heap_t *heap)
Definition: gc.c:5383