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