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

freestanding: malloc/free with mm_alloc an mm_free

Added more array support that depends on malloc. Added string clone (that uses malloc). Added test for it. Eliminated stack allocated buffers from most of the unit checks.
This commit is contained in:
bogen85
2019-12-08 04:44:52 -06:00
committed by Alexander Medvednikov
parent 6ec626c5e9
commit 8178e1f7da
10 changed files with 96 additions and 56 deletions

View File

@ -34,3 +34,21 @@ fn (a mut array) set(i int, val voidptr) {
}
mem_copy(a.data + a.element_size * i, val, a.element_size)
}
// array.repeat returns new array with the given array elements
// repeated `nr_repeat` times
pub fn (a array) repeat(nr_repeats int) array {
assert nr_repeats >= 0
arr := array {
len: nr_repeats * a.len
cap: nr_repeats * a.len
element_size: a.element_size
data: malloc(nr_repeats * a.len * a.element_size)
}
for i := 0; i < nr_repeats; i++ {
mem_copy(arr.data + i * a.len * a.element_size, a.data, a.len * a.element_size)
}
return arr
}