From a27e7f76755c5ba108ead7061cbee91b30e0d65a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 23 May 2021 12:37:23 +0300 Subject: [PATCH] builtin: remove commented code; improve print_backtrace with tcc --- vlib/builtin/builtin.c.v | 92 +++++++++++++++------------------------- vlib/builtin/builtin.v | 22 ++++++---- vlib/builtin/string.v | 9 ++-- 3 files changed, 54 insertions(+), 69 deletions(-) diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 047dd2cd05..9a84fdf1dd 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -34,24 +34,22 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) { eprintln('=========================================') $if exit_after_panic_message ? { C.exit(1) + } $else $if no_backtrace ? { + C.exit(1) } $else { - $if no_backtrace ? { - C.exit(1) - } $else { - $if tinyc { - $if panics_break_into_debugger ? { - break_if_debugger_attached() - } $else { - C.tcc_backtrace(c'Backtrace') - } - C.exit(1) - } - print_backtrace_skipping_top_frames(1) + $if tinyc { $if panics_break_into_debugger ? { break_if_debugger_attached() + } $else { + C.tcc_backtrace(c'Backtrace') } C.exit(1) } + print_backtrace_skipping_top_frames(1) + $if panics_break_into_debugger ? { + break_if_debugger_attached() + } + C.exit(1) } } } @@ -71,24 +69,22 @@ pub fn panic(s string) { eprintln('v hash: $vcommithash()') $if exit_after_panic_message ? { C.exit(1) + } $else $if no_backtrace ? { + C.exit(1) } $else { - $if no_backtrace ? { - C.exit(1) - } $else { - $if tinyc { - $if panics_break_into_debugger ? { - break_if_debugger_attached() - } $else { - C.tcc_backtrace(c'Backtrace') - } - C.exit(1) - } - print_backtrace_skipping_top_frames(1) + $if tinyc { $if panics_break_into_debugger ? { break_if_debugger_attached() + } $else { + C.tcc_backtrace(c'Backtrace') } C.exit(1) } + print_backtrace_skipping_top_frames(1) + $if panics_break_into_debugger ? { + break_if_debugger_attached() + } + C.exit(1) } } } @@ -170,8 +166,7 @@ pub fn eprint(s string) { pub fn print(s string) { $if android { C.fprintf(C.stdout, c'%.*s', s.len, s.str) - } - $if ios { + } $else $if ios { // TODO: Implement a buffer as NSLog doesn't have a "print" C.WrappedNSLog(s.str) } $else $if freestanding { @@ -181,13 +176,6 @@ pub fn print(s string) { } } -/* -#include "@VEXEROOT/vlib/darwin/darwin.m" -fn C.nsstring2(s string) voidptr -fn C.NSLog(x voidptr) -#include -fn C.asl_log(voidptr, voidptr, int, charptr) -*/ // println prints a message with a line end, to stdout. stdout is flushed. pub fn println(s string) { if s.str == 0 { @@ -196,7 +184,6 @@ pub fn println(s string) { } $else $if ios { C.WrappedNSLog(c'println(NIL)') } $else $if freestanding { - bare_print(s.str, u64(s.len)) bare_print(c'println(NIL)\n', 13) } $else { _ = C.write(1, c'println(NIL)\n', 13) @@ -205,8 +192,7 @@ pub fn println(s string) { } $if android { C.fprintf(C.stdout, c'%.*s\n', s.len, s.str) - } - $if ios { + } $else $if ios { C.WrappedNSLog(s.str) } $else $if freestanding { bare_print(s.str, u64(s.len)) @@ -238,7 +224,7 @@ pub fn malloc(n int) &byte { } mut res := &byte(0) $if prealloc { - res = unsafe { prealloc_malloc(n) } + return unsafe { prealloc_malloc(n) } } $else $if gcboehm ? { unsafe { res = C.GC_MALLOC(n) @@ -265,10 +251,6 @@ pub fn malloc(n int) &byte { return res } -/* -#include -fn malloc_size(b byteptr) int -*/ // v_realloc resizes the memory block `b` with `n` bytes. // The `b byteptr` must be a pointer to an existing memory block // previously allocated with `malloc`, `v_calloc` or `vcalloc`. @@ -281,15 +263,14 @@ pub fn v_realloc(b &byte, n int) &byte { new_ptr = malloc(n) C.memcpy(new_ptr, b, n) } + return new_ptr + } $else $if gcboehm ? { + new_ptr = unsafe { C.GC_REALLOC(b, n) } } $else { - $if gcboehm ? { - new_ptr = unsafe { C.GC_REALLOC(b, n) } - } $else { - new_ptr = unsafe { C.realloc(b, n) } - } - if new_ptr == 0 { - panic('realloc($n) failed') - } + new_ptr = unsafe { C.realloc(b, n) } + } + if new_ptr == 0 { + panic('realloc($n) failed') } return new_ptr } @@ -348,8 +329,7 @@ pub fn vcalloc(n int) &byte { } $if prealloc { return unsafe { prealloc_calloc(n) } - } - $if gcboehm ? { + } $else $if gcboehm ? { return unsafe { &byte(C.GC_MALLOC(n)) } } $else { return unsafe { C.calloc(1, n) } @@ -361,8 +341,7 @@ pub fn vcalloc(n int) &byte { pub fn vcalloc_noscan(n int) &byte { $if prealloc { return unsafe { prealloc_calloc(n) } - } - $if gcboehm ? { + } $else $if gcboehm ? { $if vplayground ? { if n > 10000 { panic('allocating more than 10 KB is not allowed in the playground') @@ -382,8 +361,7 @@ pub fn vcalloc_noscan(n int) &byte { pub fn free(ptr voidptr) { $if prealloc { return - } - $if gcboehm ? { + } $else $if gcboehm ? { // It is generally better to leave it to Boehm's gc to free things. // Calling C.GC_FREE(ptr) was tried initially, but does not work // well with programs that do manual management themselves. @@ -392,9 +370,9 @@ pub fn free(ptr voidptr) { $if gcboehm_leak ? { unsafe { C.GC_FREE(ptr) } } - return + } $else { + C.free(ptr) } - C.free(ptr) } // memdup dynamically allocates a `sz` bytes block of memory on the heap diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 66e8dbd0bb..9ccf4d78e5 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -23,15 +23,21 @@ fn on_panic(f fn(int)int) { // print_backtrace shows a backtrace of the current call stack on stdout pub fn print_backtrace() { - // at the time of backtrace_symbols_fd call, the C stack would look something like this: - // 1 frame for print_backtrace_skipping_top_frames - // 1 frame for print_backtrace itself - // ... print the rest of the backtrace frames ... + // At the time of backtrace_symbols_fd call, the C stack would look something like this: + // * print_backtrace_skipping_top_frames + // * print_backtrace itself + // * the rest of the backtrace frames // => top 2 frames should be skipped, since they will not be informative to the developer - $if freestanding { - println(bare_backtrace()) - } $else { - print_backtrace_skipping_top_frames(2) + $if !no_backtrace ? { + $if freestanding { + println(bare_backtrace()) + } $else { + $if tinyc { + C.tcc_backtrace(c'Backtrace') + } $else { + print_backtrace_skipping_top_frames(2) + } + } } } diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index e2aff1d11a..99e9f92351 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1521,20 +1521,21 @@ pub fn (c byte) is_letter() bool { } // free allows for manually freeing the memory occupied by the string -[unsafe] +[manualfree; unsafe] pub fn (s &string) free() { $if prealloc { return } if s.is_lit == -98761234 { + dsfree_msg := c'double string.free() detected\n' $if freestanding { - bare_eprint(c'double string.free() detected\n', u64(unsafe { C.strlen(c'double string.free() detected\n') })) + bare_eprint(dsfree_msg, u64(unsafe { C.strlen(dsfree_msg) })) } $else { - C.printf(c'double string.free() detected\n') + C.printf(dsfree_msg) } return } - if s.is_lit == 1 || s.len == 0 { + if s.is_lit == 1 || s.len == 0 || s.str == 0 { return } unsafe {