2 * UCW Library: Reading and writing Varints on Fastbuf Streams
4 * (c) 2013 Tomas Valla <tom@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _UCW_FF_VARINT_H
11 #define _UCW_FF_VARINT_H
13 #include <ucw/fastbuf.h>
14 #include <ucw/varint.h>
16 u64 bget_varint_slow(struct fastbuf *b, u64 repl);
17 void bput_varint_slow(struct fastbuf *b, u64 u);
20 * Reads u64 encoded as varint from the fastbuf b.
21 * If the read is unsuccessful, returns repl.
23 static inline u64 bget_varint_repl(struct fastbuf *b, u64 repl)
26 if (bavailr(b) >= 1) {
27 l = varint_len(*b->bptr);
28 if (bavailr(b) >= l) {
29 varint_get(b->bptr, &repl);
34 return bget_varint_slow(b, repl);
38 * Reads u64 encoded as varint from the fastbuf b.
39 * If the read is unsuccessful, returns ~0LLU.
41 static inline u64 bget_varint(struct fastbuf *b)
43 return bget_varint_repl(b, ~0LLU);
46 /** Writes u64 u encoded as varint to the fastbuf b. **/
47 static inline void bput_varint(struct fastbuf *b, u64 u)
49 uns l = varint_space(u);
51 b->bptr += varint_put(b->bptr, u);
53 bput_varint_slow(b, u);