From: Willy Tarreau Date: Thu, 14 Jun 2018 12:38:11 +0000 (+0200) Subject: MINOR: buffer: replace bo_getblk_nc() with b_getblk_nc() which takes an offset X-Git-Tag: v1.9-dev1~142 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1f78fb65241ee6780ddec088344a16513089822;p=thirdparty%2Fhaproxy.git MINOR: buffer: replace bo_getblk_nc() with b_getblk_nc() which takes an offset 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. --- diff --git a/include/common/buf.h b/include/common/buf.h index ad2e2ba0d0..45686a23e1 100644 --- a/include/common/buf.h +++ b/include/common/buf.h @@ -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 after the beginning of its output, and limited to + * no more than bytes. The caller is responsible for ensuring that + * neither nor + 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. 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 */ diff --git a/include/common/buffer.h b/include/common/buffer.h index 896e050628..52d9dcc29b 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -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. 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 into input data at buffer . Supports wrapping. * Data are truncated if buffer is full. */ diff --git a/src/channel.c b/src/channel.c index 42514179b2..ad309b8e15 100644 --- a/src/channel.c +++ b/src/channel.c @@ -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. diff --git a/src/mux_h2.c b/src/mux_h2.c index 028427915e..f2002865c7 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -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;