summaryrefslogtreecommitdiff
path: root/include/haproxy/buf.h
diff options
context:
space:
mode:
authorChristopher Faulet <cfaulet@haproxy.com>2021-02-04 10:54:37 +0100
committerChristopher Faulet <cfaulet@haproxy.com>2021-05-25 10:41:50 +0200
commit4fc51a73e669fdd479213682ef714c755657a06f (patch)
tree5a5cb0b63dcf17235dd4a19cb4647f6aff7663d0 /include/haproxy/buf.h
parent7a835f3cb0c5eab84199ad126098af30d94ca226 (diff)
downloadhaproxy-4fc51a73e669fdd479213682ef714c755657a06f.tar.gz
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.
Diffstat (limited to 'include/haproxy/buf.h')
-rw-r--r--include/haproxy/buf.h31
1 files changed, 31 insertions, 0 deletions
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 <ofs>. Depending of the <ofs> value, the resulting
+ * buffer may also wrap. A temporary swap area at least as large as b->size must
+ * be provided in <swap>. It's up to the caller to ensuze <ofs> 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 <c> at the end of buffer <b>. Supports
* wrapping. Data are truncated if buffer is full.
*/