From ab3c1f2a08f54f05324a297539ef68275d9220c3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 11 Jul 2020 13:22:16 +0200 Subject: [PATCH] prealloc: skip frees for now --- vlib/builtin/array.v | 3 +++ vlib/builtin/builtin.v | 24 ++++++++++++++++++++---- vlib/builtin/map.v | 2 +- vlib/builtin/string.v | 6 ++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index f2088df01f..100f8510ca 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -438,6 +438,9 @@ pub fn (a array) reverse() array { // pub fn (a []int) free() { [unsafe_fn] pub fn (a &array) free() { + $if prealloc { + return + } // if a.is_slice { // return // } diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 24349a2ed2..324dae4326 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -165,13 +165,23 @@ TODO */ } +//#include +//fn malloc_size(b byteptr) int + [unsafe_fn] pub fn v_realloc(b byteptr, n u32) byteptr { - ptr := C.realloc(b, n) - if ptr == 0 { - panic('realloc($n) failed') + $if prealloc { + new_ptr := malloc(int(n)) + size := 0 //malloc_size(b) + C.memcpy(new_ptr, b, size) + return new_ptr + } $else { + ptr := C.realloc(b, n) + if ptr == 0 { + panic('realloc($n) failed') + } + return ptr } - return ptr } [unsafe_fn] @@ -191,6 +201,9 @@ pub fn vcalloc(n int) byteptr { [unsafe_fn] pub fn free(ptr voidptr) { + $if prealloc { + return + } C.free(ptr) } @@ -203,6 +216,9 @@ pub fn memdup(src voidptr, sz int) voidptr { } fn v_ptr_free(ptr voidptr) { + $if prealloc { + return + } C.free(ptr) } diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 2fccc3f9c1..b75dcf21e7 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -122,7 +122,7 @@ fn new_dense_array(value_bytes int) DenseArray { fn (mut d DenseArray) push(key string, value voidptr) u32 { if d.cap == d.len { d.cap += d.cap >> 3 - d.keys = &string(v_realloc(d.keys, sizeof(string) * d.cap)) + d.keys = &string(v_realloc(byteptr(d.keys), sizeof(string) * d.cap)) d.values = v_realloc(d.values, u32(d.value_bytes) * d.cap) } push_index := d.len diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index a3f089ab4b..ea8dd27d2d 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1170,6 +1170,9 @@ pub fn (u ustring) at(idx int) string { } fn (u &ustring) free() { + $if prealloc { + return + } u.runes.free() } @@ -1194,6 +1197,9 @@ pub fn (c byte) is_letter() bool { } pub fn (s &string) free() { + $if prealloc { + return + } if s.is_lit == -98761234 { C.printf('double string.free() detected\n') return