MINOR: buffer: use b_room() to determine available space in a buffer
authorWilly Tarreau <w@1wt.eu>
Fri, 15 Jun 2018 11:59:36 +0000 (13:59 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Jul 2018 14:23:41 +0000 (16:23 +0200)
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.

include/common/buffer.h
src/channel.c
src/checks.c
src/compression.c
src/flt_http_comp.c
src/hlua.c

index 2e4d165..44b1883 100644 (file)
@@ -58,11 +58,8 @@ void buffer_dump(FILE *o, struct buffer *b, int from, int to);
 /*****************************************************************/
 
 
-/* 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 @@ static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
        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 int buffer_replace(struct buffer *b, char *pos, char *end, const c
  */
 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 void bo_putchr(struct buffer *b, char c)
  */
 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 int bo_putchk(struct buffer *b, const struct chunk *chk)
  */
 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 void bi_putchr(struct buffer *b, char c)
  */
 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 @@ static inline int bi_istput(struct buffer *b, const struct ist ist)
        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 @@ static inline int bo_istput(struct buffer *b, const struct ist ist)
        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);
index b3382a7..c5a51f7 100644 (file)
@@ -151,7 +151,7 @@ int ci_putblk(struct channel *chn, const char *blk, int len)
                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.
index a499f23..4c777b7 100644 (file)
@@ -2670,7 +2670,7 @@ static int tcpcheck_main(struct check *check)
                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 @@ static int tcpcheck_main(struct check *check)
                        }
 
                        /* 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);
index 567827d..397fd94 100644 (file)
@@ -222,7 +222,7 @@ static int identity_init(struct comp_ctx **comp_ctx, int level)
 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 @@ static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int
        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 @@ static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out
        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 */
index 99b18cc..9365364 100644 (file)
@@ -203,7 +203,7 @@ comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
        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 @@ http_compression_buffer_init(struct buffer *in, struct buffer *out)
         * 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 @@ http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
         * 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))
index 7976242..e784682 100644 (file)
@@ -2025,7 +2025,7 @@ static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext
        }
 
        /* 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 @@ __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KConte
                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 @@ __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KConte
        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 @@ __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext
         * 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 @@ __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext
         * 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 @@ __LJMP static int hlua_channel_is_full(lua_State *L)
        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);