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

Boehm-GC: fix global const handling (#9477)

* define global `__v_inside_init`

* unset `__v_inside_init` after `_vinit()`

* define `C.GC_MALLOC_UNCOLLECTABLE()`

* allocate uncollectable memory during `_vinit()`

* ci: run test cases with V copiler that uses GC-Boehm itself

* wrap `__v_inside_init` access into `#ifdef _VGCBOEHM`
This commit is contained in:
Uwe Krüger
2021-03-26 15:44:45 +01:00
committed by GitHub
parent a38fc89150
commit 3220ab7053
6 changed files with 33 additions and 15 deletions

View File

@@ -189,7 +189,13 @@ pub fn malloc(n int) byteptr {
nr_mallocs++
} $else {
$if gcboehm ? {
res = unsafe { C.GC_MALLOC(n) }
unsafe {
if C.__v_inside_init == 0 {
res = C.GC_MALLOC(n)
} else {
res = C.GC_MALLOC_UNCOLLECTABLE(n)
}
}
} $else {
res = unsafe { C.malloc(n) }
}
@@ -282,16 +288,6 @@ pub fn realloc_data(old_data byteptr, old_size int, new_size int) byteptr {
return nptr
}
// v_calloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
// v_calloc returns a `byteptr` pointing to the memory address of the allocated space.
pub fn v_calloc(n int) byteptr {
$if gcboehm ? {
return C.GC_MALLOC(n)
} $else {
return C.calloc(1, n)
}
}
// vcalloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
// vcalloc returns a `byteptr` pointing to the memory address of the allocated space.
// Unlike `v_calloc` vcalloc checks for negative values given in `n`.
@@ -302,7 +298,11 @@ pub fn vcalloc(n int) byteptr {
return byteptr(0)
}
$if gcboehm ? {
return C.GC_MALLOC(n)
return if C.__v_inside_init == 0 {
byteptr(C.GC_MALLOC(n))
} else {
byteptr(C.GC_MALLOC_UNCOLLECTABLE(n))
}
} $else {
return C.calloc(1, n)
}