From da382a3978d3db2380c7e9a69207545562dfd727 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 30 Apr 2020 17:05:59 -0400 Subject: internal/unsafeheader: consolidate stringHeader and sliceHeader declarations into an internal package The new package "internal/unsafeheader" depends only on "unsafe", and provides declarations equivalent to reflect.StringHeader and reflect.SliceHeader but with Data fields of the proper unsafe.Pointer type (instead of uintptr). Unlike the types it replaces, the "internal/unsafeheader" package has a regression test to ensure that its header types remain equivalent to the declarations provided by the "reflect" package. Since "internal/unsafeheader" has almost no dependencies, it can be used in other low-level packages such as "syscall" and "reflect". This change is based on the corresponding x/sys change in CL 231177. Fixes #37805 Updates #19367 Change-Id: I7a6d93ef8dd6e235bcab94e7c47270aad047af31 Reviewed-on: https://go-review.googlesource.com/c/go/+/231223 Reviewed-by: Ian Lance Taylor --- src/syscall/syscall_unix.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/syscall') diff --git a/src/syscall/syscall_unix.go b/src/syscall/syscall_unix.go index b8b8a7c111..56abce19cd 100644 --- a/src/syscall/syscall_unix.go +++ b/src/syscall/syscall_unix.go @@ -9,6 +9,7 @@ package syscall import ( "internal/oserror" "internal/race" + "internal/unsafeheader" "runtime" "sync" "unsafe" @@ -60,15 +61,12 @@ func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (d return nil, errno } - // Slice memory layout - var sl = struct { - addr uintptr - len int - cap int - }{addr, length, length} - - // Use unsafe to turn sl into a []byte. - b := *(*[]byte)(unsafe.Pointer(&sl)) + // Use unsafe to turn addr into a []byte. + var b []byte + hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b)) + hdr.Data = unsafe.Pointer(addr) + hdr.Cap = length + hdr.Len = length // Register mapping in m and return it. p := &b[cap(b)-1] -- cgit v1.3-5-g45d5