]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
45465487 | 2 | * A generic kernel FIFO implementation. |
1da177e4 | 3 | * |
45465487 | 4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> |
1da177e4 LT |
5 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | * | |
21 | */ | |
22 | #ifndef _LINUX_KFIFO_H | |
23 | #define _LINUX_KFIFO_H | |
24 | ||
1da177e4 LT |
25 | #include <linux/kernel.h> |
26 | #include <linux/spinlock.h> | |
27 | ||
28 | struct kfifo { | |
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) */ | |
1da177e4 LT |
33 | }; |
34 | ||
45465487 | 35 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
c1e13f25 | 36 | unsigned int size); |
45465487 | 37 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
c1e13f25 | 38 | gfp_t gfp_mask); |
1da177e4 | 39 | extern void kfifo_free(struct kfifo *fifo); |
e64c026d | 40 | extern unsigned int kfifo_put(struct kfifo *fifo, |
f8a7c1a9 | 41 | const unsigned char *buffer, unsigned int len); |
e64c026d | 42 | extern unsigned int kfifo_get(struct kfifo *fifo, |
1da177e4 LT |
43 | unsigned char *buffer, unsigned int len); |
44 | ||
1da177e4 LT |
45 | /** |
46 | * kfifo_reset - removes the entire FIFO contents | |
47 | * @fifo: the fifo to be emptied. | |
48 | */ | |
49 | static inline void kfifo_reset(struct kfifo *fifo) | |
50 | { | |
e64c026d | 51 | fifo->in = fifo->out = 0; |
c1e13f25 SS |
52 | } |
53 | ||
54 | /** | |
e64c026d | 55 | * kfifo_len - returns the number of used bytes in the FIFO |
c1e13f25 SS |
56 | * @fifo: the fifo to be used. |
57 | */ | |
e64c026d | 58 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
c1e13f25 SS |
59 | { |
60 | register unsigned int out; | |
1da177e4 | 61 | |
c1e13f25 SS |
62 | out = fifo->out; |
63 | smp_rmb(); | |
64 | return fifo->in - out; | |
1da177e4 LT |
65 | } |
66 | ||
67 | /** | |
c1e13f25 | 68 | * kfifo_put_locked - puts some data into the FIFO using a spinlock for locking |
1da177e4 | 69 | * @fifo: the fifo to be used. |
c1e13f25 SS |
70 | * @from: the data to be added. |
71 | * @n: the length of the data to be added. | |
72 | * @lock: pointer to the spinlock to use for locking. | |
1da177e4 | 73 | * |
c1e13f25 | 74 | * This function copies at most @len bytes from the @from buffer into |
1da177e4 LT |
75 | * the FIFO depending on the free space, and returns the number of |
76 | * bytes copied. | |
77 | */ | |
c1e13f25 SS |
78 | static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo, |
79 | const unsigned char *from, unsigned int n, spinlock_t *lock) | |
1da177e4 LT |
80 | { |
81 | unsigned long flags; | |
82 | unsigned int ret; | |
83 | ||
c1e13f25 | 84 | spin_lock_irqsave(lock, flags); |
1da177e4 | 85 | |
e64c026d | 86 | ret = kfifo_put(fifo, from, n); |
1da177e4 | 87 | |
c1e13f25 | 88 | spin_unlock_irqrestore(lock, flags); |
1da177e4 LT |
89 | |
90 | return ret; | |
91 | } | |
92 | ||
93 | /** | |
c1e13f25 | 94 | * kfifo_get_locked - gets some data from the FIFO using a spinlock for locking |
1da177e4 | 95 | * @fifo: the fifo to be used. |
c1e13f25 SS |
96 | * @to: where the data must be copied. |
97 | * @n: the size of the destination buffer. | |
98 | * @lock: pointer to the spinlock to use for locking. | |
1da177e4 | 99 | * |
72fd4a35 | 100 | * This function copies at most @len bytes from the FIFO into the |
c1e13f25 | 101 | * @to buffer and returns the number of copied bytes. |
1da177e4 | 102 | */ |
c1e13f25 SS |
103 | static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo, |
104 | unsigned char *to, unsigned int n, spinlock_t *lock) | |
1da177e4 LT |
105 | { |
106 | unsigned long flags; | |
107 | unsigned int ret; | |
108 | ||
c1e13f25 | 109 | spin_lock_irqsave(lock, flags); |
1da177e4 | 110 | |
e64c026d | 111 | ret = kfifo_get(fifo, to, n); |
1da177e4 LT |
112 | |
113 | /* | |
114 | * optimization: if the FIFO is empty, set the indices to 0 | |
115 | * so we don't wrap the next time | |
116 | */ | |
117 | if (fifo->in == fifo->out) | |
118 | fifo->in = fifo->out = 0; | |
119 | ||
c1e13f25 | 120 | spin_unlock_irqrestore(lock, flags); |
1da177e4 LT |
121 | |
122 | return ret; | |
123 | } | |
124 | ||
1da177e4 | 125 | #endif |