Skip to content

Commit

Permalink
feat: convert byte slices to unix iovecs utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hayabusa-cloud committed Jul 6, 2024
1 parent 5198c92 commit 8595abf
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"unsafe"
)

type ioVec = unix.Iovec

func errFromUnixErrno(err error) error {
if err == nil {
return nil
Expand Down Expand Up @@ -48,9 +50,86 @@ func errFromUnixErrno(err error) error {
func ioVecFromBytesSlice(iov [][]byte) (addr uintptr, n int) {
vec := make([]unix.Iovec, len(iov))
for i := range len(iov) {
vec[i] = unix.Iovec{Base: &iov[i][0], Len: uint64(len(iov[i]))}
vec[i] = unix.Iovec{Base: unsafe.SliceData(iov[i]), Len: uint64(len(iov[i]))}
}
addr, n = uintptr(unsafe.Pointer(&iov)), len(iov)
addr, n = uintptr(unsafe.Pointer(unsafe.SliceData(vec))), len(vec)

return
}

func ioVecAddrLen(vec []ioVec) (addr uintptr, n int) {
addr, n = uintptr(unsafe.Pointer(unsafe.SliceData(vec))), len(vec)
return
}

func ioVecFromPicoBuffers(buffers []PicoBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range len(buffers) {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizePico}
}

return vec
}

func ioVecFromNanoBuffers(buffers []NanoBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range len(buffers) {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeNano}
}

return vec
}

func ioVecFromMicroBuffers(buffers []MicroBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeMicro}
}

return vec
}

func ioVecFromSmallBuffers(buffers []SmallBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeSmall}
}

return vec
}

func ioVecFromMediumBuffers(buffers []MediumBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeMedium}
}

return vec
}

func ioVecFromLargeBuffers(buffers []LargeBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeLarge}
}

return vec
}

func ioVecFromHugeBuffers(buffers []HugeBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeHuge}
}

return vec
}

func ioVecFromGiantBuffers(buffers []GiantBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeGiant}
}

return vec
}

0 comments on commit 8595abf

Please sign in to comment.