builtin.wasm_bare: use walloc as malloc/free implementation (#13731)

This commit is contained in:
playX 2022-03-14 10:20:20 +03:00 committed by GitHub
parent 4d99157cd5
commit c8b0f51c13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 553 additions and 84 deletions

View file

@ -1,8 +1,13 @@
module builtin
import dlmalloc
//__global global_allocator dlmalloc.Dlmalloc
__global global_allocator dlmalloc.Dlmalloc
[unsafe]
pub fn __malloc(size usize) voidptr {
unsafe {
return malloc(int(size))
}
}
[unsafe]
pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
@ -16,12 +21,6 @@ pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
return unsafe { dest }
}
[export: 'malloc']
[unsafe]
fn __malloc(n usize) &C.void {
return unsafe { global_allocator.malloc(n) }
}
[unsafe]
fn strlen(_s &C.void) usize {
s := unsafe { &byte(_s) }
@ -106,14 +105,6 @@ fn memcmp(a &C.void, b &C.void, n usize) int {
return 0
}
[export: 'free']
[unsafe]
fn __free(ptr &C.void) {
unsafe {
global_allocator.free_(ptr)
}
}
fn vsprintf(str &char, format &char, ap &byte) int {
panic('vsprintf(): string interpolation is not supported in `-freestanding`')
}
@ -169,5 +160,5 @@ fn __qsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
}
fn init_global_allocator() {
global_allocator = dlmalloc.new(get_wasm_allocator())
// global_allocator = dlmalloc.new(get_wasm_allocator())
}