basic/io-util: invert return value from IOVEC_INCREMENT()

We would try to return a value that could be nonzero only if the kernel
reported writing more bytes than we gave to it, hopefully a rare occurence.
Instead, assert that this doesn't happen.

Instead, return true if we got to the end of the iovec array. The caller
can use this information to know that the whole iovec array was written.
This allows one loop to be dropped in write_to_syslog().

Also drop _unlikely_: this function is called with very short arrays, and
it *is* likely that we trigger this condition. Let's just let the compiler
generate normal code without giving it a potentially false hint.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-05-04 14:57:49 +02:00
parent 0f6bb1ed05
commit 32f65484d7
2 changed files with 14 additions and 9 deletions
+11 -6
View File
@@ -33,14 +33,17 @@ static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) {
return r;
}
static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
size_t j;
static inline bool IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
/* Returns true if there is nothing else to send (bytes written cover all of the iovec),
* false if there's still work to do. */
for (j = 0; j < n; j++) {
for (size_t j = 0; j < n; j++) {
size_t sub;
if (_unlikely_(k <= 0))
break;
if (i[j].iov_len == 0)
continue;
if (k == 0)
return false;
sub = MIN(i[j].iov_len, k);
i[j].iov_len -= sub;
@@ -48,7 +51,9 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
k -= sub;
}
return k;
assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
* or the kernel reported writing more bytes than sent. */
return true;
}
static inline bool FILE_SIZE_VALID(uint64_t l) {
+3 -3
View File
@@ -491,11 +491,11 @@ static int write_to_syslog(
if (n < 0)
return -errno;
if (!syslog_is_stream ||
(size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
if (!syslog_is_stream)
break;
IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
if (IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n))
break;
}
return 1;