Ruby 4.1.0dev (2026-09-27 revision f6ff9e7d02e46360f8930b280a3dd921cccbda29)
gc.h
Go to the documentation of this file.
1#ifndef RBIMPL_GC_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_GC_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h> /* size_t */
27#endif
28
29#ifdef HAVE_SYS_TYPES_H
30# include <sys/types.h> /* ssize_t */
31#endif
32
33#include "ruby/assert.h"
43#include "ruby/internal/value.h"
44
46
47#define RUBY_REF_EDGE(s, p) offsetof(s, p)
48#define RUBY_REFS_LIST_PTR(l) (RUBY_DATA_FUNC)(uintptr_t)(l)
49#define RUBY_REF_END SIZE_MAX
50#define RUBY_REFERENCES(t) static const size_t t[]
51#define RUBY_REFERENCES_START(t) RUBY_REFERENCES(t) = {
52#define RUBY_REFERENCES_END RUBY_REF_END, };
53
54/* gc.c */
55
65void rb_memerror(void);
66
74int rb_during_gc(void);
75
92void rb_gc_mark_locations(const VALUE *start, const VALUE *end);
93
104void rb_mark_tbl(struct st_table *tbl);
105
115void rb_mark_tbl_no_pin(struct st_table *tbl);
116
127void rb_mark_set(struct st_table *tbl);
128
137void rb_mark_hash(struct st_table *tbl);
138
149
160void rb_gc_mark_maybe(VALUE obj);
161
169void rb_gc_mark(VALUE obj);
170
200void rb_gc_mark_movable(VALUE obj);
201
212
223void rb_gc_update_moved(VALUE *ptr);
224
250void rb_gc(void);
251
266void rb_gc_copy_finalizer(VALUE dst, VALUE src);
267
280VALUE rb_gc_enable(void);
281
297VALUE rb_gc_disable(void);
298
304VALUE rb_gc_start(void);
305
327
340
346size_t rb_gc_count(void);
347
364size_t rb_gc_stat(VALUE key_or_buf);
365
384
397void rb_gc_adjust_memory_usage(ssize_t diff);
398
427void rb_gc_register_address(VALUE *valptr);
428
436
444void rb_gc_unregister_address(VALUE *valptr);
445
453
455
463#undef USE_RGENGC
464#define USE_RGENGC 1
465
470#ifndef USE_RGENGC_LOGGING_WB_UNPROTECT
471# define USE_RGENGC_LOGGING_WB_UNPROTECT 0
472#endif
473
492#define RB_OBJ_WRITE(old, slot, young) \
493 RBIMPL_CAST(rb_obj_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
494
504#define RB_OBJ_WRITTEN(old, oldv, young) \
505 RBIMPL_CAST(rb_obj_written((VALUE)(old), (VALUE)(oldv), (VALUE)(young), __FILE__, __LINE__))
508#define OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
509#define OBJ_PROMOTED RB_OBJ_PROMOTED
510#define OBJ_WB_UNPROTECT RB_OBJ_WB_UNPROTECT
519#define RB_OBJ_WB_UNPROTECT(x) rb_obj_wb_unprotect(x, __FILE__, __LINE__)
520
532#define RB_OBJ_WB_UNPROTECT_FOR(type, obj) OBJ_WB_UNPROTECT(obj)
533
540#define RGENGC_LOGGING_WB_UNPROTECT rb_gc_unprotect_logging
541
543#define RB_OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
544#define RB_OBJ_PROMOTED RB_OBJ_PROMOTED
555void rb_gc_writebarrier(VALUE old, VALUE young);
556
564
565#if USE_RGENGC_LOGGING_WB_UNPROTECT
578void rb_gc_unprotect_logging(void *objptr, const char *filename, int line);
579#endif
580
582
593static inline bool
599
613static inline bool
615{
616 if (! RB_FL_ABLE(obj)) {
617 return false;
618 }
619 else {
620 return RB_OBJ_PROMOTED_RAW(obj);
621 }
622}
623
633static inline VALUE
635 VALUE x,
637 const char *filename,
639 int line)
640{
641#if USE_RGENGC_LOGGING_WB_UNPROTECT
642 RGENGC_LOGGING_WB_UNPROTECT(RBIMPL_CAST((void *)x), filename, line);
643#endif
645 return x;
646}
647
661static inline VALUE
662rb_obj_written(
663 VALUE a,
665 VALUE oldv,
666 VALUE b,
668 const char *filename,
670 int line)
671{
672#if USE_RGENGC_LOGGING_WB_UNPROTECT
673 RGENGC_LOGGING_OBJ_WRITTEN(a, oldv, b, filename, line);
674#endif
675
676 if (!RB_SPECIAL_CONST_P(b)) {
677 rb_gc_writebarrier(a, b);
678 }
679
680 return a;
681}
682
696static inline VALUE
697rb_obj_write(
698 VALUE a, VALUE *slot, VALUE b,
700 const char *filename,
702 int line)
703{
704#ifdef RGENGC_LOGGING_WRITE
705 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
706#endif
707
708 *slot = b;
709
710 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
711 return a;
712}
713
714#endif /* RBIMPL_GC_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
Defines RBIMPL_ATTR_COLD.
#define RBIMPL_ATTR_COLD()
Wraps (or simulates) __attribute__((cold))
Definition cold.h:34
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:384
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
Definition fl_type.h:456
@ RUBY_FL_PROMOTED
Ruby objects are "generational".
Definition fl_type.h:205
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
void rb_mark_tbl_no_pin(struct st_table *tbl)
Identical to rb_mark_tbl(), except it marks objects using rb_gc_mark_movable().
Definition gc.c:3316
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:6302
size_t rb_gc_stat(VALUE key_or_buf)
Obtains various GC related profiles.
Definition gc.c:5468
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
Definition gc.c:2962
VALUE rb_gc_disable(void)
Disables GC.
Definition gc.c:5614
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
Definition gc.c:5396
static VALUE rb_obj_wb_unprotect(VALUE x, const char *filename, int line)
This is the implementation of RB_OBJ_WB_UNPROTECT().
Definition gc.h:634
void rb_gc_update_moved(VALUE *ptr)
Updates a reference to a possibly moved object.
Definition gc.c:3199
VALUE rb_gc_latest_gc_info(VALUE key_or_buf)
Obtains various info regarding the most recent GC run.
Definition gc.c:5431
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
Definition gc.c:3300
VALUE rb_gc_enable(void)
(Re-) enables GC.
Definition gc.c:5602
void rb_mark_hash(struct st_table *tbl)
Marks keys and values associated inside of the given table.
Definition gc.c:3101
VALUE rb_undefine_finalizer(VALUE obj)
Modifies the object so that it has no finalisers at all.
Definition gc.c:2177
int rb_during_gc(void)
Queries if the GC is busy.
Definition gc.c:5411
void rb_gc_register_address(VALUE *valptr)
Inform the garbage collector that the global or static variable pointed by valptr stores a live Ruby ...
Definition gc.c:4079
void rb_gc_unregister_address(VALUE *valptr)
Inform the garbage collector that a pointer previously passed to rb_gc_register_address() no longer p...
Definition gc.c:4107
void rb_gc_mark_maybe(VALUE obj)
Identical to rb_gc_mark(), except it allows the passed value be a non-object.
Definition gc.c:2992
void rb_gc_writebarrier(VALUE old, VALUE young)
This is the implementation of RB_OBJ_WRITE().
Definition gc.c:3869
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
Definition gc.c:3193
void rb_gc_writebarrier_unprotect(VALUE obj)
This is the implementation of RB_OBJ_WB_UNPROTECT().
Definition gc.c:3875
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Marks objects between the two pointers.
Definition gc.c:3023
void rb_gc(void)
Triggers a GC process.
Definition gc.c:5403
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition gc.c:3959
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
Definition gc.c:4678
#define RGENGC_LOGGING_WB_UNPROTECT
This is an implementation detail of rb_obj_wb_unprotect().
Definition gc.h:540
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
Definition gc.c:3053
VALUE rb_define_finalizer(VALUE obj, VALUE block)
Assigns a finaliser for an object.
Definition gc.c:2296
void rb_gc_copy_finalizer(VALUE dst, VALUE src)
Copy&paste an object's finaliser to another.
Definition gc.c:2206
static bool RB_OBJ_PROMOTED(VALUE obj)
Tests if the object is "promoted" – that is, whether the object experienced one or more GC marks.
Definition gc.h:614
static bool RB_OBJ_PROMOTED_RAW(VALUE obj)
This is the implementation of RB_OBJ_PROMOTED().
Definition gc.h:594
void rb_gc_adjust_memory_usage(ssize_t diff)
Informs that there are external memory usages.
Definition gc.c:6626
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Definition gc.c:5419
Defines RBIMPL_ATTR_MAYBE_UNUSED.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition pure.h:33
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
Defines enum ruby_special_consts.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
@ RUBY_Qundef
Represents so-called undef.
C99 shim for <stdbool.h>
Definition st.h:79
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40