| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * A simple kernel FIFO implementation. | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2004 Stelian Pop <stelian@popies.net> | 
 | 5 |  * | 
 | 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. | 
 | 10 |  * | 
 | 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. | 
 | 15 |  * | 
 | 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. | 
 | 19 |  * | 
 | 20 |  */ | 
 | 21 |  | 
 | 22 | #include <linux/kernel.h> | 
 | 23 | #include <linux/module.h> | 
 | 24 | #include <linux/slab.h> | 
 | 25 | #include <linux/err.h> | 
 | 26 | #include <linux/kfifo.h> | 
 | 27 |  | 
 | 28 | /** | 
 | 29 |  * kfifo_init - allocates a new FIFO using a preallocated buffer | 
 | 30 |  * @buffer: the preallocated buffer to be used. | 
 | 31 |  * @size: the size of the internal buffer, this have to be a power of 2. | 
 | 32 |  * @gfp_mask: get_free_pages mask, passed to kmalloc() | 
 | 33 |  * @lock: the lock to be used to protect the fifo buffer | 
 | 34 |  * | 
 | 35 |  * Do NOT pass the kfifo to kfifo_free() after use ! Simply free the | 
 | 36 |  * struct kfifo with kfree(). | 
 | 37 |  */ | 
 | 38 | struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size, | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 39 | 			 gfp_t gfp_mask, spinlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { | 
 | 41 | 	struct kfifo *fifo; | 
 | 42 |  | 
 | 43 | 	/* size must be a power of 2 */ | 
 | 44 | 	BUG_ON(size & (size - 1)); | 
 | 45 |  | 
 | 46 | 	fifo = kmalloc(sizeof(struct kfifo), gfp_mask); | 
 | 47 | 	if (!fifo) | 
 | 48 | 		return ERR_PTR(-ENOMEM); | 
 | 49 |  | 
 | 50 | 	fifo->buffer = buffer; | 
 | 51 | 	fifo->size = size; | 
 | 52 | 	fifo->in = fifo->out = 0; | 
 | 53 | 	fifo->lock = lock; | 
 | 54 |  | 
 | 55 | 	return fifo; | 
 | 56 | } | 
 | 57 | EXPORT_SYMBOL(kfifo_init); | 
 | 58 |  | 
 | 59 | /** | 
 | 60 |  * kfifo_alloc - allocates a new FIFO and its internal buffer | 
 | 61 |  * @size: the size of the internal buffer to be allocated. | 
 | 62 |  * @gfp_mask: get_free_pages mask, passed to kmalloc() | 
 | 63 |  * @lock: the lock to be used to protect the fifo buffer | 
 | 64 |  * | 
 | 65 |  * The size will be rounded-up to a power of 2. | 
 | 66 |  */ | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 67 | struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { | 
 | 69 | 	unsigned char *buffer; | 
 | 70 | 	struct kfifo *ret; | 
 | 71 |  | 
 | 72 | 	/* | 
 | 73 | 	 * round up to the next power of 2, since our 'let the indices | 
 | 74 | 	 * wrap' tachnique works only in this case. | 
 | 75 | 	 */ | 
 | 76 | 	if (size & (size - 1)) { | 
 | 77 | 		BUG_ON(size > 0x80000000); | 
 | 78 | 		size = roundup_pow_of_two(size); | 
 | 79 | 	} | 
 | 80 |  | 
 | 81 | 	buffer = kmalloc(size, gfp_mask); | 
 | 82 | 	if (!buffer) | 
 | 83 | 		return ERR_PTR(-ENOMEM); | 
 | 84 |  | 
 | 85 | 	ret = kfifo_init(buffer, size, gfp_mask, lock); | 
 | 86 |  | 
 | 87 | 	if (IS_ERR(ret)) | 
 | 88 | 		kfree(buffer); | 
 | 89 |  | 
 | 90 | 	return ret; | 
 | 91 | } | 
 | 92 | EXPORT_SYMBOL(kfifo_alloc); | 
 | 93 |  | 
 | 94 | /** | 
 | 95 |  * kfifo_free - frees the FIFO | 
 | 96 |  * @fifo: the fifo to be freed. | 
 | 97 |  */ | 
 | 98 | void kfifo_free(struct kfifo *fifo) | 
 | 99 | { | 
 | 100 | 	kfree(fifo->buffer); | 
 | 101 | 	kfree(fifo); | 
 | 102 | } | 
 | 103 | EXPORT_SYMBOL(kfifo_free); | 
 | 104 |  | 
 | 105 | /** | 
 | 106 |  * __kfifo_put - puts some data into the FIFO, no locking version | 
 | 107 |  * @fifo: the fifo to be used. | 
 | 108 |  * @buffer: the data to be added. | 
 | 109 |  * @len: the length of the data to be added. | 
 | 110 |  * | 
 | 111 |  * This function copies at most 'len' bytes from the 'buffer' into | 
 | 112 |  * the FIFO depending on the free space, and returns the number of | 
 | 113 |  * bytes copied. | 
 | 114 |  * | 
 | 115 |  * Note that with only one concurrent reader and one concurrent | 
 | 116 |  * writer, you don't need extra locking to use these functions. | 
 | 117 |  */ | 
 | 118 | unsigned int __kfifo_put(struct kfifo *fifo, | 
 | 119 | 			 unsigned char *buffer, unsigned int len) | 
 | 120 | { | 
 | 121 | 	unsigned int l; | 
 | 122 |  | 
 | 123 | 	len = min(len, fifo->size - fifo->in + fifo->out); | 
 | 124 |  | 
| Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 125 | 	/* | 
 | 126 | 	 * Ensure that we sample the fifo->out index -before- we | 
 | 127 | 	 * start putting bytes into the kfifo. | 
 | 128 | 	 */ | 
 | 129 |  | 
 | 130 | 	smp_mb(); | 
 | 131 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 	/* first put the data starting from fifo->in to buffer end */ | 
 | 133 | 	l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); | 
 | 134 | 	memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l); | 
 | 135 |  | 
 | 136 | 	/* then put the rest (if any) at the beginning of the buffer */ | 
 | 137 | 	memcpy(fifo->buffer, buffer + l, len - l); | 
 | 138 |  | 
| Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 139 | 	/* | 
 | 140 | 	 * Ensure that we add the bytes to the kfifo -before- | 
 | 141 | 	 * we update the fifo->in index. | 
 | 142 | 	 */ | 
 | 143 |  | 
 | 144 | 	smp_wmb(); | 
 | 145 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | 	fifo->in += len; | 
 | 147 |  | 
 | 148 | 	return len; | 
 | 149 | } | 
 | 150 | EXPORT_SYMBOL(__kfifo_put); | 
 | 151 |  | 
 | 152 | /** | 
 | 153 |  * __kfifo_get - gets some data from the FIFO, no locking version | 
 | 154 |  * @fifo: the fifo to be used. | 
 | 155 |  * @buffer: where the data must be copied. | 
 | 156 |  * @len: the size of the destination buffer. | 
 | 157 |  * | 
 | 158 |  * This function copies at most 'len' bytes from the FIFO into the | 
 | 159 |  * 'buffer' and returns the number of copied bytes. | 
 | 160 |  * | 
 | 161 |  * Note that with only one concurrent reader and one concurrent | 
 | 162 |  * writer, you don't need extra locking to use these functions. | 
 | 163 |  */ | 
 | 164 | unsigned int __kfifo_get(struct kfifo *fifo, | 
 | 165 | 			 unsigned char *buffer, unsigned int len) | 
 | 166 | { | 
 | 167 | 	unsigned int l; | 
 | 168 |  | 
 | 169 | 	len = min(len, fifo->in - fifo->out); | 
 | 170 |  | 
| Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 171 | 	/* | 
 | 172 | 	 * Ensure that we sample the fifo->in index -before- we | 
 | 173 | 	 * start removing bytes from the kfifo. | 
 | 174 | 	 */ | 
 | 175 |  | 
 | 176 | 	smp_rmb(); | 
 | 177 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | 	/* first get the data from fifo->out until the end of the buffer */ | 
 | 179 | 	l = min(len, fifo->size - (fifo->out & (fifo->size - 1))); | 
 | 180 | 	memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l); | 
 | 181 |  | 
 | 182 | 	/* then get the rest (if any) from the beginning of the buffer */ | 
 | 183 | 	memcpy(buffer + l, fifo->buffer, len - l); | 
 | 184 |  | 
| Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 185 | 	/* | 
 | 186 | 	 * Ensure that we remove the bytes from the kfifo -before- | 
 | 187 | 	 * we update the fifo->out index. | 
 | 188 | 	 */ | 
 | 189 |  | 
 | 190 | 	smp_mb(); | 
 | 191 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | 	fifo->out += len; | 
 | 193 |  | 
 | 194 | 	return len; | 
 | 195 | } | 
 | 196 | EXPORT_SYMBOL(__kfifo_get); |