runtime: add "end of heap" to detect out of memory

This can be used in the future to trigger garbage collection. For now,
it provides a more useful error message in case the heap is completely
filled up.
Этот коммит содержится в:
Ayke van Laethem 2018-11-17 15:30:38 +01:00
родитель ef93001ab8
коммит 9181f2d4ce
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
5 изменённых файлов: 27 добавлений и 4 удалений

Просмотреть файл

@ -14,7 +14,13 @@ const TargetBits = 8
//go:extern _heap_start //go:extern _heap_start
var heapStartSymbol unsafe.Pointer var heapStartSymbol unsafe.Pointer
var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) //go:extern _heap_end
var heapEndSymbol unsafe.Pointer
var (
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(unsafe.Pointer(&heapEndSymbol))
)
// Align on a word boundary. // Align on a word boundary.
func align(ptr uintptr) uintptr { func align(ptr uintptr) uintptr {

Просмотреть файл

@ -14,7 +14,13 @@ const TargetBits = 32
//go:extern _heap_start //go:extern _heap_start
var heapStartSymbol unsafe.Pointer var heapStartSymbol unsafe.Pointer
var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) //go:extern _heap_end
var heapEndSymbol unsafe.Pointer
var (
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(unsafe.Pointer(&heapEndSymbol))
)
// Align on word boundary. // Align on word boundary.
func align(ptr uintptr) uintptr { func align(ptr uintptr) uintptr {

Просмотреть файл

@ -14,7 +14,12 @@ const TargetBits = 32
//go:extern __heap_base //go:extern __heap_base
var heapStartSymbol unsafe.Pointer var heapStartSymbol unsafe.Pointer
var heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) var (
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = (heapStart + wasmPageSize - 1) & (wasmPageSize - 1) // conservative guess: one page of heap memory
)
const wasmPageSize = 64 * 1024
// Align on word boundary. // Align on word boundary.
func align(ptr uintptr) uintptr { func align(ptr uintptr) uintptr {

Просмотреть файл

@ -20,6 +20,9 @@ func alloc(size uintptr) unsafe.Pointer {
size = align(size) size = align(size)
addr := heapptr addr := heapptr
heapptr += size heapptr += size
if heapptr >= heapEnd {
runtimePanic("out of memory")
}
for i := uintptr(0); i < uintptr(size); i += 4 { for i := uintptr(0); i < uintptr(size); i += 4 {
ptr := (*uint32)(unsafe.Pointer(addr + i)) ptr := (*uint32)(unsafe.Pointer(addr + i))
*ptr = 0 *ptr = 0

Просмотреть файл

@ -14,7 +14,10 @@ func _Cfunc_clock_gettime(clk_id uint, ts *timespec)
const heapSize = 1 * 1024 * 1024 // 1MB to start const heapSize = 1 * 1024 * 1024 // 1MB to start
var heapStart = uintptr(_Cfunc_malloc(heapSize)) var (
heapStart = uintptr(_Cfunc_malloc(heapSize))
heapEnd = heapStart + heapSize
)
type timeUnit int64 type timeUnit int64