From patchwork Wed Nov 12 13:59:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 419619 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id CDA191400A0 for ; Wed, 10 Dec 2014 23:19:42 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 38C4E28C1DD; Wed, 10 Dec 2014 13:16:30 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, DATE_IN_PAST_96_XX, FREEMAIL_FROM,T_DKIM_INVALID autolearn=no version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 909A128C1D7 for ; Wed, 10 Dec 2014 13:16:05 +0100 (CET) X-policyd-weight: using cached result; rate:hard: -8.5 Received: from mail-pa0-f54.google.com (mail-pa0-f54.google.com [209.85.220.54]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Wed, 10 Dec 2014 13:16:05 +0100 (CET) Received: by mail-pa0-f54.google.com with SMTP id fb1so2697800pad.41 for ; Wed, 10 Dec 2014 04:17:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LD4/ZhReuIWmEmn14BnroUlUYBEW5peuOO7hcNjjxTs=; b=be3rtDjZaqqc2pbqQe76GiAHZufpiWNHs88vqfsIuKB6baX7q15eeIOEbzPUc4OPpt WVhs7nB7Kl11FYNAagpvXrNp21BLruXkl3ZIMY1Bu/0KTSUFGLIdqB/k9ldckF9aZDbM fx1/c3M0jLeH+KuUwghryGPp7hQPVAgju/3IXjhp/GvjAN2XT1QLdUstOUDU76gRCytP MDznH4KM54y9JMj6EU3/GxYw1W6GknwynyRPyEQoLc7SONRn9Sjitke6CjKbo705d7bz ipnWzbw7GR/VI/heCOojlSvxMGFQodPBzEIMYvMs1P/KFCX2f/vVZvTNiHz1UjC9Zitg +5BA== X-Received: by 10.68.136.6 with SMTP id pw6mr6280365pbb.157.1418213867263; Wed, 10 Dec 2014 04:17:47 -0800 (PST) Received: from debian.lan ([103.29.140.56]) by mx.google.com with ESMTPSA id hc10sm4063709pbd.78.2014.12.10.04.17.44 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Dec 2014 04:17:46 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Wed, 12 Nov 2014 21:59:17 +0800 Message-Id: <1415800763-14311-5-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1415800763-14311-1-git-send-email-yszhou4tech@gmail.com> References: <1415800763-14311-1-git-send-email-yszhou4tech@gmail.com> Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH v2 04/10] ustream: add function ustream_fill_with_read_buf(). X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" 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 --- ustream.c | 24 ++++++++++++++++++++++++ ustream.h | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/ustream.c b/ustream.c index 828a025..64cdc6a 100644 --- a/ustream.c +++ b/ustream.c @@ -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) diff --git a/ustream.h b/ustream.h index 6431744..48dfa67 100644 --- a/ustream.h +++ b/ustream.h @@ -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