readv_posix.go 788 B

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