reflect: use SliceHeader and StringHeader variant for internal use

These variants uses an unsafe.Pointer instead of uintptr so that the
pointer/non-pointer fields match those of real slices and strings. This
may be necessary in the future once we switch to a precise garbage
collector.
This commit is contained in:
Ayke van Laethem
2021-05-22 18:20:26 +02:00
committed by Ron Evans
parent 711889bc3f
commit c8742e2b96
3 changed files with 35 additions and 20 deletions
+4 -4
View File
@@ -24,15 +24,15 @@ func Swapper(slice interface{}) func(i, j int) {
typ := v.typecode.Elem()
size := typ.Size()
header := (*SliceHeader)(v.value)
header := (*sliceHeader)(v.value)
tmp := unsafe.Pointer(&make([]byte, size)[0])
return func(i, j int) {
if uint(i) >= uint(header.Len) || uint(j) >= uint(header.Len) {
if uint(i) >= uint(header.len) || uint(j) >= uint(header.len) {
panic("reflect: slice index out of range")
}
val1 := unsafe.Pointer(header.Data + uintptr(i)*size)
val2 := unsafe.Pointer(header.Data + uintptr(j)*size)
val1 := unsafe.Pointer(uintptr(header.data) + uintptr(i)*size)
val2 := unsafe.Pointer(uintptr(header.data) + uintptr(j)*size)
memcpy(tmp, val1, size)
memcpy(val1, val2, size)
memcpy(val2, tmp, size)