// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin || linux || openbsd

package unix

import 

// 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 ( []Iovec,  [][]byte) []Iovec {
	for ,  := range  {
		var  Iovec
		.SetLen(len())
		if len() > 0 {
			.Base = &[0]
		} else {
			.Base = (*byte)(unsafe.Pointer(&_zero))
		}
		 = append(, )
	}
	return 
}

// writevRaceDetect tells the race detector that the program
// has read the first n bytes stored in iovecs.
func ( []Iovec,  int) {
	if !raceenabled {
		return
	}
	for  := 0;  > 0 &&  < len(); ++ {
		 := min(int([].Len), )
		 -= 
		if  > 0 {
			raceReadRange(unsafe.Pointer([].Base), )
		}
	}
}

// readvRaceDetect tells the race detector that the program
// has written to the first n bytes stored in iovecs.
func ( []Iovec,  int,  error) {
	if !raceenabled {
		return
	}
	for  := 0;  > 0 &&  < len(); ++ {
		 := min(int([].Len), )
		 -= 
		if  > 0 {
			raceWriteRange(unsafe.Pointer([].Base), )
		}
	}
	if  == nil {
		raceAcquire(unsafe.Pointer(&ioSync))
	}
}

func ( int,  [][]byte) ( int,  error) {
	 := make([]Iovec, 0, minIovec)
	 = appendBytes(, )
	,  = readv(, )
	readvRaceDetect(, , )
	return , 
}

func ( int,  [][]byte,  int64) ( int,  error) {
	 := make([]Iovec, 0, minIovec)
	 = appendBytes(, )
	,  = preadv(, , )
	readvRaceDetect(, , )
	return , 
}

func ( int,  [][]byte) ( int,  error) {
	 := make([]Iovec, 0, minIovec)
	 = appendBytes(, )
	if raceenabled {
		raceReleaseMerge(unsafe.Pointer(&ioSync))
	}
	,  = writev(, )
	writevRaceDetect(, )
	return , 
}

func ( int,  [][]byte,  int64) ( int,  error) {
	 := make([]Iovec, 0, minIovec)
	 = appendBytes(, )
	if raceenabled {
		raceReleaseMerge(unsafe.Pointer(&ioSync))
	}
	,  = pwritev(, , )
	writevRaceDetect(, )
	return , 
}