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

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

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)(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
225 RBIMPL_ATTR_DEPRECATED(("this is now a no-op function"))
226void rb_gc_force_recycle(VALUE obj);
227
253void rb_gc(void);
254
269void rb_gc_copy_finalizer(VALUE dst, VALUE src);
270
283 VALUE rb_gc_enable(void);
284
300 VALUE rb_gc_disable(void);
301
307 VALUE rb_gc_start(void);
308
329 VALUE rb_define_finalizer(VALUE obj, VALUE block);
330
342 VALUE rb_undefine_finalizer(VALUE obj);
343
349size_t rb_gc_count(void);
350
367size_t rb_gc_stat(VALUE key_or_buf);
368
386 VALUE rb_gc_latest_gc_info(VALUE key_or_buf);
387
400void rb_gc_adjust_memory_usage(ssize_t diff);
401
413void rb_gc_register_address(VALUE *valptr);
414
418void rb_global_variable(VALUE *);
419
424void rb_gc_unregister_address(VALUE *valptr);
425
432void rb_gc_register_mark_object(VALUE object);
433
434 RBIMPL_SYMBOL_EXPORT_END()
435
443#undef USE_RGENGC
444#define USE_RGENGC 1
445
450#ifndef USE_RGENGC_LOGGING_WB_UNPROTECT
451# define USE_RGENGC_LOGGING_WB_UNPROTECT 0
452#endif
453
461#ifndef RGENGC_WB_PROTECTED_ARRAY
462# define RGENGC_WB_PROTECTED_ARRAY 1
463#endif
464
472#ifndef RGENGC_WB_PROTECTED_HASH
473# define RGENGC_WB_PROTECTED_HASH 1
474#endif
475
483#ifndef RGENGC_WB_PROTECTED_STRUCT
484# define RGENGC_WB_PROTECTED_STRUCT 1
485#endif
486
494#ifndef RGENGC_WB_PROTECTED_STRING
495# define RGENGC_WB_PROTECTED_STRING 1
496#endif
497
505#ifndef RGENGC_WB_PROTECTED_OBJECT
506# define RGENGC_WB_PROTECTED_OBJECT 1
507#endif
508
516#ifndef RGENGC_WB_PROTECTED_REGEXP
517# define RGENGC_WB_PROTECTED_REGEXP 1
518#endif
519
527#ifndef RGENGC_WB_PROTECTED_MATCH
528# define RGENGC_WB_PROTECTED_MATCH 1
529#endif
530
538#ifndef RGENGC_WB_PROTECTED_CLASS
539# define RGENGC_WB_PROTECTED_CLASS 1
540#endif
541
549#ifndef RGENGC_WB_PROTECTED_FLOAT
550# define RGENGC_WB_PROTECTED_FLOAT 1
551#endif
552
560#ifndef RGENGC_WB_PROTECTED_COMPLEX
561# define RGENGC_WB_PROTECTED_COMPLEX 1
562#endif
563
571#ifndef RGENGC_WB_PROTECTED_RATIONAL
572# define RGENGC_WB_PROTECTED_RATIONAL 1
573#endif
574
582#ifndef RGENGC_WB_PROTECTED_BIGNUM
583# define RGENGC_WB_PROTECTED_BIGNUM 1
584#endif
585
597#ifndef RGENGC_WB_PROTECTED_NODE_CREF
598# define RGENGC_WB_PROTECTED_NODE_CREF 1
599#endif
600
619#define RB_OBJ_WRITE(old, slot, young) \
620 RBIMPL_CAST(rb_obj_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
621
631#define RB_OBJ_WRITTEN(old, oldv, young) \
632 RBIMPL_CAST(rb_obj_written((VALUE)(old), (VALUE)(oldv), (VALUE)(young), __FILE__, __LINE__))
635#define OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
636#define OBJ_PROMOTED RB_OBJ_PROMOTED
637#define OBJ_WB_UNPROTECT RB_OBJ_WB_UNPROTECT
646#define RB_OBJ_WB_UNPROTECT(x) rb_obj_wb_unprotect(x, __FILE__, __LINE__)
647
659#define RB_OBJ_WB_UNPROTECT_FOR(type, obj) \
660 (RGENGC_WB_PROTECTED_##type ? OBJ_WB_UNPROTECT(obj) : obj)
661
668#define RGENGC_LOGGING_WB_UNPROTECT rb_gc_unprotect_logging
669
671#define RB_OBJ_PROMOTED_RAW RB_OBJ_PROMOTED_RAW
672#define RB_OBJ_PROMOTED RB_OBJ_PROMOTED
675 RBIMPL_SYMBOL_EXPORT_BEGIN()
683void rb_gc_writebarrier(VALUE old, VALUE young);
684
691void rb_gc_writebarrier_unprotect(VALUE obj);
692
693#if USE_RGENGC_LOGGING_WB_UNPROTECT
706void rb_gc_unprotect_logging(void *objptr, const char *filename, int line);
707#endif
708
709 RBIMPL_SYMBOL_EXPORT_END()
710
711 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
712 RBIMPL_ATTR_ARTIFICIAL()
721static inline bool
722RB_OBJ_PROMOTED_RAW(VALUE obj)
723{
724 RBIMPL_ASSERT_OR_ASSUME(RB_FL_ABLE(obj));
725 return RB_FL_ANY_RAW(obj, RUBY_FL_PROMOTED);
726}
727
728 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
729 RBIMPL_ATTR_ARTIFICIAL()
741static inline bool
742RB_OBJ_PROMOTED(VALUE obj)
743{
744 if (! RB_FL_ABLE(obj)) {
745 return false;
746 }
747 else {
748 return RB_OBJ_PROMOTED_RAW(obj);
749 }
750}
751
761static inline VALUE
762rb_obj_wb_unprotect(
763 VALUE x,
764 RBIMPL_ATTR_MAYBE_UNUSED()
765 const char *filename,
766 RBIMPL_ATTR_MAYBE_UNUSED()
767 int line)
768{
769#if USE_RGENGC_LOGGING_WB_UNPROTECT
770 RGENGC_LOGGING_WB_UNPROTECT(RBIMPL_CAST((void *)x), filename, line);
771#endif
772 rb_gc_writebarrier_unprotect(x);
773 return x;
774}
775
789static inline VALUE
790rb_obj_written(
791 VALUE a,
792 RBIMPL_ATTR_MAYBE_UNUSED()
793 VALUE oldv,
794 VALUE b,
795 RBIMPL_ATTR_MAYBE_UNUSED()
796 const char *filename,
797 RBIMPL_ATTR_MAYBE_UNUSED()
798 int line)
799{
800#if USE_RGENGC_LOGGING_WB_UNPROTECT
801 RGENGC_LOGGING_OBJ_WRITTEN(a, oldv, b, filename, line);
802#endif
803
804 if (!RB_SPECIAL_CONST_P(b)) {
805 rb_gc_writebarrier(a, b);
806 }
807
808 return a;
809}
810
824static inline VALUE
825rb_obj_write(
826 VALUE a, VALUE *slot, VALUE b,
827 RBIMPL_ATTR_MAYBE_UNUSED()
828 const char *filename,
829 RBIMPL_ATTR_MAYBE_UNUSED()
830 int line)
831{
832#ifdef RGENGC_LOGGING_WRITE
833 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
834#endif
835
836 *slot = b;
837
838 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
839 return a;
840}
841
842#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(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:229
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_mark_tbl_no_pin
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:6929
rb_memerror
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:12295
rb_gc_stat
size_t rb_gc_stat(VALUE key_or_buf)
Obtains various GC related profiles.
Definition gc.c:11653
rb_gc_mark_movable
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:7114
rb_gc_disable
VALUE rb_gc_disable(void)
Disables GC.
Definition gc.c:11865
rb_gc_start
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
Definition gc.c:11251
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:762
rb_gc_latest_gc_info
VALUE rb_gc_latest_gc_info(VALUE key_or_buf)
Obtains various info regarding the most recent GC run.
Definition gc.c:11417
rb_mark_tbl
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:6923
rb_gc_enable
VALUE rb_gc_enable(void)
(Re-) enables GC.
Definition gc.c:11828
rb_mark_hash
void rb_mark_hash(struct st_table *tbl)
Marks keys and values associated inside of the given table.
Definition gc.c:6726
rb_undefine_finalizer
VALUE rb_undefine_finalizer(VALUE obj)
Modifies the object so that it has no finalisers at all.
Definition gc.c:4233
rb_during_gc
int rb_during_gc(void)
Queries if the GC is busy.
Definition gc.c:11266
rb_gc_register_address
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:9314
rb_gc_unregister_address
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:9339
rb_gc_mark_maybe
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:6960
rb_gc_writebarrier
void rb_gc_writebarrier(VALUE old, VALUE young)
This is the implementation of RB_OBJ_WRITE().
Definition gc.c:9105
rb_gc_location
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
Definition gc.c:10545
rb_gc_writebarrier_unprotect
void rb_gc_writebarrier_unprotect(VALUE obj)
This is the implementation of RB_OBJ_WB_UNPROTECT().
Definition gc.c:9143
rb_gc_mark_locations
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Marks objects between the two pointers.
Definition gc.c:6574
rb_gc
void rb_gc(void)
Triggers a GC process.
Definition gc.c:11258
rb_gc_force_recycle
void rb_gc_force_recycle(VALUE obj)
Asserts that the passed object is no longer needed.
Definition gc.c:9283
rb_gc_update_tbl_refs
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
Definition gc.c:10375
RGENGC_LOGGING_WB_UNPROTECT
#define RGENGC_LOGGING_WB_UNPROTECT
This is an implementation detail of rb_obj_wb_unprotect().
Definition gc.h:668
rb_mark_set
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:6670
rb_define_finalizer
VALUE rb_define_finalizer(VALUE obj, VALUE block)
Assigns a finaliser for an object.
Definition gc.c:4385
rb_gc_copy_finalizer
void rb_gc_copy_finalizer(VALUE dst, VALUE src)
Copy&paste an object's finaliser to another.
Definition gc.c:4393
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:742
RB_OBJ_PROMOTED_RAW
static bool RB_OBJ_PROMOTED_RAW(VALUE obj)
This is the implementation of RB_OBJ_PROMOTED().
Definition gc.h:722
rb_gc_adjust_memory_usage
void rb_gc_adjust_memory_usage(ssize_t diff)
Informs that there are external memory usages.
Definition gc.c:13100
rb_gc_count
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Definition gc.c:11290
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:66
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:429
st_table
Definition st.h:79
value.h
Defines VALUE and ID.
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40