prealloc: skip frees for now

pull/5801/head
Alexander Medvednikov 2020-07-11 13:22:16 +02:00
parent fae601fe39
commit ab3c1f2a08
4 changed files with 30 additions and 5 deletions

View File

@ -438,6 +438,9 @@ pub fn (a array) reverse() array {
// pub fn (a []int) free() { // pub fn (a []int) free() {
[unsafe_fn] [unsafe_fn]
pub fn (a &array) free() { pub fn (a &array) free() {
$if prealloc {
return
}
// if a.is_slice { // if a.is_slice {
// return // return
// } // }

View File

@ -165,14 +165,24 @@ TODO
*/ */
} }
//#include <malloc/malloc.h>
//fn malloc_size(b byteptr) int
[unsafe_fn] [unsafe_fn]
pub fn v_realloc(b byteptr, n u32) byteptr { pub fn v_realloc(b byteptr, n u32) byteptr {
$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) ptr := C.realloc(b, n)
if ptr == 0 { if ptr == 0 {
panic('realloc($n) failed') panic('realloc($n) failed')
} }
return ptr return ptr
} }
}
[unsafe_fn] [unsafe_fn]
pub fn v_calloc(n int) byteptr { pub fn v_calloc(n int) byteptr {
@ -191,6 +201,9 @@ pub fn vcalloc(n int) byteptr {
[unsafe_fn] [unsafe_fn]
pub fn free(ptr voidptr) { pub fn free(ptr voidptr) {
$if prealloc {
return
}
C.free(ptr) C.free(ptr)
} }
@ -203,6 +216,9 @@ pub fn memdup(src voidptr, sz int) voidptr {
} }
fn v_ptr_free(ptr voidptr) { fn v_ptr_free(ptr voidptr) {
$if prealloc {
return
}
C.free(ptr) C.free(ptr)
} }

View File

@ -122,7 +122,7 @@ fn new_dense_array(value_bytes int) DenseArray {
fn (mut d DenseArray) push(key string, value voidptr) u32 { fn (mut d DenseArray) push(key string, value voidptr) u32 {
if d.cap == d.len { if d.cap == d.len {
d.cap += d.cap >> 3 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) d.values = v_realloc(d.values, u32(d.value_bytes) * d.cap)
} }
push_index := d.len push_index := d.len

View File

@ -1170,6 +1170,9 @@ pub fn (u ustring) at(idx int) string {
} }
fn (u &ustring) free() { fn (u &ustring) free() {
$if prealloc {
return
}
u.runes.free() u.runes.free()
} }
@ -1194,6 +1197,9 @@ pub fn (c byte) is_letter() bool {
} }
pub fn (s &string) free() { pub fn (s &string) free() {
$if prealloc {
return
}
if s.is_lit == -98761234 { if s.is_lit == -98761234 {
C.printf('double string.free() detected\n') C.printf('double string.free() detected\n')
return return