readv_posix.go 823 B

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