MINOR: buffer: use b_room() to determine available space in a buffer
We used to have variations around buffer_total_space() and
size-buffer_len() or size-b_data(). Let's simplify all this. buffer_len()
was also removed as not used anymore.
diff --git a/include/common/buffer.h b/include/common/buffer.h
index 2e4d165..44b1883 100644
--- a/include/common/buffer.h
+++ b/include/common/buffer.h
@@ -58,11 +58,8 @@
/*****************************************************************/
-/* Return the buffer's length in bytes by summing the input and the output */
-static inline int buffer_len(const struct buffer *buf)
-{
- return buf->i + buf->o;
-}
+
+/***** FIXME: OLD API BELOW *****/
/* Return non-zero only if the buffer is not empty */
static inline int buffer_not_empty(const struct buffer *buf)
@@ -109,14 +106,6 @@
return ptr;
}
-/* Return the maximum amount of bytes that can be written into the buffer,
- * including reserved space which may be overwritten.
- */
-static inline int buffer_total_space(const struct buffer *buf)
-{
- return buf->size - buffer_len(buf);
-}
-
/* Returns the amount of byte that can be written starting from <p> into the
* input buffer at once, including reserved space which may be overwritten.
* This is used by Lua to insert data in the input side just before the other
@@ -226,7 +215,7 @@
*/
static inline void bo_putchr(struct buffer *b, char c)
{
- if (buffer_len(b) == b->size)
+ if (b_data(b) == b->size)
return;
*b->p = c;
b->p = b_peek(b, b->o + 1);
@@ -239,7 +228,7 @@
*/
static inline int bo_putblk(struct buffer *b, const char *blk, int len)
{
- int cur_len = buffer_len(b);
+ int cur_len = b_data(b);
int half;
if (len > b->size - cur_len)
@@ -285,7 +274,7 @@
*/
static inline void bi_putchr(struct buffer *b, char c)
{
- if (buffer_len(b) == b->size)
+ if (b_data(b) == b->size)
return;
*b_tail(b) = c;
b->i++;
@@ -297,7 +286,7 @@
*/
static inline int bi_putblk(struct buffer *b, const char *blk, int len)
{
- int cur_len = buffer_len(b);
+ int cur_len = b_data(b);
int half;
if (len > b->size - cur_len)
@@ -532,7 +521,7 @@
struct ist r = ist;
char *p;
- if (r.len > (size_t)(b->size - b_data(b)))
+ if (r.len > (size_t)b_room(b))
return r.len < b->size ? 0 : -1;
p = b_tail(b);
@@ -561,7 +550,7 @@
struct ist r = ist;
char *p;
- if (r.len > (size_t)(b->size - b_data(b)))
+ if (r.len > (size_t)b_room(b))
return r.len < b->size ? 0 : -1;
p = b_tail(b);
diff --git a/src/channel.c b/src/channel.c
index b3382a7..c5a51f7 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -151,7 +151,7 @@
return -3;
max = channel_recv_limit(chn);
- if (unlikely(len > max - buffer_len(chn->buf))) {
+ if (unlikely(len > max - b_data(chn->buf))) {
/* we can't write this chunk right now because the buffer is
* almost full or because the block is too large. Return the
* available space or -2 if impossible.
diff --git a/src/checks.c b/src/checks.c
index a499f23..4c777b7 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -2670,7 +2670,7 @@
if (check->bo->o &&
(&check->current_step->list == head ||
check->current_step->action != TCPCHK_ACT_SEND ||
- check->current_step->string_len >= buffer_total_space(check->bo))) {
+ check->current_step->string_len >= b_room(check->bo))) {
int ret;
__cs_want_send(cs);
@@ -2869,7 +2869,7 @@
}
/* do not try to send if there is no space */
- if (check->current_step->string_len >= buffer_total_space(check->bo))
+ if (check->current_step->string_len >= b_room(check->bo))
continue;
bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
diff --git a/src/compression.c b/src/compression.c
index 567827d..397fd94 100644
--- a/src/compression.c
+++ b/src/compression.c
@@ -222,7 +222,7 @@
static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
{
char *out_data = b_tail(out);
- int out_len = out->size - buffer_len(out);
+ int out_len = b_room(out);
if (out_len < in_len)
return -1;
@@ -570,7 +570,7 @@
int ret;
z_stream *strm = &comp_ctx->strm;
char *out_data = b_tail(out);
- int out_len = out->size - buffer_len(out);
+ int out_len = b_room(out);
if (in_len <= 0)
return 0;
@@ -603,13 +603,13 @@
strm->next_in = NULL;
strm->avail_in = 0;
strm->next_out = (unsigned char *)b_tail(out);
- strm->avail_out = out->size - buffer_len(out);
+ strm->avail_out = b_room(out);
ret = deflate(strm, flag);
if (ret != Z_OK && ret != Z_STREAM_END)
return -1;
- out_len = (out->size - buffer_len(out)) - strm->avail_out;
+ out_len = b_room(out) - strm->avail_out;
out->i += out_len;
/* compression limit */
diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c
index 99b18cc..9365364 100644
--- a/src/flt_http_comp.c
+++ b/src/flt_http_comp.c
@@ -203,7 +203,7 @@
if (msg->flags & HTTP_MSGF_TE_CHNK) {
int block;
- len = MIN(tmpbuf->size - buffer_len(tmpbuf), len);
+ len = MIN(b_room(tmpbuf), len);
c_adv(chn, *nxt);
block = ci_contig_data(chn);
@@ -608,7 +608,7 @@
* at least 8 bytes for the gzip trailer (crc+len), plus a possible
* plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
*/
- if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
+ if (b_room(in) < 20 + 5 * ((in->i + 32767) >> 15))
return -1;
/* prepare an empty output buffer in which we reserve enough room for
@@ -641,7 +641,7 @@
* data, and the available output buffer size. The compressors are
* assumed to be able to process all the bytes we pass to them at
* once. */
- data_process_len = MIN(out->size - buffer_len(out), sz);
+ data_process_len = MIN(b_room(out), sz);
block1 = data_process_len;
if (block1 > b_contig_data(in, in->o))
diff --git a/src/hlua.c b/src/hlua.c
index 7976242..e784682 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -2025,7 +2025,7 @@
}
/* Check for avalaible space. */
- len = buffer_total_space(s->req.buf);
+ len = b_room(s->req.buf);
if (len <= 0) {
goto hlua_socket_write_yield_return;
}
@@ -2925,7 +2925,7 @@
WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
}
- max = channel_recv_limit(chn) - buffer_len(chn->buf);
+ max = channel_recv_limit(chn) - b_data(chn->buf);
if (max > len - l)
max = len - l;
@@ -2943,7 +2943,7 @@
lua_pushinteger(L, l);
hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
- max = channel_recv_limit(chn) - buffer_len(chn->buf);
+ max = channel_recv_limit(chn) - b_data(chn->buf);
if (max == 0 && chn->buf->o == 0) {
/* There are no space avalaible, and the output buffer is empty.
* in this case, we cannot add more data, so we cannot yield,
@@ -3024,7 +3024,7 @@
* The reserve is guaranted for the processing of incoming
* data, because the buffer will be flushed.
*/
- max = chn->buf->size - buffer_len(chn->buf);
+ max = b_room(chn->buf);
/* If there are no space avalaible, and the output buffer is empty.
* in this case, we cannot add more data, so we cannot yield,
@@ -3059,7 +3059,7 @@
* in this case, we cannot add more data, so we cannot yield,
* we return the amount of copyied data.
*/
- max = chn->buf->size - buffer_len(chn->buf);
+ max = b_room(chn->buf);
if (max == 0 && chn->buf->o == 0)
return 1;
@@ -3177,9 +3177,7 @@
MAY_LJMP(check_args(L, 1, "is_full"));
chn = MAY_LJMP(hlua_checkchannel(L, 1));
- rem = chn->buf->size;
- rem -= chn->buf->o; /* Output size */
- rem -= chn->buf->i; /* Input size */
+ rem = b_room(chn->buf);
rem -= global.tune.maxrewrite; /* Rewrite reserved size */
lua_pushboolean(L, rem <= 0);