mirror of
https://github.com/vlang/v.git
synced 2025-09-09 15:27:05 -04:00
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:
parent
09095985d3
commit
8793aebe65
@ -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) {}
|
||||||
|
@ -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() {
|
||||||
|
@ -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) {}
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user