From patchwork Wed Jan 10 07:08:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mark Cave-Ayland X-Patchwork-Id: 761428 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp1744129wrz; Tue, 9 Jan 2024 23:10:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwP6dGfUQRzuJ5so467k019popBhsDM/I+MvhrTAlDbQ8OJeVeCsap75hPqdDZ1PeiRgEm X-Received: by 2002:ad4:5ba9:0:b0:67a:a002:70e6 with SMTP id 9-20020ad45ba9000000b0067aa00270e6mr732613qvq.20.1704870633132; Tue, 09 Jan 2024 23:10:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704870633; cv=none; d=google.com; s=arc-20160816; b=KtqXKU/xSfzzRM675+XiY2VhhT+R4sbGhGT4R9KmRFILfjUAaNTdqb36I79A5d8yB5 QP6RkikJ77e3hN9XGE8JdgF7CHeohYCnF81sgvhMlOoq/GOJJSpLTJH2xAWnGMJH7sUb AzGXTvQZr1Wvp+0veRBcpJcRdSgF3u9osKdcSYc25EC8x0vj6tQz7FC0DKEDLtvACpI9 Fy/hQF1YXQPSukDIfPxd7wcknrW6VPMEORLWp7Xb42hbCyY8X0ZLnhdJ0KnDiI9ISmoK 7ceciECaKemLaXG/5Kf9pxUJoeDVIibPJI4lvW3qoIpea5IojTRHUA5t07jLtwGIFuNa X2ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=JLUr53VGhwGQ3iLEEqmy6bmfInLx4ziu3X7VKm/dbE4=; fh=hMu95QxNBp5EbwTvcBy893Pnbr/ndGd5Ye8kBkdprVk=; b=Fsqlb0XFmgz0G3HyTvxHe0XR5iO+H7qWRQv6JW7h+U3cVHB5zh/lC618GVwqSpbXpe Tgqe+uR7Al7OHCeoBZJRmlZcYIzeqQ2/8EmGrzJ0Jefz94uyK4LwgwpCwvXOL9FK0ZzR 7dhLmezxgqgylHXmuzXU8da4IhF/FVEcR/gvVV6usdtS9TRe1fZ5YgSBdoPVYeui7Sb5 AEpZI2Fue6f0gQPHvj4lU50cZVaZUNYk7Ur93diJhLh4fsqUskOCtAtlakUzcfUYQwPe xJxM+cBGT5dh8eQIlt78MmNrxyclxYXukUuFkd0zRwZ2Y808nYf8hLuUqbBbqx/7fxHO gVrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@ilande.co.uk header.s=20220518 header.b=F3uugInZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ilande.co.uk Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id f6-20020a0cf3c6000000b0067efd85e8ecsi3858686qvm.382.2024.01.09.23.10.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Jan 2024 23:10:33 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@ilande.co.uk header.s=20220518 header.b=F3uugInZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ilande.co.uk Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNSjC-0006Um-JY; Wed, 10 Jan 2024 02:10:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNSj0-0006LG-I9 for qemu-devel@nongnu.org; Wed, 10 Jan 2024 02:10:09 -0500 Received: from mail.ilande.co.uk ([2001:41c9:1:41f::167]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNSiy-0005St-NE for qemu-devel@nongnu.org; Wed, 10 Jan 2024 02:10:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ilande.co.uk; s=20220518; h=Subject:Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:To:From:Sender:Reply-To: Cc:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=JLUr53VGhwGQ3iLEEqmy6bmfInLx4ziu3X7VKm/dbE4=; b=F3uugInZQrWnBUEdtik4JWT0tz fkZBMIBh6mpclIvz3Y7UF2Myut++6J/+Hx48hi7WiMmIsw61f+9JSDjB2fIvVo14DQvNYIF11mBP2 CX+8FG/UbUaK4AKgkXpyF4eRCecMEGNkfQa3Qr5tR0d/AKbDXttGW1ThkYFZqKLnvFSGwWxhdlfU9 CA1nYw8HnZRmY5PzMVN0dPzi4cRgRxv3Fxs7vwuReUXBVpt4ttFZ+xSr2lgn/WNspMJtHHtSNdcag QC7Zxl+cWhTsNMJ1IhkrOVmn5j7IhYgfMlVDx3PWjABgIowtWY6mvY/UhfGkY5+Z+qT+GALr+xvyw ldw/RFC89ZCCD2HtlXTLS0jwosm0HU0hOHlEFrtt4amk8Be7wJhrDZjMcJfVD5ILFP6hL8rHnw7vY Urn77MZMV9l8f0kavTZDDCKQkDc1kVIOg7kuw15kNZnIi+wd/PcROLqP7DfiHH5M7oEllSW6I1V5V 5NcesSdyv8n3xlyc5DMCe6z2uBZuzbDYpJuHDXugQvl3CDIwwDKgNN8btuqFI+YWiYA+pC2GbDpfB 7ERGq40S37Jqv7kt+AkWwiCyw3tXdZPc/W4dcCez8n+a9NRdd/viu+9jR6ZpX1mr8gUK0A4Boov6+ 9AQvAw5SfyKfItRWnr4GiUjbMClyUn6cqZm/35GnM=; Received: from [2a00:23c4:8bb1:9800:b804:1635:e423:adbd] (helo=localhost.localdomain) by mail.ilande.co.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rNSiP-0001XX-S1; Wed, 10 Jan 2024 07:09:33 +0000 From: Mark Cave-Ayland To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Wed, 10 Jan 2024 07:08:07 +0000 Message-Id: <20240110070808.369516-2-mark.cave-ayland@ilande.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240110070808.369516-1-mark.cave-ayland@ilande.co.uk> References: <20240110070808.369516-1-mark.cave-ayland@ilande.co.uk> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a00:23c4:8bb1:9800:b804:1635:e423:adbd X-SA-Exim-Mail-From: mark.cave-ayland@ilande.co.uk Subject: [PULL 1/2] util/fifo8: Allow fifo8_pop_buf() to not populate popped length X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on mail.ilande.co.uk) Received-SPF: pass client-ip=2001:41c9:1:41f::167; envelope-from=mark.cave-ayland@ilande.co.uk; helo=mail.ilande.co.uk X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Philippe Mathieu-Daudé There might be cases where we know the number of bytes we can pop from the FIFO, or we simply don't care how many bytes is returned. Allow fifo8_pop_buf() to take a NULL numptr. Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Francisco Iglesias Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson Tested-by: Alex Bennée Message-Id: <20231109192814.95977-2-philmd@linaro.org> Signed-off-by: Mark Cave-Ayland --- include/qemu/fifo8.h | 10 +++++----- util/fifo8.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h index 16be02f361..d0d02bc73d 100644 --- a/include/qemu/fifo8.h +++ b/include/qemu/fifo8.h @@ -71,7 +71,7 @@ uint8_t fifo8_pop(Fifo8 *fifo); * fifo8_pop_buf: * @fifo: FIFO to pop from * @max: maximum number of bytes to pop - * @num: actual number of returned bytes + * @numptr: pointer filled with number of bytes returned (can be NULL) * * Pop a number of elements from the FIFO up to a maximum of max. The buffer * containing the popped data is returned. This buffer points directly into @@ -82,16 +82,16 @@ uint8_t fifo8_pop(Fifo8 *fifo); * around in the ring buffer; in this case only a contiguous part of the data * is returned. * - * The number of valid bytes returned is populated in *num; will always return - * at least 1 byte. max must not be 0 or greater than the number of bytes in - * the FIFO. + * The number of valid bytes returned is populated in *numptr; will always + * return at least 1 byte. max must not be 0 or greater than the number of + * bytes in the FIFO. * * Clients are responsible for checking the availability of requested data * using fifo8_num_used(). * * Returns: A pointer to popped data. */ -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num); +const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); /** * fifo8_reset: diff --git a/util/fifo8.c b/util/fifo8.c index de8fd0f1c5..2eeed56e80 100644 --- a/util/fifo8.c +++ b/util/fifo8.c @@ -66,16 +66,20 @@ uint8_t fifo8_pop(Fifo8 *fifo) return ret; } -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num) +const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) { uint8_t *ret; + uint32_t num; assert(max > 0 && max <= fifo->num); - *num = MIN(fifo->capacity - fifo->head, max); + num = MIN(fifo->capacity - fifo->head, max); ret = &fifo->data[fifo->head]; - fifo->head += *num; + fifo->head += num; fifo->head %= fifo->capacity; - fifo->num -= *num; + fifo->num -= num; + if (numptr) { + *numptr = num; + } return ret; } From patchwork Wed Jan 10 07:08:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mark Cave-Ayland X-Patchwork-Id: 761429 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp1744280wrz; Tue, 9 Jan 2024 23:10:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IF123xXstsdRbky56UM2LQbOPBTpf5PG9NconHKtjFwONpsiC/V/J57d6z4feM/B85+54n1 X-Received: by 2002:ac8:5805:0:b0:429:a256:7b0b with SMTP id g5-20020ac85805000000b00429a2567b0bmr767897qtg.78.1704870658109; Tue, 09 Jan 2024 23:10:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704870658; cv=none; d=google.com; s=arc-20160816; b=O7j9tosz/jF0AN8h1qJQmfCnBqY3xr9xmX0WceaXzeL/MovRKrvz47kMMm0wVJ5VhU kC4OvIDEVq1Xil6l+P7nXQnfB0LuYBFDMEVcOD9XbvpbplJ4qR5nPamRPgmqPCOJ7zXu Te7oAfl6toLJBU5ViEBu8TZ+AjZZ6euOhfRxYvcJJDjDiHbkZ8lXpqTTBOVS9M4bICyX AVEc1Q1+a9Y8Ij9cX0fr62etPXw5vaxyhUst3b4EcSuPOVRr1LVVy5UgzXNtsUd89Pxr XvXdnn6yeMJMQkfNCH3aS8mq2fGc04iFoF7VQSvD4cygJ62ogwej6KH313LYrHyx9QbF zcfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=tE5WW7Z9fiCY6JlqITracdAm9enLLbV05zXpQrGF9/8=; fh=hMu95QxNBp5EbwTvcBy893Pnbr/ndGd5Ye8kBkdprVk=; b=ykWzWcIqxh+5Lj0dKR8pQhlmRlj1xVKPwL+tCGEDfEPF5r9ABs3vIeywzYexk5weAq oxLJw2O7zB8u3Z7HZ0fRYSJw02jwAS6LWfBCChNjldjDfmlgQQw+mef3veQa33nvbeCc gY50Uj3XTd1zK+zakezsX0gB78V+qJmk1mbR/uPoa8qF7agg7XHxXLTDXsJu2dp99k1l yl05dvv1f9NP3Sfsbl8o5QkG78Vt6GcCCy2DhSrUoxL49weRHnLvinLZBIul+1D9+sce XCIBRF4ISywQZmcyLaep8z7H6XyNf7K4GeEbAcjMI6wt4UHJ8ojgYq9EcGOhBDcn016J kDpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@ilande.co.uk header.s=20220518 header.b=tpoxVglQ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ilande.co.uk Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id b22-20020ac87fd6000000b004299cb0ad0esi3745736qtk.104.2024.01.09.23.10.57 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Jan 2024 23:10:58 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@ilande.co.uk header.s=20220518 header.b=tpoxVglQ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ilande.co.uk Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNSjX-0006iN-Kl; Wed, 10 Jan 2024 02:10:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNSj9-0006Ri-EK for qemu-devel@nongnu.org; Wed, 10 Jan 2024 02:10:17 -0500 Received: from mail.ilande.co.uk ([2001:41c9:1:41f::167]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rNSj4-0005eF-BN for qemu-devel@nongnu.org; Wed, 10 Jan 2024 02:10:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ilande.co.uk; s=20220518; h=Subject:Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:To:From:Sender:Reply-To: Cc:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=tE5WW7Z9fiCY6JlqITracdAm9enLLbV05zXpQrGF9/8=; b=tpoxVglQ4fgS8EaYDA+Wc1ty7x AoUW2ZQmvjvFdWPk1vdBqa0roFYGgQlVd4e6eMELpTN7xUG/h/6yRznU03V2MJT6gIoclwJS1dIPr S0rBYBZGp/mA3sHMOQcuoodGvNDTlQmGyQE9nc4Mv64Tuoohykl6fkTLmaDcgFFhDwqZcA+5TNDJu ZrcMmQLoiuCEnxZ07l7x1bsQdKB8WMZyBmHPkjSXMWb1A3ivr5t5r7pLBCEyGlfGEZQ//Rh31L++5 tKgA3Keq3Wbq+wkdp29pHIZd2eAsvmMfIKZK2nlw8ss6AC2/YCFrUUgJDA8AZolyBesB45L6UdO2Q D85/CNuEQwVxDA1DJ38ImuoGVTymEdMROOnBryeTJv7ewx9l662D3M1/gsGFe5C6kPhBo93AcXvLO tNJvO2qu7Wdt59wKo5rl5+/EdG1aI31HIU9cT5ZkcYEA5XZN4w3qrqVVAIxTF0gtMDE1BCbsYqsID iSNC8RO6DrswMsjckvsApPm09CGPFKZm0CW34s4ofzsb6fx/3hC/mWPGpyDu0lJEdI2QU2ZdVzCit F1RfBXoVLW3v94hGbce2HTu1L5jZCSZu9UZ3oVcZLf52OggwCDDkVZKMYpUM6LwWGpDWD8JbGVNNu JIFuaZeZ5iAmMEek1xCd3MA1eODyZLz16ccPTMqHM=; Received: from [2a00:23c4:8bb1:9800:b804:1635:e423:adbd] (helo=localhost.localdomain) by mail.ilande.co.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rNSiU-0001XX-1Q; Wed, 10 Jan 2024 07:09:38 +0000 From: Mark Cave-Ayland To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Wed, 10 Jan 2024 07:08:08 +0000 Message-Id: <20240110070808.369516-3-mark.cave-ayland@ilande.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240110070808.369516-1-mark.cave-ayland@ilande.co.uk> References: <20240110070808.369516-1-mark.cave-ayland@ilande.co.uk> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a00:23c4:8bb1:9800:b804:1635:e423:adbd X-SA-Exim-Mail-From: mark.cave-ayland@ilande.co.uk Subject: [PULL 2/2] util/fifo8: Introduce fifo8_peek_buf() X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on mail.ilande.co.uk) Received-SPF: pass client-ip=2001:41c9:1:41f::167; envelope-from=mark.cave-ayland@ilande.co.uk; helo=mail.ilande.co.uk X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Philippe Mathieu-Daudé To be able to peek at FIFO content without popping it, introduce the fifo8_peek_buf() method by factoring common content from fifo8_pop_buf(). Reviewed-by: Francisco Iglesias Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson Tested-by: Alex Bennée Message-Id: <20231109192814.95977-3-philmd@linaro.org> Signed-off-by: Mark Cave-Ayland --- include/qemu/fifo8.h | 27 +++++++++++++++++++++++++++ util/fifo8.c | 22 ++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h index d0d02bc73d..c6295c6ff0 100644 --- a/include/qemu/fifo8.h +++ b/include/qemu/fifo8.h @@ -93,6 +93,33 @@ uint8_t fifo8_pop(Fifo8 *fifo); */ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); +/** + * fifo8_peek_buf: read upto max bytes from the fifo + * @fifo: FIFO to read from + * @max: maximum number of bytes to peek + * @numptr: pointer filled with number of bytes returned (can be NULL) + * + * Peek into a number of elements from the FIFO up to a maximum of max. + * The buffer containing the data peeked into is returned. This buffer points + * directly into the FIFO backing store. Since data is invalidated once any + * of the fifo8_* APIs are called on the FIFO, it is the caller responsibility + * to access it before doing further API calls. + * + * The function may return fewer bytes than requested when the data wraps + * around in the ring buffer; in this case only a contiguous part of the data + * is returned. + * + * The number of valid bytes returned is populated in *numptr; will always + * return at least 1 byte. max must not be 0 or greater than the number of + * bytes in the FIFO. + * + * Clients are responsible for checking the availability of requested data + * using fifo8_num_used(). + * + * Returns: A pointer to peekable data. + */ +const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); + /** * fifo8_reset: * @fifo: FIFO to reset diff --git a/util/fifo8.c b/util/fifo8.c index 2eeed56e80..4e01b532d9 100644 --- a/util/fifo8.c +++ b/util/fifo8.c @@ -66,7 +66,8 @@ uint8_t fifo8_pop(Fifo8 *fifo) return ret; } -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) +static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max, + uint32_t *numptr, bool do_pop) { uint8_t *ret; uint32_t num; @@ -74,15 +75,28 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) assert(max > 0 && max <= fifo->num); num = MIN(fifo->capacity - fifo->head, max); ret = &fifo->data[fifo->head]; - fifo->head += num; - fifo->head %= fifo->capacity; - fifo->num -= num; + + if (do_pop) { + fifo->head += num; + fifo->head %= fifo->capacity; + fifo->num -= num; + } if (numptr) { *numptr = num; } return ret; } +const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) +{ + return fifo8_peekpop_buf(fifo, max, numptr, false); +} + +const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) +{ + return fifo8_peekpop_buf(fifo, max, numptr, true); +} + void fifo8_reset(Fifo8 *fifo) { fifo->num = 0;