all: use unsafe.Add instead of unsafe.Pointer(uintptr(...) + ...)

We have an optimization for this specific pattern, but it's really just
a hack. With the addition of unsafe.Add in Go 1.17 we can directly
specify the intent instead and eventually remove this special case.

The code is also easier to read.
This commit is contained in:
Ayke van Laethem
2022-12-21 16:14:36 +01:00
committed by Ron Evans
parent d98c0afbab
commit 4ec1e58aa6
29 changed files with 84 additions and 95 deletions
+3 -3
View File
@@ -61,7 +61,7 @@ func stringConcat(x, y _string) _string {
length := x.length + y.length
buf := alloc(length, nil)
memcpy(buf, unsafe.Pointer(x.ptr), x.length)
memcpy(unsafe.Pointer(uintptr(buf)+x.length), unsafe.Pointer(y.ptr), y.length)
memcpy(unsafe.Add(buf, x.length), unsafe.Pointer(y.ptr), y.length)
return _string{ptr: (*byte)(buf), length: length}
}
}
@@ -107,7 +107,7 @@ func stringFromRunes(runeSlice []rune) (s _string) {
for _, r := range runeSlice {
array, numBytes := encodeUTF8(r)
for _, c := range array[:numBytes] {
*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(s.ptr)) + index)) = c
*(*byte)(unsafe.Add(unsafe.Pointer(s.ptr), index)) = c
index++
}
}
@@ -243,7 +243,7 @@ func isContinuation(b byte) bool {
func cgo_CString(s _string) unsafe.Pointer {
buf := malloc(s.length + 1)
memcpy(buf, unsafe.Pointer(s.ptr), s.length)
*(*byte)(unsafe.Pointer(uintptr(buf) + s.length)) = 0 // trailing 0 byte
*(*byte)(unsafe.Add(buf, s.length)) = 0 // trailing 0 byte
return buf
}