Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / buffer.h @ 6b3f1a54

History | View | Annotate | Download (1.55 KB)

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
#define BUFFER_TYPE(v)                typeof(* (v).data)
18
#define BUFFER_SIZE(v)                ((v).size * sizeof(* (v).data))
19

    
20
#ifndef PARSER
21
#define BUFFER(type) BUFFER_(type)
22
#endif
23

    
24
#define BUFFER_INIT(v,pool,isize)                                        \
25
  ({                                                                        \
26
    (v).used = 0;                                                        \
27
    (v).size = (isize);                                                        \
28
    (v).data = mb_alloc(pool, BUFFER_SIZE(v));                                \
29
  })
30

    
31
#define BUFFER_SET(v,nsize)                                                \
32
  ({                                                                        \
33
    (v).used = (nsize);                                                        \
34
    if ((v).used > (v).size)                                                \
35
      buffer_realloc((void **) &((v).data), &((v).size), (v).used, sizeof(* (v).data)); \
36
  })
37

    
38
#define BUFFER_INC(v,step)                                                \
39
  ({                                                                        \
40
    uint _o = (v).used;                                                        \
41
    BUFFER_SET(v, (v).used + (step));                                        \
42
    (v).data + _o;                                                        \
43
  })
44

    
45
#define BUFFER_DEC(v,step)        ({ (v).used -= (step); })
46

    
47
#define BUFFER_PUSH(v)                (*BUFFER_INC(v,1))
48

    
49
#define BUFFER_POP(v)                BUFFER_DEC(v,1)
50

    
51
#define BUFFER_FLUSH(v)                ({ (v).used = 0; })
52

    
53
#define BUFFER_WALK(v,n)                                                \
54
  for (BUFFER_TYPE(v) *_n = (v).data, n; _n < ((v).data + (v).used) && (n = *_n, 1); _n++)
55

    
56
#define BUFFER_SHALLOW_COPY(dst, src)                                        \
57
  ({                                                                        \
58
    (dst).used = (src).used;                                                \
59
    (dst).size = (src).size;                                                \
60
    (dst).data = (src).data;                                                \
61
  })
62

    
63
#endif /* _BIRD_BUFFER_H_ */