From bc59f359dcc3bc72d07bfc750e927748e70406a5 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 15 Jun 2018 13:45:17 +0200 Subject: [PATCH] MINOR: buffer: get rid of b_ptr() and convert its last users Now the new API functions are being used everywhere, we can get rid of b_ptr(). A few last users like bi_istput() and bo_istput() appear to only differ by what part of the buffer they're increasing, but that should quickly be merged. --- include/common/buffer.h | 66 ++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/include/common/buffer.h b/include/common/buffer.h index 0b768f267..2e4d16542 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -57,21 +57,6 @@ void buffer_dump(FILE *o, struct buffer *b, int from, int to); /* These functions are used to compute various buffer area sizes */ /*****************************************************************/ -/* Returns an absolute pointer for a position relative to the current buffer's - * pointer. It is written so that it is optimal when is a const. It is - * written as a macro instead of an inline function so that the compiler knows - * when it can optimize out the sign test on when passed an unsigned int. - * Note that callers MUST cast to int if they expect negative values. - */ -#define b_ptr(b, ofs) \ - ({ \ - char *__ret = (b)->p + (ofs); \ - if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ - __ret -= (b)->size; \ - else if ((ofs) < 0 && __ret < (b)->data) \ - __ret += (b)->size; \ - __ret; \ - }) /* Return the buffer's length in bytes by summing the input and the output */ static inline int buffer_len(const struct buffer *buf) @@ -244,7 +229,7 @@ static inline void bo_putchr(struct buffer *b, char c) if (buffer_len(b) == b->size) return; *b->p = c; - b->p = b_ptr(b, 1); + b->p = b_peek(b, b->o + 1); b->o++; } @@ -267,12 +252,13 @@ static inline int bo_putblk(struct buffer *b, const char *blk, int len) half = len; memcpy(b->p, blk, half); - b->p = b_ptr(b, half); + b->p = b_peek(b, b->o + half); + b->o += half; if (len > half) { memcpy(b->p, blk + half, len - half); - b->p = b_ptr(b, half); + b->p = b_peek(b, b->o + len - half); + b->o += len - half; } - b->o += len; return len; } @@ -325,7 +311,7 @@ static inline int bi_putblk(struct buffer *b, const char *blk, int len) memcpy(b_tail(b), blk, half); if (len > half) - memcpy(b_ptr(b, b->i + half), blk + half, len - half); + memcpy(b_peek(b, b->o + b->i + half), blk + half, len - half); b->i += len; return len; } @@ -486,13 +472,13 @@ static inline void offer_buffers(void *from, unsigned int threshold) /* functions used to manipulate strings and blocks with wrapping buffers */ /*************************************************************************/ -/* returns > 0 if the first characters of buffer starting at - * offset relative to b->p match . (empty strings do match). It is +/* returns > 0 if the first characters of buffer starting at offset + * relative to the buffer's head match . (empty strings do match). It is * designed to be use with reasonably small strings (ie matches a single byte * per iteration). This function is usable both with input and output data. To * be used like this depending on what to match : - * - input contents : b_isteq(b, 0, b->i, ist); - * - output contents : b_isteq(b, -b->o, b->o, ist); + * - input contents : b_isteq(b, b->o, b->i, ist); + * - output contents : b_isteq(b, 0, b->o, ist); * Return value : * >0 : the number of matching bytes * =0 : not enough bytes (or matching of empty string) @@ -502,12 +488,12 @@ static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, cons { struct ist r = ist; const char *p; - const char *end = b->data + b->size; + const char *end = b_wrap(b); if (n < r.len) return 0; - p = b_ptr(b, o); + p = b_peek(b, o); while (r.len--) { if (*p++ != *r.ptr++) return -1; @@ -517,22 +503,22 @@ static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, cons return ist.len; } -/* "eats" string from the input region of buffer . Wrapping data is - * explicitly supported. It matches a single byte per iteration so strings - * should remain reasonably small. Returns : +/* "eats" string from the head of buffer . Wrapping data is explicitly + * supported. It matches a single byte per iteration so strings should remain + * reasonably small. Returns : * > 0 : number of bytes matched and eaten * = 0 : not enough bytes (or matching an empty string) * < 0 : non-matching byte found */ -static inline int bi_eat(struct buffer *b, const struct ist ist) +static inline int b_eat(struct buffer *b, const struct ist ist) { - int ret = b_isteq(b, 0, b->i, ist); + int ret = b_isteq(b, 0, b_data(b), ist); if (ret > 0) b_del(b, ret); return ret; } -/* injects string into the input region of buffer provided that it +/* injects string at the tail of input buffer provided that it * fits. Wrapping is supported. It's designed for small strings as it only * writes a single byte per iteration. Returns the number of characters copied * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It @@ -542,14 +528,14 @@ static inline int bi_eat(struct buffer *b, const struct ist ist) */ static inline int bi_istput(struct buffer *b, const struct ist ist) { - const char *end = b->data + b->size; + const char *end = b_wrap(b); struct ist r = ist; char *p; - if (r.len > (size_t)(b->size - b->i - b->o)) + if (r.len > (size_t)(b->size - b_data(b))) return r.len < b->size ? 0 : -1; - p = b_ptr(b, b->i); + p = b_tail(b); b->i += r.len; while (r.len--) { *p++ = *r.ptr++; @@ -560,7 +546,7 @@ static inline int bi_istput(struct buffer *b, const struct ist ist) } -/* injects string into the output region of buffer provided that it +/* injects string at the tail of output buffer provided that it * fits. Input data is assumed not to exist and will silently be overwritten. * Wrapping is supported. It's designed for small strings as it only writes a * single byte per iteration. Returns the number of characters copied (ist.len), @@ -571,16 +557,16 @@ static inline int bi_istput(struct buffer *b, const struct ist ist) */ static inline int bo_istput(struct buffer *b, const struct ist ist) { - const char *end = b->data + b->size; + const char *end = b_wrap(b); struct ist r = ist; char *p; - if (r.len > (size_t)(b->size - b->o)) + if (r.len > (size_t)(b->size - b_data(b))) return r.len < b->size ? 0 : -1; - p = b->p; + p = b_tail(b); + b->p = b_peek(b, b->o + r.len); b->o += r.len; - b->p = b_ptr(b, r.len); while (r.len--) { *p++ = *r.ptr++; if (unlikely(p == end))