all: replace unsafe slice operations with unsafe.Slice

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-03-02 15:37:34 +01:00
parent af8ca96414
commit fea2f888bb
6 changed files with 13 additions and 38 deletions
+3 -3
View File
@@ -619,12 +619,12 @@ func GlobalFree(h syscall.Handle) {
_GlobalFree.Call(uintptr(h))
}
func GlobalLock(h syscall.Handle) (uintptr, error) {
func GlobalLock(h syscall.Handle) (unsafe.Pointer, error) {
r, _, err := _GlobalLock.Call(uintptr(h))
if r == 0 {
return 0, fmt.Errorf("GlobalLock: %v", err)
return nil, fmt.Errorf("GlobalLock: %v", err)
}
return r, nil
return unsafe.Pointer(r), nil
}
func GlobalUnlock(h syscall.Handle) {
+1 -6
View File
@@ -126,7 +126,6 @@ import (
"math"
"os"
"path/filepath"
"reflect"
"runtime"
"runtime/debug"
"sync"
@@ -1252,11 +1251,7 @@ func goString(env *C.JNIEnv, str C.jstring) string {
}
strlen := C.jni_GetStringLength(env, C.jstring(str))
chars := C.jni_GetStringChars(env, C.jstring(str))
var utf16Chars []uint16
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&utf16Chars))
hdr.Data = uintptr(unsafe.Pointer(chars))
hdr.Cap = int(strlen)
hdr.Len = int(strlen)
utf16Chars := unsafe.Slice((*uint16)(unsafe.Pointer(chars)), strlen)
utf8 := utf16.Decode(utf16Chars)
return string(utf8)
}
+1 -6
View File
@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"image"
"reflect"
"runtime"
"sort"
"strings"
@@ -690,11 +689,7 @@ func (w *window) writeClipboard(s string) error {
windows.GlobalFree(mem)
return err
}
var u16v []uint16
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&u16v))
hdr.Data = ptr
hdr.Cap = len(u16)
hdr.Len = len(u16)
u16v := unsafe.Slice((*uint16)(ptr), len(u16))
copy(u16v, u16)
windows.GlobalUnlock(mem)
if err := windows.SetClipboardData(windows.CF_UNICODETEXT, mem); err != nil {
+1 -1
View File
@@ -1172,7 +1172,7 @@ func newUniformBuffer(b driver.Device, uniformBlock interface{}) *uniformBuffer
// Determine the size of the uniforms structure, *uniforms.
size := ref.Elem().Type().Size()
// Map the uniforms structure as a byte slice.
ptr := (*[1 << 30]byte)(unsafe.Pointer(ref.Pointer()))[:size:size]
ptr := unsafe.Slice((*byte)(unsafe.Pointer(ref.Pointer())), size)
ubuf, err := b.NewBuffer(driver.BufferBindingUniforms, len(ptr))
if err != nil {
panic(err)
+1 -7
View File
@@ -7,7 +7,6 @@ import (
"fmt"
"image"
"math"
"reflect"
"unsafe"
"golang.org/x/sys/windows"
@@ -850,10 +849,5 @@ func toBlendFactor(f driver.BlendFactor) (uint32, uint32) {
// sliceOf returns a slice from a (native) pointer.
func sliceOf(ptr uintptr, cap int) []byte {
var data []byte
h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
h.Data = ptr
h.Cap = cap
h.Len = cap
return data
return unsafe.Slice((*byte)(unsafe.Pointer(ptr)), cap)
}
+6 -15
View File
@@ -11,14 +11,9 @@ import (
// Struct returns a byte slice view of a struct.
func Struct(s interface{}) []byte {
v := reflect.ValueOf(s).Elem()
sz := int(v.Type().Size())
var res []byte
h := (*reflect.SliceHeader)(unsafe.Pointer(&res))
h.Data = uintptr(unsafe.Pointer(v.UnsafeAddr()))
h.Cap = sz
h.Len = sz
return res
v := reflect.ValueOf(s)
sz := int(v.Elem().Type().Size())
return unsafe.Slice((*byte)(unsafe.Pointer(v.Pointer())), sz)
}
// Uint32 returns a byte slice view of a uint32 slice.
@@ -28,7 +23,7 @@ func Uint32(s []uint32) []byte {
return nil
}
blen := n * int(unsafe.Sizeof(s[0]))
return (*[1 << 30]byte)(unsafe.Pointer(&s[0]))[:blen:blen]
return unsafe.Slice((*byte)(unsafe.Pointer(&s[0])), blen)
}
// Slice returns a byte slice view of a slice.
@@ -36,10 +31,6 @@ func Slice(s interface{}) []byte {
v := reflect.ValueOf(s)
first := v.Index(0)
sz := int(first.Type().Size())
var res []byte
h := (*reflect.SliceHeader)(unsafe.Pointer(&res))
h.Data = first.UnsafeAddr()
h.Cap = v.Cap() * sz
h.Len = v.Len() * sz
return res
res := unsafe.Slice((*byte)(unsafe.Pointer(v.Pointer())), sz*v.Cap())
return res[:sz*v.Len()]
}