Added realloc implementation to GCs

When using the latest wasi-libc I experienced a
panic on an attempt to call realloc. My first attempt to
add it to arch_tinygowasm.go was obviously not good (PR #2194). So here
is another suggestion.
This commit is contained in:
Rouven Broszeit
2021-10-22 21:30:52 +02:00
committed by Ron Evans
parent ef8c1a187d
commit 0f69d016a0
5 changed files with 41 additions and 2 deletions
+22
View File
@@ -341,6 +341,28 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
}
}
func realloc(ptr unsafe.Pointer, size uintptr) unsafe.Pointer {
if ptr == nil {
return alloc(size, nil)
}
ptrAddress := uintptr(ptr)
endOfTailAddress := blockFromAddr(ptrAddress).findNext().address()
// this might be a few bytes longer than the original size of
// ptr, because we align to full blocks of size bytesPerBlock
oldSize := endOfTailAddress - ptrAddress
if size <= oldSize {
return ptr
}
newAlloc := alloc(size, nil)
memcpy(newAlloc, ptr, oldSize)
free(ptr)
return newAlloc
}
func free(ptr unsafe.Pointer) {
// TODO: free blocks on request, when the compiler knows they're unused.
}