7b277c8c768f142bc3881b48c617095c80fce69f
[lunaix-os.git] / lunaix-os / kernel / ds / buffer.c
1 #include <lunaix/buffer.h>
2 #include <lunaix/mm/valloc.h>
3
4 struct vecbuf*
5 vbuf_alloc(struct vecbuf* vec, void* buf, size_t size)
6 {
7     struct vecbuf* vbuf = valloc(sizeof(struct vecbuf));
8
9     *vbuf = (struct vecbuf){ .buf = { .buffer = buf, .size = size },
10                              .acc_sz = vbuf_size(vec) + size };
11
12     if (vec) {
13         llist_append(&vec->components, &vbuf->components);
14     } else {
15         llist_init_head(&vbuf->components);
16     }
17
18     return vbuf;
19 }
20
21 void
22 vbuf_free(struct vecbuf* vbuf)
23 {
24     struct vecbuf *pos, *n;
25     llist_for_each(pos, n, &vbuf->components, components)
26     {
27         vfree(pos);
28     }
29     vfree(pos);
30 }