refactor: full rewrite of signal feature
[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     struct vecbuf* _vec = *vec;
9
10     *vbuf = (struct vecbuf){ .buf = { .buffer = buf, .size = size },
11                              .acc_sz = vbuf_size(_vec) + size };
12
13     if (_vec) {
14         llist_append(&_vec->components, &vbuf->components);
15     } else {
16         llist_init_head(&vbuf->components);
17         *vec = vbuf;
18     }
19
20     return vbuf;
21 }
22
23 void
24 vbuf_free(struct vecbuf* vbuf)
25 {
26     struct vecbuf *pos, *n;
27     llist_for_each(pos, n, &vbuf->components, components)
28     {
29         vfree(pos);
30     }
31     vfree(pos);
32 }