1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

sys/iolist: introduce iolist_to_buffer()

This commit is contained in:
Benjamin Valentin 2022-05-02 22:48:21 +02:00
parent 1b92fb9858
commit 501ca7e34d
2 changed files with 32 additions and 0 deletions

View File

@ -82,6 +82,18 @@ struct iovec;
*/
size_t iolist_to_iovec(const iolist_t *iolist, struct iovec *iov, unsigned *count);
/**
* @brief Copies the bytes of the iolist to a buffer
*
* @param[in] iolist iolist to read from
* @param[out] dst Destination buffer
* @param[in] len Size of the destination buffer
*
* @returns iolist_size(iolist) on success
* -ENOBUFS if the buffer is too small
*/
ssize_t iolist_to_buffer(const iolist_t *iolist, void *dst, size_t len);
#ifdef __cplusplus
}
#endif

View File

@ -18,6 +18,9 @@
* @}
*/
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <sys/uio.h>
#include "iolist.h"
@ -60,3 +63,20 @@ size_t iolist_to_iovec(const iolist_t *iolist, struct iovec *iov, unsigned *coun
return bytes;
}
ssize_t iolist_to_buffer(const iolist_t *iolist, void *buf, size_t len)
{
char *dst = buf;
while (iolist) {
if (iolist->iol_len > len) {
return -ENOBUFS;
}
memcpy(dst, iolist->iol_base, iolist->iol_len);
len -= iolist->iol_len;
dst += iolist->iol_len;
iolist = iolist->iol_next;
}
return (uintptr_t)dst - (uintptr_t)buf;
}