1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gc: extend optimized mode to strings (#10409)

This commit is contained in:
Uwe Krüger
2021-06-11 23:03:05 +02:00
committed by GitHub
parent d0100faf26
commit 3f654a69e3
2 changed files with 84 additions and 15 deletions

View File

@@ -256,6 +256,59 @@ pub fn malloc(n int) &byte {
return res
}
[unsafe]
pub fn malloc_noscan(n int) &byte {
if n <= 0 {
panic('> V malloc(<=0)')
}
$if vplayground ? {
if n > 10000 {
panic('allocating more than 10 KB at once is not allowed in the V playground')
}
if total_m > 50 * 1024 * 1024 {
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 := &byte(0)
$if prealloc {
return unsafe { prealloc_malloc(n) }
} $else $if gcboehm ? {
$if gcboehm_opt ? {
unsafe {
res = C.GC_MALLOC_ATOMIC(n)
}
} $else {
unsafe {
res = C.GC_MALLOC(n)
}
}
} $else $if freestanding {
mut e := Errno{}
res, e = mm_alloc(u64(n))
if e != .enoerror {
eprint('malloc() failed: ')
eprintln(e.str())
panic('malloc() failed')
}
} $else {
res = unsafe { C.malloc(n) }
}
if res == 0 {
panic('malloc($n) failed')
}
$if debug_malloc ? {
// Fill in the memory with something != 0, so it is easier to spot
// when the calling code wrongly relies on it being zeroed.
unsafe { C.memset(res, 0x88, n) }
}
return res
}
// 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`.
@@ -369,7 +422,11 @@ pub fn vcalloc_noscan(n int) &byte {
if n < 0 {
panic('calloc(<0)')
}
return unsafe { &byte(C.memset(C.GC_MALLOC_ATOMIC(n), 0, n)) }
return $if gcboehm_opt ? {
unsafe { &byte(C.memset(C.GC_MALLOC_ATOMIC(n), 0, n)) }
} $else {
unsafe { &byte(C.GC_MALLOC(n)) }
}
} $else {
return unsafe { vcalloc(n) }
}