Skip to content

Commit

Permalink
git add
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Nov 28, 2024
1 parent 4ca17f9 commit 2b90535
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions init_tinygowasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build tinygo.wasm

package wasilibs

/*
#cgo LDFLAGS: -Linternal/wasm -lcre2 -lre2 -lc++ -lc++abi -lclang_rt.builtins-wasm32
*/
import "C"
Binary file added internal/wasm/libc++.a
Binary file not shown.
Binary file added internal/wasm/libc++abi.a
Binary file not shown.
Binary file added internal/wasm/libclang_rt.builtins-wasm32.a
Binary file not shown.
Binary file added internal/wasm/libcre2.a
Binary file not shown.
Binary file added internal/wasm/libre2.a
Binary file not shown.
54 changes: 54 additions & 0 deletions malloc_tinygowasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build tinygo.wasm && !gc.custom && !custommalloc

package wasilibs

import (
"unsafe"
)

/*
#include <stdint.h>
void* malloc(size_t size);
void free(void* ptr);
*/
import "C"

// TinyGo currently only includes a subset of malloc functions by default, so we
// reimplement the remaining here.

//export posix_memalign
func posix_memalign(memptr *uint32, align uint32, size uint32) int32 {
if align < 4 {
return 22 /* EINVAL */
}

// Ignore alignment and hope for best, TinyGo by default does not
// provide a way to allocate aligned memory.
mem := uint32(uintptr(C.malloc(C.size_t(size))))
if mem == 0 {
// TODO(anuraaga): Needs to read errno to be precise
return 1
}

*memptr = mem

return 0
}

//export calloc
func calloc(num uint32, size uint32) uint32

//export __libc_calloc
func __libc_calloc(num uint32, size uint32) uint32 {
return calloc(num, size)
}

//export __libc_malloc
func __libc_malloc(size uint32) uint32 {
return uint32(uintptr(C.malloc(C.size_t(size))))
}

//export __libc_free
func __libc_free(ptr uint32) {
C.free(unsafe.Pointer(uintptr(ptr)))
}

0 comments on commit 2b90535

Please sign in to comment.