Skip to content

Commit fca2122

Browse files
committed
CLEANUP: buffers: simplify b_get_varint()
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.
1 parent 8b5a1fd commit fca2122

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

src/buf.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -623,35 +623,9 @@ int b_put_varint(struct buffer *b, uint64_t v)
623623
*/
624624
int b_get_varint(struct buffer *b, uint64_t *vptr)
625625
{
626-
const uint8_t *head = (const uint8_t *)b_head(b);
627-
const uint8_t *wrap = (const uint8_t *)b_wrap(b);
628-
size_t data = b->data;
629-
size_t size = b_size(b);
630-
uint64_t v = 0;
631-
int bits = 0;
632-
633-
if (data != 0 && (*head >= 0xF0)) {
634-
v = *head;
635-
bits += 4;
636-
while (1) {
637-
if (++head == wrap)
638-
head -= size;
639-
data--;
640-
if (!data || !(*head & 0x80))
641-
break;
642-
v += (uint64_t)*head << bits;
643-
bits += 7;
644-
}
645-
}
646-
647-
/* last byte */
648-
if (!data)
649-
return 0;
626+
int size;
650627

651-
v += (uint64_t)*head << bits;
652-
*vptr = v;
653-
data--;
654-
size = b->data - data;
628+
size = b_peek_varint(b, 0, vptr);
655629
b_del(b, size);
656630
return size;
657631
}

0 commit comments

Comments
 (0)