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;