blob: ad6bdf5a5970c5fdbceb6b5c440a196b7a620b22 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/spinlock.h>
26
27struct 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
35extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +010036 gfp_t gfp_mask, spinlock_t *lock);
37extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 spinlock_t *lock);
39extern void kfifo_free(struct kfifo *fifo);
40extern unsigned int __kfifo_put(struct kfifo *fifo,
Alan Coxf8a7c1a2009-09-19 13:13:17 -070041 const unsigned char *buffer, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042extern unsigned int __kfifo_get(struct kfifo *fifo,
43 unsigned char *buffer, unsigned int len);
44
45/**
46 * __kfifo_reset - removes the entire FIFO contents, no locking version
47 * @fifo: the fifo to be emptied.
48 */
49static inline void __kfifo_reset(struct kfifo *fifo)
50{
51 fifo->in = fifo->out = 0;
52}
53
54/**
55 * kfifo_reset - removes the entire FIFO contents
56 * @fifo: the fifo to be emptied.
57 */
58static inline void kfifo_reset(struct kfifo *fifo)
59{
60 unsigned long flags;
61
62 spin_lock_irqsave(fifo->lock, flags);
63
64 __kfifo_reset(fifo);
65
66 spin_unlock_irqrestore(fifo->lock, flags);
67}
68
69/**
70 * kfifo_put - puts some data into the FIFO
71 * @fifo: the fifo to be used.
72 * @buffer: the data to be added.
73 * @len: the length of the data to be added.
74 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080075 * This function copies at most @len bytes from the @buffer into
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * the FIFO depending on the free space, and returns the number of
77 * bytes copied.
78 */
79static inline unsigned int kfifo_put(struct kfifo *fifo,
Alan Coxf8a7c1a2009-09-19 13:13:17 -070080 const unsigned char *buffer, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 unsigned long flags;
83 unsigned int ret;
84
85 spin_lock_irqsave(fifo->lock, flags);
86
87 ret = __kfifo_put(fifo, buffer, len);
88
89 spin_unlock_irqrestore(fifo->lock, flags);
90
91 return ret;
92}
93
94/**
95 * kfifo_get - gets some data from the FIFO
96 * @fifo: the fifo to be used.
97 * @buffer: where the data must be copied.
98 * @len: the size of the destination buffer.
99 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800100 * This function copies at most @len bytes from the FIFO into the
101 * @buffer and returns the number of copied bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103static inline unsigned int kfifo_get(struct kfifo *fifo,
104 unsigned char *buffer, unsigned int len)
105{
106 unsigned long flags;
107 unsigned int ret;
108
109 spin_lock_irqsave(fifo->lock, flags);
110
111 ret = __kfifo_get(fifo, buffer, len);
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
120 spin_unlock_irqrestore(fifo->lock, flags);
121
122 return ret;
123}
124
125/**
126 * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
127 * @fifo: the fifo to be used.
128 */
129static inline unsigned int __kfifo_len(struct kfifo *fifo)
130{
131 return fifo->in - fifo->out;
132}
133
134/**
135 * kfifo_len - returns the number of bytes available in the FIFO
136 * @fifo: the fifo to be used.
137 */
138static inline unsigned int kfifo_len(struct kfifo *fifo)
139{
140 unsigned long flags;
141 unsigned int ret;
142
143 spin_lock_irqsave(fifo->lock, flags);
144
145 ret = __kfifo_len(fifo);
146
147 spin_unlock_irqrestore(fifo->lock, flags);
148
149 return ret;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif