9#include "debug_counter.h"
12#include "internal/array.h"
13#include "internal/gc.h"
14#include "internal/hash.h"
15#include "internal/sanitizers.h"
16#include "internal/static_assert.h"
17#include "internal/struct.h"
18#include "internal/variable.h"
21#include "ruby_assert.h"
22#include "transient_heap.h"
31#ifndef TRANSIENT_HEAP_CHECK_MODE
32#define TRANSIENT_HEAP_CHECK_MODE 0
34#define TH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(TRANSIENT_HEAP_CHECK_MODE > 0, expr, #expr)
41#define TRANSIENT_HEAP_DEBUG 0
47#define TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK 0
49#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
56#define TRANSIENT_HEAP_DEBUG_DONT_PROMOTE 0
59#define TRANSIENT_HEAP_PROMOTED_DEFAULT_SIZE 1024
62#define TRANSIENT_HEAP_BLOCK_SIZE (1024 * 32 )
63#ifndef TRANSIENT_HEAP_TOTAL_SIZE
64#define TRANSIENT_HEAP_TOTAL_SIZE (1024 * 1024 * 32)
66#define TRANSIENT_HEAP_ALLOC_MAX (1024 * 2 )
67#define TRANSIENT_HEAP_BLOCK_NUM (TRANSIENT_HEAP_TOTAL_SIZE / TRANSIENT_HEAP_BLOCK_SIZE)
68#define TRANSIENT_HEAP_USABLE_SIZE (TRANSIENT_HEAP_BLOCK_SIZE - sizeof(struct transient_heap_block_header))
70#define TRANSIENT_HEAP_ALLOC_MAGIC 0xfeab
71#define TRANSIENT_HEAP_ALLOC_ALIGN RUBY_ALIGNOF(void *)
73#define TRANSIENT_HEAP_ALLOC_MARKING_LAST -1
74#define TRANSIENT_HEAP_ALLOC_MARKING_FREE -2
76enum transient_heap_status {
78 transient_heap_marking,
79 transient_heap_escaping
82struct transient_heap_block {
83 struct transient_heap_block_header {
85 int16_t last_marked_index;
87 struct transient_heap_block *next_block;
89 char buff[TRANSIENT_HEAP_USABLE_SIZE];
92struct transient_heap {
93 struct transient_heap_block *using_blocks;
94 struct transient_heap_block *marked_blocks;
95 struct transient_heap_block *free_blocks;
97 int total_marked_objects;
99 enum transient_heap_status status;
101 VALUE *promoted_objects;
102 int promoted_objects_size;
103 int promoted_objects_index;
105 struct transient_heap_block *arena;
109struct transient_alloc_header {
112 int16_t next_marked_index;
117static struct transient_heap global_transient_heap;
119static void transient_heap_promote_add(
struct transient_heap* theap,
VALUE obj);
120static const void *transient_heap_ptr(
VALUE obj,
int error);
121static int transient_header_managed_ptr_p(
struct transient_heap* theap,
const void *ptr);
123#define ROUND_UP(v, a) (((size_t)(v) + (a) - 1) & ~((a) - 1))
126transient_heap_block_dump(
struct transient_heap* theap,
struct transient_heap_block *block)
130 while (i<block->info.index) {
131 void *ptr = &block->buff[i];
132 struct transient_alloc_header *header = ptr;
133 fprintf(stderr,
"%4d %8d %p size:%4d next:%4d %s\n", n, i, ptr, header->size, header->next_marked_index, rb_obj_info(header->obj));
140transient_heap_blocks_dump(
struct transient_heap* theap,
struct transient_heap_block *block,
const char *type_str)
143 fprintf(stderr,
"- transient_heap_dump: %s:%p index:%d objects:%d last_marked_index:%d next:%p\n",
144 type_str, (
void *)block, block->info.index, block->info.objects, block->info.last_marked_index, (
void *)block->info.next_block);
146 transient_heap_block_dump(theap, block);
147 block = block->info.next_block;
152transient_heap_dump(
struct transient_heap* theap)
154 fprintf(stderr,
"transient_heap_dump objects:%d marked_objects:%d blocks:%d\n", theap->total_objects, theap->total_marked_objects, theap->total_blocks);
155 transient_heap_blocks_dump(theap, theap->using_blocks,
"using_blocks");
156 transient_heap_blocks_dump(theap, theap->marked_blocks,
"marked_blocks");
157 transient_heap_blocks_dump(theap, theap->free_blocks,
"free_blocks");
162rb_transient_heap_dump(
void)
164 transient_heap_dump(&global_transient_heap);
167#if TRANSIENT_HEAP_CHECK_MODE >= 2
168ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void transient_heap_ptr_check(
struct transient_heap *theap,
VALUE obj));
170transient_heap_ptr_check(
struct transient_heap *theap,
VALUE obj)
173 const void *ptr = transient_heap_ptr(obj, FALSE);
174 TH_ASSERT(ptr == NULL || transient_header_managed_ptr_p(theap, ptr));
178ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static int transient_heap_block_verify(
struct transient_heap *theap,
struct transient_heap_block *block));
180transient_heap_block_verify(
struct transient_heap *theap,
struct transient_heap_block *block)
183 struct transient_alloc_header *header;
185 while (i<block->info.index) {
186 header = (
void *)&block->buff[i];
187 TH_ASSERT(header->magic == TRANSIENT_HEAP_ALLOC_MAGIC);
188 transient_heap_ptr_check(theap, header->obj);
192 TH_ASSERT(block->info.objects == n);
198transient_heap_blocks_verify(
struct transient_heap *theap,
struct transient_heap_block *blocks,
int *block_num_ptr)
201 struct transient_heap_block *block = blocks;
203 n += transient_heap_block_verify(theap, block);
205 block = block->info.next_block;
213transient_heap_verify(
struct transient_heap *theap)
215#if TRANSIENT_HEAP_CHECK_MODE >= 2
216 int n=0, block_num=0;
218 n += transient_heap_blocks_verify(theap, theap->using_blocks, &block_num);
219 n += transient_heap_blocks_verify(theap, theap->marked_blocks, &block_num);
221 TH_ASSERT(n == theap->total_objects);
222 TH_ASSERT(n >= theap->total_marked_objects);
223 TH_ASSERT(block_num == theap->total_blocks);
229rb_transient_heap_verify(
void)
231 transient_heap_verify(&global_transient_heap);
234static struct transient_heap*
235transient_heap_get(
void)
237 struct transient_heap* theap = &global_transient_heap;
238 transient_heap_verify(theap);
243reset_block(
struct transient_heap_block *block)
245 __msan_allocated_memory(block,
sizeof block);
246 block->info.index = 0;
247 block->info.objects = 0;
248 block->info.last_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_LAST;
249 block->info.next_block = NULL;
250 __asan_poison_memory_region(&block->buff,
sizeof block->buff);
254connect_to_free_blocks(
struct transient_heap *theap,
struct transient_heap_block *block)
256 block->info.next_block = theap->free_blocks;
257 theap->free_blocks = block;
261connect_to_using_blocks(
struct transient_heap *theap,
struct transient_heap_block *block)
263 block->info.next_block = theap->using_blocks;
264 theap->using_blocks = block;
269connect_to_marked_blocks(
struct transient_heap *theap,
struct transient_heap_block *block)
271 block->info.next_block = theap->marked_blocks;
272 theap->marked_blocks = block;
277append_to_marked_blocks(
struct transient_heap *theap,
struct transient_heap_block *append_blocks)
279 if (theap->marked_blocks) {
280 struct transient_heap_block *block = theap->marked_blocks, *last_block = NULL;
283 block = block->info.next_block;
286 TH_ASSERT(last_block->info.next_block == NULL);
287 last_block->info.next_block = append_blocks;
290 theap->marked_blocks = append_blocks;
294static struct transient_heap_block *
295transient_heap_block_alloc(
struct transient_heap* theap)
297 struct transient_heap_block *block;
298#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
299 block = mmap(NULL, TRANSIENT_HEAP_BLOCK_SIZE, PROT_READ | PROT_WRITE,
300 MAP_PRIVATE | MAP_ANONYMOUS,
302 if (block == MAP_FAILED)
rb_bug(
"transient_heap_block_alloc: err:%d\n", errno);
304 if (theap->arena == NULL) {
305 theap->arena = rb_aligned_malloc(TRANSIENT_HEAP_BLOCK_SIZE, TRANSIENT_HEAP_TOTAL_SIZE);
306 if (theap->arena == NULL) {
307 rb_bug(
"transient_heap_block_alloc: failed\n");
311 TH_ASSERT(theap->arena_index < TRANSIENT_HEAP_BLOCK_NUM);
312 block = &theap->arena[theap->arena_index++];
313 TH_ASSERT(((intptr_t)block & (TRANSIENT_HEAP_BLOCK_SIZE - 1)) == 0);
317 TH_ASSERT(((intptr_t)block->buff & (TRANSIENT_HEAP_ALLOC_ALIGN-1)) == 0);
318 if (0) fprintf(stderr,
"transient_heap_block_alloc: %4d %p\n", theap->total_blocks, (
void *)block);
323static struct transient_heap_block *
324transient_heap_allocatable_block(
struct transient_heap* theap)
326 struct transient_heap_block *block;
328#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
329 block = transient_heap_block_alloc(theap);
330 theap->total_blocks++;
333 block = theap->free_blocks;
335 theap->free_blocks = block->info.next_block;
336 block->info.next_block = NULL;
337 theap->total_blocks++;
344static struct transient_alloc_header *
345transient_heap_allocatable_header(
struct transient_heap* theap,
size_t size)
347 struct transient_heap_block *block = theap->using_blocks;
350 TH_ASSERT(block->info.index <= (int16_t)TRANSIENT_HEAP_USABLE_SIZE);
352 if (TRANSIENT_HEAP_USABLE_SIZE - block->info.index >= size) {
353 struct transient_alloc_header *header = (
void *)&block->buff[block->info.index];
354 block->info.index += size;
355 block->info.objects++;
359 block = transient_heap_allocatable_block(theap);
360 if (block) connect_to_using_blocks(theap, block);
368rb_transient_heap_alloc(
VALUE obj,
size_t req_size)
371 if (ruby_single_main_ractor == NULL)
return NULL;
374 struct transient_heap* theap = transient_heap_get();
375 size_t size = ROUND_UP(req_size +
sizeof(
struct transient_alloc_header), TRANSIENT_HEAP_ALLOC_ALIGN);
377 TH_ASSERT(RB_TYPE_P(obj,
T_ARRAY) ||
382 if (size > TRANSIENT_HEAP_ALLOC_MAX) {
383 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_alloc: [too big: %ld] %s\n", (
long)size, rb_obj_info(obj));
386#if TRANSIENT_HEAP_DEBUG_DONT_PROMOTE == 0
387 else if (RB_OBJ_PROMOTED_RAW(obj)) {
388 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_alloc: [promoted object] %s\n", rb_obj_info(obj));
392 else if (RBASIC_CLASS(obj) == 0) {
393 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_alloc: [hidden object] %s\n", rb_obj_info(obj));
398 struct transient_alloc_header *header = transient_heap_allocatable_header(theap, size);
404 asan_unpoison_memory_region(header,
sizeof *header,
true);
407 header->magic = TRANSIENT_HEAP_ALLOC_MAGIC;
408 header->next_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_FREE;
412 asan_poison_memory_region(header,
sizeof *header);
415 theap->total_objects++;
417#if TRANSIENT_HEAP_DEBUG_DONT_PROMOTE
418 if (RB_OBJ_PROMOTED_RAW(obj)) {
419 transient_heap_promote_add(theap, obj);
422 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_alloc: header:%p ptr:%p size:%d obj:%s\n", (
void *)header, ptr, (
int)size, rb_obj_info(obj));
424 RB_DEBUG_COUNTER_INC(theap_alloc);
427 asan_unpoison_memory_region(ptr, size -
sizeof *header,
true);
431 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_alloc: [no enough space: %ld] %s\n", (
long)size, rb_obj_info(obj));
432 RB_DEBUG_COUNTER_INC(theap_alloc_fail);
441Init_TransientHeap(
void)
444 struct transient_heap* theap = transient_heap_get();
446#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
449 TH_ASSERT(TRANSIENT_HEAP_BLOCK_SIZE * TRANSIENT_HEAP_BLOCK_NUM == TRANSIENT_HEAP_TOTAL_SIZE);
450 block_num = TRANSIENT_HEAP_BLOCK_NUM;
452 for (i=0; i<block_num; i++) {
453 connect_to_free_blocks(theap, transient_heap_block_alloc(theap));
455 theap->using_blocks = transient_heap_allocatable_block(theap);
457 theap->promoted_objects_size = TRANSIENT_HEAP_PROMOTED_DEFAULT_SIZE;
458 theap->promoted_objects_index = 0;
460 theap->promoted_objects = malloc(
sizeof(
VALUE) * theap->promoted_objects_size);
463 sizeof(
VALUE) <= SIZE_MAX / TRANSIENT_HEAP_PROMOTED_DEFAULT_SIZE);
464 if (theap->promoted_objects == NULL)
rb_bug(
"Init_TransientHeap: malloc failed.");
467static struct transient_heap_block *
468blocks_alloc_header_to_block(
struct transient_heap *theap,
struct transient_heap_block *blocks,
struct transient_alloc_header *header)
470 struct transient_heap_block *block = blocks;
473 if (block->buff <= (
char *)header && (
char *)header < block->buff + TRANSIENT_HEAP_USABLE_SIZE) {
476 block = block->info.next_block;
482static struct transient_heap_block *
483alloc_header_to_block_verbose(
struct transient_heap *theap,
struct transient_alloc_header *header)
485 struct transient_heap_block *block;
487 if ((block = blocks_alloc_header_to_block(theap, theap->marked_blocks, header)) != NULL) {
488 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"alloc_header_to_block: found in marked_blocks\n");
491 else if ((block = blocks_alloc_header_to_block(theap, theap->using_blocks, header)) != NULL) {
492 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"alloc_header_to_block: found in using_blocks\n");
500static struct transient_alloc_header *
501ptr_to_alloc_header(
const void *ptr)
503 struct transient_alloc_header *header = (
void *)ptr;
509transient_header_managed_ptr_p(
struct transient_heap* theap,
const void *ptr)
511 if (alloc_header_to_block_verbose(theap, ptr_to_alloc_header(ptr))) {
521rb_transient_heap_managed_ptr_p(
const void *ptr)
523 return transient_header_managed_ptr_p(transient_heap_get(), ptr);
526static struct transient_heap_block *
527alloc_header_to_block(
struct transient_heap *theap,
struct transient_alloc_header *header)
529 struct transient_heap_block *block;
530#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
531 block = alloc_header_to_block_verbose(theap, header);
533 transient_heap_dump(theap);
534 rb_bug(
"alloc_header_to_block: not found in mark_blocks (%p)\n", header);
537 block = (
void *)((intptr_t)header & ~(TRANSIENT_HEAP_BLOCK_SIZE-1));
538 TH_ASSERT(block == alloc_header_to_block_verbose(theap, header));
544rb_transient_heap_mark(
VALUE obj,
const void *ptr)
548 struct transient_alloc_header *header = ptr_to_alloc_header(ptr);
549 asan_unpoison_memory_region(header,
sizeof *header,
false);
550 if (header->magic != TRANSIENT_HEAP_ALLOC_MAGIC)
rb_bug(
"rb_transient_heap_mark: wrong header, %s (%p)", rb_obj_info(obj), ptr);
551 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_mark: %s (%p)\n", rb_obj_info(obj), ptr);
553#if TRANSIENT_HEAP_CHECK_MODE > 0
555 struct transient_heap* theap = transient_heap_get();
556 TH_ASSERT(theap->status == transient_heap_marking);
557 TH_ASSERT(transient_header_managed_ptr_p(theap, ptr));
559 if (header->magic != TRANSIENT_HEAP_ALLOC_MAGIC) {
560 transient_heap_dump(theap);
561 rb_bug(
"rb_transient_heap_mark: magic is broken");
563 else if (header->obj != obj) {
565 rb_bug(
"rb_transient_heap_mark: unmatch (%s is stored, but %s is given)\n",
566 rb_obj_info(header->obj), rb_obj_info(obj));
571 if (header->next_marked_index != TRANSIENT_HEAP_ALLOC_MARKING_FREE) {
576 struct transient_heap* theap = transient_heap_get();
577 struct transient_heap_block *block = alloc_header_to_block(theap, header);
578 __asan_unpoison_memory_region(&block->info,
sizeof block->info);
579 header->next_marked_index = block->info.last_marked_index;
580 block->info.last_marked_index = (int)((
char *)header - block->buff);
581 theap->total_marked_objects++;
583 transient_heap_verify(theap);
587ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static const void *transient_heap_ptr(
VALUE obj,
int error));
589transient_heap_ptr(
VALUE obj,
int error)
591 const void *ptr = NULL;
595 if (RARRAY_TRANSIENT_P(obj)) {
596 TH_ASSERT(!ARY_EMBED_P(obj));
597 ptr =
RARRAY(obj)->as.heap.ptr;
601 if (ROBJ_TRANSIENT_P(obj)) {
603 ptr = ROBJECT_IVPTR(obj);
607 if (RSTRUCT_TRANSIENT_P(obj)) {
608 ptr = rb_struct_const_heap_ptr(obj);
612 if (RHASH_TRANSIENT_P(obj)) {
613 TH_ASSERT(RHASH_AR_TABLE_P(obj));
614 ptr = (
VALUE *)(RHASH(obj)->as.ar);
622 rb_bug(
"transient_heap_ptr: unknown obj %s\n", rb_obj_info(obj));
630transient_heap_promote_add(
struct transient_heap* theap,
VALUE obj)
632 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
"rb_transient_heap_promote: %s\n", rb_obj_info(obj));
634 if (TRANSIENT_HEAP_DEBUG_DONT_PROMOTE) {
637 for (i=0; i<theap->promoted_objects_index; i++) {
638 if (theap->promoted_objects[i] == obj)
return;
642 if (theap->promoted_objects_size <= theap->promoted_objects_index) {
643 theap->promoted_objects_size *= 2;
644 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"rb_transient_heap_promote: expand table to %d\n", theap->promoted_objects_size);
645 if (UNLIKELY((
size_t)theap->promoted_objects_size > SIZE_MAX /
sizeof(
VALUE))) {
647 theap->promoted_objects = NULL;
650 theap->promoted_objects = realloc(theap->promoted_objects, theap->promoted_objects_size *
sizeof(
VALUE));
652 if (theap->promoted_objects == NULL)
rb_bug(
"rb_transient_heap_promote: realloc failed");
654 theap->promoted_objects[theap->promoted_objects_index++] = obj;
658rb_transient_heap_promote(
VALUE obj)
662 if (transient_heap_ptr(obj, FALSE)) {
663 struct transient_heap* theap = transient_heap_get();
664 transient_heap_promote_add(theap, obj);
671static struct transient_alloc_header *
672alloc_header(
struct transient_heap_block* block,
int index)
674 return (
void *)&block->buff[index];
678transient_heap_reset(
void)
682 struct transient_heap* theap = transient_heap_get();
683 struct transient_heap_block* block;
685 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"!! transient_heap_reset\n");
687 block = theap->marked_blocks;
689 struct transient_heap_block *next_block = block->info.next_block;
690 theap->total_objects -= block->info.objects;
691#if TRANSIENT_HEAP_DEBUG_INFINITE_BLOCK
692 if (madvise(block, TRANSIENT_HEAP_BLOCK_SIZE, MADV_DONTNEED) != 0) {
693 rb_bug(
"madvise err:%d", errno);
695 if (mprotect(block, TRANSIENT_HEAP_BLOCK_SIZE, PROT_NONE) != 0) {
696 rb_bug(
"mprotect err:%d", errno);
700 connect_to_free_blocks(theap, block);
702 theap->total_blocks--;
706 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"!! transient_heap_reset block_num:%d\n", theap->total_blocks);
708 theap->marked_blocks = NULL;
709 theap->total_marked_objects = 0;
713transient_heap_block_evacuate(
struct transient_heap* theap,
struct transient_heap_block* block)
715 int marked_index = block->info.last_marked_index;
716 block->info.last_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_LAST;
718 while (marked_index >= 0) {
719 struct transient_alloc_header *header = alloc_header(block, marked_index);
720 asan_unpoison_memory_region(header,
sizeof *header,
true);
721 VALUE obj = header->obj;
722 TH_ASSERT(header->magic == TRANSIENT_HEAP_ALLOC_MAGIC);
723 if (header->magic != TRANSIENT_HEAP_ALLOC_MAGIC)
rb_bug(
"transient_heap_block_evacuate: wrong header %p %s\n", (
void *)header, rb_obj_info(obj));
725 if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr,
" * transient_heap_block_evacuate %p %s\n", (
void *)header, rb_obj_info(obj));
728 RB_DEBUG_COUNTER_INC(theap_evacuate);
732 rb_ary_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
735 rb_obj_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
738 rb_struct_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
741 rb_hash_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
744 rb_bug(
"unsupported: %s\n", rb_obj_info(obj));
748 marked_index = header->next_marked_index;
749 asan_poison_memory_region(header,
sizeof *header);
753#if USE_RUBY_DEBUG_LOG
755transient_heap_status_cstr(
enum transient_heap_status status)
758 case transient_heap_none:
return "none";
759 case transient_heap_marking:
return "marking";
760 case transient_heap_escaping:
return "escaping";
767transient_heap_update_status(
struct transient_heap* theap,
enum transient_heap_status status)
769 RUBY_DEBUG_LOG(
"%s -> %s",
770 transient_heap_status_cstr(theap->status),
771 transient_heap_status_cstr(status));
773 TH_ASSERT(theap->status != status);
774 theap->status = status;
778transient_heap_evacuate(
void *dmy)
780 struct transient_heap* theap = transient_heap_get();
782 if (theap->total_marked_objects == 0)
return;
783 if (ruby_single_main_ractor == NULL)
rb_bug(
"not single ractor mode");
784 if (theap->status == transient_heap_marking) {
785 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"!! transient_heap_evacuate: skip while transient_heap_marking\n");
788 VALUE gc_disabled = rb_gc_disable_no_rest();
790 struct transient_heap_block* block;
792 RUBY_DEBUG_LOG(
"start gc_disabled:%d",
RTEST(gc_disabled));
794 if (TRANSIENT_HEAP_DEBUG >= 1) {
796 fprintf(stderr,
"!! transient_heap_evacuate start total_blocks:%d\n", theap->total_blocks);
797 if (TRANSIENT_HEAP_DEBUG >= 4) {
798 for (i=0; i<theap->promoted_objects_index; i++) fprintf(stderr,
"%4d %s\n", i, rb_obj_info(theap->promoted_objects[i]));
801 if (TRANSIENT_HEAP_DEBUG >= 2) transient_heap_dump(theap);
803 TH_ASSERT(theap->status == transient_heap_none);
804 transient_heap_update_status(theap, transient_heap_escaping);
807 block = theap->marked_blocks;
809 transient_heap_block_evacuate(theap, block);
810 block = block->info.next_block;
815 block = theap->using_blocks;
817 transient_heap_block_evacuate(theap, block);
818 block = block->info.next_block;
822 transient_heap_reset();
824 if (TRANSIENT_HEAP_DEBUG > 0) {
825 fprintf(stderr,
"!! transient_heap_evacuate end total_blocks:%d\n", theap->total_blocks);
828 transient_heap_verify(theap);
829 transient_heap_update_status(theap, transient_heap_none);
831 if (gc_disabled !=
Qtrue) rb_gc_enable();
832 RUBY_DEBUG_LOG(
"finish");
837rb_transient_heap_evacuate(
void)
839 transient_heap_evacuate(NULL);
843clear_marked_index(
struct transient_heap_block* block)
845 int marked_index = block->info.last_marked_index;
847 while (marked_index != TRANSIENT_HEAP_ALLOC_MARKING_LAST) {
848 struct transient_alloc_header *header = alloc_header(block, marked_index);
851 asan_unpoison_memory_region(header,
sizeof *header,
false);
852 TH_ASSERT(marked_index != TRANSIENT_HEAP_ALLOC_MARKING_FREE);
853 if (0) fprintf(stderr,
"clear_marked_index - block:%p mark_index:%d\n", (
void *)block, marked_index);
855 marked_index = header->next_marked_index;
856 header->next_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_FREE;
859 block->info.last_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_LAST;
863blocks_clear_marked_index(
struct transient_heap_block* block)
866 clear_marked_index(block);
867 block = block->info.next_block;
872transient_heap_block_update_refs(
struct transient_heap* theap,
struct transient_heap_block* block)
874 int marked_index = block->info.last_marked_index;
876 while (marked_index >= 0) {
877 struct transient_alloc_header *header = alloc_header(block, marked_index);
879 asan_unpoison_memory_region(header,
sizeof *header,
false);
881 header->obj = rb_gc_location(header->obj);
883 marked_index = header->next_marked_index;
884 asan_poison_memory_region(header,
sizeof *header);
889transient_heap_blocks_update_refs(
struct transient_heap* theap,
struct transient_heap_block *block,
const char *type_str)
892 transient_heap_block_update_refs(theap, block);
893 block = block->info.next_block;
898rb_transient_heap_update_references(
void)
902 struct transient_heap* theap = transient_heap_get();
905 transient_heap_blocks_update_refs(theap, theap->using_blocks,
"using_blocks");
906 transient_heap_blocks_update_refs(theap, theap->marked_blocks,
"marked_blocks");
908 for (i=0; i<theap->promoted_objects_index; i++) {
909 VALUE obj = theap->promoted_objects[i];
910 theap->promoted_objects[i] = rb_gc_location(obj);
915rb_transient_heap_start_marking(
int full_marking)
918 RUBY_DEBUG_LOG(
"full?:%d", full_marking);
920 struct transient_heap* theap = transient_heap_get();
922 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"!! rb_transient_heap_start_marking objects:%d blocks:%d promoted:%d full_marking:%d\n",
923 theap->total_objects, theap->total_blocks, theap->promoted_objects_index, full_marking);
924 if (TRANSIENT_HEAP_DEBUG >= 2) transient_heap_dump(theap);
926 blocks_clear_marked_index(theap->marked_blocks);
927 blocks_clear_marked_index(theap->using_blocks);
929 if (theap->using_blocks) {
930 if (theap->using_blocks->info.objects > 0) {
931 append_to_marked_blocks(theap, theap->using_blocks);
932 theap->using_blocks = NULL;
935 append_to_marked_blocks(theap, theap->using_blocks->info.next_block);
936 theap->using_blocks->info.next_block = NULL;
940 if (theap->using_blocks == NULL) {
941 theap->using_blocks = transient_heap_allocatable_block(theap);
944 TH_ASSERT(theap->status == transient_heap_none);
945 transient_heap_update_status(theap, transient_heap_marking);
946 theap->total_marked_objects = 0;
949 theap->promoted_objects_index = 0;
953 for (i=0; i<theap->promoted_objects_index; i++) {
954 VALUE obj = theap->promoted_objects[i];
955 const void *ptr = transient_heap_ptr(obj, TRUE);
957 rb_transient_heap_mark(obj, ptr);
962 transient_heap_verify(theap);
966rb_transient_heap_finish_marking(
void)
969 struct transient_heap* theap = transient_heap_get();
971 RUBY_DEBUG_LOG(
"objects:%d, marked:%d",
972 theap->total_objects,
973 theap->total_marked_objects);
974 if (TRANSIENT_HEAP_DEBUG >= 2) transient_heap_dump(theap);
976 TH_ASSERT(theap->total_objects >= theap->total_marked_objects);
978 TH_ASSERT(theap->status == transient_heap_marking);
979 transient_heap_update_status(theap, transient_heap_none);
981 if (theap->total_marked_objects > 0) {
982 if (TRANSIENT_HEAP_DEBUG >= 1) fprintf(stderr,
"-> rb_transient_heap_finish_marking register escape func.\n");
986 transient_heap_reset();
989 transient_heap_verify(theap);
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
int rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
Identical to rb_postponed_job_register_one(), except it additionally checks for duplicated registrati...
#define Qundef
Old name of RUBY_Qundef.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_HASH
Old name of RUBY_T_HASH.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
#define RARRAY(obj)
Convenient casting macro.
#define RTEST
This is an old name of RB_TEST.
uintptr_t VALUE
Type that represents a Ruby object.