diff options
| author | Bryan C. Mills <bcmills@google.com> | 2020-04-30 17:05:59 -0400 |
|---|---|---|
| committer | Bryan C. Mills <bcmills@google.com> | 2020-05-01 02:31:29 +0000 |
| commit | da382a3978d3db2380c7e9a69207545562dfd727 (patch) | |
| tree | d8105be3c7281e8bdf1c475f7c7866fe21aac806 /src/syscall | |
| parent | 4c78d54fdd9ffc81c15ffc3c4a2946f89d4fca22 (diff) | |
| download | go-da382a3978d3db2380c7e9a69207545562dfd727.tar.xz | |
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 <iant@golang.org>
Diffstat (limited to 'src/syscall')
| -rw-r--r-- | src/syscall/syscall_unix.go | 16 |
1 files changed, 7 insertions, 9 deletions
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] |
