1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: heap memory usage api (#18103)

This commit is contained in:
Felipe Pena 2023-05-03 14:33:52 -03:00 committed by GitHub
parent 40a97aed1a
commit 5008515b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -643,6 +643,36 @@ pub fn memdup_uncollectable(src voidptr, sz int) voidptr {
}
}
pub struct GCHeapUsage {
pub:
heap_size usize
free_bytes usize
total_bytes usize
unmapped_bytes usize
bytes_since_gc usize
}
// gc_heap_usage returns the info about heap usage
pub fn gc_heap_usage() GCHeapUsage {
$if gcboehm ? {
mut res := GCHeapUsage{}
C.GC_get_heap_usage_safe(&res.heap_size, &res.free_bytes, &res.unmapped_bytes,
&res.bytes_since_gc, &res.total_bytes)
return res
} $else {
return GCHeapUsage{}
}
}
// gc_memory_use returns the total memory use in bytes by all allocated blocks
pub fn gc_memory_use() usize {
$if gcboehm ? {
return C.GC_get_memory_use()
} $else {
return 0
}
}
[inline]
fn v_fixed_index(i int, len int) int {
$if !no_bounds_checking {

View File

@ -132,3 +132,6 @@ pub fn gc_check_leaks() {
C.GC_gcollect()
}
}
fn C.GC_get_heap_usage_safe(pheap_size &usize, pfree_bytes &usize, punmapped_bytes &usize, pbytes_since_gc &usize, ptotal_bytes &usize)
fn C.GC_get_memory_use() usize

View File

@ -14,6 +14,10 @@ fn C.GC_REALLOC(ptr voidptr, n usize) voidptr
fn C.GC_FREE(ptr voidptr)
fn C.GC_get_heap_usage_safe(pheap_size &usize, pfree_bytes &usize, punmapped_bytes &usize, pbytes_since_gc &usize, ptotal_bytes &usize)
fn C.GC_get_memory_use() usize
// provide an empty function when manual memory management is used
// to simplify leak detection
//