return right - left;
}
+/* Advances the buffer by <adv> bytes, which means that the buffer
+ * pointer advances, and that as many bytes from in are transferred
+ * to out. The caller is responsible for ensuring that adv is always
+ * smaller than or equal to b->i. The BF_OUT_EMPTY flag is updated.
+ */
+static inline void b_adv(struct buffer *b, unsigned int adv)
+{
+ b->i -= adv;
+ b->o += adv;
+ if (b->o)
+ b->flags &= ~BF_OUT_EMPTY;
+ b->p = b_ptr(b, adv);
+}
/* Return the amount of bytes that can be written into the buffer at once,
* excluding the amount of reserved space passed in <res>, which is
{
unsigned int new_forward;
unsigned int forwarded;
+ unsigned int bytes32;
- if (!bytes)
- return 0;
- if (bytes <= (unsigned long long)buf->i) {
- buf->p = b_ptr(buf, (unsigned int)bytes);
- buf->o += bytes;
- buf->i -= bytes;
- buf->flags &= ~BF_OUT_EMPTY;
- return bytes;
+ bytes32 = bytes;
+
+ /* hint: avoid comparisons on long long for the fast case, since if the
+ * length does not fit in an unsigned it, it will never be forwarded at
+ * once anyway.
+ */
+ if (bytes <= ~0U) {
+ if (bytes32 <= buf->i) {
+ /* OK this amount of bytes might be forwarded at once */
+ if (!bytes32)
+ return 0;
+ b_adv(buf, bytes32);
+ return bytes;
+ }
}
forwarded = buf->i;
- buf->p = bi_end(buf);
- buf->o += forwarded;
- buf->i = 0;
-
- if (buf->o)
- buf->flags &= ~BF_OUT_EMPTY;
-
- if (buf->o < buffer_max_len(buf))
- buf->flags &= ~BF_FULL;
- else
- buf->flags |= BF_FULL;
+ b_adv(buf, buf->i);
/* Note: the case below is the only case where we may return
* a byte count that does not fit into a 32-bit number.
fwd = buf->to_forward;
buf->to_forward -= fwd;
}
- buf->o += fwd;
- buf->i -= fwd;
- buf->p = b_ptr(buf, fwd);
- buf->flags &= ~BF_OUT_EMPTY;
+ b_adv(buf, fwd);
}
buf->flags &= ~BF_FULL;