2 * A simple kernel FIFO implementation.
4 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <asm/uaccess.h>
30 * kfifo_init - allocates a new FIFO using a preallocated buffer
31 * @buffer: the preallocated buffer to be used.
32 * @size: the size of the internal buffer, this have to be a power of 2.
33 * @gfp_mask: get_free_pages mask, passed to kmalloc()
34 * @lock: the lock to be used to protect the fifo buffer
36 * Do NOT pass the kfifo to kfifo_free() after use ! Simply free the
37 * struct kfifo with kfree().
39 struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
40 unsigned int gfp_mask, void *lock)
44 /* size must be a power of 2 */
45 BUG_ON(size & (size - 1));
47 fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
49 return ERR_PTR(-ENOMEM);
51 fifo->buffer = buffer;
53 fifo->in = fifo->out = 0;
59 * kfifo_alloc - allocates a new FIFO and its internal buffer
60 * @size: the size of the internal buffer to be allocated.
61 * @gfp_mask: get_free_pages mask, passed to kmalloc()
62 * @lock: the lock to be used to protect the fifo buffer
64 * The size will be rounded-up to a power of 2.
66 struct kfifo *kfifo_alloc(unsigned int size, unsigned int gfp_mask, void *lock)
68 unsigned char *buffer;
72 * round up to the next power of 2, since our 'let the indices
73 * wrap' tachnique works only in this case.
75 if (size & (size - 1)) {
76 BUG_ON(size > 0x80000000);
77 printk("Do not support no power of two!\n");
78 //size = roundup_pow_of_two(size);
81 buffer = kmalloc(size, gfp_mask);
83 return ERR_PTR(-ENOMEM);
85 ret = kfifo_init(buffer, size, gfp_mask, lock);
94 * kfifo_free - frees the FIFO
95 * @fifo: the fifo to be freed.
97 void kfifo_free(struct kfifo *fifo)
104 * __kfifo_put - puts some data into the FIFO, no locking version
105 * @fifo: the fifo to be used.
106 * @buffer: the data to be added.
107 * @len: the length of the data to be added.
109 * This function copies at most 'len' bytes from the 'buffer' into
110 * the FIFO depending on the free space, and returns the number of
113 * Note that with only one concurrent reader and one concurrent
114 * writer, you don't need extra locking to use these functions.
116 unsigned int __kfifo_put(struct kfifo *fifo,
117 unsigned char *buffer, unsigned int len)
121 len = min(len, fifo->size - fifo->in + fifo->out);
123 /* first put the data starting from fifo->in to buffer end */
124 l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
125 memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
127 /* then put the rest (if any) at the beginning of the buffer */
128 memcpy(fifo->buffer, buffer + l, len - l);
135 /** __kfifio_put_user works like __kfifo_put, but copies data from
139 unsigned int __kfifo_put_user(struct kfifo *fifo,
140 unsigned char *buffer, unsigned int len)
144 len = min(len, fifo->size - fifo->in + fifo->out);
146 /* first put the data starting from fifo->in to buffer end */
147 l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
148 copy_from_user(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
150 /* then put the rest (if any) at the beginning of the buffer */
151 copy_from_user(fifo->buffer, buffer + l, len - l);
161 * __kfifo_get - gets some data from the FIFO, no locking version
162 * @fifo: the fifo to be used.
163 * @buffer: where the data must be copied.
164 * @len: the size of the destination buffer.
166 * This function copies at most 'len' bytes from the FIFO into the
167 * 'buffer' and returns the number of copied bytes.
169 * Note that with only one concurrent reader and one concurrent
170 * writer, you don't need extra locking to use these functions.
172 unsigned int __kfifo_get(struct kfifo *fifo,
173 unsigned char *buffer, unsigned int len)
177 len = min(len, fifo->in - fifo->out);
179 /* first get the data from fifo->out until the end of the buffer */
180 l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
181 memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
183 /* then get the rest (if any) from the beginning of the buffer */
184 memcpy(buffer + l, fifo->buffer, len - l);