aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime/slice.go
diff options
context:
space:
mode:
authorDamian Gryski <[email protected]>2023-02-25 16:49:04 -0800
committerAyke <[email protected]>2023-03-03 05:21:02 -0800
commit9b86080c8015c474abc216b4033038253fc5fa18 (patch)
tree0be61b275697cc4b9d243420a92961a837250e76 /src/runtime/slice.go
parent517098c468fa1f8809cd715c70504ab18b3f8392 (diff)
downloadtinygo-9b86080c8015c474abc216b4033038253fc5fa18.tar.gz
tinygo-9b86080c8015c474abc216b4033038253fc5fa18.zip
runtime: add sliceGrow function for reflect
Diffstat (limited to 'src/runtime/slice.go')
-rw-r--r--src/runtime/slice.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/runtime/slice.go b/src/runtime/slice.go
index b58fab360..485910fa5 100644
--- a/src/runtime/slice.go
+++ b/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
+}