readv_posix.go 844 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build !windows && !wasm && !illumos
  2. // +build !windows,!wasm,!illumos
  3. package buf
  4. import (
  5. "syscall"
  6. "unsafe"
  7. )
  8. type posixReader struct {
  9. iovecs []syscall.Iovec
  10. }
  11. func (r *posixReader) Init(bs []*Buffer) {
  12. iovecs := r.iovecs
  13. if iovecs == nil {
  14. iovecs = make([]syscall.Iovec, 0, len(bs))
  15. }
  16. for idx, b := range bs {
  17. iovecs = append(iovecs, syscall.Iovec{
  18. Base: &(b.v[0]),
  19. })
  20. iovecs[idx].SetLen(int(Size))
  21. }
  22. r.iovecs = iovecs
  23. }
  24. func (r *posixReader) Read(fd uintptr) int32 {
  25. n, _, e := syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&r.iovecs[0])), uintptr(len(r.iovecs)))
  26. if e != 0 {
  27. return -1
  28. }
  29. return int32(n)
  30. }
  31. func (r *posixReader) Clear() {
  32. for idx := range r.iovecs {
  33. r.iovecs[idx].Base = nil
  34. }
  35. r.iovecs = r.iovecs[:0]
  36. }
  37. func newMultiReader() multiReader {
  38. return &posixReader{}
  39. }