From patchwork Mon Feb 15 11:17:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Christophe Dubois X-Patchwork-Id: 8313271 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id AE0869F372 for ; Mon, 15 Feb 2016 11:18:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E061320340 for ; Mon, 15 Feb 2016 11:18:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DA1A820361 for ; Mon, 15 Feb 2016 11:18:13 +0000 (UTC) Received: from localhost ([::1]:59138 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVHAT-0000Y8-6U for patchwork-qemu-devel@patchwork.kernel.org; Mon, 15 Feb 2016 06:18:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38943) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVHAH-0000Uo-Jo for qemu-devel@nongnu.org; Mon, 15 Feb 2016 06:18:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aVHAF-0001kC-VF for qemu-devel@nongnu.org; Mon, 15 Feb 2016 06:18:01 -0500 Received: from zose-mta03.web4all.fr ([185.49.20.44]:41975) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVHAF-0001k4-M9 for qemu-devel@nongnu.org; Mon, 15 Feb 2016 06:17:59 -0500 Received: from localhost (localhost [127.0.0.1]) by zose-mta03.web4all.fr (Postfix) with ESMTP id 36C1341177; Mon, 15 Feb 2016 12:17:59 +0100 (CET) Received: from zose-mta03.web4all.fr ([127.0.0.1]) by localhost (zose-mta03.web4all.fr [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 3d6YHvgFWZS5; Mon, 15 Feb 2016 12:17:58 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by zose-mta03.web4all.fr (Postfix) with ESMTP id AE0084309A; Mon, 15 Feb 2016 12:17:58 +0100 (CET) X-Virus-Scanned: amavisd-new at zose-mta-03.w4a.fr Received: from zose-mta03.web4all.fr ([127.0.0.1]) by localhost (zose-mta03.web4all.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id eeTIjQnERZVL; Mon, 15 Feb 2016 12:17:58 +0100 (CET) Received: from localhost.localdomain (smm49-1-78-235-240-156.fbx.proxad.net [78.235.240.156]) by zose-mta03.web4all.fr (Postfix) with ESMTPSA id 5BDA041177; Mon, 15 Feb 2016 12:17:58 +0100 (CET) From: Jean-Christophe Dubois To: qemu-devel@nongnu.org, peter.maydell@linaro.org, mar.krzeminski@gmail.com, crosthwaite.peter@gmail.com Date: Mon, 15 Feb 2016 12:17:56 +0100 Message-Id: X-Mailer: git-send-email 2.5.0 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 185.49.20.44 Cc: Jean-Christophe Dubois Subject: [Qemu-devel] [PATCH v2 1/4] FIFO: Add a FIFO32 implementation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This one is build on top of the existing FIFO8 Signed-off-by: Jean-Christophe Dubois --- Changes since v1: * None include/qemu/fifo32.h | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 include/qemu/fifo32.h diff --git a/include/qemu/fifo32.h b/include/qemu/fifo32.h new file mode 100644 index 0000000..d934516 --- /dev/null +++ b/include/qemu/fifo32.h @@ -0,0 +1,206 @@ +#ifndef FIFO32_H +#define FIFO32_H + +#include "qemu/fifo8.h" + +typedef Fifo8 Fifo32; + +/** + * fifo32_create: + * @fifo: struct Fifo32 to initialise with new FIFO + * @capacity: capacity of the newly created FIFO + * + * Create a FIFO of the specified size. Clients should call fifo32_destroy() + * when finished using the fifo. The FIFO is initially empty. + */ + +static inline void fifo32_create(Fifo32 *fifo, uint32_t capacity) +{ + fifo8_create(fifo, capacity * sizeof(uint32_t)); +} + +/** + * fifo32_destroy: + * @fifo: FIFO to cleanup + * + * Cleanup a FIFO created with fifo32_create(). Frees memory created for FIFO + *storage. The FIFO is no longer usable after this has been called. + */ + +static inline void fifo32_destroy(Fifo32 *fifo) +{ + fifo8_destroy(fifo); +} + +/** + * fifo32_num_free: + * @fifo: FIFO to check + * + * Return the number of free uint32_t slots in the FIFO. + * + * Returns: Number of free bytes. + */ + +static inline uint32_t fifo32_num_free(Fifo32 *fifo) +{ + return (fifo8_num_free(fifo) + sizeof(uint32_t) - 1) / sizeof(uint32_t); +} + +/** + * fifo32_num_used: + * @fifo: FIFO to check + * + * Return the number of used uint32_t slots in the FIFO. + * + * Returns: Number of used bytes. + */ + +static inline uint32_t fifo32_num_used(Fifo32 *fifo) +{ + return (fifo8_num_used(fifo) + sizeof(uint32_t) - 1) / sizeof(uint32_t); +} + +/** + * fifo32_push: + * @fifo: FIFO to push to + * @data: data byte to push + * + * Push a data byte to the FIFO. Behaviour is undefined if the FIFO is full. + * Clients are responsible for checking for fullness using fifo32_is_full(). + */ + +static inline void fifo32_push(Fifo32 *fifo, uint32_t data) +{ + uint8_t *ptr = (uint8_t *)&data; + int i; + + if (fifo32_num_free(fifo) == 0) { + abort(); + } + + for (i=0; i < sizeof(data); i++) { + fifo8_push(fifo, ptr[i]); + } +} + +/** + * fifo32_push_all: + * @fifo: FIFO to push to + * @data: data to push + * @size: number of bytes to push + * + * Push a byte array to the FIFO. Behaviour is undefined if the FIFO is full. + * Clients are responsible for checking the space left in the FIFO using + * fifo32_num_free(). + */ + +static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, + uint32_t num) +{ + fifo8_push_all(fifo, (const uint8_t *)data, num * sizeof(uint32_t)); +} + +/** + * fifo32_pop: + * @fifo: fifo to pop from + * + * Pop a data byte from the FIFO. Behaviour is undefined if the FIFO is empty. + * Clients are responsible for checking for emptyness using fifo32_is_empty(). + * + * Returns: The popped data byte. + */ + +static inline uint32_t fifo32_pop(Fifo32 *fifo) +{ + uint32_t ret = 0; + uint8_t *ptr = (uint8_t *)&ret; + int i; + + for (i=0; i < sizeof(uint32_t); i++) { + if (fifo8_is_empty(fifo)) { + break; + } + ptr[i] = fifo8_pop(fifo); + } + + return ret; +} + +/** + * fifo32_pop_buf: + * @fifo: FIFO to pop from + * @max: maximum number of bytes to pop + * @num: actual number of returned bytes + * + * 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 fifo32_* 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 *num; 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 fifo32_num_used(). + * + * Returns: A pointer to popped data. + */ + +static inline const uint32_t *fifo32_pop_buf(Fifo32 *fifo, uint32_t max, + uint32_t *num) +{ + const uint8_t *ptr = fifo8_pop_buf(fifo, max * sizeof(uint32_t), num); + + *num /= sizeof(uint32_t); + + return (const uint32_t *)ptr; +} + +/** + * fifo32_reset: + * @fifo: FIFO to reset + * + * Reset a FIFO. All data is discarded and the FIFO is emptied. + */ + +static inline void fifo32_reset(Fifo32 *fifo) +{ + fifo8_reset(fifo); +} + +/** + * fifo32_is_empty: + * @fifo: FIFO to check + * + * Check if a FIFO is empty. + * + * Returns: True if the fifo is empty, false otherwise. + */ + +static inline bool fifo32_is_empty(Fifo32 *fifo) +{ + return fifo8_is_empty(fifo); +} + +/** + * fifo32_is_full: + * @fifo: FIFO to check + * + * Check if a FIFO is full. + * + * Returns: True if the fifo is full, false otherwise. + */ + +static inline bool fifo32_is_full(Fifo32 *fifo) +{ + return fifo8_num_free(fifo) < sizeof(uint32_t) ? true : false; +} + +#define VMSTATE_FIFO32(_field, _state) VMSTATE_FIFO8(_field, _state) + +#endif /* FIFO32_H */