Browse Source

runtime: add sliceGrow function for reflect

pull/3497/head
Damian Gryski 2 years ago
committed by Ayke
parent
commit
9b86080c80
  1. 29
      src/runtime/slice.go

29
src/runtime/slice.go

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

Loading…
Cancel
Save