builtin: add gc_collect/0, gc_get_warn_proc/0, gc_set_warn_proc/1. Use them to turn off GC warnings by default. (#20788)

This commit is contained in:
Delyan Angelov 2024-02-11 16:07:36 +02:00 committed by GitHub
parent 09095985d3
commit 8793aebe65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 4 deletions

View File

@ -134,6 +134,13 @@ fn C.GC_is_disabled() int
// protect memory block from being freed before this call // protect memory block from being freed before this call
fn C.GC_reachable_here(voidptr) fn C.GC_reachable_here(voidptr)
// gc_collect explicitly performs a garbage collection run.
// Note, that garbage collections are done automatically when needed in most cases,
// so usually you should need to call that function often.
pub fn gc_collect() {
C.GC_gcollect()
}
// for leak detection it is advisable to do explicit garbage collections // for leak detection it is advisable to do explicit garbage collections
pub fn gc_check_leaks() { pub fn gc_check_leaks() {
$if gcboehm_leak ? { $if gcboehm_leak ? {
@ -165,3 +172,22 @@ fn C.GC_remove_roots(voidptr, voidptr)
fn C.GC_get_sp_corrector() fn (voidptr, voidptr) fn C.GC_get_sp_corrector() fn (voidptr, voidptr)
fn C.GC_set_sp_corrector(fn (voidptr, voidptr)) fn C.GC_set_sp_corrector(fn (voidptr, voidptr))
// GC warnings are silenced by default, but can be redirected to a custom cb function by programs too:
type FnGC_WarnCB = fn (msg &char, arg usize)
fn C.GC_get_warn_proc() FnGC_WarnCB
fn C.GC_set_warn_proc(cb FnGC_WarnCB)
// gc_get_warn_proc returns the current callback fn, that will be used for printing GC warnings
pub fn gc_get_warn_proc() FnGC_WarnCB {
return C.GC_get_warn_proc()
}
// gc_set_warn_proc sets the callback fn, that will be used for printing GC warnings
pub fn gc_set_warn_proc(cb FnGC_WarnCB) {
C.GC_set_warn_proc(cb)
}
// used by builtin_init:
fn internal_gc_warn_proc_none(msg &char, arg usize) {}

View File

@ -4,7 +4,7 @@
module builtin module builtin
fn builtin_init() { fn builtin_init() {
// Do nothing gc_set_warn_proc(internal_gc_warn_proc_none)
} }
fn break_if_debugger_attached() { fn break_if_debugger_attached() {

View File

@ -18,7 +18,28 @@ fn C.GC_get_heap_usage_safe(pheap_size &usize, pfree_bytes &usize, punmapped_byt
fn C.GC_get_memory_use() usize fn C.GC_get_memory_use() usize
// provide an empty function when manual memory management is used fn C.GC_gcollect()
// to simplify leak detection
// // gc_check_leaks is useful for detecting leaks, but it needs the GC to run.
// When GC is not used, it is a NOP.
pub fn gc_check_leaks() {} pub fn gc_check_leaks() {}
// gc_collect explicitly performs a garbage collection.
// When the GC is not on, it is a NOP.
pub fn gc_collect() {}
type FnGC_WarnCB = fn (msg &char, arg usize)
fn C.GC_get_warn_proc() FnGC_WarnCB
fn C.GC_set_warn_proc(cb FnGC_WarnCB)
// gc_get_warn_proc returns the current callback fn, that will be used for printing GC warnings.
// When the GC is not on, it is a NOP.
pub fn gc_get_warn_proc() {}
// gc_set_warn_proc sets the callback fn, that will be used for printing GC warnings.
// When the GC is not on, it is a NOP.
pub fn gc_set_warn_proc(cb FnGC_WarnCB) {}
// used by builtin_init
fn internal_gc_warn_proc_none(msg &char, arg usize) {}

View File

@ -48,6 +48,7 @@ const enable_wrap_at_eol_output = 2
const evable_virtual_terminal_processing = 4 const evable_virtual_terminal_processing = 4
fn builtin_init() { fn builtin_init() {
gc_set_warn_proc(internal_gc_warn_proc_none)
g_original_codepage = C.GetConsoleOutputCP() g_original_codepage = C.GetConsoleOutputCP()
C.SetConsoleOutputCP(cp_utf8) C.SetConsoleOutputCP(cp_utf8)
C.atexit(restore_codepage) C.atexit(restore_codepage)