1 Star 1 Fork 0

czy/go-ceph

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
iovec.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Sven Anderson 提交于 2021-01-17 04:14 +08:00 . cutil: use SyncBuffer for iovec type
package cutil
/*
#include <stdlib.h>
#include <sys/uio.h>
*/
import "C"
import (
"unsafe"
)
// Iovec is a slice of iovec structs. Might have allocated C memory, so it must
// be freed with the Free() method.
type Iovec struct {
iovec []C.struct_iovec
sbs []*SyncBuffer
}
const iovecSize = C.sizeof_struct_iovec
// ByteSlicesToIovec creates an Iovec and links it to Go buffers in data.
func ByteSlicesToIovec(data [][]byte) (v Iovec) {
n := len(data)
iovecMem := C.malloc(iovecSize * C.size_t(n))
v.iovec = (*[MaxIdx]C.struct_iovec)(iovecMem)[:n:n]
for i, b := range data {
sb := NewSyncBuffer(CPtr(&v.iovec[i].iov_base), b)
v.sbs = append(v.sbs, sb)
v.iovec[i].iov_len = C.size_t(len(b))
}
return
}
// Sync makes sure the slices contain the same as the C buffers
func (v *Iovec) Sync() {
for _, sb := range v.sbs {
sb.Sync()
}
}
// Pointer returns a pointer to the iovec
func (v *Iovec) Pointer() unsafe.Pointer {
return unsafe.Pointer(&v.iovec[0])
}
// Len returns a pointer to the iovec
func (v *Iovec) Len() int {
return len(v.iovec)
}
// Free the C memory in the Iovec.
func (v *Iovec) Free() {
for _, sb := range v.sbs {
sb.Release()
}
if len(v.iovec) != 0 {
C.free(unsafe.Pointer(&v.iovec[0]))
}
v.iovec = nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/czy233/go-ceph.git
git@gitee.com:czy233/go-ceph.git
czy233
go-ceph
go-ceph
v0.14.2

搜索帮助