diff options
| author | Stefani Seibold <stefani@seibold.net> | 2009-12-21 17:37:30 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-22 17:17:56 -0500 |
| commit | 37bdfbbfaab47811fcec84dff23c4e8da1a09f9e (patch) | |
| tree | 89e88b5859674b9d9255fa1a87aaf34090da574d | |
| parent | 9842c38e917636fa7dc6b88aff17a8f1fd7f0cc0 (diff) | |
kfifo: add DEFINE_KFIFO and friends, add very tiny functions
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>
Acked-by: Greg Kroah-Hartman <gregkh@suse.de>
Acked-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
| -rw-r--r-- | include/linux/kfifo.h | 94 |
1 files changed, 92 insertions, 2 deletions
diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h index 5ed2565c89b6..dd53eed3e2af 100644 --- a/include/linux/kfifo.h +++ b/include/linux/kfifo.h | |||
| @@ -51,6 +51,60 @@ struct kfifo { | |||
| 51 | unsigned int out; /* data is extracted from off. (out % size) */ | 51 | unsigned int out; /* data is extracted from off. (out % size) */ |
| 52 | }; | 52 | }; |
| 53 | 53 | ||
| 54 | /* | ||
| 55 | * Macros for declaration and initialization of the kfifo datatype | ||
| 56 | */ | ||
| 57 | |||
| 58 | /* helper macro */ | ||
| 59 | #define __kfifo_initializer(s, b) \ | ||
| 60 | (struct kfifo) { \ | ||
| 61 | .size = s, \ | ||
| 62 | .in = 0, \ | ||
| 63 | .out = 0, \ | ||
| 64 | .buffer = b \ | ||
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer | ||
| 69 | * @name: name of the declared kfifo datatype | ||
| 70 | * @size: size of the fifo buffer | ||
| 71 | * | ||
| 72 | * Note: the macro can be used inside struct or union declaration | ||
| 73 | * Note: the macro creates two objects: | ||
| 74 | * A kfifo object with the given name and a buffer for the kfifo | ||
| 75 | * object named name##kfifo_buffer | ||
| 76 | */ | ||
| 77 | #define DECLARE_KFIFO(name, size) \ | ||
| 78 | union { \ | ||
| 79 | struct kfifo name; \ | ||
| 80 | unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \ | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO | ||
| 85 | * @name: name of the declared kfifo datatype | ||
| 86 | * @size: size of the fifo buffer | ||
| 87 | */ | ||
| 88 | #define INIT_KFIFO(name) \ | ||
| 89 | name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \ | ||
| 90 | sizeof(struct kfifo), name##kfifo_buffer) | ||
| 91 | |||
| 92 | /** | ||
| 93 | * DEFINE_KFIFO - macro to define and initialize a kfifo | ||
| 94 | * @name: name of the declared kfifo datatype | ||
| 95 | * @size: size of the fifo buffer | ||
| 96 | * | ||
| 97 | * Note: the macro can be used for global and local kfifo data type variables | ||
| 98 | * Note: the macro creates two objects: | ||
| 99 | * A kfifo object with the given name and a buffer for the kfifo | ||
| 100 | * object named name##kfifo_buffer | ||
| 101 | */ | ||
| 102 | #define DEFINE_KFIFO(name, size) \ | ||
| 103 | unsigned char name##kfifo_buffer[size]; \ | ||
| 104 | struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer) | ||
| 105 | |||
| 106 | #undef __kfifo_initializer | ||
| 107 | |||
| 54 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, | 108 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
| 55 | unsigned int size); | 109 | unsigned int size); |
| 56 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, | 110 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
| @@ -71,6 +125,15 @@ static inline void kfifo_reset(struct kfifo *fifo) | |||
| 71 | } | 125 | } |
| 72 | 126 | ||
| 73 | /** | 127 | /** |
| 128 | * kfifo_size - returns the size of the fifo in bytes | ||
| 129 | * @fifo: the fifo to be used. | ||
| 130 | */ | ||
| 131 | static inline __must_check unsigned int kfifo_size(struct kfifo *fifo) | ||
| 132 | { | ||
| 133 | return fifo->size; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 74 | * kfifo_len - returns the number of used bytes in the FIFO | 137 | * kfifo_len - returns the number of used bytes in the FIFO |
| 75 | * @fifo: the fifo to be used. | 138 | * @fifo: the fifo to be used. |
| 76 | */ | 139 | */ |
| @@ -84,6 +147,33 @@ static inline unsigned int kfifo_len(struct kfifo *fifo) | |||
| 84 | } | 147 | } |
| 85 | 148 | ||
| 86 | /** | 149 | /** |
| 150 | * kfifo_is_empty - returns true if the fifo is empty | ||
| 151 | * @fifo: the fifo to be used. | ||
| 152 | */ | ||
| 153 | static inline __must_check int kfifo_is_empty(struct kfifo *fifo) | ||
| 154 | { | ||
| 155 | return fifo->in == fifo->out; | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 159 | * kfifo_is_full - returns true if the fifo is full | ||
| 160 | * @fifo: the fifo to be used. | ||
| 161 | */ | ||
| 162 | static inline __must_check int kfifo_is_full(struct kfifo *fifo) | ||
| 163 | { | ||
| 164 | return kfifo_len(fifo) == kfifo_size(fifo); | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * kfifo_avail - returns the number of bytes available in the FIFO | ||
| 169 | * @fifo: the fifo to be used. | ||
| 170 | */ | ||
| 171 | static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo) | ||
| 172 | { | ||
| 173 | return kfifo_size(fifo) - kfifo_len(fifo); | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 87 | * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking | 177 | * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking |
| 88 | * @fifo: the fifo to be used. | 178 | * @fifo: the fifo to be used. |
| 89 | * @from: the data to be added. | 179 | * @from: the data to be added. |
| @@ -133,8 +223,8 @@ static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, | |||
| 133 | * optimization: if the FIFO is empty, set the indices to 0 | 223 | * optimization: if the FIFO is empty, set the indices to 0 |
| 134 | * so we don't wrap the next time | 224 | * so we don't wrap the next time |
| 135 | */ | 225 | */ |
| 136 | if (fifo->in == fifo->out) | 226 | if (kfifo_is_empty(fifo)) |
| 137 | fifo->in = fifo->out = 0; | 227 | kfifo_reset(fifo); |
| 138 | 228 | ||
| 139 | spin_unlock_irqrestore(lock, flags); | 229 | spin_unlock_irqrestore(lock, flags); |
| 140 | 230 | ||
