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

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
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())
}

View File

@ -1,68 +1,5 @@
// malloc/free implementation for freestanding webassembly target. We just use walloc at the moment
module builtin
import dlmalloc
// Corresponding intrinsic to wasms `memory.grow` instruction
//
// This function, when called, will attempt to grow the default linear memory by the specified delta of pages.
// The current WebAssembly page size is 65536 bytes (64 KB). If memory is successfully grown then the previous size of memory, in pages, is returned.
// If memory cannot be grown then -1 is returned.
//
// The argument mem is the numerical index of which memory to return the size of. Note that currently the WebAssembly specification only supports one memory,
// so it is required that zero is passed in. The argument is present to be forward-compatible with future WebAssembly revisions.
// If a nonzero argument is passed to this function it will currently unconditionally abort
fn C.__builtin_wasm_memory_grow(mem u32, delta usize) usize
/// Corresponding intrinsic to wasm's `memory.size` instruction
///
/// This function, when called, will return the current memory size in units of
/// pages. The current WebAssembly page size is 65536 bytes (64 KB).
fn C.__builtin_wasm_memory_size(mem u32) usize
const page_size = 65536
fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
pages := size / page_size
prev := C.__builtin_wasm_memory_grow(0, pages)
if prev == -1 {
return voidptr(0), 0, 0
}
return voidptr(prev * page_size), pages * page_size, 0
}
fn system_remap(_ voidptr, _ voidptr, _ usize, _ usize, _ bool) voidptr {
return voidptr(0)
}
fn system_free_part(_ voidptr, _ voidptr, _ usize, _ usize) bool {
return false
}
fn system_free(_ voidptr, _ voidptr, _ usize) bool {
return false
}
fn system_allocates_zeros(_ voidptr) bool {
return false
}
fn system_page_size(_ voidptr) usize {
return page_size
}
fn system_can_release_part(_ voidptr, _ u32) bool {
return false
}
fn get_wasm_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)
}
}
#flag -I @VEXEROOT/thirdparty/walloc/
#include "walloc.c"