@@ -333,6 +333,30 @@ char *ustream_get_read_buf(struct ustream *s, int *buflen)
return data;
}
+int ustream_fill_with_read_buf(struct ustream *s, char *buf, int buflen)
+{
+ int len = 0;
+ int chunk_len;
+ struct ustream_buf *sbuf;
+
+ if (!s->r.head) {
+ return 0;
+ }
+
+ sbuf = s->r.head;
+ do {
+ chunk_len = sbuf->tail - sbuf->data;
+ if (chunk_len > buflen)
+ chunk_len = buflen;
+ memcpy(&buf[len], sbuf->data, chunk_len);
+ buflen -= chunk_len;
+ len += chunk_len;
+ sbuf = sbuf->next;
+ } while (buflen && sbuf);
+
+ return len;
+}
+
static void ustream_write_error(struct ustream *s)
{
if (!s->write_error)
@@ -150,6 +150,11 @@ int ustream_vprintf(struct ustream *s, const char *format, va_list arg);
/* ustream_get_read_buf: get a pointer to the next read buffer data */
char *ustream_get_read_buf(struct ustream *s, int *buflen);
+/*
+ * ustream_fill_with_read_buf: fill buf with read buffer data and return
+ * size of actual data written.
+ :*/
+int ustream_fill_with_read_buf(struct ustream *s, char *buf, int buflen);
/*
* ustream_set_read_blocked: set read blocked state
Useful in the following use pattern. int available = ustream_pending_data(s, false); if (available >= sizeof(struct msghdr)) { struct msghdr h; ustream_fill_with_read_buf(s, &h, sizeof(h)); } Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com> --- ustream.c | 24 ++++++++++++++++++++++++ ustream.h | 5 +++++ 2 files changed, 29 insertions(+)