Skip to content

Commit c1f8c9f

Browse files
committed
syscall: remove sliceHeader, use unsafe.SliceData more
1 parent f5d0ec9 commit c1f8c9f

4 files changed

Lines changed: 13 additions & 32 deletions

File tree

src/syscall/env_libc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ func Getenv(key string) (value string, found bool) {
6666
for size := uintptr(0); ; size++ {
6767
v := *(*byte)(unsafe.Pointer(ptr))
6868
if v == 0 {
69-
src := *(*[]byte)(unsafe.Pointer(&sliceHeader{buf: raw, len: size, cap: size}))
70-
return string(src), true
69+
return string(unsafe.Slice(raw, size)), true
7170
}
7271
ptr += unsafe.Sizeof(byte(0))
7372
}

src/syscall/syscall_libc.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import (
66
"unsafe"
77
)
88

9-
type sliceHeader struct {
10-
buf *byte
11-
len uintptr
12-
cap uintptr
13-
}
14-
159
func Close(fd int) (err error) {
1610
if libc_close(int32(fd)) < 0 {
1711
err = getErrno()
@@ -58,8 +52,7 @@ func Fsync(fd int) (err error) {
5852

5953
func Readlink(path string, p []byte) (n int, err error) {
6054
data := cstring(path)
61-
buf, count := splitSlice(p)
62-
n = libc_readlink(&data[0], buf, uint(count))
55+
n = libc_readlink(&data[0], unsafe.SliceData(p), uint(len(p)))
6356
if n < 0 {
6457
err = getErrno()
6558
}
@@ -264,11 +257,6 @@ func cstring(s string) []byte {
264257
return data
265258
}
266259

267-
func splitSlice(p []byte) (buf *byte, len uintptr) {
268-
slice := (*sliceHeader)(unsafe.Pointer(&p))
269-
return slice.buf, slice.len
270-
}
271-
272260
// These two functions are provided by the runtime.
273261
func runtimeSetenv(key, value string)
274262
func runtimeUnsetenv(key string)

src/syscall/syscall_libc_default.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package syscall
44

5+
import "unsafe"
6+
57
// These are the default Read/Write/Pread/Pwrite implementations for
68
// libc-backed wasm targets that do NOT have the cooperative scheduler
79
// + wasip1 netpoll integration. They are simple pass-throughs to the
@@ -12,35 +14,31 @@ package syscall
1214
// that park the goroutine on EAGAIN; see syscall_libc_wasip1.go.
1315

1416
func Write(fd int, p []byte) (n int, err error) {
15-
buf, count := splitSlice(p)
16-
n = libc_write(int32(fd), buf, uint(count))
17+
n = libc_write(int32(fd), unsafe.SliceData(p), uint(len(p)))
1718
if n < 0 {
1819
err = getErrno()
1920
}
2021
return
2122
}
2223

2324
func Read(fd int, p []byte) (n int, err error) {
24-
buf, count := splitSlice(p)
25-
n = libc_read(int32(fd), buf, uint(count))
25+
n = libc_read(int32(fd), unsafe.SliceData(p), uint(len(p)))
2626
if n < 0 {
2727
err = getErrno()
2828
}
2929
return
3030
}
3131

3232
func Pread(fd int, p []byte, offset int64) (n int, err error) {
33-
buf, count := splitSlice(p)
34-
n = libc_pread(int32(fd), buf, uint(count), offset)
33+
n = libc_pread(int32(fd), unsafe.SliceData(p), uint(len(p)), offset)
3534
if n < 0 {
3635
err = getErrno()
3736
}
3837
return
3938
}
4039

4140
func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
42-
buf, count := splitSlice(p)
43-
n = libc_pwrite(int32(fd), buf, uint(count), offset)
41+
n = libc_pwrite(int32(fd), unsafe.SliceData(p), uint(len(p)), offset)
4442
if n < 0 {
4543
err = getErrno()
4644
}

src/syscall/syscall_libc_wasip1.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package syscall
44

55
import (
66
"internal/task"
7-
_ "unsafe" // for go:linkname
7+
"unsafe"
88
)
99

1010
// pollMode constants must mirror runtime/netpoll_wasip1.go's pollRead/
@@ -26,9 +26,8 @@ func runtime_netpoll_done(pd uintptr)
2626
// goroutine until the cooperative scheduler's pollIO wakes us, then
2727
// retry. EINTR retries immediately without parking.
2828
func Write(fd int, p []byte) (n int, err error) {
29-
buf, count := splitSlice(p)
3029
for {
31-
n = libc_write(int32(fd), buf, uint(count))
30+
n = libc_write(int32(fd), unsafe.SliceData(p), uint(len(p)))
3231
if n >= 0 {
3332
return
3433
}
@@ -45,9 +44,8 @@ func Write(fd int, p []byte) (n int, err error) {
4544
}
4645

4746
func Read(fd int, p []byte) (n int, err error) {
48-
buf, count := splitSlice(p)
4947
for {
50-
n = libc_read(int32(fd), buf, uint(count))
48+
n = libc_read(int32(fd), unsafe.SliceData(p), uint(len(p)))
5149
if n >= 0 {
5250
return
5351
}
@@ -64,9 +62,8 @@ func Read(fd int, p []byte) (n int, err error) {
6462
}
6563

6664
func Pread(fd int, p []byte, offset int64) (n int, err error) {
67-
buf, count := splitSlice(p)
6865
for {
69-
n = libc_pread(int32(fd), buf, uint(count), offset)
66+
n = libc_pread(int32(fd), unsafe.SliceData(p), uint(len(p)), offset)
7067
if n >= 0 {
7168
return
7269
}
@@ -83,9 +80,8 @@ func Pread(fd int, p []byte, offset int64) (n int, err error) {
8380
}
8481

8582
func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
86-
buf, count := splitSlice(p)
8783
for {
88-
n = libc_pwrite(int32(fd), buf, uint(count), offset)
84+
n = libc_pwrite(int32(fd), unsafe.SliceData(p), uint(len(p)), offset)
8985
if n >= 0 {
9086
return
9187
}

0 commit comments

Comments
 (0)