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

builtin: use dlmalloc for -freestanding (#13054)

This commit is contained in:
playX
2022-01-06 15:10:37 +03:00
committed by GitHub
parent fb66ec7cfb
commit ec91de3504
12 changed files with 284 additions and 108 deletions

View File

@ -1,5 +1,9 @@
module builtin
import dlmalloc
__global global_allocator dlmalloc.Dlmalloc
[unsafe]
pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
dest_ := unsafe { &byte(dest) }
@ -15,7 +19,7 @@ pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
[export: 'malloc']
[unsafe]
fn __malloc(n usize) &C.void {
return unsafe { malloc(int(n)) }
return unsafe { global_allocator.malloc(n) }
}
[unsafe]
@ -107,10 +111,14 @@ fn memcmp(a &C.void, b &C.void, n usize) int {
[export: 'free']
[unsafe]
fn __free(ptr &C.void) {
/*
err := mm_free(ptr)
if err != .enoerror {
eprintln('free error:')
panic(err)
}*/
unsafe {
global_allocator.free_(ptr)
}
}
@ -127,7 +135,7 @@ fn bare_read(buf &byte, count u64) (i64, Errno) {
return sys_read(0, buf, count)
}
fn bare_print(buf &byte, len u64) {
pub fn bare_print(buf &byte, len u64) {
sys_write(1, buf, len)
}
@ -160,3 +168,7 @@ fn __exit(code int) {
fn __qsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
panic('qsort() is not yet implemented in `-freestanding`')
}
fn init_global_allocator() {
global_allocator = dlmalloc.new(get_linux_allocator())
}

View File

@ -262,6 +262,13 @@ fn sys_munmap(addr voidptr, len u64) Errno {
return Errno(-sys_call2(11, u64(addr), len))
}
// 25 sys_mremap
fn sys_mremap(old_addr voidptr, old_len u64, new_len u64, flags u64) (&byte, Errno) {
rc := sys_call4(25, u64(old_addr), old_len, new_len, flags)
a, e := split_int_errno(rc)
return &byte(a), e
}
// 22 sys_pipe
fn sys_pipe(filedes &int) Errno {
return Errno(sys_call1(22, u64(filedes)))

View File

@ -1,5 +1,7 @@
module builtin
import dlmalloc
fn mm_alloc(size u64) (&byte, Errno) {
// BEGIN CONSTS
// the constants need to be here, since the initialization of other constants,
@ -27,3 +29,64 @@ fn mm_free(addr &byte) Errno {
return sys_munmap(addr - sizeof(u64), size + sizeof(u64))
}
}
fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
// BEGIN CONSTS
// the constants need to be here, since the initialization of other constants,
// which happen before these ones would, require malloc
mem_prot := MemProt(int(MemProt.prot_read) | int(MemProt.prot_write))
map_flags := MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous))
// END CONSTS
a, e := sys_mmap(&byte(0), u64(size + sizeof(u64)), mem_prot, map_flags, -1, 0)
if e == .enoerror {
return a, size, 0
}
return voidptr(0), 0, 0
}
fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr {
return voidptr(0)
}
fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool {
_, e := sys_mremap(ptr, u64(oldsize), u64(newsize), 0)
if e == .enoerror {
return true
}
e2 := sys_munmap(voidptr(usize(ptr) + newsize), u64(oldsize - newsize))
return e2 == .enoerror
}
fn system_free(_ voidptr, ptr voidptr, size usize) bool {
unsafe {
return sys_munmap(ptr, u64(size)) == .enoerror
}
}
fn system_can_release_part(_ voidptr, _ u32) bool {
return true
}
fn system_allocates_zeros(_ voidptr) bool {
return true
}
fn system_page_size(_ voidptr) usize {
return 4096
}
fn get_linux_allocator() dlmalloc.Allocator {
return dlmalloc.Allocator{
alloc: system_alloc
remap: system_remap
free_part: system_free_part
free_: system_free
can_release_part: system_can_release_part
allocates_zeros: system_allocates_zeros
page_size: system_page_size
data: voidptr(0)
}
}