blob: 7ad6d32dd673d19b1a789bab112d81b5fdce7a3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefani Seibold45465482009-12-21 14:37:26 -08002 * A generic kernel FIFO implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Stefani Seibold45465482009-12-21 14:37:26 -08004 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
6 *
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.
11 *
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.
16 *
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.
20 *
21 */
Stefani Seibold7acd72e2009-12-21 14:37:28 -080022
23/*
24 * Howto porting drivers to the new generic fifo API:
25 *
26 * - Modify the declaration of the "struct kfifo *" object into a
27 * in-place "struct kfifo" object
28 * - Init the in-place object with kfifo_alloc() or kfifo_init()
29 * Note: The address of the in-place "struct kfifo" object must be
30 * passed as the first argument to this functions
31 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
32 * into kfifo_out
33 * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get
34 * into kfifo_out_locked
35 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
36 * must be passed now to the kfifo_in_locked and kfifo_out_locked
37 * as the last parameter.
38 * - All formerly name __kfifo_* functions has been renamed into kfifo_*
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifndef _LINUX_KFIFO_H
42#define _LINUX_KFIFO_H
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/kernel.h>
45#include <linux/spinlock.h>
46
47struct kfifo {
48 unsigned char *buffer; /* the buffer holding the data */
49 unsigned int size; /* the size of the allocated buffer */
50 unsigned int in; /* data is added at offset (in % size) */
51 unsigned int out; /* data is extracted from off. (out % size) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080054/*
55 * Macros for declaration and initialization of the kfifo datatype
56 */
57
58/* helper macro */
59#define __kfifo_initializer(s, b) \
60 (struct kfifo) { \
61 .size = s, \
62 .in = 0, \
63 .out = 0, \
64 .buffer = b \
65 }
66
67/**
68 * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
69 * @name: name of the declared kfifo datatype
70 * @size: size of the fifo buffer
71 *
Randy Dunlap9c717de2009-12-23 09:23:33 -080072 * Note1: the macro can be used inside struct or union declaration
73 * Note2: the macro creates two objects:
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080074 * A kfifo object with the given name and a buffer for the kfifo
75 * object named name##kfifo_buffer
76 */
77#define DECLARE_KFIFO(name, size) \
78union { \
79 struct kfifo name; \
80 unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
81}
82
83/**
Rolf Eike Beered656d82009-12-26 17:58:11 +010084 * INIT_KFIFO - Initialize a kfifo declared by DECLARE_KFIFO
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080085 * @name: name of the declared kfifo datatype
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080086 */
87#define INIT_KFIFO(name) \
88 name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
89 sizeof(struct kfifo), name##kfifo_buffer)
90
91/**
92 * DEFINE_KFIFO - macro to define and initialize a kfifo
93 * @name: name of the declared kfifo datatype
94 * @size: size of the fifo buffer
95 *
Randy Dunlap9c717de2009-12-23 09:23:33 -080096 * Note1: the macro can be used for global and local kfifo data type variables
97 * Note2: the macro creates two objects:
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080098 * A kfifo object with the given name and a buffer for the kfifo
99 * object named name##kfifo_buffer
100 */
101#define DEFINE_KFIFO(name, size) \
102 unsigned char name##kfifo_buffer[size]; \
103 struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
104
105#undef __kfifo_initializer
106
Andi Kleen8ecc2952010-01-15 17:01:12 -0800107extern void kfifo_init(struct kfifo *fifo, void *buffer,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800108 unsigned int size);
Stefani Seibold45465482009-12-21 14:37:26 -0800109extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800110 gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111extern void kfifo_free(struct kfifo *fifo);
Stefani Seibold9842c382009-12-21 14:37:29 -0800112extern unsigned int kfifo_in(struct kfifo *fifo,
Andi Kleen8ecc2952010-01-15 17:01:12 -0800113 const void *from, unsigned int len);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800114extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
Andi Kleen8ecc2952010-01-15 17:01:12 -0800115 void *to, unsigned int len);
Andi Kleena5b9e2c2010-01-15 17:01:16 -0800116extern __must_check unsigned int kfifo_out_peek(struct kfifo *fifo,
117 void *to, unsigned int len, unsigned offset);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * kfifo_reset - removes the entire FIFO contents
122 * @fifo: the fifo to be emptied.
123 */
124static inline void kfifo_reset(struct kfifo *fifo)
125{
Stefani Seibolde64c0262009-12-21 14:37:28 -0800126 fifo->in = fifo->out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/**
Stefani Seibolda121f242009-12-21 14:37:31 -0800130 * kfifo_reset_out - skip FIFO contents
131 * @fifo: the fifo to be emptied.
132 */
133static inline void kfifo_reset_out(struct kfifo *fifo)
134{
135 smp_mb();
136 fifo->out = fifo->in;
137}
138
139/**
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800140 * kfifo_size - returns the size of the fifo in bytes
141 * @fifo: the fifo to be used.
142 */
143static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
144{
145 return fifo->size;
146}
147
148/**
Stefani Seibolde64c0262009-12-21 14:37:28 -0800149 * kfifo_len - returns the number of used bytes in the FIFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800151 */
Stefani Seibolde64c0262009-12-21 14:37:28 -0800152static inline unsigned int kfifo_len(struct kfifo *fifo)
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800153{
154 register unsigned int out;
155
156 out = fifo->out;
157 smp_rmb();
158 return fifo->in - out;
159}
160
161/**
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800162 * kfifo_is_empty - returns true if the fifo is empty
163 * @fifo: the fifo to be used.
164 */
165static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
166{
167 return fifo->in == fifo->out;
168}
169
170/**
171 * kfifo_is_full - returns true if the fifo is full
172 * @fifo: the fifo to be used.
173 */
174static inline __must_check int kfifo_is_full(struct kfifo *fifo)
175{
176 return kfifo_len(fifo) == kfifo_size(fifo);
177}
178
179/**
180 * kfifo_avail - returns the number of bytes available in the FIFO
181 * @fifo: the fifo to be used.
182 */
183static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
184{
185 return kfifo_size(fifo) - kfifo_len(fifo);
186}
187
188/**
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800189 * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800190 * @fifo: the fifo to be used.
191 * @from: the data to be added.
192 * @n: the length of the data to be added.
193 * @lock: pointer to the spinlock to use for locking.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800195 * This function copies at most @len bytes from the @from buffer into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * the FIFO depending on the free space, and returns the number of
197 * bytes copied.
198 */
Stefani Seibold9842c382009-12-21 14:37:29 -0800199static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
Andi Kleen8ecc2952010-01-15 17:01:12 -0800200 const void *from, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long flags;
203 unsigned int ret;
204
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800205 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800207 ret = kfifo_in(fifo, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800209 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return ret;
212}
213
214/**
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800215 * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800217 * @to: where the data must be copied.
218 * @n: the size of the destination buffer.
219 * @lock: pointer to the spinlock to use for locking.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800221 * This function copies at most @len bytes from the FIFO into the
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800222 * @to buffer and returns the number of copied bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800224static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
Andi Kleen8ecc2952010-01-15 17:01:12 -0800225 void *to, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned long flags;
228 unsigned int ret;
229
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800230 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800232 ret = kfifo_out(fifo, to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800234 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 return ret;
237}
238
Stefani Seibolda121f242009-12-21 14:37:31 -0800239extern void kfifo_skip(struct kfifo *fifo, unsigned int len);
240
Andi Kleen64ce1032010-01-15 17:01:15 -0800241extern __must_check int kfifo_from_user(struct kfifo *fifo,
242 const void __user *from, unsigned int n, unsigned *lenout);
Stefani Seibolda121f242009-12-21 14:37:31 -0800243
Andi Kleen64ce1032010-01-15 17:01:15 -0800244extern __must_check int kfifo_to_user(struct kfifo *fifo,
245 void __user *to, unsigned int n, unsigned *lenout);
Stefani Seibolda121f242009-12-21 14:37:31 -0800246
Randy Dunlap9c717de2009-12-23 09:23:33 -0800247/*
Stefani Seibolda121f242009-12-21 14:37:31 -0800248 * __kfifo_add_out internal helper function for updating the out offset
249 */
250static inline void __kfifo_add_out(struct kfifo *fifo,
251 unsigned int off)
252{
253 smp_mb();
254 fifo->out += off;
255}
256
Randy Dunlap9c717de2009-12-23 09:23:33 -0800257/*
Stefani Seibolda121f242009-12-21 14:37:31 -0800258 * __kfifo_add_in internal helper function for updating the in offset
259 */
260static inline void __kfifo_add_in(struct kfifo *fifo,
261 unsigned int off)
262{
263 smp_wmb();
264 fifo->in += off;
265}
266
Randy Dunlap9c717de2009-12-23 09:23:33 -0800267/*
Stefani Seibolda121f242009-12-21 14:37:31 -0800268 * __kfifo_off internal helper function for calculating the index of a
269 * given offeset
270 */
271static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off)
272{
273 return off & (fifo->size - 1);
274}
275
Randy Dunlap9c717de2009-12-23 09:23:33 -0800276/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800277 * __kfifo_peek_n internal helper function for determinate the length of
278 * the next record in the fifo
279 */
280static inline unsigned int __kfifo_peek_n(struct kfifo *fifo,
281 unsigned int recsize)
282{
283#define __KFIFO_GET(fifo, off, shift) \
284 ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift))
285
286 unsigned int l;
287
288 l = __KFIFO_GET(fifo, 0, 0);
289
290 if (--recsize)
291 l |= __KFIFO_GET(fifo, 1, 8);
292
293 return l;
294#undef __KFIFO_GET
295}
296
Randy Dunlap9c717de2009-12-23 09:23:33 -0800297/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800298 * __kfifo_poke_n internal helper function for storing the length of
299 * the next record into the fifo
300 */
301static inline void __kfifo_poke_n(struct kfifo *fifo,
302 unsigned int recsize, unsigned int n)
303{
304#define __KFIFO_PUT(fifo, off, val, shift) \
305 ( \
306 (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \
307 (unsigned char)((val) >> (shift)) \
308 )
309
310 __KFIFO_PUT(fifo, 0, n, 0);
311
312 if (--recsize)
313 __KFIFO_PUT(fifo, 1, n, 8);
314#undef __KFIFO_PUT
315}
316
Randy Dunlap9c717de2009-12-23 09:23:33 -0800317/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800318 * __kfifo_in_... internal functions for put date into the fifo
319 * do not call it directly, use kfifo_in_rec() instead
320 */
321extern unsigned int __kfifo_in_n(struct kfifo *fifo,
322 const void *from, unsigned int n, unsigned int recsize);
323
324extern unsigned int __kfifo_in_generic(struct kfifo *fifo,
325 const void *from, unsigned int n, unsigned int recsize);
326
327static inline unsigned int __kfifo_in_rec(struct kfifo *fifo,
328 const void *from, unsigned int n, unsigned int recsize)
329{
330 unsigned int ret;
331
332 ret = __kfifo_in_n(fifo, from, n, recsize);
333
334 if (likely(ret == 0)) {
335 if (recsize)
336 __kfifo_poke_n(fifo, recsize, n);
337 __kfifo_add_in(fifo, n + recsize);
338 }
339 return ret;
340}
341
342/**
343 * kfifo_in_rec - puts some record data into the FIFO
344 * @fifo: the fifo to be used.
345 * @from: the data to be added.
346 * @n: the length of the data to be added.
347 * @recsize: size of record field
348 *
349 * This function copies @n bytes from the @from into the FIFO and returns
350 * the number of bytes which cannot be copied.
351 * A returned value greater than the @n value means that the record doesn't
352 * fit into the buffer.
353 *
354 * Note that with only one concurrent reader and one concurrent
355 * writer, you don't need extra locking to use these functions.
356 */
357static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo,
358 void *from, unsigned int n, unsigned int recsize)
359{
360 if (!__builtin_constant_p(recsize))
361 return __kfifo_in_generic(fifo, from, n, recsize);
362 return __kfifo_in_rec(fifo, from, n, recsize);
363}
364
Randy Dunlap9c717de2009-12-23 09:23:33 -0800365/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800366 * __kfifo_out_... internal functions for get date from the fifo
367 * do not call it directly, use kfifo_out_rec() instead
368 */
369extern unsigned int __kfifo_out_n(struct kfifo *fifo,
370 void *to, unsigned int reclen, unsigned int recsize);
371
372extern unsigned int __kfifo_out_generic(struct kfifo *fifo,
373 void *to, unsigned int n,
374 unsigned int recsize, unsigned int *total);
375
376static inline unsigned int __kfifo_out_rec(struct kfifo *fifo,
377 void *to, unsigned int n, unsigned int recsize,
378 unsigned int *total)
379{
380 unsigned int l;
381
382 if (!recsize) {
383 l = n;
384 if (total)
385 *total = l;
386 } else {
387 l = __kfifo_peek_n(fifo, recsize);
388 if (total)
389 *total = l;
390 if (n < l)
391 return l;
392 }
393
394 return __kfifo_out_n(fifo, to, l, recsize);
395}
396
397/**
398 * kfifo_out_rec - gets some record data from the FIFO
399 * @fifo: the fifo to be used.
400 * @to: where the data must be copied.
401 * @n: the size of the destination buffer.
402 * @recsize: size of record field
403 * @total: pointer where the total number of to copied bytes should stored
404 *
405 * This function copies at most @n bytes from the FIFO to @to and returns the
406 * number of bytes which cannot be copied.
407 * A returned value greater than the @n value means that the record doesn't
408 * fit into the @to buffer.
409 *
410 * Note that with only one concurrent reader and one concurrent
411 * writer, you don't need extra locking to use these functions.
412 */
413static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo,
414 void *to, unsigned int n, unsigned int recsize,
415 unsigned int *total)
416
417{
418 if (!__builtin_constant_p(recsize))
419 return __kfifo_out_generic(fifo, to, n, recsize, total);
420 return __kfifo_out_rec(fifo, to, n, recsize, total);
421}
422
Randy Dunlap9c717de2009-12-23 09:23:33 -0800423/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800424 * __kfifo_from_user_... internal functions for transfer from user space into
425 * the fifo. do not call it directly, use kfifo_from_user_rec() instead
426 */
427extern unsigned int __kfifo_from_user_n(struct kfifo *fifo,
428 const void __user *from, unsigned int n, unsigned int recsize);
429
430extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
431 const void __user *from, unsigned int n, unsigned int recsize);
432
433static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo,
434 const void __user *from, unsigned int n, unsigned int recsize)
435{
436 unsigned int ret;
437
438 ret = __kfifo_from_user_n(fifo, from, n, recsize);
439
440 if (likely(ret == 0)) {
441 if (recsize)
442 __kfifo_poke_n(fifo, recsize, n);
443 __kfifo_add_in(fifo, n + recsize);
444 }
445 return ret;
446}
447
448/**
449 * kfifo_from_user_rec - puts some data from user space into the FIFO
450 * @fifo: the fifo to be used.
451 * @from: pointer to the data to be added.
452 * @n: the length of the data to be added.
453 * @recsize: size of record field
454 *
455 * This function copies @n bytes from the @from into the
456 * FIFO and returns the number of bytes which cannot be copied.
457 *
458 * If the returned value is equal or less the @n value, the copy_from_user()
459 * functions has failed. Otherwise the record doesn't fit into the buffer.
460 *
461 * Note that with only one concurrent reader and one concurrent
462 * writer, you don't need extra locking to use these functions.
463 */
464static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo,
465 const void __user *from, unsigned int n, unsigned int recsize)
466{
467 if (!__builtin_constant_p(recsize))
468 return __kfifo_from_user_generic(fifo, from, n, recsize);
469 return __kfifo_from_user_rec(fifo, from, n, recsize);
470}
471
Randy Dunlap9c717de2009-12-23 09:23:33 -0800472/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800473 * __kfifo_to_user_... internal functions for transfer fifo data into user space
474 * do not call it directly, use kfifo_to_user_rec() instead
475 */
476extern unsigned int __kfifo_to_user_n(struct kfifo *fifo,
477 void __user *to, unsigned int n, unsigned int reclen,
478 unsigned int recsize);
479
480extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
481 void __user *to, unsigned int n, unsigned int recsize,
482 unsigned int *total);
483
484static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo,
485 void __user *to, unsigned int n,
486 unsigned int recsize, unsigned int *total)
487{
488 unsigned int l;
489
490 if (!recsize) {
491 l = n;
492 if (total)
493 *total = l;
494 } else {
495 l = __kfifo_peek_n(fifo, recsize);
496 if (total)
497 *total = l;
498 if (n < l)
499 return l;
500 }
501
502 return __kfifo_to_user_n(fifo, to, n, l, recsize);
503}
504
505/**
506 * kfifo_to_user_rec - gets data from the FIFO and write it to user space
507 * @fifo: the fifo to be used.
508 * @to: where the data must be copied.
509 * @n: the size of the destination buffer.
510 * @recsize: size of record field
511 * @total: pointer where the total number of to copied bytes should stored
512 *
513 * This function copies at most @n bytes from the FIFO to the @to.
514 * In case of an error, the function returns the number of bytes which cannot
515 * be copied.
516 * If the returned value is equal or less the @n value, the copy_to_user()
517 * functions has failed. Otherwise the record doesn't fit into the @to buffer.
518 *
519 * Note that with only one concurrent reader and one concurrent
520 * writer, you don't need extra locking to use these functions.
521 */
522static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo,
523 void __user *to, unsigned int n, unsigned int recsize,
524 unsigned int *total)
525{
526 if (!__builtin_constant_p(recsize))
527 return __kfifo_to_user_generic(fifo, to, n, recsize, total);
528 return __kfifo_to_user_rec(fifo, to, n, recsize, total);
529}
530
Randy Dunlap9c717de2009-12-23 09:23:33 -0800531/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800532 * __kfifo_peek_... internal functions for peek into the next fifo record
533 * do not call it directly, use kfifo_peek_rec() instead
534 */
535extern unsigned int __kfifo_peek_generic(struct kfifo *fifo,
536 unsigned int recsize);
537
538/**
539 * kfifo_peek_rec - gets the size of the next FIFO record data
540 * @fifo: the fifo to be used.
541 * @recsize: size of record field
542 *
543 * This function returns the size of the next FIFO record in number of bytes
544 */
545static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo,
546 unsigned int recsize)
547{
548 if (!__builtin_constant_p(recsize))
549 return __kfifo_peek_generic(fifo, recsize);
550 if (!recsize)
551 return kfifo_len(fifo);
552 return __kfifo_peek_n(fifo, recsize);
553}
554
Randy Dunlap9c717de2009-12-23 09:23:33 -0800555/*
Stefani Seibold86d48802009-12-21 14:37:32 -0800556 * __kfifo_skip_... internal functions for skip the next fifo record
557 * do not call it directly, use kfifo_skip_rec() instead
558 */
559extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize);
560
561static inline void __kfifo_skip_rec(struct kfifo *fifo,
562 unsigned int recsize)
563{
564 unsigned int l;
565
566 if (recsize) {
567 l = __kfifo_peek_n(fifo, recsize);
568
569 if (l + recsize <= kfifo_len(fifo)) {
570 __kfifo_add_out(fifo, l + recsize);
571 return;
572 }
573 }
574 kfifo_reset_out(fifo);
575}
576
577/**
578 * kfifo_skip_rec - skip the next fifo out record
579 * @fifo: the fifo to be used.
580 * @recsize: size of record field
581 *
582 * This function skips the next FIFO record
583 */
584static inline void kfifo_skip_rec(struct kfifo *fifo,
585 unsigned int recsize)
586{
587 if (!__builtin_constant_p(recsize))
588 __kfifo_skip_generic(fifo, recsize);
589 else
590 __kfifo_skip_rec(fifo, recsize);
591}
592
593/**
594 * kfifo_avail_rec - returns the number of bytes available in a record FIFO
595 * @fifo: the fifo to be used.
596 * @recsize: size of record field
597 */
598static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo,
599 unsigned int recsize)
600{
601 unsigned int l = kfifo_size(fifo) - kfifo_len(fifo);
602
603 return (l > recsize) ? l - recsize : 0;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#endif