From 15a4afb22a439bcc388d83754c031f5ba00e352d Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 17 Nov 2018 14:11:58 +0100 Subject: [PATCH] runtime: unify GC interface Make sure every to-be-implemented GC can use the same interface. As a result, a 1MB chunk of RAM is allocated on Unix systems on init instead of allocating on demand. --- src/runtime/arch_amd64.go | 5 +++++ src/runtime/arch_avr.go | 10 +++++++++- src/runtime/arch_tinygoarm.go | 9 ++++++++- src/runtime/arch_wasm.go | 9 ++++++++- src/runtime/gc.go | 5 ++--- src/runtime/runtime_avr.go | 6 ------ src/runtime/runtime_tinygoarm.go | 5 ----- src/runtime/runtime_unix.go | 30 +++++------------------------- src/runtime/runtime_wasm.go | 5 ----- 9 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/runtime/arch_amd64.go b/src/runtime/arch_amd64.go index a69abe46..b9d32f91 100644 --- a/src/runtime/arch_amd64.go +++ b/src/runtime/arch_amd64.go @@ -6,3 +6,8 @@ const GOARCH = "amd64" // The bitness of the CPU (e.g. 8, 32, 64). const TargetBits = 64 + +// Align on word boundary. +func align(ptr uintptr) uintptr { + return (ptr + 7) &^ 7 +} diff --git a/src/runtime/arch_avr.go b/src/runtime/arch_avr.go index 4964d846..778ae4d7 100644 --- a/src/runtime/arch_avr.go +++ b/src/runtime/arch_avr.go @@ -12,4 +12,12 @@ const GOARCH = "avr" const TargetBits = 8 //go:extern _heap_start -var heapStart unsafe.Pointer +var heapStartSymbol unsafe.Pointer + +var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) + +// Align on a word boundary. +func align(ptr uintptr) uintptr { + // No alignment necessary on the AVR. + return ptr +} diff --git a/src/runtime/arch_tinygoarm.go b/src/runtime/arch_tinygoarm.go index 2a517cfc..cb8feed0 100644 --- a/src/runtime/arch_tinygoarm.go +++ b/src/runtime/arch_tinygoarm.go @@ -12,4 +12,11 @@ const GOARCH = "arm" const TargetBits = 32 //go:extern _heap_start -var heapStart unsafe.Pointer +var heapStartSymbol unsafe.Pointer + +var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) + +// Align on word boundary. +func align(ptr uintptr) uintptr { + return (ptr + 3) &^ 3 +} diff --git a/src/runtime/arch_wasm.go b/src/runtime/arch_wasm.go index 4e9fa06a..e893f889 100644 --- a/src/runtime/arch_wasm.go +++ b/src/runtime/arch_wasm.go @@ -12,4 +12,11 @@ const GOARCH = "wasm" const TargetBits = 32 //go:extern __heap_base -var heapStart unsafe.Pointer +var heapStartSymbol unsafe.Pointer + +var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) + +// Align on word boundary. +func align(ptr uintptr) uintptr { + return (ptr + 3) &^ 3 +} diff --git a/src/runtime/gc.go b/src/runtime/gc.go index 90df5859..10cdc704 100644 --- a/src/runtime/gc.go +++ b/src/runtime/gc.go @@ -1,12 +1,11 @@ -// +build !linux - package runtime import ( "unsafe" ) -var heapptr = uintptr(unsafe.Pointer(&heapStart)) +// 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 diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 7dfa15d2..e01d2dce 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -120,9 +120,3 @@ func abort() { sleepWDT(WDT_PERIOD_2S) } } - -// Align on a word boundary. -func align(ptr uintptr) uintptr { - // No alignment necessary on the AVR. - return ptr -} diff --git a/src/runtime/runtime_tinygoarm.go b/src/runtime/runtime_tinygoarm.go index 9003c893..a21ee352 100644 --- a/src/runtime/runtime_tinygoarm.go +++ b/src/runtime/runtime_tinygoarm.go @@ -46,11 +46,6 @@ func abort() { } } -// Align on word boundary. -func align(ptr uintptr) uintptr { - return (ptr + 3) &^ 3 -} - // Implement memset for compiler-rt. //go:export memset func memset(ptr unsafe.Pointer, c byte, size uintptr) { diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index bc5f9803..9a3a761c 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -8,10 +8,14 @@ import ( func _Cfunc_putchar(c int) int func _Cfunc_usleep(usec uint) int -func _Cfunc_calloc(nmemb, size uintptr) unsafe.Pointer +func _Cfunc_malloc(size uintptr) unsafe.Pointer func _Cfunc_abort() func _Cfunc_clock_gettime(clk_id uint, ts *timespec) +const heapSize = 1 * 1024 * 1024 // 1MB to start + +var heapStart = uintptr(_Cfunc_malloc(heapSize)) + type timeUnit int64 const tickMicros = 1 @@ -62,27 +66,3 @@ func abort() { // panic() exits with exit code 2. _Cfunc_abort() } - -func alloc(size uintptr) unsafe.Pointer { - buf := _Cfunc_calloc(1, size) - if buf == nil { - runtimePanic("cannot allocate memory") - } - return buf -} - -func free(ptr unsafe.Pointer) { - //C.free(ptr) // TODO -} - -func GC() { - // Unimplemented. -} - -func KeepAlive(x interface{}) { - // Unimplemented. Only required with SetFinalizer(). -} - -func SetFinalizer(obj interface{}, finalizer interface{}) { - // Unimplemented. -} diff --git a/src/runtime/runtime_wasm.go b/src/runtime/runtime_wasm.go index 514ab8c8..ffc8c4cf 100644 --- a/src/runtime/runtime_wasm.go +++ b/src/runtime/runtime_wasm.go @@ -44,11 +44,6 @@ func ticks() timeUnit { return timestamp } -// Align on word boundary. -func align(ptr uintptr) uintptr { - return (ptr + 3) &^ 3 -} - // Abort executes the wasm 'unreachable' instruction. func abort() { trap()