Statistics
| Branch: | Revision:

iof-bird-daemon / lib / buffer.h @ 9b136840

History | View | Annotate | Download (849 Bytes)

1 bf139664 Ondrej Zajicek
2
#define BUFFER(type)                struct { type *data; uint used, size; }
3
4
#define BUFFER_SIZE(v)                ((v).size * sizeof(* (v).data))
5
6
#define BUFFER_INIT(v,pool,isize)                                        \
7
  ({                                                                        \
8
    (v).used = 0;                                                        \
9
    (v).size = (isize);                                                        \
10
    (v).data = mb_alloc(pool, BUFFER_SIZE(v));                                \
11
  })
12
13
#define BUFFER_SET(v,nsize)                                                \
14
  ({                                                                        \
15
    (v).used = (nsize);                                                        \
16
    if ((v).used > (v).size)                                                \
17
      buffer_realloc((void **) &((v).data), &((v).size), (v).used, sizeof(* (v).data)); \
18
  })
19
20
#define BUFFER_INC(v,step)                                                \
21
  ({                                                                        \
22
    uint _o = (v).used;                                                        \
23
    BUFFER_SET(v, (v).used + (step));                                        \
24
    (v).data + _o;                                                        \
25
  })
26
27
#define BUFFER_DEC(v,step)        ({ (v).used -= (step); })
28
29
#define BUFFER_PUSH(v)                (*BUFFER_INC(v,1))
30
31
#define BUFFER_POP(v)                BUFFER_DEC(v,1)
32
33
#define BUFFER_FLUSH(v)                ({ (v).used = 0; })
34