]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/kfifo.h
17065f7681a036cd27494cef8f94be05137dc23e
[linux-2.6-omap-h63xx.git] / include / linux / kfifo.h
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 #ifndef _LINUX_KFIFO_H
22 #define _LINUX_KFIFO_H
23
24 #include <linux/kernel.h>
25 #include <linux/spinlock.h>
26
27 struct kfifo {
28         unsigned char *buffer;  /* the buffer holding the data */
29         unsigned int size;      /* the size of the allocated buffer */
30         unsigned int in;        /* data is added at offset (in % size) */
31         unsigned int out;       /* data is extracted from off. (out % size) */
32         spinlock_t *lock;       /* protects concurrent modifications */
33 };
34
35 extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
36                                 gfp_t gfp_mask, spinlock_t *lock);
37 extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
38                                  spinlock_t *lock);
39 extern void kfifo_free(struct kfifo *fifo);
40 extern unsigned int __kfifo_put(struct kfifo *fifo,
41                                 unsigned char *buffer, unsigned int len);
42 extern unsigned int __kfifo_get(struct kfifo *fifo,
43                                 unsigned char *buffer, unsigned int len);
44 extern unsigned int __kfifo_get_to_user(struct kfifo *fifo,
45                                         unsigned char __user *buffer,
46                                         unsigned int len);
47
48 /**
49  * __kfifo_reset - removes the entire FIFO contents, no locking version
50  * @fifo: the fifo to be emptied.
51  */
52 static inline void __kfifo_reset(struct kfifo *fifo)
53 {
54         fifo->in = fifo->out = 0;
55 }
56
57 /**
58  * kfifo_reset - removes the entire FIFO contents
59  * @fifo: the fifo to be emptied.
60  */
61 static inline void kfifo_reset(struct kfifo *fifo)
62 {
63         unsigned long flags;
64
65         spin_lock_irqsave(fifo->lock, flags);
66
67         __kfifo_reset(fifo);
68
69         spin_unlock_irqrestore(fifo->lock, flags);
70 }
71
72 /**
73  * kfifo_put - puts some data into the FIFO
74  * @fifo: the fifo to be used.
75  * @buffer: the data to be added.
76  * @len: the length of the data to be added.
77  *
78  * This function copies at most @len bytes from the @buffer into
79  * the FIFO depending on the free space, and returns the number of
80  * bytes copied.
81  */
82 static inline unsigned int kfifo_put(struct kfifo *fifo,
83                                      unsigned char *buffer, unsigned int len)
84 {
85         unsigned long flags;
86         unsigned int ret;
87
88         spin_lock_irqsave(fifo->lock, flags);
89
90         ret = __kfifo_put(fifo, buffer, len);
91
92         spin_unlock_irqrestore(fifo->lock, flags);
93
94         return ret;
95 }
96
97 /**
98  * kfifo_get - gets some data from the FIFO
99  * @fifo: the fifo to be used.
100  * @buffer: where the data must be copied.
101  * @len: the size of the destination buffer.
102  *
103  * This function copies at most @len bytes from the FIFO into the
104  * @buffer and returns the number of copied bytes.
105  */
106 static inline unsigned int kfifo_get(struct kfifo *fifo,
107                                      unsigned char *buffer, unsigned int len)
108 {
109         unsigned long flags;
110         unsigned int ret;
111
112         spin_lock_irqsave(fifo->lock, flags);
113
114         ret = __kfifo_get(fifo, buffer, len);
115
116         /*
117          * optimization: if the FIFO is empty, set the indices to 0
118          * so we don't wrap the next time
119          */
120         if (fifo->in == fifo->out)
121                 fifo->in = fifo->out = 0;
122
123         spin_unlock_irqrestore(fifo->lock, flags);
124
125         return ret;
126 }
127
128 /**
129  * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
130  * @fifo: the fifo to be used.
131  */
132 static inline unsigned int __kfifo_len(struct kfifo *fifo)
133 {
134         return fifo->in - fifo->out;
135 }
136
137 /**
138  * kfifo_len - returns the number of bytes available in the FIFO
139  * @fifo: the fifo to be used.
140  */
141 static inline unsigned int kfifo_len(struct kfifo *fifo)
142 {
143         unsigned long flags;
144         unsigned int ret;
145
146         spin_lock_irqsave(fifo->lock, flags);
147
148         ret = __kfifo_len(fifo);
149
150         spin_unlock_irqrestore(fifo->lock, flags);
151
152         return ret;
153 }
154
155 /**
156  * kfifo_get_to_user - gets some data from the FIFO
157  * @fifo: the fifo to be used.
158  * @buffer: where the data must be copied. user buffer
159  * @len: the size of the destination buffer.
160  *
161  * This function copies at most @len bytes from the FIFO into the
162  * user @buffer and returns the number of copied bytes.
163  */
164 static inline unsigned int kfifo_get_to_user(struct kfifo *fifo,
165                                              unsigned char __user *buffer,
166                                              unsigned int len)
167 {
168         unsigned long flags;
169         unsigned int ret;
170
171         spin_lock_irqsave(fifo->lock, flags);
172
173         ret = __kfifo_get_to_user(fifo, buffer, len);
174
175         /*
176          * optimization: if the FIFO is empty, set the indices to 0
177          * so we don't wrap the next time
178          */
179         if (fifo->in == fifo->out)
180                 fifo->in = fifo->out = 0;
181
182         spin_unlock_irqrestore(fifo->lock, flags);
183
184         return ret;
185 }
186
187 #endif