From: Stefani Seibold <stefani@seibold.net>
To: linux-kernel <linux-kernel@vger.kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Arnd Bergmann <arnd@arndb.de>, Andi Kleen <andi@firstfloor.org>,
Amerigo Wang <xiyou.wangcong@gmail.com>,
Joe Perches <joe@perches.com>
Subject: Re: [PATCH 5/7] kfifo: add DEFINE_KFIFO and friends, add very tiny functions
Date: Sun, 16 Aug 2009 22:57:01 +0200 [thread overview]
Message-ID: <1250456221.28540.22.camel@wall-e> (raw)
In-Reply-To: <1250455161.28540.4.camel@wall-e>
Add DECLARE_KFIFO - macro to declare a kfifo and the associated buffer inside a struct
Add INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO
Add DEFINE_KFIFO - macro to define and initialize a kfifo as a global or local object
Add kfifo_size() - returns the size of the fifo in bytes
Add kfifo_is_empty() - returns true if the fifo is empty
Add kfifo_is_full() - returns true if the fifo is full
Add kfifo_avail() - returns the number of bytes available in the FIFO
Do some code cleanup
Signed-off-by: Stefani Seibold <stefani@seibold.net>
---
kfifo.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 83 insertions(+), 2 deletions(-)
diff -u -N -r linux-2.6.31-rc4-kfifo4/include/linux/kfifo.h linux-2.6.31-rc4-kfifo5/include/linux/kfifo.h
--- linux-2.6.31-rc4-kfifo4/include/linux/kfifo.h 2009-08-16 22:16:13.000000000 +0200
+++ linux-2.6.31-rc4-kfifo5/include/linux/kfifo.h 2009-08-16 22:16:43.000000000 +0200
@@ -32,6 +32,51 @@
unsigned int out; /* data is extracted from off. (out % size) */
};
+/*
+ * Macros for declaration and initialization of the kfifo datatype
+ */
+
+#define __kfifo_initializer(s, b) \
+ (struct kfifo) { \
+ .size = s, \
+ .in = 0, \
+ .out = 0, \
+ .buffer = b \
+ }
+
+/**
+ * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
+ * @name: name of the declared kfifo datatype
+ * @size: size of the fifo buffer
+ *
+ * Note: the macro can be used inside struct or union declaration
+ */
+#define DECLARE_KFIFO(name, size) \
+union { \
+ struct kfifo name; \
+ unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
+}
+
+/**
+ * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO
+ * @name: name of the declared kfifo datatype
+ * @size: size of the fifo buffer
+ */
+#define INIT_KFIFO(name) \
+ name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
+ sizeof(struct kfifo), name##kfifo_buffer)
+
+/**
+ * DEFINE_KFIFO - macro to define and initialize a kfifo
+ * @name: name of the declared kfifo datatype
+ * @size: size of the fifo buffer
+ *
+ * Note: the macro can be used for global and local kfifo data type variables
+ */
+#define DEFINE_KFIFO(name, size) \
+ unsigned char name##kfifo_buffer[size]; \
+ struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
+
extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
unsigned int size);
extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
@@ -52,6 +97,15 @@
}
/**
+ * kfifo_size - returns the size of the fifo in bytes
+ * @fifo: the fifo to be used.
+ */
+static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
+{
+ return fifo->size;
+}
+
+/**
* kfifo_len - returns the number of used bytes in the FIFO
* @fifo: the fifo to be used.
*/
@@ -65,6 +119,33 @@
}
/**
+ * kfifo_is_empty - returns true if the fifo is empty
+ * @fifo: the fifo to be used.
+ */
+static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
+{
+ return fifo->in == fifo->out;
+}
+
+/**
+ * kfifo_is_full - returns true if the fifo is full
+ * @fifo: the fifo to be used.
+ */
+static inline __must_check int kfifo_is_full(struct kfifo *fifo)
+{
+ return kfifo_len(fifo) == kfifo_size(fifo);
+}
+
+/**
+ * kfifo_avail - returns the number of bytes available in the FIFO
+ * @fifo: the fifo to be used.
+ */
+static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
+{
+ return kfifo_size(fifo) - kfifo_len(fifo);
+}
+
+/**
* kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
* @fifo: the fifo to be used.
* @from: the data to be added.
@@ -114,8 +195,8 @@
* optimization: if the FIFO is empty, set the indices to 0
* so we don't wrap the next time
*/
- if (fifo->in == fifo->out)
- fifo->in = fifo->out = 0;
+ if (kfifo_is_empty(fifo))
+ kfifo_reset(fifo);
spin_unlock_irqrestore(lock, flags);
next prev parent reply other threads:[~2009-08-16 20:57 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-16 20:39 [PATCH 0/7] kfifo: new API v0.4 Stefani Seibold
2009-08-16 20:44 ` [PATCH 1/7] kfifo: move struct kfifo in place Stefani Seibold
2009-08-16 20:46 ` [PATCH 2/7] kfifo: move out spinlock Stefani Seibold
2009-08-16 22:58 ` Alan Cox
2009-08-16 23:34 ` Andrew Morton
2009-08-17 6:48 ` Alan Cox
2009-08-17 7:36 ` Andrew Morton
2009-08-17 8:08 ` Alan Cox
2009-08-17 8:14 ` Stefani Seibold
2009-08-17 8:21 ` Andrew Morton
2009-08-17 8:48 ` Alan Cox
2009-08-17 9:22 ` Stefani Seibold
2009-08-17 7:46 ` Stefani Seibold
2009-08-17 8:15 ` Alan Cox
2009-08-17 8:28 ` Stefani Seibold
2009-08-17 8:53 ` Alan Cox
2009-08-17 9:26 ` Stefani Seibold
2009-08-17 9:51 ` Alan Cox
2009-08-17 9:52 ` Andi Kleen
2009-08-17 9:56 ` Stefani Seibold
2009-08-16 20:50 ` [PATCH 3/7] kfifo: cleanup namespace Stefani Seibold
2009-08-16 20:53 ` [PATCH 4/7] kfifo: rename kfifo_put... into kfifo_in... and kfifo_get... into kfifo_out Stefani Seibold
2009-08-16 20:57 ` Stefani Seibold [this message]
2009-08-16 21:00 ` [PATCH 6/7] kfifo: add kfifo_skip, kfifo_from_user and kfifo_to_user Stefani Seibold
2009-08-16 21:03 ` [PATCH 0/7] kfifo: add record handling functions Stefani Seibold
2009-08-16 21:04 ` [PATCH 7/7] " Stefani Seibold
2009-08-16 21:08 ` [PATCH 0/7] kfifo: new API v0.4 Stefani Seibold
-- strict thread matches above, loose matches on Subject: below --
2009-08-19 20:49 [PATCH 0/7] kfifo: new API v0.5 Stefani Seibold
2009-08-19 21:03 ` [PATCH 5/7] kfifo: add DEFINE_KFIFO and friends, add very tiny functions Stefani Seibold
2009-11-16 11:50 [PATCH 0/7] kfifo: new API v0.6 Stefani Seibold
2009-11-16 12:04 ` [PATCH 5/7] kfifo: add DEFINE_KFIFO and friends, add very tiny functions Stefani Seibold
2009-11-29 16:18 ` Thiago Farina
2009-11-20 8:15 [PATCH 0/7] kfifo: new API v0.7 Stefani Seibold
2009-11-20 8:27 ` [PATCH 5/7] kfifo: add DEFINE_KFIFO and friends, add very tiny functions Stefani Seibold
2009-11-23 22:19 ` Andrew Morton
2009-11-24 6:52 ` Stefani Seibold
2009-11-24 23:29 ` Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1250456221.28540.22.camel@wall-e \
--to=stefani@seibold.net \
--cc=akpm@linux-foundation.org \
--cc=andi@firstfloor.org \
--cc=arnd@arndb.de \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=xiyou.wangcong@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox