]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: buffer: replace bo_getblk_nc() with b_getblk_nc() which takes an offset
authorWilly Tarreau <w@1wt.eu>
Thu, 14 Jun 2018 12:38:11 +0000 (14:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Jul 2018 14:23:40 +0000 (16:23 +0200)
This will be important so that we can parse a buffer without touching it.
Now we indicate where from the buffer's head we plan to start to copy, and
for how many bytes. This will be used by send functions to loop at the end
of the buffer without having to update the buffer's output byte count.

include/common/buf.h
include/common/buffer.h
src/channel.c
src/mux_h2.c

index ad2e2ba0d03b7b56a6b502be79a042664abba7fc..45686a23e1b24059f21ea031497ca67d8f1bdc2e 100644 (file)
@@ -343,6 +343,34 @@ static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, s
        return len;
 }
 
+/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
+ * starting from offset <ofs> after the beginning of its output, and limited to
+ * no more than <max> bytes. The caller is responsible for ensuring that
+ * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
+ * the buffer. Return values :
+ *   >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
+ *   =0 : not enough data available. <blk*> are left undefined.
+ * The buffer is left unaffected. Unused buffers are left in an undefined state.
+ */
+static inline size_t b_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2, size_t ofs, size_t max)
+{
+       size_t l1;
+
+       if (!max)
+               return 0;
+
+       *blk1 = b_peek(buf, ofs);
+       l1 = b_wrap(buf) - *blk1;
+       if (l1 < max) {
+               *len1 = l1;
+               *len2 = max - l1;
+               *blk2 = buf->data;
+               return 2;
+       }
+       *len1 = max;
+       return 1;
+}
+
 
 /*********************************************/
 /* Functions used to modify the buffer state */
index 896e050628480f7e0d4b1d5db9c26b891fe823fb..52d9dcc29b86fc88265e597926abb736ab201380 100644 (file)
@@ -326,30 +326,6 @@ static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
        return bo_putblk(b, chk->str, chk->len);
 }
 
-/* Gets one or two blocks of data at once from a buffer's output.
- * Return values :
- *   >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
- *   =0 : not enough data available. <blk*> are left undefined.
- * The buffer is left unaffected. Unused buffers are left in an undefined state.
- */
-static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
-{
-       if (unlikely(buf->o == 0))
-               return 0;
-
-       if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
-               *blk1 = buf->p - buf->o + buf->size;
-               *len1 = buf->data + buf->size - *blk1;
-               *blk2 = buf->data;
-               *len2 = buf->p - buf->data;
-               return 2;
-       }
-
-       *blk1 = b_head(buf);
-       *len1 = buf->o;
-       return 1;
-}
-
 /* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
  * Data are truncated if buffer is full.
  */
index 42514179b22250e37c06d94c116328aa3714439d..ad309b8e15f278a8c4e43945a2c4ce1608adac29 100644 (file)
@@ -320,7 +320,7 @@ int co_getblk_nc(const struct channel *chn, char **blk1, int *len1, char **blk2,
                return 0;
        }
 
-       return bo_getblk_nc(chn->buf, blk1, len1, blk2, len2);
+       return b_getblk_nc(chn->buf, blk1, len1, blk2, len2, 0, chn->buf->o);
 }
 
 /* Gets one text line out of a channel's output buffer from a stream interface.
index 028427915e81bd2dde97f5e91ed0c6feaf1da6d1..f2002865c7aa17c747b9634abedde05a6b1d77ea 100644 (file)
@@ -3271,7 +3271,7 @@ static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
 
        /* copy whatever we can */
        blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
-       ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
+       ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, 0, buf->o);
        if (ret == 1)
                len2 = 0;