]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/kfifo.h
kfifo: move struct kfifo in place
[mirror_ubuntu-bionic-kernel.git] / include / linux / kfifo.h
1 /*
2 * A generic kernel FIFO implementation.
3 *
4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
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
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) */
33 spinlock_t *lock; /* protects concurrent modifications */
34 };
35
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);
45
46 /**
47 * __kfifo_reset - removes the entire FIFO contents, no locking version
48 * @fifo: the fifo to be emptied.
49 */
50 static inline void __kfifo_reset(struct kfifo *fifo)
51 {
52 fifo->in = fifo->out = 0;
53 }
54
55 /**
56 * kfifo_reset - removes the entire FIFO contents
57 * @fifo: the fifo to be emptied.
58 */
59 static inline void kfifo_reset(struct kfifo *fifo)
60 {
61 unsigned long flags;
62
63 spin_lock_irqsave(fifo->lock, flags);
64
65 __kfifo_reset(fifo);
66
67 spin_unlock_irqrestore(fifo->lock, flags);
68 }
69
70 /**
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.
75 *
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
78 * bytes copied.
79 */
80 static inline unsigned int kfifo_put(struct kfifo *fifo,
81 const unsigned char *buffer, unsigned int len)
82 {
83 unsigned long flags;
84 unsigned int ret;
85
86 spin_lock_irqsave(fifo->lock, flags);
87
88 ret = __kfifo_put(fifo, buffer, len);
89
90 spin_unlock_irqrestore(fifo->lock, flags);
91
92 return ret;
93 }
94
95 /**
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.
100 *
101 * This function copies at most @len bytes from the FIFO into the
102 * @buffer and returns the number of copied bytes.
103 */
104 static inline unsigned int kfifo_get(struct kfifo *fifo,
105 unsigned char *buffer, unsigned int len)
106 {
107 unsigned long flags;
108 unsigned int ret;
109
110 spin_lock_irqsave(fifo->lock, flags);
111
112 ret = __kfifo_get(fifo, buffer, len);
113
114 /*
115 * optimization: if the FIFO is empty, set the indices to 0
116 * so we don't wrap the next time
117 */
118 if (fifo->in == fifo->out)
119 fifo->in = fifo->out = 0;
120
121 spin_unlock_irqrestore(fifo->lock, flags);
122
123 return ret;
124 }
125
126 /**
127 * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
128 * @fifo: the fifo to be used.
129 */
130 static inline unsigned int __kfifo_len(struct kfifo *fifo)
131 {
132 return fifo->in - fifo->out;
133 }
134
135 /**
136 * kfifo_len - returns the number of bytes available in the FIFO
137 * @fifo: the fifo to be used.
138 */
139 static inline unsigned int kfifo_len(struct kfifo *fifo)
140 {
141 unsigned long flags;
142 unsigned int ret;
143
144 spin_lock_irqsave(fifo->lock, flags);
145
146 ret = __kfifo_len(fifo);
147
148 spin_unlock_irqrestore(fifo->lock, flags);
149
150 return ret;
151 }
152
153 #endif