Skip to content

Commit

Permalink
builtin: add gc_collect/0, gc_get_warn_proc/0, gc_set_warn_proc/1. Us…
Browse files Browse the repository at this point in the history
…e them to turn off GC warnings by default. (vlang#20788)
  • Loading branch information
spytheman authored Feb 11, 2024
1 parent 0909598 commit 8793aeb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
26 changes: 26 additions & 0 deletions vlib/builtin/builtin_d_gcboehm.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ fn C.GC_is_disabled() int
// protect memory block from being freed before this call
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
pub fn gc_check_leaks() {
$if gcboehm_leak ? {
Expand Down Expand Up @@ -165,3 +172,22 @@ fn C.GC_remove_roots(voidptr, voidptr)

fn C.GC_get_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) {}
2 changes: 1 addition & 1 deletion vlib/builtin/builtin_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module builtin

fn builtin_init() {
// Do nothing
gc_set_warn_proc(internal_gc_warn_proc_none)
}

fn break_if_debugger_attached() {
Expand Down
27 changes: 24 additions & 3 deletions vlib/builtin/builtin_notd_gcboehm.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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

// provide an empty function when manual memory management is used
// to simplify leak detection
//
fn C.GC_gcollect()

// 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() {}

// 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) {}
1 change: 1 addition & 0 deletions vlib/builtin/builtin_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const enable_wrap_at_eol_output = 2
const evable_virtual_terminal_processing = 4

fn builtin_init() {
gc_set_warn_proc(internal_gc_warn_proc_none)
g_original_codepage = C.GetConsoleOutputCP()
C.SetConsoleOutputCP(cp_utf8)
C.atexit(restore_codepage)
Expand Down

0 comments on commit 8793aeb

Please sign in to comment.