diff --git a/app/internal/windows/windows.go b/app/internal/windows/windows.go index e2f5c902..3b4c9615 100644 --- a/app/internal/windows/windows.go +++ b/app/internal/windows/windows.go @@ -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) { diff --git a/app/os_android.go b/app/os_android.go index 6200133e..982bae08 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -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) } diff --git a/app/os_windows.go b/app/os_windows.go index 47624846..2563a9b8 100644 --- a/app/os_windows.go +++ b/app/os_windows.go @@ -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 { diff --git a/gpu/gpu.go b/gpu/gpu.go index 0bd15c69..4bb9fb9a 100644 --- a/gpu/gpu.go +++ b/gpu/gpu.go @@ -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) diff --git a/gpu/internal/d3d11/d3d11_windows.go b/gpu/internal/d3d11/d3d11_windows.go index 08698c33..a22510c0 100644 --- a/gpu/internal/d3d11/d3d11_windows.go +++ b/gpu/internal/d3d11/d3d11_windows.go @@ -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) } diff --git a/internal/byteslice/byteslice.go b/internal/byteslice/byteslice.go index 26ebdb28..e84ed90b 100644 --- a/internal/byteslice/byteslice.go +++ b/internal/byteslice/byteslice.go @@ -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()] }