unix: merge Linux readv/writev implementation with Darwin/OpenBSD

This reduces some code duplication.

Change-Id: Ib648f90a26960d0bc25d2fe27e9b3fb1419ac02d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/777161
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor
2026-05-18 07:51:52 -07:00
committed by Gopher Robot
parent e4444cbaaa
commit 99666ae32e
4 changed files with 32 additions and 106 deletions
+15 -7
View File
@@ -2,17 +2,21 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin || openbsd
//go:build darwin || linux || openbsd
package unix
import "unsafe"
// minIovec is the size of the small initial allocation used by
// Readv, Writev, etc.
//
// This small allocation gets stack allocated, which lets the
// common use case of len(iovs) <= minIovec avoid more expensive
// heap allocations.
const minIovec = 8
// appendBytes converts bs to Iovecs and appends them to vecs.
func appendBytes(vecs []Iovec, bs [][]byte) []Iovec {
for _, b := range bs {
var v Iovec
@@ -27,7 +31,9 @@ func appendBytes(vecs []Iovec, bs [][]byte) []Iovec {
return vecs
}
func writevRacedetect(iovecs []Iovec, n int) {
// writevRaceDetect tells the race detector that the program
// has read the first n bytes stored in iovecs.
func writevRaceDetect(iovecs []Iovec, n int) {
if !raceenabled {
return
}
@@ -40,7 +46,9 @@ func writevRacedetect(iovecs []Iovec, n int) {
}
}
func readvRacedetect(iovecs []Iovec, n int, err error) {
// readvRaceDetect tells the race detector that the program
// has written to the first n bytes stored in iovecs.
func readvRaceDetect(iovecs []Iovec, n int, err error) {
if !raceenabled {
return
}
@@ -60,7 +68,7 @@ func Readv(fd int, iovs [][]byte) (n int, err error) {
iovecs := make([]Iovec, 0, minIovec)
iovecs = appendBytes(iovecs, iovs)
n, err = readv(fd, iovecs)
readvRacedetect(iovecs, n, err)
readvRaceDetect(iovecs, n, err)
return n, err
}
@@ -68,7 +76,7 @@ func Preadv(fd int, iovs [][]byte, offset int64) (n int, err error) {
iovecs := make([]Iovec, 0, minIovec)
iovecs = appendBytes(iovecs, iovs)
n, err = preadv(fd, iovecs, offset)
readvRacedetect(iovecs, n, err)
readvRaceDetect(iovecs, n, err)
return n, err
}
@@ -79,7 +87,7 @@ func Writev(fd int, iovs [][]byte) (n int, err error) {
raceReleaseMerge(unsafe.Pointer(&ioSync))
}
n, err = writev(fd, iovecs)
writevRacedetect(iovecs, n)
writevRaceDetect(iovecs, n)
return n, err
}
@@ -90,6 +98,6 @@ func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) {
raceReleaseMerge(unsafe.Pointer(&ioSync))
}
n, err = pwritev(fd, iovecs, offset)
writevRacedetect(iovecs, n)
writevRaceDetect(iovecs, n)
return n, err
}