2 * A simple kernel FIFO implementation.
4 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
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.
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.
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.
28 * kfifo_init - allocates a new FIFO using a preallocated buffer
29 * @buffer: the preallocated buffer to be used.
30 * @size: the size of the internal buffer, this have to be a power of 2.
31 * @gfp_mask: get_free_pages mask, passed to kmalloc()
32 * @lock: the lock to be used to protect the fifo buffer
34 * Do NOT pass the kfifo to kfifo_free() after use! Simply free the
35 * &struct kfifo with free().
37 struct kfifo
*kfifo_init(unsigned char *buffer
, unsigned int size
)
41 fifo
= malloc(sizeof(struct kfifo
));
45 fifo
->buffer
= buffer
;
47 fifo
->in
= fifo
->out
= 0;
53 * kfifo_alloc - allocates a new FIFO and its internal buffer
54 * @size: the size of the internal buffer to be allocated.
55 * @gfp_mask: get_free_pages mask, passed to kmalloc()
56 * @lock: the lock to be used to protect the fifo buffer
58 * The size will be rounded-up to a power of 2.
60 struct kfifo
*kfifo_alloc(unsigned int size
)
62 unsigned char *buffer
;
65 buffer
= malloc(size
);
69 ret
= kfifo_init(buffer
, size
);
78 * kfifo_free - frees the FIFO
79 * @fifo: the fifo to be freed.
81 void kfifo_free(struct kfifo
*fifo
)
88 * kfifo_put - puts some data into the FIFO, no locking version
89 * @fifo: the fifo to be used.
90 * @buffer: the data to be added.
91 * @len: the length of the data to be added.
93 * This function copies at most @len bytes from the @buffer into
94 * the FIFO depending on the free space, and returns the number of
98 unsigned int kfifo_put(struct kfifo
*fifo
,
99 unsigned char *buffer
, unsigned int len
)
103 len
= min(len
, fifo
->size
- fifo
->in
+ fifo
->out
);
105 /* first put the data starting from fifo->in to buffer end */
106 l
= min(len
, fifo
->size
- (fifo
->in
& (fifo
->size
- 1)));
107 memcpy(fifo
->buffer
+ (fifo
->in
& (fifo
->size
- 1)), buffer
, l
);
109 /* then put the rest (if any) at the beginning of the buffer */
110 memcpy(fifo
->buffer
, buffer
+ l
, len
- l
);
118 * kfifo_get - gets some data from the FIFO, no locking version
119 * @fifo: the fifo to be used.
120 * @buffer: where the data must be copied.
121 * @len: the size of the destination buffer.
123 * This function copies at most @len bytes from the FIFO into the
124 * @buffer and returns the number of copied bytes.
126 * Note that with only one concurrent reader and one concurrent
127 * writer, you don't need extra locking to use these functions.
129 unsigned int kfifo_get(struct kfifo
*fifo
,
130 unsigned char *buffer
, unsigned int len
)
134 len
= min(len
, fifo
->in
- fifo
->out
);
136 /* first get the data from fifo->out until the end of the buffer */
137 l
= min(len
, fifo
->size
- (fifo
->out
& (fifo
->size
- 1)));
138 memcpy(buffer
, fifo
->buffer
+ (fifo
->out
& (fifo
->size
- 1)), l
);
140 /* then get the rest (if any) from the beginning of the buffer */
141 memcpy(buffer
+ l
, fifo
->buffer
, len
- l
);
148 void kfifo_putc(struct kfifo
*fifo
, unsigned char c
)
150 *(fifo
->buffer
+ (fifo
->in
& (fifo
->size
- 1))) = c
;
152 if (fifo
->in
- fifo
->out
> fifo
->size
)
156 unsigned int kfifo_getc(struct kfifo
*fifo
, unsigned char *c
)
158 if (fifo
->in
== fifo
->out
)
161 *c
= *(fifo
->buffer
+ (fifo
->out
& (fifo
->size
- 1)));