runtime: add sliceGrow function for reflect

Этот коммит содержится в:
Damian Gryski 2023-02-25 16:49:04 -08:00 коммит произвёл Ayke
родитель 517098c468
коммит 9b86080c80

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

@ -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
}