From patchwork Thu Nov 9 19:28:06 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: 742596 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:110f:b0:32d:baff:b0ca with SMTP id z15csp1017904wrw; Thu, 9 Nov 2023 11:29:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFX5pg4+DQwgfA9U2kf6rknKvfK7tW9btU8aG/lSxqh6nloMCBLBXXfaoQhDjbX9ZqW1yVf X-Received: by 2002:a05:620a:2952:b0:77a:1d5b:2cc4 with SMTP id n18-20020a05620a295200b0077a1d5b2cc4mr6491966qkp.9.1699558180701; Thu, 09 Nov 2023 11:29:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699558180; cv=none; d=google.com; s=arc-20160816; b=r25tiqYpI9k/sxeSzOv4E1szh/sqvR1T8kXDNeRX16Ef876W5lgO26tr9GcIpkepRq qYExuGl8ulBrar/SaIZDrN1IvpNhyw8nnfi/Me3ZSorTavMYlMk58rdCXWy4lCOjqns7 +gOh4VIR9ZvRl3y/YE92yi8SowIjPbnM5PLA5f7E2DxJkOlaIc43O2+ylQT2WGRMtuDy 5qtntiPqkOO28ITTtednYZefG5aWilsBL/v4vF2d873KMwuK9jpITh7oASf8sxGN8E5y R6t7hDOGNz3IlCK4RO7AiAmSJ/Px3A3rhdYMK9oYr2ABez5n7c/tSa0fhpcDGdEej9lg QuVA== 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=BO0D2WVoS2EP+jLak3Jz2oTxKuP2p7K6nXjgv8ItBfQ=; fh=GQxR5x0bbpeaxpQRj0Ss2iF9lxOTaG+uCQYEI2XZ3x0=; b=STZG+0I7n0Hqy+xw/tWKd0k522+aCqCOwAR4ekIcs7Zav3CGtrH6YacAyYG1ioCN8A yf+XNwiFaAN++uFTQx3nr2Ibb8Yf4kx2qnXSCRuaMlyzV++ENeyKrygFFYj8kOFfk4je ATSlvvU7BFZEhmBsOl8G/+Yufb6fHjxqF2sZRTuF1eVHH2gkoqV1f9q/UQuVD4i78V/R x1cXn9NNG5tv0UwnkcgcwFDOWCgWT9ls8cTCd0XXACoINbJDDsi9IkKEdgObYbj69gZL QzWNPawnOwJg0nL5k+oytXyRa/IGThB47C1QBpya2WNHx8Zukf16XWc2IRk5I6ql6AJa tvcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tfNv+2mj; 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 z10-20020ae9c10a000000b0076daa449cb8si3227568qki.224.2023.11.09.11.29.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2023 11:29:40 -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=pass header.i=@linaro.org header.s=google header.b=tfNv+2mj; 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 1r1Ahf-00054S-Dq; Thu, 09 Nov 2023 14:28:35 -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 1r1Ahd-00053k-Tj for qemu-devel@nongnu.org; Thu, 09 Nov 2023 14:28:33 -0500 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r1Ahc-0005GV-8r for qemu-devel@nongnu.org; Thu, 09 Nov 2023 14:28:33 -0500 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-9d267605ceeso209734566b.2 for ; Thu, 09 Nov 2023 11:28:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699558109; x=1700162909; darn=nongnu.org; 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=BO0D2WVoS2EP+jLak3Jz2oTxKuP2p7K6nXjgv8ItBfQ=; b=tfNv+2mjSdiHuj6izxKvhPqyNSpT00DzsUACdTaTi/LgitchUy9nGEVrqdU4KvHnfH sSPVwyGRVULCB+c24BZbwimRyY+8tXKAoKpMfgSer/d2J/EAQvn+KsN8bXvXtQcN4GoB eYS3mkDHFQpN/DDcRwmXlBZa5UmYZ9wr7PUlTj6wDcL/F9tu5mVhiTV6SYOG49IK57VI OaV3KjisNz+TJqoFK86fdRP1J6NfRsMHPlBZmrge+nV5oJKn+Sz6mrSD7B8dInVevfgf Uzrymg0h9Ep5lr+MW3qzQLE4UP+GLr4SDvki8yuB+0i63AvJRrr4VUh1T7WS0aiqYD3I tpIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699558109; x=1700162909; 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=BO0D2WVoS2EP+jLak3Jz2oTxKuP2p7K6nXjgv8ItBfQ=; b=RrsrkKzlNUQgA4wqOKK5hXR3429bu1j5U0lUGv15Vpg6HamjF0WF2nYfTKfFUpUo+O ZlRrb+sZK50QUlfzsmGDgd1cp4YlrS61NyQazQcCDcjW+DH3k4vNGgNAikhi24KpiFLu sb44tDcLUGXELNlfg/yssqZ2KxX9t9LD1rvix98W0WE0qqcmBkFn0SpqhDKG335Dp/5n JD2D9vjKE430QWYdCVP5ub+hOoBivvJ5V2dqOuoV5evW1FExBvEs0CIDe2R2ExflfPxk XsFhJ+5qFD7RbNqpcY4LSyopoW6xjvWMmupTzX8co1/WIa0vX3Ulld2URNlQCDh9SThe 1KMw== X-Gm-Message-State: AOJu0Yx7VGTO4s1YapLH3plkNKuxZeRkMwZeGnYdk7QtA6kh4YqmCJz4 2MPb3f2KQSLHxSr/D6ko+k7JLd7kU3WtCU7ycOL+hQ== X-Received: by 2002:a17:907:7faa:b0:9e0:5f68:143c with SMTP id qk42-20020a1709077faa00b009e05f68143cmr5572161ejc.35.1699558109337; Thu, 09 Nov 2023 11:28:29 -0800 (PST) Received: from m1x-phil.lan ([176.187.199.161]) by smtp.gmail.com with ESMTPSA id qh23-20020a170906ecb700b009a19701e7b5sm2954549ejb.96.2023.11.09.11.28.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 09 Nov 2023 11:28:28 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Alex_Benn=C3=A9e?= , Gavin Shan , Paolo Bonzini , Mark Cave-Ayland , Peter Maydell , Evgeny Iakovlev , qemu-arm@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Francisco Iglesias Subject: [PATCH-for-8.2 v4 02/10] util/fifo8: Introduce fifo8_peek_buf() Date: Thu, 9 Nov 2023 20:28:06 +0100 Message-ID: <20231109192814.95977-3-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231109192814.95977-1-philmd@linaro.org> References: <20231109192814.95977-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62b; envelope-from=philmd@linaro.org; helo=mail-ej1-x62b.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=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 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 --- 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 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;