]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: buffers: simplify b_get_varint()
authorWilly Tarreau <w@1wt.eu>
Fri, 18 Oct 2024 16:28:39 +0000 (18:28 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 18 Oct 2024 16:28:39 +0000 (18:28 +0200)
The function is an exact copy of b_peek_varint() with ofs==0 and doing a
b_del() at the end. We can simply call that other one and delete the
contents. It turns out that the code is bigger with this change because
b_peek_varint() passes its offset to b_peek() which performs a wrapping
check. When ofs==0 the wrapping cannot happen, but there's no real way
to tell that to the compiler. Instead conditioning the if() in b_peek()
with (!__builtin_constant_p(ofs) || ofs) does the job, but it's not worth
it at the moment since we have no users of b_get_varint() for now. Let's
just stick to the simple normal code.

src/buf.c

index 2e07c0bc188086f171868be4b7d5358a9c64ebd2..afe33110b1028d7c98ba761d67b91f2fc3980ffd 100644 (file)
--- a/src/buf.c
+++ b/src/buf.c
@@ -623,35 +623,9 @@ int b_put_varint(struct buffer *b, uint64_t v)
  */
 int b_get_varint(struct buffer *b, uint64_t *vptr)
 {
-       const uint8_t *head = (const uint8_t *)b_head(b);
-       const uint8_t *wrap = (const uint8_t *)b_wrap(b);
-       size_t data = b->data;
-       size_t size = b_size(b);
-       uint64_t v = 0;
-       int bits = 0;
-
-       if (data != 0 && (*head >= 0xF0)) {
-               v = *head;
-               bits += 4;
-               while (1) {
-                       if (++head == wrap)
-                               head -= size;
-                       data--;
-                       if (!data || !(*head & 0x80))
-                               break;
-                       v += (uint64_t)*head << bits;
-                       bits += 7;
-               }
-       }
-
-       /* last byte */
-       if (!data)
-               return 0;
+       int size;
 
-       v += (uint64_t)*head << bits;
-       *vptr = v;
-       data--;
-       size = b->data - data;
+       size = b_peek_varint(b, 0, vptr);
        b_del(b, size);
        return size;
 }