2 * A generic kernel FIFO implementation.
4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
5 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef _LINUX_KFIFO_H
23 #define _LINUX_KFIFO_H
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
29 unsigned char *buffer; /* the buffer holding the data */
30 unsigned int size; /* the size of the allocated buffer */
31 unsigned int in; /* data is added at offset (in % size) */
32 unsigned int out; /* data is extracted from off. (out % size) */
33 spinlock_t *lock; /* protects concurrent modifications */
36 extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
37 unsigned int size, spinlock_t *lock);
38 extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
39 gfp_t gfp_mask, spinlock_t *lock);
40 extern void kfifo_free(struct kfifo *fifo);
41 extern unsigned int __kfifo_put(struct kfifo *fifo,
42 const unsigned char *buffer, unsigned int len);
43 extern unsigned int __kfifo_get(struct kfifo *fifo,
44 unsigned char *buffer, unsigned int len);
47 * __kfifo_reset - removes the entire FIFO contents, no locking version
48 * @fifo: the fifo to be emptied.
50 static inline void __kfifo_reset(struct kfifo *fifo)
52 fifo->in = fifo->out = 0;
56 * kfifo_reset - removes the entire FIFO contents
57 * @fifo: the fifo to be emptied.
59 static inline void kfifo_reset(struct kfifo *fifo)
63 spin_lock_irqsave(fifo->lock, flags);
67 spin_unlock_irqrestore(fifo->lock, flags);
71 * kfifo_put - puts some data into the FIFO
72 * @fifo: the fifo to be used.
73 * @buffer: the data to be added.
74 * @len: the length of the data to be added.
76 * This function copies at most @len bytes from the @buffer into
77 * the FIFO depending on the free space, and returns the number of
80 static inline unsigned int kfifo_put(struct kfifo *fifo,
81 const unsigned char *buffer, unsigned int len)
86 spin_lock_irqsave(fifo->lock, flags);
88 ret = __kfifo_put(fifo, buffer, len);
90 spin_unlock_irqrestore(fifo->lock, flags);
96 * kfifo_get - gets some data from the FIFO
97 * @fifo: the fifo to be used.
98 * @buffer: where the data must be copied.
99 * @len: the size of the destination buffer.
101 * This function copies at most @len bytes from the FIFO into the
102 * @buffer and returns the number of copied bytes.
104 static inline unsigned int kfifo_get(struct kfifo *fifo,
105 unsigned char *buffer, unsigned int len)
110 spin_lock_irqsave(fifo->lock, flags);
112 ret = __kfifo_get(fifo, buffer, len);
115 * optimization: if the FIFO is empty, set the indices to 0
116 * so we don't wrap the next time
118 if (fifo->in == fifo->out)
119 fifo->in = fifo->out = 0;
121 spin_unlock_irqrestore(fifo->lock, flags);
127 * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
128 * @fifo: the fifo to be used.
130 static inline unsigned int __kfifo_len(struct kfifo *fifo)
132 return fifo->in - fifo->out;
136 * kfifo_len - returns the number of bytes available in the FIFO
137 * @fifo: the fifo to be used.
139 static inline unsigned int kfifo_len(struct kfifo *fifo)
144 spin_lock_irqsave(fifo->lock, flags);
146 ret = __kfifo_len(fifo);
148 spin_unlock_irqrestore(fifo->lock, flags);