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)) _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)) r, _, err := _GlobalLock.Call(uintptr(h))
if r == 0 { 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) { func GlobalUnlock(h syscall.Handle) {
+1 -6
View File
@@ -126,7 +126,6 @@ import (
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"sync" "sync"
@@ -1252,11 +1251,7 @@ func goString(env *C.JNIEnv, str C.jstring) string {
} }
strlen := C.jni_GetStringLength(env, C.jstring(str)) strlen := C.jni_GetStringLength(env, C.jstring(str))
chars := C.jni_GetStringChars(env, C.jstring(str)) chars := C.jni_GetStringChars(env, C.jstring(str))
var utf16Chars []uint16 utf16Chars := unsafe.Slice((*uint16)(unsafe.Pointer(chars)), strlen)
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&utf16Chars))
hdr.Data = uintptr(unsafe.Pointer(chars))
hdr.Cap = int(strlen)
hdr.Len = int(strlen)
utf8 := utf16.Decode(utf16Chars) utf8 := utf16.Decode(utf16Chars)
return string(utf8) return string(utf8)
} }
+1 -6
View File
@@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"image" "image"
"reflect"
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
@@ -690,11 +689,7 @@ func (w *window) writeClipboard(s string) error {
windows.GlobalFree(mem) windows.GlobalFree(mem)
return err return err
} }
var u16v []uint16 u16v := unsafe.Slice((*uint16)(ptr), len(u16))
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&u16v))
hdr.Data = ptr
hdr.Cap = len(u16)
hdr.Len = len(u16)
copy(u16v, u16) copy(u16v, u16)
windows.GlobalUnlock(mem) windows.GlobalUnlock(mem)
if err := windows.SetClipboardData(windows.CF_UNICODETEXT, mem); err != nil { 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. // Determine the size of the uniforms structure, *uniforms.
size := ref.Elem().Type().Size() size := ref.Elem().Type().Size()
// Map the uniforms structure as a byte slice. // 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)) ubuf, err := b.NewBuffer(driver.BufferBindingUniforms, len(ptr))
if err != nil { if err != nil {
panic(err) panic(err)
+1 -7
View File
@@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"image" "image"
"math" "math"
"reflect"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@@ -850,10 +849,5 @@ func toBlendFactor(f driver.BlendFactor) (uint32, uint32) {
// sliceOf returns a slice from a (native) pointer. // sliceOf returns a slice from a (native) pointer.
func sliceOf(ptr uintptr, cap int) []byte { func sliceOf(ptr uintptr, cap int) []byte {
var data []byte return unsafe.Slice((*byte)(unsafe.Pointer(ptr)), cap)
h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
h.Data = ptr
h.Cap = cap
h.Len = cap
return data
} }
+6 -15
View File
@@ -11,14 +11,9 @@ import (
// Struct returns a byte slice view of a struct. // Struct returns a byte slice view of a struct.
func Struct(s interface{}) []byte { func Struct(s interface{}) []byte {
v := reflect.ValueOf(s).Elem() v := reflect.ValueOf(s)
sz := int(v.Type().Size()) sz := int(v.Elem().Type().Size())
var res []byte return unsafe.Slice((*byte)(unsafe.Pointer(v.Pointer())), sz)
h := (*reflect.SliceHeader)(unsafe.Pointer(&res))
h.Data = uintptr(unsafe.Pointer(v.UnsafeAddr()))
h.Cap = sz
h.Len = sz
return res
} }
// Uint32 returns a byte slice view of a uint32 slice. // Uint32 returns a byte slice view of a uint32 slice.
@@ -28,7 +23,7 @@ func Uint32(s []uint32) []byte {
return nil return nil
} }
blen := n * int(unsafe.Sizeof(s[0])) 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. // Slice returns a byte slice view of a slice.
@@ -36,10 +31,6 @@ func Slice(s interface{}) []byte {
v := reflect.ValueOf(s) v := reflect.ValueOf(s)
first := v.Index(0) first := v.Index(0)
sz := int(first.Type().Size()) sz := int(first.Type().Size())
var res []byte res := unsafe.Slice((*byte)(unsafe.Pointer(v.Pointer())), sz*v.Cap())
h := (*reflect.SliceHeader)(unsafe.Pointer(&res)) return res[:sz*v.Len()]
h.Data = first.UnsafeAddr()
h.Cap = v.Cap() * sz
h.Len = v.Len() * sz
return res
} }