Ruby: include/ruby/internal/gc.h Source File

Ruby 3.4.7p58 (2025-10-08 revision 7a5688e2a27668e48f8d6ff4af5b2208b98a2f5e)

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"
34#include "ruby/internal/attr/cold.h"
35#include "ruby/internal/attr/nonnull.h"
36#include "ruby/internal/attr/noreturn.h"
37#include "ruby/internal/attr/artificial.h"
38#include "ruby/internal/attr/maybe_unused.h"
39#include "ruby/internal/attr/pure.h"
40#include "ruby/internal/dllexport.h"
41#include "ruby/internal/special_consts.h"
42#include "ruby/internal/stdbool.h"
43#include "ruby/internal/value.h"
44
45 RBIMPL_SYMBOL_EXPORT_BEGIN()
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
56 RBIMPL_ATTR_COLD()
57 RBIMPL_ATTR_NORETURN()
65void rb_memerror(void);
66
67 RBIMPL_ATTR_PURE()
74int rb_during_gc(void);
75
76 RBIMPL_ATTR_NONNULL((1))
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
148void rb_gc_update_tbl_refs(st_table *ptr);
149
160void rb_gc_mark_maybe(VALUE obj);
161
169void rb_gc_mark(VALUE obj);
170
199void rb_gc_mark_movable(VALUE obj);
200
210 VALUE rb_gc_location(VALUE obj);
211
237void rb_gc(void);
238
253void rb_gc_copy_finalizer(VALUE dst, VALUE src);
254
267 VALUE rb_gc_enable(void);
268
284 VALUE rb_gc_disable(void);
285
291 VALUE rb_gc_start(void);
292
313 VALUE rb_define_finalizer(VALUE obj, VALUE block);
314
326 VALUE rb_undefine_finalizer(VALUE obj);
327
333size_t rb_gc_count(void);
334
351size_t rb_gc_stat(VALUE key_or_buf);
352
370 VALUE rb_gc_latest_gc_info(VALUE key_or_buf);
371
384void rb_gc_adjust_memory_usage(ssize_t diff);
385
397void rb_gc_register_address(VALUE *valptr);
398
402void rb_global_variable(VALUE *);
403
408void rb_gc_unregister_address(VALUE *valptr);
409
416void rb_gc_register_mark_object(VALUE object);
417
418 RBIMPL_SYMBOL_EXPORT_END()
419
427#undef USE_RGENGC
428#define USE_RGENGC 1
429
434#ifndef USE_RGENGC_LOGGING_WB_UNPROTECT
435# define USE_RGENGC_LOGGING_WB_UNPROTECT 0
436#endif
437
445#ifndef RGENGC_WB_PROTECTED_ARRAY
446# define RGENGC_WB_PROTECTED_ARRAY 1
447#endif
448
456#ifndef RGENGC_WB_PROTECTED_HASH
457# define RGENGC_WB_PROTECTED_HASH 1
458#endif
459
467#ifndef RGENGC_WB_PROTECTED_STRUCT
468# define RGENGC_WB_PROTECTED_STRUCT 1
469#endif
470
478#ifndef RGENGC_WB_PROTECTED_STRING
479# define RGENGC_WB_PROTECTED_STRING 1
480#endif
481
489#ifndef RGENGC_WB_PROTECTED_OBJECT
490# define RGENGC_WB_PROTECTED_OBJECT 1
491#endif
492
500#ifndef RGENGC_WB_PROTECTED_REGEXP
501# define RGENGC_WB_PROTECTED_REGEXP 1
502#endif
503
511#ifndef RGENGC_WB_PROTECTED_MATCH
512# define RGENGC_WB_PROTECTED_MATCH 1
513#endif
514
522#ifndef RGENGC_WB_PROTECTED_CLASS
523# define RGENGC_WB_PROTECTED_CLASS 1
524#endif
525
533#ifndef RGENGC_WB_PROTECTED_FLOAT
534# define RGENGC_WB_PROTECTED_FLOAT 1
535#endif
536
544#ifndef RGENGC_WB_PROTECTED_COMPLEX
545# define RGENGC_WB_PROTECTED_COMPLEX 1
546#endif
547
555#ifndef RGENGC_WB_PROTECTED_RATIONAL
556# define RGENGC_WB_PROTECTED_RATIONAL 1
557#endif
558
566#ifndef RGENGC_WB_PROTECTED_BIGNUM
567# define RGENGC_WB_PROTECTED_BIGNUM 1
568#endif
569
581#ifndef RGENGC_WB_PROTECTED_NODE_CREF
582# define RGENGC_WB_PROTECTED_NODE_CREF 1
583#endif
584
593
603#define RB_OBJ_WRITE(old, slot, young) \
604 RBIMPL_CAST(rb_obj_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
605
615#define RB_OBJ_WRITTEN(old, oldv, young) \
616 RBIMPL_CAST(rb_obj_written((VALUE)(old), (VALUE)(oldv), (VALUE)(young), __FILE__, __LINE__))
617
618
619#define OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
620#define OBJ_PROMOTED RB_OBJ_PROMOTED
621#define OBJ_WB_UNPROTECT RB_OBJ_WB_UNPROTECT
622
630#define RB_OBJ_WB_UNPROTECT(x) rb_obj_wb_unprotect(x, __FILE__, __LINE__)
631
643#define RB_OBJ_WB_UNPROTECT_FOR(type, obj) \
644 (RGENGC_WB_PROTECTED_##type ? OBJ_WB_UNPROTECT(obj) : obj)
645
652#define RGENGC_LOGGING_WB_UNPROTECT rb_gc_unprotect_logging
653
655#define RB_OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
656#define RB_OBJ_PROMOTED RB_OBJ_PROMOTED
658
659 RBIMPL_SYMBOL_EXPORT_BEGIN()
667void rb_gc_writebarrier(VALUE old, VALUE young);
668
675void rb_gc_writebarrier_unprotect(VALUE obj);
676
677#if USE_RGENGC_LOGGING_WB_UNPROTECT
690void rb_gc_unprotect_logging(void *objptr, const char *filename, int line);
691#endif
692
693 RBIMPL_SYMBOL_EXPORT_END()
694
695 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
696 RBIMPL_ATTR_ARTIFICIAL()
705static inline bool
706RB_OBJ_PROMOTED_RAW(VALUE obj)
707{
708 RBIMPL_ASSERT_OR_ASSUME(RB_FL_ABLE(obj));
709 return RB_FL_ANY_RAW(obj, RUBY_FL_PROMOTED);
710}
711
712 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
713 RBIMPL_ATTR_ARTIFICIAL()
725static inline bool
726RB_OBJ_PROMOTED(VALUE obj)
727{
728 if (! RB_FL_ABLE(obj)) {
729 return false;
730 }
731 else {
732 return RB_OBJ_PROMOTED_RAW(obj);
733 }
734}
735
745static inline VALUE
746rb_obj_wb_unprotect(
747 VALUE x,
748 RBIMPL_ATTR_MAYBE_UNUSED()
749 const char *filename,
750 RBIMPL_ATTR_MAYBE_UNUSED()
751 int line)
752{
753#if USE_RGENGC_LOGGING_WB_UNPROTECT
754 RGENGC_LOGGING_WB_UNPROTECT(RBIMPL_CAST((void *)x), filename, line);
755#endif
756 rb_gc_writebarrier_unprotect(x);
757 return x;
758}
759
773static inline VALUE
774rb_obj_written(
775 VALUE a,
776 RBIMPL_ATTR_MAYBE_UNUSED()
777 VALUE oldv,
778 VALUE b,
779 RBIMPL_ATTR_MAYBE_UNUSED()
780 const char *filename,
781 RBIMPL_ATTR_MAYBE_UNUSED()
782 int line)
783{
784#if USE_RGENGC_LOGGING_WB_UNPROTECT
785 RGENGC_LOGGING_OBJ_WRITTEN(a, oldv, b, filename, line);
786#endif
787
788 if (!RB_SPECIAL_CONST_P(b)) {
789 rb_gc_writebarrier(a, b);
790 }
791
792 return a;
793}
794
808static inline VALUE
809rb_obj_write(
810 VALUE a, VALUE *slot, VALUE b,
811 RBIMPL_ATTR_MAYBE_UNUSED()
812 const char *filename,
813 RBIMPL_ATTR_MAYBE_UNUSED()
814 int line)
815{
816#ifdef RGENGC_LOGGING_WRITE
817 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
818#endif
819
820 *slot = b;
821
822 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
823 return a;
824}
825
826 RBIMPL_ATTR_DEPRECATED(("Will be removed soon"))
827static inline void rb_gc_force_recycle(VALUE obj){}
828
829#endif /* RBIMPL_GC_H */
artificial.h
Defines RBIMPL_ATTR_ARTIFICIAL.
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
assert.h
RBIMPL_ASSERT_OR_ASSUME
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
cold.h
Defines RBIMPL_ATTR_COLD.
RBIMPL_ATTR_COLD
#define RBIMPL_ATTR_COLD()
Wraps (or simulates) __attribute__((cold))
Definition cold.h:34
RBIMPL_ATTR_DEPRECATED
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition deprecated.h:64
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
RB_FL_ABLE
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:444
RB_FL_ANY_RAW
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
Definition fl_type.h:518
RUBY_FL_PROMOTED
@ RUBY_FL_PROMOTED
Ruby objects are "generational".
Definition fl_type.h:218
rb_obj_wb_unprotect
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:746
rb_gc_writebarrier
void rb_gc_writebarrier(VALUE old, VALUE young)
This is the implementation of RB_OBJ_WRITE().
Definition gc.c:2933
rb_gc_writebarrier_unprotect
void rb_gc_writebarrier_unprotect(VALUE obj)
This is the implementation of RB_OBJ_WB_UNPROTECT().
Definition gc.c:2939
RGENGC_LOGGING_WB_UNPROTECT
#define RGENGC_LOGGING_WB_UNPROTECT
This is an implementation detail of rb_obj_wb_unprotect().
Definition gc.h:652
RB_OBJ_PROMOTED
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:726
RB_OBJ_PROMOTED_RAW
static bool RB_OBJ_PROMOTED_RAW(VALUE obj)
This is the implementation of RB_OBJ_PROMOTED().
Definition gc.h:706
maybe_unused.h
Defines RBIMPL_ATTR_MAYBE_UNUSED.
RBIMPL_ATTR_MAYBE_UNUSED
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Definition maybe_unused.h:35
nonnull.h
Defines RBIMPL_ATTR_NONNULL.
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
noreturn.h
Defines RBIMPL_ATTR_NORETURN.
RBIMPL_ATTR_NORETURN
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
pure.h
Defines RBIMPL_ATTR_PURE.
RBIMPL_ATTR_PURE
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition pure.h:33
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
special_consts.h
Defines enum ruby_special_consts.
RB_SPECIAL_CONST_P
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition special_consts.h:327
RUBY_Qundef
@ RUBY_Qundef
Represents so-called undef.
Definition special_consts.h:91
stdbool.h
C99 shim for <stdbool.h>
rb_global_variable
Definition variable.c:519
value.h
Defines VALUE and ID.
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40