]> mj.ucw.cz Git - libucw.git/blob - ucw/ff-varint.h
Merge branch 'master' into dev-opt
[libucw.git] / ucw / ff-varint.h
1 /*
2  *      UCW Library: Reading and writing Varints on Fastbuf Streams
3  *
4  *      (c) 2013 Tomas Valla <tom@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_FF_VARINT_H
11 #define _UCW_FF_VARINT_H
12
13 #include <ucw/fastbuf.h>
14 #include <ucw/varint.h>
15
16 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define bget_varint_slow ucw_bget_varint_slow
18 #define bput_varint_slow ucw_bput_varint_slow
19 #endif
20
21 u64 bget_varint_slow(struct fastbuf *b, u64 repl);
22 void bput_varint_slow(struct fastbuf *b, u64 u);
23
24 /**
25  * Reads u64 encoded as varint from the fastbuf b.
26  * If the read is unsuccessful, returns repl.
27  **/
28 static inline u64 bget_varint_repl(struct fastbuf *b, u64 repl)
29 {
30         uns l;
31         if (bavailr(b) >= 1) {
32                 l = varint_len(*b->bptr);
33                 if (bavailr(b) >= l) {
34                         varint_get(b->bptr, &repl);
35                         b->bptr += l;
36                         return repl;
37                 }
38         }
39         return bget_varint_slow(b, repl);
40 }
41
42 /**
43  * Reads u64 encoded as varint from the fastbuf b.
44  * If the read is unsuccessful, returns ~0LLU.
45  **/
46 static inline u64 bget_varint(struct fastbuf *b)
47 {
48         return bget_varint_repl(b, ~0LLU);
49 }
50
51 /** Writes u64 u encoded as varint to the fastbuf b. **/
52 static inline void bput_varint(struct fastbuf *b, u64 u)
53 {
54         uns l = varint_space(u);
55         if (bavailw(b) >= l)
56                 b->bptr += varint_put(b->bptr, u);
57         else
58                 bput_varint_slow(b, u);
59 }
60
61 #endif