From patchwork Mon May 22 15:31:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 684718 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1260754wrt; Mon, 22 May 2023 08:33:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6djy7M9Wc4zrF32pMFLUlFJ6zOD0wGLlFGss7HC3uXx/vjJfz5FQtziZWBuClon7Q/KcD1 X-Received: by 2002:a05:622a:44:b0:3f4:e666:4583 with SMTP id y4-20020a05622a004400b003f4e6664583mr18676909qtw.49.1684769633486; Mon, 22 May 2023 08:33:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684769633; cv=none; d=google.com; s=arc-20160816; b=hNtxrg42g8eoJoAdM8ltwXKSCCiljkNxMIPndZKm772GpghfBOQFrFVWNmTYpgB891 wCSZ/G1Qf1r0KjnQ2S/xfju21ikVYpHlV/et+ybu2Li10WNJLzWM/wGx07E2lIKFi6cq Ow5GeYvZrf05/ksD4kNxxqO8L3byAr4l8DEcnv5M2DnSVy1rWiq3e8xhwyJ2GtjG1cph IAYjE9K/7YSSQ7I3EWMSI6OgpZritW69LqHPX5TgbQ+si78TAVIADV+MhFiCPA4oSEs5 wuWFJ42GNVICavyUBbFp9F8zd5S3QBDdcDxXe5jWi/cywMeoSLZP9HOoXvaPYVH+z5fq Gf1A== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=D/v69wjcgQjzqhC98DKwcbYpCW+hDbd/3U2ovkdSDfw=; b=goPfjqIF3ITxPbdZofDhlXeeMb5m8mut1KiNM7FYOypSsLW3zY5sviSqB5prtkhkl4 V+/XqNc5W5+T8qdbXRw1kiaHc9eT75+8ccLJB3AVazYLI5n9tYX23mxEB0VlaC4g2siM kWvMVfpodjdUFMYEIcbiWH6Uz8R2+5v3nA4E8x5iPuj3o8mHyM5U0ObxToYBZV71RWtO b4bM5bn7PLrLZi11u543/0VT0Ur01JnEYC3N46Eq4qfa4JoDn+UoFEC9kuDTiIO6Ptig UtHejxRq2fUdSlXyo3RJtraSjrCXABiBbNDPDBTWAPqxV82/YFctnlFmQsVbEhc1FD7E up+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GcIeHWq7; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id o11-20020a05622a044b00b003f221c60353si3806913qtx.748.2023.05.22.08.33.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 May 2023 08:33:53 -0700 (PDT) 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=pass header.i=@linaro.org header.s=google header.b=GcIeHWq7; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q17Wb-0007VB-6v; Mon, 22 May 2023 11:32:41 -0400 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 1q17W4-0007NO-6T for qemu-devel@nongnu.org; Mon, 22 May 2023 11:32:15 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q17W2-0001Lg-AC for qemu-devel@nongnu.org; Mon, 22 May 2023 11:32:07 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3f42b984405so38783925e9.3 for ; Mon, 22 May 2023 08:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684769524; x=1687361524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D/v69wjcgQjzqhC98DKwcbYpCW+hDbd/3U2ovkdSDfw=; b=GcIeHWq7ho6Wb3vxdbkOS5rHBfm+Q2V7gO4V4OinnFC6S0gSjLClHkroxEPcEyBaiu 7MN6KFbKVsg6IS2/mEYBDXvvWGb5hFPXSOYD8BTCxDIs2O3v0Z2hXbxqz/04U0D2XO81 zfmxZ1tI2Kgjtvi4WPDOOwZx+MhlPIzOCmN//oGJgNdRbHG98G30XTWgB4dArEtQHTEs yPZoNvsSaATl7Vpc/dk4BOvEEwPVxp1b6X2mlxkv8eqLchb+TTC1dtkBsUF2CWhh1B0C AXudY0VYO+0Jz0PNoSG0gV0Y410OlUG7MEioweVbxP+FhwaiBT/CsVCVxoCnsld6T4Ej X1Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684769524; x=1687361524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D/v69wjcgQjzqhC98DKwcbYpCW+hDbd/3U2ovkdSDfw=; b=f82gFw7GBNDKrOXc1Qn/DOfzCecOlbwYlv2FRwViaTFNDrkaP8/fnzEVo0+WTDmrHo yb2H/rRrSJgQ4bpfrck2FsMG81U1FVrGDEYbdFVkJGDzYBdGsh6R2DWELivhIIc2o8rQ BjTldXkvECJkd/uVRk6J+Ho+ts3FiwhuEOoLyLYLCh7hWs1F6vPjZRpM7LR2JciTiQNK kM1T0NiVysWS4UYC0cjS2uZ4PtDaBvY8m0xZ99UZlvs9UCtQebI0jv168H2HdN+/JA5g WJz/PsR/LrMtcTZvTLizEue5jUkJfSJy0eFAvOAsgNTTegCmugABKIOCuW5Wsll2LS/Z 8Reg== X-Gm-Message-State: AC+VfDzHbGckKVYMSlLIEGk4FFRaFErVIn22ypKCveL772u5wFFj442r E2LU36hBs3V5O7wunNGaF0kO5xDPyNxCrOQUawY= X-Received: by 2002:a1c:f70e:0:b0:3f4:2b1a:553b with SMTP id v14-20020a1cf70e000000b003f42b1a553bmr7727352wmh.28.1684769524506; Mon, 22 May 2023 08:32:04 -0700 (PDT) Received: from localhost.localdomain ([176.176.153.164]) by smtp.gmail.com with ESMTPSA id o3-20020a1c7503000000b003f09d7b6e20sm8747145wmc.2.2023.05.22.08.32.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 22 May 2023 08:32:04 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , qemu-arm@nongnu.org, =?utf-8?q?Alex?= =?utf-8?q?_Benn=C3=A9e?= , Evgeny Iakovlev , =?utf-8?q?Marc-Andr=C3=A9_?= =?utf-8?q?Lureau?= , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH 03/12] util/fifo8: Introduce fifo8_peek_buf() Date: Mon, 22 May 2023 17:31:35 +0200 Message-Id: <20230522153144.30610-4-philmd@linaro.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230522153144.30610-1-philmd@linaro.org> References: <20230522153144.30610-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=philmd@linaro.org; helo=mail-wm1-x335.google.com 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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 To be able to poke at FIFO content without popping it, introduce the fifo8_peek_buf() method by factoring common content from fifo8_pop_buf(). Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Francisco Iglesias --- include/qemu/fifo8.h | 26 ++++++++++++++++++++++++++ util/fifo8.c | 22 ++++++++++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h index d0d02bc73d..7acf6d1347 100644 --- a/include/qemu/fifo8.h +++ b/include/qemu/fifo8.h @@ -93,6 +93,32 @@ uint8_t fifo8_pop(Fifo8 *fifo); */ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); +/** + * fifo8_peek_buf: + * @fifo: FIFO to poke from + * @max: maximum number of bytes to pop + * @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 + * the FIFO backing store and data is invalidated once any of the fifo8_* APIs + * are called on the FIFO. + * + * 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 032e985440..e12477843e 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;