2 * A generic kernel FIFO implementation.
4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
5 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
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.
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.
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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/err.h>
27 #include <linux/kfifo.h>
28 #include <linux/log2.h>
30 static void _kfifo_init(struct kfifo
*fifo
, unsigned char *buffer
,
33 fifo
->buffer
= buffer
;
40 * kfifo_init - initialize a FIFO using a preallocated buffer
41 * @fifo: the fifo to assign the buffer
42 * @buffer: the preallocated buffer to be used.
43 * @size: the size of the internal buffer, this have to be a power of 2.
46 void kfifo_init(struct kfifo
*fifo
, unsigned char *buffer
, unsigned int size
)
48 /* size must be a power of 2 */
49 BUG_ON(!is_power_of_2(size
));
51 _kfifo_init(fifo
, buffer
, size
);
53 EXPORT_SYMBOL(kfifo_init
);
56 * kfifo_alloc - allocates a new FIFO internal buffer
57 * @fifo: the fifo to assign then new buffer
58 * @size: the size of the buffer to be allocated, this have to be a power of 2.
59 * @gfp_mask: get_free_pages mask, passed to kmalloc()
61 * This function dynamically allocates a new fifo internal buffer
63 * The size will be rounded-up to a power of 2.
64 * The buffer will be release with kfifo_free().
65 * Return 0 if no error, otherwise the an error code
67 int kfifo_alloc(struct kfifo
*fifo
, unsigned int size
, gfp_t gfp_mask
)
69 unsigned char *buffer
;
72 * round up to the next power of 2, since our 'let the indices
73 * wrap' technique works only in this case.
75 if (!is_power_of_2(size
)) {
76 BUG_ON(size
> 0x80000000);
77 size
= roundup_pow_of_two(size
);
80 buffer
= kmalloc(size
, gfp_mask
);
82 _kfifo_init(fifo
, 0, 0);
86 _kfifo_init(fifo
, buffer
, size
);
90 EXPORT_SYMBOL(kfifo_alloc
);
93 * kfifo_free - frees the FIFO internal buffer
94 * @fifo: the fifo to be freed.
96 void kfifo_free(struct kfifo
*fifo
)
100 EXPORT_SYMBOL(kfifo_free
);
103 * kfifo_put - puts some data into the FIFO, no locking version
104 * @fifo: the fifo to be used.
105 * @buffer: the data to be added.
106 * @len: the length of the data to be added.
108 * This function copies at most @len bytes from the @buffer into
109 * the FIFO depending on the free space, and returns the number of
112 * Note that with only one concurrent reader and one concurrent
113 * writer, you don't need extra locking to use these functions.
115 unsigned int kfifo_put(struct kfifo
*fifo
,
116 const unsigned char *buffer
, unsigned int len
)
120 len
= min(len
, fifo
->size
- fifo
->in
+ fifo
->out
);
123 * Ensure that we sample the fifo->out index -before- we
124 * start putting bytes into the kfifo.
129 /* first put the data starting from fifo->in to buffer end */
130 l
= min(len
, fifo
->size
- (fifo
->in
& (fifo
->size
- 1)));
131 memcpy(fifo
->buffer
+ (fifo
->in
& (fifo
->size
- 1)), buffer
, l
);
133 /* then put the rest (if any) at the beginning of the buffer */
134 memcpy(fifo
->buffer
, buffer
+ l
, len
- l
);
137 * Ensure that we add the bytes to the kfifo -before-
138 * we update the fifo->in index.
147 EXPORT_SYMBOL(kfifo_put
);
150 * kfifo_get - gets some data from the FIFO, no locking version
151 * @fifo: the fifo to be used.
152 * @buffer: where the data must be copied.
153 * @len: the size of the destination buffer.
155 * This function copies at most @len bytes from the FIFO into the
156 * @buffer and returns the number of copied bytes.
158 * Note that with only one concurrent reader and one concurrent
159 * writer, you don't need extra locking to use these functions.
161 unsigned int kfifo_get(struct kfifo
*fifo
,
162 unsigned char *buffer
, unsigned int len
)
166 len
= min(len
, fifo
->in
- fifo
->out
);
169 * Ensure that we sample the fifo->in index -before- we
170 * start removing bytes from the kfifo.
175 /* first get the data from fifo->out until the end of the buffer */
176 l
= min(len
, fifo
->size
- (fifo
->out
& (fifo
->size
- 1)));
177 memcpy(buffer
, fifo
->buffer
+ (fifo
->out
& (fifo
->size
- 1)), l
);
179 /* then get the rest (if any) from the beginning of the buffer */
180 memcpy(buffer
+ l
, fifo
->buffer
, len
- l
);
183 * Ensure that we remove the bytes from the kfifo -before-
184 * we update the fifo->out index.
193 EXPORT_SYMBOL(kfifo_get
);
This page took 0.035389 seconds and 6 git commands to generate.