blob: 48eccd865bd8add9d9353f13771eab89745f0000 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#ifdef __KERNEL__
25
26#include <linux/kernel.h>
27#include <linux/spinlock.h>
28
29struct kfifo {
30 unsigned char *buffer; /* the buffer holding the data */
31 unsigned int size; /* the size of the allocated buffer */
32 unsigned int in; /* data is added at offset (in % size) */
33 unsigned int out; /* data is extracted from off. (out % size) */
34 spinlock_t *lock; /* protects concurrent modifications */
35};
36
37extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +010038 gfp_t gfp_mask, spinlock_t *lock);
39extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 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 * __kfifo_reset - removes the entire FIFO contents, no locking version
49 * @fifo: the fifo to be emptied.
50 */
51static inline void __kfifo_reset(struct kfifo *fifo)
52{
53 fifo->in = fifo->out = 0;
54}
55
56/**
57 * kfifo_reset - removes the entire FIFO contents
58 * @fifo: the fifo to be emptied.
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 * kfifo_put - puts some data into the FIFO
73 * @fifo: the fifo to be used.
74 * @buffer: the data to be added.
75 * @len: the length of the data to be added.
76 *
77 * This function copies at most 'len' bytes from the 'buffer' into
78 * the FIFO depending on the free space, and returns the number of
79 * bytes copied.
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 * kfifo_get - gets some data from the FIFO
98 * @fifo: the fifo to be used.
99 * @buffer: where the data must be copied.
100 * @len: the size of the destination buffer.
101 *
102 * This function copies at most 'len' bytes from the FIFO into the
103 * 'buffer' and returns the number of copied bytes.
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 * optimization: if the FIFO is empty, set the indices to 0
117 * so we don't wrap the next time
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 * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
129 * @fifo: the fifo to be used.
130 */
131static inline unsigned int __kfifo_len(struct kfifo *fifo)
132{
133 return fifo->in - fifo->out;
134}
135
136/**
137 * kfifo_len - returns the number of bytes available in the FIFO
138 * @fifo: the fifo to be used.
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 /* __KERNEL__ */
157#endif