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

builtin: improve support for large arrays ([]int{len: 1_000_000_000} now works), fix an arr.repeat() bug (#14294)

This commit is contained in:
Delyan Angelov
2022-05-04 18:42:06 +03:00
committed by GitHub
parent af8be14639
commit 1a4d9017e2
8 changed files with 216 additions and 150 deletions

View File

@@ -21,16 +21,16 @@ __global g_memory_block &VMemoryBlock
struct VMemoryBlock {
mut:
id int
cap int
cap isize
start &byte = 0
previous &VMemoryBlock = 0
remaining int
remaining isize
current &u8 = 0
mallocs int
}
[unsafe]
fn vmemory_block_new(prev &VMemoryBlock, at_least int) &VMemoryBlock {
fn vmemory_block_new(prev &VMemoryBlock, at_least isize) &VMemoryBlock {
mut v := unsafe { &VMemoryBlock(C.calloc(1, sizeof(VMemoryBlock))) }
if prev != 0 {
v.id = prev.id + 1
@@ -45,7 +45,7 @@ fn vmemory_block_new(prev &VMemoryBlock, at_least int) &VMemoryBlock {
}
[unsafe]
fn vmemory_block_malloc(n int) &byte {
fn vmemory_block_malloc(n isize) &byte {
unsafe {
if g_memory_block.remaining < n {
g_memory_block = vmemory_block_new(g_memory_block, n)
@@ -95,12 +95,12 @@ fn prealloc_vcleanup() {
}
[unsafe]
fn prealloc_malloc(n int) &byte {
fn prealloc_malloc(n isize) &byte {
return unsafe { vmemory_block_malloc(n) }
}
[unsafe]
fn prealloc_realloc(old_data &byte, old_size int, new_size int) &byte {
fn prealloc_realloc(old_data &byte, old_size isize, new_size isize) &byte {
new_ptr := unsafe { vmemory_block_malloc(new_size) }
min_size := if old_size < new_size { old_size } else { new_size }
unsafe { C.memcpy(new_ptr, old_data, min_size) }
@@ -108,7 +108,7 @@ fn prealloc_realloc(old_data &byte, old_size int, new_size int) &byte {
}
[unsafe]
fn prealloc_calloc(n int) &byte {
fn prealloc_calloc(n isize) &byte {
new_ptr := unsafe { vmemory_block_malloc(n) }
unsafe { C.memset(new_ptr, 0, n) }
return new_ptr