X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/63ba0036aaf8ec77590db47d936942e78ff3ed6f..3b6a05fc894d0c1a3d431045ee5a53955ba093a0:/lunaix-os/includes/lunaix/buffer.h diff --git a/lunaix-os/includes/lunaix/buffer.h b/lunaix-os/includes/lunaix/buffer.h new file mode 100644 index 0000000..605cf8b --- /dev/null +++ b/lunaix-os/includes/lunaix/buffer.h @@ -0,0 +1,48 @@ +#ifndef __LUNAIX_BUFFER_H +#define __LUNAIX_BUFFER_H + +#include +#include + +struct membuf +{ + void* buffer; + size_t size; +}; + +struct vecbuf +{ + struct llist_header components; + struct membuf buf; + size_t acc_sz; +}; + +/** + * @brief Free a vectorized buffer + * + * @param vbuf + */ +void +vbuf_free(struct vecbuf* vbuf); + +/** + * @brief Allocate a buffer, or append to `vec` (if not NULL) as a component + * thus to form a vectorized buffer. + * + * @param vec the buffer used to construct a vectorized buffer. + * @param buf a memeory region that holds data or partial data if vectorized + * @param len maximum number of bytes should recieved. + * @return struct vecbuf* + */ +struct vecbuf* +vbuf_alloc(struct vecbuf* vec, void* buf, size_t len); + +static inline size_t +vbuf_size(struct vecbuf* vbuf) +{ + struct vecbuf* last = + list_entry(vbuf->components.prev, struct vecbuf, components); + return last->acc_sz; +} + +#endif /* __LUNAIX_BUFFER_H */