mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
da0161d6ab
On WebAssembly it is possible to grow the heap with the memory.grow instruction. This commit implements this feature and with that also removes the -heap-size flag that was reportedly broken (I haven't verified that). This should make it easier to use TinyGo for WebAssembly, where there was no good reason to use a fixed heap size. This commit has no effect on baremetal targets with optimizations enabled.
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
// +build gc.leaking
|
|
|
|
package runtime
|
|
|
|
// This GC implementation is the simplest useful memory allocator possible: it
|
|
// only allocates memory and never frees it. For some constrained systems, it
|
|
// may be the only memory allocator possible.
|
|
|
|
import (
|
|
"unsafe"
|
|
)
|
|
|
|
// Ever-incrementing pointer: no memory is freed.
|
|
var heapptr = heapStart
|
|
|
|
func alloc(size uintptr) unsafe.Pointer {
|
|
// TODO: this can be optimized by not casting between pointers and ints so
|
|
// much. And by using platform-native data types (e.g. *uint8 for 8-bit
|
|
// systems).
|
|
size = align(size)
|
|
addr := heapptr
|
|
heapptr += size
|
|
for heapptr >= heapEnd {
|
|
// Try to increase the heap and check again.
|
|
if growHeap() {
|
|
continue
|
|
}
|
|
// Failed to make the heap bigger, so we must really be out of memory.
|
|
runtimePanic("out of memory")
|
|
}
|
|
for i := uintptr(0); i < uintptr(size); i += 4 {
|
|
ptr := (*uint32)(unsafe.Pointer(addr + i))
|
|
*ptr = 0
|
|
}
|
|
return unsafe.Pointer(addr)
|
|
}
|
|
|
|
func free(ptr unsafe.Pointer) {
|
|
// Memory is never freed.
|
|
}
|
|
|
|
func GC() {
|
|
// No-op.
|
|
}
|
|
|
|
func KeepAlive(x interface{}) {
|
|
// Unimplemented. Only required with SetFinalizer().
|
|
}
|
|
|
|
func SetFinalizer(obj interface{}, finalizer interface{}) {
|
|
// Unimplemented.
|
|
}
|
|
|
|
func initHeap() {
|
|
// Nothing to initialize.
|
|
}
|
|
|
|
// setHeapEnd sets a new (larger) heapEnd pointer.
|
|
func setHeapEnd(newHeapEnd uintptr) {
|
|
// This "heap" is so simple that simply assigning a new value is good
|
|
// enough.
|
|
heapEnd = newHeapEnd
|
|
}
|