From 7dcd47369bc4ebe1da8977fd930ec501b6d13ed6 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Dec 2019 18:41:25 +0300 Subject: [PATCH] option to pre-allocate a memory block --- vlib/builtin/builtin.v | 24 +++++++++++++++++------- vlib/compiler/main.v | 6 +++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 3ac621ff12..2e57cb86b6 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -4,6 +4,8 @@ module builtin +__global g_m2_ptr byteptr + fn init() { $if windows { if is_atty(0) > 0 { @@ -108,14 +110,27 @@ pub fn print(s string) { } } + __global total_m i64 = 0 -//__global nr_mallocs int = 0 +__global nr_mallocs int = 0 + [unsafe_fn] pub fn malloc(n int) byteptr { if n < 0 { panic('malloc(<0)') } - //nr_mallocs++ + $if debug { + res := g_m2_ptr + g_m2_ptr += n + nr_mallocs++ + return res + } $else { + ptr := C.malloc(n) + if ptr == 0 { + panic('malloc($n) failed') + } + return ptr + } /* TODO #ifdef VPLAY @@ -129,11 +144,6 @@ TODO print_backtrace() #endif */ - ptr := C.malloc(n) - if ptr == 0 { - panic('malloc($n) failed') - } - return ptr } pub fn calloc(n int) byteptr { diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 9d750f6aa4..67f1c097e1 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -199,7 +199,7 @@ pub fn (v mut V) compile() { println(v.files) } v.add_v_files_to_compile() - if v.pref.is_verbose || v.pref.is_debug { + if v.pref.is_verbose { println('all .v files:') println(v.files) } @@ -418,6 +418,10 @@ fn (v mut V) generate_init() { // vlib can't have `init_consts()` v.cgen.genln('void init() { g_str_buf=malloc(1000); +#if VDEBUG +g_m2_ptr=malloc(50 * 1000 * 1000); +puts("allocated 50 mb"); +#endif $call_mod_init_consts $consts_init_body builtin__init();