31 Dec 2012 (updated 31 Dec 2012 at 08:47 UTC)
»
Here is the most satisfying function I have written in years:
int init_iovec_from_circular_buffer(
struct iovec* iov, char* buf, size_t size, size_t tail, size_t head)
{
int count = 0;
if (head < tail) { // wrapped
iov->iov_base = buf + tail;
iov->iov_len = size - tail;
tail = 0; ++iov; count = 1;
}
if (tail < head) {
iov->iov_base = buf + tail;
iov->iov_len = head - tail;
++count;
}
return count;
}
It sets an
iovec array to point to the occupied part of a circular buffer, ready to pass to
writev(). Pass trivially different values for
head and
tail, and it sets up
iov to pass to
readv(). If it can be improved upon, I don't know how.
dmarti: Thank you, as always. The Xiph article was especially enlightening. Mims's article wouldn't load, although I didn't try enabling quantserve's javascript.
But Simon Doonan cannot be trusted. He is in thrall to Camille Paglia, whose metier is rhetorical tricks that rely mainly on confirmation bias. Not everything Paglia writes is false; only (1) what disagrees with people equipped to know better, or (2) what seems gratifying to believe. But that's almost everything.