From b67705f5684105b6a349ccc3c17c5eb11bb340f6 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 22 Dec 2022 19:36:15 +0200 Subject: [PATCH] builtin: move the `-d trace_*alloc` lines at the start of their corresponding functions, implement `-d trace_memdup` too --- vlib/builtin/builtin.c.v | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 237a49b2ee..c8e81544b9 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -311,6 +311,11 @@ __global total_m = i64(0) // unlike the `calloc` family of functions - malloc will not zero the memory block. [unsafe] pub fn malloc(n isize) &u8 { + $if trace_malloc ? { + total_m += n + C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m) + // print_backtrace() + } if n <= 0 { panic('malloc(${n} <= 0)') } @@ -322,11 +327,6 @@ pub fn malloc(n isize) &u8 { panic('allocating more than 50 MB is not allowed in the V playground') } } - $if trace_malloc ? { - total_m += n - C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m) - // print_backtrace() - } mut res := &u8(0) $if prealloc { return unsafe { prealloc_malloc(n) } @@ -354,6 +354,11 @@ pub fn malloc(n isize) &u8 { [unsafe] pub fn malloc_noscan(n isize) &u8 { + $if trace_malloc ? { + total_m += n + C.fprintf(C.stderr, c'malloc_noscan %6d total %10d\n', n, total_m) + // print_backtrace() + } if n <= 0 { panic('malloc_noscan(${n} <= 0)') } @@ -365,11 +370,6 @@ pub fn malloc_noscan(n isize) &u8 { panic('allocating more than 50 MB is not allowed in the V playground') } } - $if trace_malloc ? { - total_m += n - C.fprintf(C.stderr, c'malloc_noscan %6d total %10d\n', n, total_m) - // print_backtrace() - } mut res := &u8(0) $if prealloc { return unsafe { prealloc_malloc(n) } @@ -403,6 +403,11 @@ pub fn malloc_noscan(n isize) &u8 { // on the heap, which will NOT be garbage-collected (but its contents will). [unsafe] pub fn malloc_uncollectable(n isize) &u8 { + $if trace_malloc ? { + total_m += n + C.fprintf(C.stderr, c'malloc_uncollectable %6d total %10d\n', n, total_m) + // print_backtrace() + } if n <= 0 { panic('malloc_uncollectable(${n} <= 0)') } @@ -414,11 +419,6 @@ pub fn malloc_uncollectable(n isize) &u8 { panic('allocating more than 50 MB is not allowed in the V playground') } } - $if trace_malloc ? { - total_m += n - C.fprintf(C.stderr, c'malloc_uncollectable %6d total %10d\n', n, total_m) - // print_backtrace() - } mut res := &u8(0) $if prealloc { return unsafe { prealloc_malloc(n) } @@ -519,15 +519,15 @@ pub fn realloc_data(old_data &u8, old_size int, new_size int) &u8 { // vcalloc returns a `byteptr` pointing to the memory address of the allocated space. // Unlike `v_calloc` vcalloc checks for negative values given in `n`. pub fn vcalloc(n isize) &u8 { + $if trace_vcalloc ? { + total_m += n + C.fprintf(C.stderr, c'vcalloc %6d total %10d\n', n, total_m) + } if n < 0 { panic('calloc(${n} < 0)') } else if n == 0 { return &u8(0) } - $if trace_vcalloc ? { - total_m += n - C.fprintf(C.stderr, c'vcalloc %6d total %10d\n', n, total_m) - } $if prealloc { return unsafe { prealloc_calloc(n) } } $else $if gcboehm ? { @@ -589,6 +589,9 @@ pub fn free(ptr voidptr) { // returns a pointer to the newly allocated space. [unsafe] pub fn memdup(src voidptr, sz int) voidptr { + $if trace_memdup ? { + C.fprintf(C.stderr, c'memdup size: %10d\n', sz) + } if sz == 0 { return vcalloc(1) } @@ -600,6 +603,9 @@ pub fn memdup(src voidptr, sz int) voidptr { [unsafe] pub fn memdup_noscan(src voidptr, sz int) voidptr { + $if trace_memdup ? { + C.fprintf(C.stderr, c'memdup_noscan size: %10d\n', sz) + } if sz == 0 { return vcalloc_noscan(1) } @@ -615,6 +621,9 @@ pub fn memdup_noscan(src voidptr, sz int) voidptr { // space and returns a pointer to the newly allocated space. [unsafe] pub fn memdup_uncollectable(src voidptr, sz int) voidptr { + $if trace_memdup ? { + C.fprintf(C.stderr, c'memdup_uncollectable size: %10d\n', sz) + } if sz == 0 { return vcalloc(1) }