move msi-related functionality to generic isrm
[lunaix-os.git] / lunaix-os / includes / lunaix / buffer.h
1 #ifndef __LUNAIX_BUFFER_H
2 #define __LUNAIX_BUFFER_H
3
4 #include <lunaix/ds/llist.h>
5 #include <lunaix/types.h>
6
7 struct membuf
8 {
9     void* buffer;
10     size_t size;
11 };
12
13 struct vecbuf
14 {
15     struct llist_header components;
16     struct membuf buf;
17     size_t acc_sz;
18 };
19
20 /**
21  * @brief Free a vectorized buffer
22  *
23  * @param vbuf
24  */
25 void
26 vbuf_free(struct vecbuf* vbuf);
27
28 /**
29  * @brief Allocate a buffer, or append to `vec` (if not NULL) as a component
30  * thus to form a vectorized buffer.
31  *
32  * @param vec the buffer used to construct a vectorized buffer.
33  * @param buf a memeory region that holds data or partial data if vectorized
34  * @param len maximum number of bytes should recieved.
35  * @return struct vecbuf*
36  */
37 struct vecbuf*
38 vbuf_alloc(struct vecbuf** vec, void* buf, size_t len);
39
40 static inline size_t
41 vbuf_size(struct vecbuf* vbuf)
42 {
43     if (!vbuf) {
44         return 0;
45     }
46
47     struct vecbuf* last =
48       list_entry(vbuf->components.prev, struct vecbuf, components);
49     return last->acc_sz;
50 }
51
52 #endif /* __LUNAIX_BUFFER_H */