1 /*
2  *	BIRD Library -- Generic Buffer Structure
3  *
4  *	(c) 2013 Ondrej Zajicek <santiago@crfreenet.org>
5  *	(c) 2013 CZ.NIC z.s.p.o.
6  *
7  *	Can be freely distributed and used under the terms of the GNU GPL.
8  */
9 
10 #ifndef _BIRD_BUFFER_H_
11 #define _BIRD_BUFFER_H_
12 
13 #include "lib/resource.h"
14 #include "sysdep/config.h"
15 
16 #define BUFFER(type)		struct { type *data; uint used, size; }
17 
18 #define BUFFER_SIZE(v)		((v).size * sizeof(* (v).data))
19 
20 #define BUFFER_INIT(v,pool,isize)					\
21   ({									\
22     (v).used = 0;							\
23     (v).size = (isize);							\
24     (v).data = mb_alloc(pool, BUFFER_SIZE(v));				\
25   })
26 
27 #define BUFFER_SET(v,nsize)						\
28   ({									\
29     (v).used = (nsize);							\
30     if ((v).used > (v).size)						\
31       buffer_realloc((void **) &((v).data), &((v).size), (v).used, sizeof(* (v).data)); \
32   })
33 
34 #define BUFFER_INC(v,step)						\
35   ({									\
36     uint _o = (v).used;							\
37     BUFFER_SET(v, (v).used + (step));					\
38     (v).data + _o;							\
39   })
40 
41 #define BUFFER_DEC(v,step)	({ (v).used -= (step); })
42 
43 #define BUFFER_PUSH(v)		(*BUFFER_INC(v,1))
44 
45 #define BUFFER_POP(v)		BUFFER_DEC(v,1)
46 
47 #define BUFFER_FLUSH(v)		({ (v).used = 0; })
48 
49 #endif /* _BIRD_BUFFER_H_ */
50