Skip to content

Commit bce0516

Browse files
authored
Allow custom wasm malloc implementation (#3245)
1 parent 726d74a commit bce0516

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

src/runtime/arch_tinygowasm.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -88,55 +88,3 @@ func growHeap() bool {
8888
// Heap has grown successfully.
8989
return true
9090
}
91-
92-
// The below functions override the default allocator of wasi-libc. This ensures
93-
// code linked from other languages can allocate memory without colliding with
94-
// our GC allocations.
95-
96-
var allocs = make(map[uintptr][]byte)
97-
98-
//export malloc
99-
func libc_malloc(size uintptr) unsafe.Pointer {
100-
buf := make([]byte, size)
101-
ptr := unsafe.Pointer(&buf[0])
102-
allocs[uintptr(ptr)] = buf
103-
return ptr
104-
}
105-
106-
//export free
107-
func libc_free(ptr unsafe.Pointer) {
108-
if ptr == nil {
109-
return
110-
}
111-
if _, ok := allocs[uintptr(ptr)]; ok {
112-
delete(allocs, uintptr(ptr))
113-
} else {
114-
panic("free: invalid pointer")
115-
}
116-
}
117-
118-
//export calloc
119-
func libc_calloc(nmemb, size uintptr) unsafe.Pointer {
120-
// No difference between calloc and malloc.
121-
return libc_malloc(nmemb * size)
122-
}
123-
124-
//export realloc
125-
func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
126-
// It's hard to optimize this to expand the current buffer with our GC, but
127-
// it is theoretically possible. For now, just always allocate fresh.
128-
buf := make([]byte, size)
129-
130-
if oldPtr != nil {
131-
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok {
132-
copy(buf, oldBuf)
133-
delete(allocs, uintptr(oldPtr))
134-
} else {
135-
panic("realloc: invalid pointer")
136-
}
137-
}
138-
139-
ptr := unsafe.Pointer(&buf[0])
140-
allocs[uintptr(ptr)] = buf
141-
return ptr
142-
}

src/runtime/arch_tinygowasm_malloc.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build tinygo.wasm && !custommalloc
2+
// +build tinygo.wasm,!custommalloc
3+
4+
package runtime
5+
6+
import "unsafe"
7+
8+
// The below functions override the default allocator of wasi-libc. This ensures
9+
// code linked from other languages can allocate memory without colliding with
10+
// our GC allocations.
11+
12+
var allocs = make(map[uintptr][]byte)
13+
14+
//export malloc
15+
func libc_malloc(size uintptr) unsafe.Pointer {
16+
buf := make([]byte, size)
17+
ptr := unsafe.Pointer(&buf[0])
18+
allocs[uintptr(ptr)] = buf
19+
return ptr
20+
}
21+
22+
//export free
23+
func libc_free(ptr unsafe.Pointer) {
24+
if ptr == nil {
25+
return
26+
}
27+
if _, ok := allocs[uintptr(ptr)]; ok {
28+
delete(allocs, uintptr(ptr))
29+
} else {
30+
panic("free: invalid pointer")
31+
}
32+
}
33+
34+
//export calloc
35+
func libc_calloc(nmemb, size uintptr) unsafe.Pointer {
36+
// No difference between calloc and malloc.
37+
return libc_malloc(nmemb * size)
38+
}
39+
40+
//export realloc
41+
func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
42+
// It's hard to optimize this to expand the current buffer with our GC, but
43+
// it is theoretically possible. For now, just always allocate fresh.
44+
buf := make([]byte, size)
45+
46+
if oldPtr != nil {
47+
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok {
48+
copy(buf, oldBuf)
49+
delete(allocs, uintptr(oldPtr))
50+
} else {
51+
panic("realloc: invalid pointer")
52+
}
53+
}
54+
55+
ptr := unsafe.Pointer(&buf[0])
56+
allocs[uintptr(ptr)] = buf
57+
return ptr
58+
}

0 commit comments

Comments
 (0)