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 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define bget_varint_slow ucw_bget_varint_slow
18 #define bput_varint_slow ucw_bput_varint_slow
21 u64 bget_varint_slow(struct fastbuf *b, u64 repl);
22 void bput_varint_slow(struct fastbuf *b, u64 u);
25 * Reads u64 encoded as varint from the fastbuf b.
26 * If the read is unsuccessful, returns repl.
28 static inline u64 bget_varint_repl(struct fastbuf *b, u64 repl)
31 if (bavailr(b) >= 1) {
32 l = varint_len(*b->bptr);
33 if (bavailr(b) >= l) {
34 varint_get(b->bptr, &repl);
39 return bget_varint_slow(b, repl);
43 * Reads u64 encoded as varint from the fastbuf b.
44 * If the read is unsuccessful, returns ~0LLU.
46 static inline u64 bget_varint(struct fastbuf *b)
48 return bget_varint_repl(b, ~0LLU);
51 /** Writes u64 u encoded as varint to the fastbuf b. **/
52 static inline void bput_varint(struct fastbuf *b, u64 u)
54 uns l = varint_space(u);
56 b->bptr += varint_put(b->bptr, u);
58 bput_varint_slow(b, u);