From 4fc51a73e669fdd479213682ef714c755657a06f Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Thu, 4 Feb 2021 10:54:37 +0100 Subject: [PATCH] MINOR: buf: Add function to realign a buffer with a specific head position b_slow_realign() function may be used to realign a buffer with a given amount of output data, eventually 0. In such case, the head is set to 0. This function is not designed to be used with input only buffers, like those used in the muxes. It is the purpose of b_slow_realign_ofs() function. It does almost the same, realign a buffer. But it do so by setting the buffer head to a specific offset. --- include/haproxy/buf.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/haproxy/buf.h b/include/haproxy/buf.h index a0e1cb811..f66f5c687 100644 --- a/include/haproxy/buf.h +++ b/include/haproxy/buf.h @@ -469,6 +469,37 @@ static inline void b_slow_realign(struct buffer *b, char *swap, size_t output) b->head = (output ? b_size(b) - output : 0); } +/* b_slow_realign_ofs() : this function realigns a possibly wrapping buffer + * setting its new head at . Depending of the value, the resulting + * buffer may also wrap. A temporary swap area at least as large as b->size must + * be provided in . It's up to the caller to ensuze is not larger + * than b->size. + */ +static inline void b_slow_realign_ofs(struct buffer *b, char *swap, size_t ofs) +{ + size_t block1 = b_data(b); + size_t block2 = 0; + + if (__b_tail_ofs(b) >= b_size(b)) { + block2 = b_tail_ofs(b); + block1 -= block2; + } + memcpy(swap, b_head(b), block1); + memcpy(swap + block1, b_orig(b), block2); + + block1 = b_data(b); + block2 = 0; + if (block1 > b_size(b) - ofs) { + block1 = b_size(b) - ofs; + block2 = b_data(b) - block1; + } + memcpy(b_orig(b) + ofs, swap, block1); + memcpy(b_orig(b), swap + block1, block2); + + b->head = ofs; +} + + /* b_putchar() : tries to append char at the end of buffer . Supports * wrapping. Data are truncated if buffer is full. */