1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef _LINUX_KFIFO_H
22#define _LINUX_KFIFO_H
23
24#ifdef __KERNEL__
25
26#include <linux/kernel.h>
27#include <linux/spinlock.h>
28
29struct kfifo {
30 unsigned char *buffer;
31 unsigned int size;
32 unsigned int in;
33 unsigned int out;
34 spinlock_t *lock;
35};
36
37extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
38 int gfp_mask, spinlock_t *lock);
39extern struct kfifo *kfifo_alloc(unsigned int size, int gfp_mask,
40 spinlock_t *lock);
41extern void kfifo_free(struct kfifo *fifo);
42extern unsigned int __kfifo_put(struct kfifo *fifo,
43 unsigned char *buffer, unsigned int len);
44extern unsigned int __kfifo_get(struct kfifo *fifo,
45 unsigned char *buffer, unsigned int len);
46
47
48
49
50
51static inline void __kfifo_reset(struct kfifo *fifo)
52{
53 fifo->in = fifo->out = 0;
54}
55
56
57
58
59
60static inline void kfifo_reset(struct kfifo *fifo)
61{
62 unsigned long flags;
63
64 spin_lock_irqsave(fifo->lock, flags);
65
66 __kfifo_reset(fifo);
67
68 spin_unlock_irqrestore(fifo->lock, flags);
69}
70
71
72
73
74
75
76
77
78
79
80
81static inline unsigned int kfifo_put(struct kfifo *fifo,
82 unsigned char *buffer, unsigned int len)
83{
84 unsigned long flags;
85 unsigned int ret;
86
87 spin_lock_irqsave(fifo->lock, flags);
88
89 ret = __kfifo_put(fifo, buffer, len);
90
91 spin_unlock_irqrestore(fifo->lock, flags);
92
93 return ret;
94}
95
96
97
98
99
100
101
102
103
104
105static inline unsigned int kfifo_get(struct kfifo *fifo,
106 unsigned char *buffer, unsigned int len)
107{
108 unsigned long flags;
109 unsigned int ret;
110
111 spin_lock_irqsave(fifo->lock, flags);
112
113 ret = __kfifo_get(fifo, buffer, len);
114
115
116
117
118
119 if (fifo->in == fifo->out)
120 fifo->in = fifo->out = 0;
121
122 spin_unlock_irqrestore(fifo->lock, flags);
123
124 return ret;
125}
126
127
128
129
130
131static inline unsigned int __kfifo_len(struct kfifo *fifo)
132{
133 return fifo->in - fifo->out;
134}
135
136
137
138
139
140static inline unsigned int kfifo_len(struct kfifo *fifo)
141{
142 unsigned long flags;
143 unsigned int ret;
144
145 spin_lock_irqsave(fifo->lock, flags);
146
147 ret = __kfifo_len(fifo);
148
149 spin_unlock_irqrestore(fifo->lock, flags);
150
151 return ret;
152}
153
154#else
155#warning "don't include kernel headers in userspace"
156#endif
157#endif
158