cmd/cgo: How to call a C library api like writev
via cgo without allocation and copying of the whole byte slice vector?
#44991
Labels
writev
via cgo without allocation and copying of the whole byte slice vector?
#44991
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
All version.
What operating system and processor architecture are you using (
go env
)?go env
OutputQuestion
Say here is a C api like
ssize_t writev(const struct iovec *iov, int iovcnt)
which the definition of iovec is like below:For C api which like
ssize_t write(const void *buf, size_t nbyte)
, the solution would be quite straight forward:But how to do
writev
without extra memory allocation or copying of the whole byte slice vector?Does the codes above is right?
I have read cgo's docs carefully, and here is a constraint from https://golang.org/cmd/cgo/#hdr-Passing_pointers:
And https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md#proposal:
If the Go memory pointed by
unsafe.Pointer(&iovec[0])
contains pointer which points to these byte slice members of bsv, so it would be Violation of the cgo constraint above. And that means you could not call Cwritev
style API without allocation and copying of the whole vector.Please correct me if I get something wrong. Thanks a lot :-D
The text was updated successfully, but these errors were encountered: