mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 02:57:46 +00:00
runtime: add sliceGrow function for reflect
This commit is contained in:
@@ -51,3 +51,32 @@ func sliceCopy(dst, src unsafe.Pointer, dstLen, srcLen uintptr, elemSize uintptr
|
||||
memmove(dst, src, n*elemSize)
|
||||
return int(n)
|
||||
}
|
||||
|
||||
// sliceGrow returns a new slice with space for at least newCap elements
|
||||
func sliceGrow(oldBuf unsafe.Pointer, oldLen, oldCap, newCap, elemSize uintptr) (unsafe.Pointer, uintptr, uintptr) {
|
||||
|
||||
// TODO(dgryski): sliceGrow() and sliceAppend() should be refactored to share the base growth code.
|
||||
|
||||
if oldCap >= newCap {
|
||||
// No need to grow, return the input slice.
|
||||
return oldBuf, oldLen, oldCap
|
||||
}
|
||||
|
||||
// allow nil slice
|
||||
if oldCap == 0 {
|
||||
oldCap++
|
||||
}
|
||||
|
||||
// grow capacity
|
||||
for oldCap < newCap {
|
||||
oldCap *= 2
|
||||
}
|
||||
|
||||
buf := alloc(oldCap*elemSize, nil)
|
||||
if oldLen > 0 {
|
||||
// copy any data to new slice
|
||||
memmove(buf, oldBuf, oldLen*elemSize)
|
||||
}
|
||||
|
||||
return buf, oldLen, oldCap
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user