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.
This commit is contained in:
parent
8b5a1fd1fc
commit
fca212292a
30
src/buf.c
30
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)
|
int b_get_varint(struct buffer *b, uint64_t *vptr)
|
||||||
{
|
{
|
||||||
const uint8_t *head = (const uint8_t *)b_head(b);
|
int size;
|
||||||
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)) {
|
size = b_peek_varint(b, 0, vptr);
|
||||||
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;
|
|
||||||
|
|
||||||
v += (uint64_t)*head << bits;
|
|
||||||
*vptr = v;
|
|
||||||
data--;
|
|
||||||
size = b->data - data;
|
|
||||||
b_del(b, size);
|
b_del(b, size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue