menu: simplify usage for clients
[barebox-mini2440.git] / lib / kfifo.c
blobbf5cee1de0909fff30552bf4e5ef97bd3e6b3671
1 /*
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.
22 #include <common.h>
23 #include <malloc.h>
24 #include <kfifo.h>
25 #include <errno.h>
27 /**
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)
39 struct kfifo *fifo;
41 fifo = malloc(sizeof(struct kfifo));
42 if (!fifo)
43 return NULL;
45 fifo->buffer = buffer;
46 fifo->size = size;
47 fifo->in = fifo->out = 0;
49 return fifo;
52 /**
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;
63 struct kfifo *ret;
65 buffer = malloc(size);
66 if (!buffer)
67 return NULL;
69 ret = kfifo_init(buffer, size);
71 if (!ret)
72 free(buffer);
74 return ret;
77 /**
78 * kfifo_free - frees the FIFO
79 * @fifo: the fifo to be freed.
81 void kfifo_free(struct kfifo *fifo)
83 free(fifo->buffer);
84 free(fifo);
87 /**
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
95 * bytes copied.
98 unsigned int kfifo_put(struct kfifo *fifo,
99 unsigned char *buffer, unsigned int len)
101 unsigned int l;
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);
112 fifo->in += len;
114 return len;
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)
132 unsigned int l;
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);
143 fifo->out += len;
145 return len;
148 void kfifo_putc(struct kfifo *fifo, unsigned char c)
150 *(fifo->buffer + (fifo->in & (fifo->size - 1))) = c;
151 fifo->in++;
152 if (fifo->in - fifo->out > fifo->size)
153 fifo->out++;
156 unsigned int kfifo_getc(struct kfifo *fifo, unsigned char *c)
158 if (fifo->in == fifo->out)
159 return -1;
161 *c = *(fifo->buffer + (fifo->out & (fifo->size - 1)));
162 fifo->out++;
164 return 0;