2 * A generic kernel FIFO implementation
4 * Copyright (C) 2013 Stefani Seibold <stefani@seibold.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 #ifndef _LINUX_KFIFO_H
23 #define _LINUX_KFIFO_H
26 * How to porting drivers to the new generic FIFO API:
28 * - Modify the declaration of the "struct kfifo *" object into a
29 * in-place "struct kfifo" object
30 * - Init the in-place object with kfifo_alloc() or kfifo_init()
31 * Note: The address of the in-place "struct kfifo" object must be
32 * passed as the first argument to this functions
33 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
35 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
36 * into kfifo_out_spinlocked
37 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
38 * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
39 * as the last parameter
40 * - The formerly __kfifo_* functions are renamed into kfifo_*
44 * Note about locking: There is no locking required until only one reader
45 * and one writer is using the fifo and no kfifo_reset() will be called.
46 * kfifo_reset_out() can be safely used, until it will be only called
47 * in the reader thread.
48 * For multiple writer and one reader there is only a need to lock the writer.
49 * And vice versa for only one writer and multiple reader there is only a need
53 #include <linux/kernel.h>
54 #include <linux/spinlock.h>
55 #include <linux/stddef.h>
56 #include <linux/scatterlist.h>
66 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
68 struct __kfifo kfifo; \
70 const datatype *const_type; \
71 char (*rectype)[recsize]; \
73 ptrtype const *ptr_const; \
76 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
78 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
79 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
82 #define STRUCT_KFIFO(type, size) \
83 struct __STRUCT_KFIFO(type, size, 0, type)
85 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
87 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
91 #define STRUCT_KFIFO_PTR(type) \
92 struct __STRUCT_KFIFO_PTR(type, 0, type)
95 * define compatibility "struct kfifo" for dynamic allocated fifos
97 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
99 #define STRUCT_KFIFO_REC_1(size) \
100 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
102 #define STRUCT_KFIFO_REC_2(size) \
103 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
106 * define kfifo_rec types
108 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
109 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
112 * helper macro to distinguish between real in place fifo where the fifo
113 * array is a part of the structure and the fifo type where the array is
114 * outside of the fifo structure.
116 #define __is_kfifo_ptr(fifo) \
117 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
120 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
121 * @fifo: name of the declared fifo
122 * @type: type of the fifo elements
124 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
127 * DECLARE_KFIFO - macro to declare a fifo object
128 * @fifo: name of the declared fifo
129 * @type: type of the fifo elements
130 * @size: the number of elements in the fifo, this must be a power of 2
132 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
135 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
136 * @fifo: name of the declared fifo datatype
138 #define INIT_KFIFO(fifo) \
140 typeof(&(fifo)) __tmp = &(fifo); \
141 struct __kfifo *__kfifo = &__tmp->kfifo; \
144 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
145 __kfifo->esize = sizeof(*__tmp->buf); \
146 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
150 * DEFINE_KFIFO - macro to define and initialize a fifo
151 * @fifo: name of the declared fifo datatype
152 * @type: type of the fifo elements
153 * @size: the number of elements in the fifo, this must be a power of 2
155 * Note: the macro can be used for global and local fifo data type variables.
157 #define DEFINE_KFIFO(fifo, type, size) \
158 DECLARE_KFIFO(fifo, type, size) = \
164 .mask = __is_kfifo_ptr(&(fifo)) ? \
166 ARRAY_SIZE((fifo).buf) - 1, \
167 .esize = sizeof(*(fifo).buf), \
168 .data = __is_kfifo_ptr(&(fifo)) ? \
176 static inline unsigned int __must_check
177 __kfifo_uint_must_check_helper(unsigned int val)
182 static inline int __must_check
183 __kfifo_int_must_check_helper(int val)
189 * kfifo_initialized - Check if the fifo is initialized
190 * @fifo: address of the fifo to check
192 * Return %true if fifo is initialized, otherwise %false.
193 * Assumes the fifo was 0 before.
195 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
198 * kfifo_esize - returns the size of the element managed by the fifo
199 * @fifo: address of the fifo to be used
201 #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
204 * kfifo_recsize - returns the size of the record length field
205 * @fifo: address of the fifo to be used
207 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
210 * kfifo_size - returns the size of the fifo in elements
211 * @fifo: address of the fifo to be used
213 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
216 * kfifo_reset - removes the entire fifo content
217 * @fifo: address of the fifo to be used
219 * Note: usage of kfifo_reset() is dangerous. It should be only called when the
220 * fifo is exclusived locked or when it is secured that no other thread is
221 * accessing the fifo.
223 #define kfifo_reset(fifo) \
225 typeof((fifo) + 1) __tmp = (fifo); \
226 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
230 * kfifo_reset_out - skip fifo content
231 * @fifo: address of the fifo to be used
233 * Note: The usage of kfifo_reset_out() is safe until it will be only called
234 * from the reader thread and there is only one concurrent reader. Otherwise
235 * it is dangerous and must be handled in the same way as kfifo_reset().
237 #define kfifo_reset_out(fifo) \
239 typeof((fifo) + 1) __tmp = (fifo); \
240 __tmp->kfifo.out = __tmp->kfifo.in; \
244 * kfifo_len - returns the number of used elements in the fifo
245 * @fifo: address of the fifo to be used
247 #define kfifo_len(fifo) \
249 typeof((fifo) + 1) __tmpl = (fifo); \
250 __tmpl->kfifo.in - __tmpl->kfifo.out; \
254 * kfifo_is_empty - returns true if the fifo is empty
255 * @fifo: address of the fifo to be used
257 #define kfifo_is_empty(fifo) \
259 typeof((fifo) + 1) __tmpq = (fifo); \
260 __tmpq->kfifo.in == __tmpq->kfifo.out; \
264 * kfifo_is_full - returns true if the fifo is full
265 * @fifo: address of the fifo to be used
267 #define kfifo_is_full(fifo) \
269 typeof((fifo) + 1) __tmpq = (fifo); \
270 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
274 * kfifo_avail - returns the number of unused elements in the fifo
275 * @fifo: address of the fifo to be used
277 #define kfifo_avail(fifo) \
278 __kfifo_uint_must_check_helper( \
280 typeof((fifo) + 1) __tmpq = (fifo); \
281 const size_t __recsize = sizeof(*__tmpq->rectype); \
282 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
283 (__recsize) ? ((__avail <= __recsize) ? 0 : \
284 __kfifo_max_r(__avail - __recsize, __recsize)) : \
290 * kfifo_skip - skip output data
291 * @fifo: address of the fifo to be used
293 #define kfifo_skip(fifo) \
295 typeof((fifo) + 1) __tmp = (fifo); \
296 const size_t __recsize = sizeof(*__tmp->rectype); \
297 struct __kfifo *__kfifo = &__tmp->kfifo; \
299 __kfifo_skip_r(__kfifo, __recsize); \
305 * kfifo_peek_len - gets the size of the next fifo record
306 * @fifo: address of the fifo to be used
308 * This function returns the size of the next fifo record in number of bytes.
310 #define kfifo_peek_len(fifo) \
311 __kfifo_uint_must_check_helper( \
313 typeof((fifo) + 1) __tmp = (fifo); \
314 const size_t __recsize = sizeof(*__tmp->rectype); \
315 struct __kfifo *__kfifo = &__tmp->kfifo; \
316 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
317 __kfifo_len_r(__kfifo, __recsize); \
322 * kfifo_alloc - dynamically allocates a new fifo buffer
323 * @fifo: pointer to the fifo
324 * @size: the number of elements in the fifo, this must be a power of 2
325 * @gfp_mask: get_free_pages mask, passed to kmalloc()
327 * This macro dynamically allocates a new fifo buffer.
329 * The number of elements will be rounded-up to a power of 2.
330 * The fifo will be release with kfifo_free().
331 * Return 0 if no error, otherwise an error code.
333 #define kfifo_alloc(fifo, size, gfp_mask) \
334 __kfifo_int_must_check_helper( \
336 typeof((fifo) + 1) __tmp = (fifo); \
337 struct __kfifo *__kfifo = &__tmp->kfifo; \
338 __is_kfifo_ptr(__tmp) ? \
339 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
345 * kfifo_free - frees the fifo
346 * @fifo: the fifo to be freed
348 #define kfifo_free(fifo) \
350 typeof((fifo) + 1) __tmp = (fifo); \
351 struct __kfifo *__kfifo = &__tmp->kfifo; \
352 if (__is_kfifo_ptr(__tmp)) \
353 __kfifo_free(__kfifo); \
357 * kfifo_init - initialize a fifo using a preallocated buffer
358 * @fifo: the fifo to assign the buffer
359 * @buffer: the preallocated buffer to be used
360 * @size: the size of the internal buffer, this have to be a power of 2
362 * This macro initializes a fifo using a preallocated buffer.
364 * The number of elements will be rounded-up to a power of 2.
365 * Return 0 if no error, otherwise an error code.
367 #define kfifo_init(fifo, buffer, size) \
369 typeof((fifo) + 1) __tmp = (fifo); \
370 struct __kfifo *__kfifo = &__tmp->kfifo; \
371 __is_kfifo_ptr(__tmp) ? \
372 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
377 * kfifo_put - put data into the fifo
378 * @fifo: address of the fifo to be used
379 * @val: the data to be added
381 * This macro copies the given value into the fifo.
382 * It returns 0 if the fifo was full. Otherwise it returns the number
383 * processed elements.
385 * Note that with only one concurrent reader and one concurrent
386 * writer, you don't need extra locking to use these macro.
388 #define kfifo_put(fifo, val) \
390 typeof((fifo) + 1) __tmp = (fifo); \
391 typeof(*__tmp->const_type) __val = (val); \
392 unsigned int __ret; \
393 size_t __recsize = sizeof(*__tmp->rectype); \
394 struct __kfifo *__kfifo = &__tmp->kfifo; \
396 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
399 __ret = !kfifo_is_full(__tmp); \
401 (__is_kfifo_ptr(__tmp) ? \
402 ((typeof(__tmp->type))__kfifo->data) : \
404 )[__kfifo->in & __tmp->kfifo.mask] = \
405 *(typeof(__tmp->type))&__val; \
414 * kfifo_get - get data from the fifo
415 * @fifo: address of the fifo to be used
416 * @val: address where to store the data
418 * This macro reads the data from the fifo.
419 * It returns 0 if the fifo was empty. Otherwise it returns the number
420 * processed elements.
422 * Note that with only one concurrent reader and one concurrent
423 * writer, you don't need extra locking to use these macro.
425 #define kfifo_get(fifo, val) \
426 __kfifo_uint_must_check_helper( \
428 typeof((fifo) + 1) __tmp = (fifo); \
429 typeof(__tmp->ptr) __val = (val); \
430 unsigned int __ret; \
431 const size_t __recsize = sizeof(*__tmp->rectype); \
432 struct __kfifo *__kfifo = &__tmp->kfifo; \
434 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
437 __ret = !kfifo_is_empty(__tmp); \
439 *(typeof(__tmp->type))__val = \
440 (__is_kfifo_ptr(__tmp) ? \
441 ((typeof(__tmp->type))__kfifo->data) : \
443 )[__kfifo->out & __tmp->kfifo.mask]; \
453 * kfifo_peek - get data from the fifo without removing
454 * @fifo: address of the fifo to be used
455 * @val: address where to store the data
457 * This reads the data from the fifo without removing it from the fifo.
458 * It returns 0 if the fifo was empty. Otherwise it returns the number
459 * processed elements.
461 * Note that with only one concurrent reader and one concurrent
462 * writer, you don't need extra locking to use these macro.
464 #define kfifo_peek(fifo, val) \
465 __kfifo_uint_must_check_helper( \
467 typeof((fifo) + 1) __tmp = (fifo); \
468 typeof(__tmp->ptr) __val = (val); \
469 unsigned int __ret; \
470 const size_t __recsize = sizeof(*__tmp->rectype); \
471 struct __kfifo *__kfifo = &__tmp->kfifo; \
473 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
476 __ret = !kfifo_is_empty(__tmp); \
478 *(typeof(__tmp->type))__val = \
479 (__is_kfifo_ptr(__tmp) ? \
480 ((typeof(__tmp->type))__kfifo->data) : \
482 )[__kfifo->out & __tmp->kfifo.mask]; \
491 * kfifo_in - put data into the fifo
492 * @fifo: address of the fifo to be used
493 * @buf: the data to be added
494 * @n: number of elements to be added
496 * This macro copies the given buffer into the fifo and returns the
497 * number of copied elements.
499 * Note that with only one concurrent reader and one concurrent
500 * writer, you don't need extra locking to use these macro.
502 #define kfifo_in(fifo, buf, n) \
504 typeof((fifo) + 1) __tmp = (fifo); \
505 typeof(__tmp->ptr_const) __buf = (buf); \
506 unsigned long __n = (n); \
507 const size_t __recsize = sizeof(*__tmp->rectype); \
508 struct __kfifo *__kfifo = &__tmp->kfifo; \
510 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
511 __kfifo_in(__kfifo, __buf, __n); \
515 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
516 * @fifo: address of the fifo to be used
517 * @buf: the data to be added
518 * @n: number of elements to be added
519 * @lock: pointer to the spinlock to use for locking
521 * This macro copies the given values buffer into the fifo and returns the
522 * number of copied elements.
524 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
526 unsigned long __flags; \
527 unsigned int __ret; \
528 spin_lock_irqsave(lock, __flags); \
529 __ret = kfifo_in(fifo, buf, n); \
530 spin_unlock_irqrestore(lock, __flags); \
534 /* alias for kfifo_in_spinlocked, will be removed in a future release */
535 #define kfifo_in_locked(fifo, buf, n, lock) \
536 kfifo_in_spinlocked(fifo, buf, n, lock)
539 * kfifo_out - get data from the fifo
540 * @fifo: address of the fifo to be used
541 * @buf: pointer to the storage buffer
542 * @n: max. number of elements to get
544 * This macro get some data from the fifo and return the numbers of elements
547 * Note that with only one concurrent reader and one concurrent
548 * writer, you don't need extra locking to use these macro.
550 #define kfifo_out(fifo, buf, n) \
551 __kfifo_uint_must_check_helper( \
553 typeof((fifo) + 1) __tmp = (fifo); \
554 typeof(__tmp->ptr) __buf = (buf); \
555 unsigned long __n = (n); \
556 const size_t __recsize = sizeof(*__tmp->rectype); \
557 struct __kfifo *__kfifo = &__tmp->kfifo; \
559 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
560 __kfifo_out(__kfifo, __buf, __n); \
565 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
566 * @fifo: address of the fifo to be used
567 * @buf: pointer to the storage buffer
568 * @n: max. number of elements to get
569 * @lock: pointer to the spinlock to use for locking
571 * This macro get the data from the fifo and return the numbers of elements
574 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
575 __kfifo_uint_must_check_helper( \
577 unsigned long __flags; \
578 unsigned int __ret; \
579 spin_lock_irqsave(lock, __flags); \
580 __ret = kfifo_out(fifo, buf, n); \
581 spin_unlock_irqrestore(lock, __flags); \
586 /* alias for kfifo_out_spinlocked, will be removed in a future release */
587 #define kfifo_out_locked(fifo, buf, n, lock) \
588 kfifo_out_spinlocked(fifo, buf, n, lock)
591 * kfifo_from_user - puts some data from user space into the fifo
592 * @fifo: address of the fifo to be used
593 * @from: pointer to the data to be added
594 * @len: the length of the data to be added
595 * @copied: pointer to output variable to store the number of copied bytes
597 * This macro copies at most @len bytes from the @from into the
598 * fifo, depending of the available space and returns -EFAULT/0.
600 * Note that with only one concurrent reader and one concurrent
601 * writer, you don't need extra locking to use these macro.
603 #define kfifo_from_user(fifo, from, len, copied) \
604 __kfifo_uint_must_check_helper( \
606 typeof((fifo) + 1) __tmp = (fifo); \
607 const void __user *__from = (from); \
608 unsigned int __len = (len); \
609 unsigned int *__copied = (copied); \
610 const size_t __recsize = sizeof(*__tmp->rectype); \
611 struct __kfifo *__kfifo = &__tmp->kfifo; \
613 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
614 __kfifo_from_user(__kfifo, __from, __len, __copied); \
619 * kfifo_to_user - copies data from the fifo into user space
620 * @fifo: address of the fifo to be used
621 * @to: where the data must be copied
622 * @len: the size of the destination buffer
623 * @copied: pointer to output variable to store the number of copied bytes
625 * This macro copies at most @len bytes from the fifo into the
626 * @to buffer and returns -EFAULT/0.
628 * Note that with only one concurrent reader and one concurrent
629 * writer, you don't need extra locking to use these macro.
631 #define kfifo_to_user(fifo, to, len, copied) \
632 __kfifo_uint_must_check_helper( \
634 typeof((fifo) + 1) __tmp = (fifo); \
635 void __user *__to = (to); \
636 unsigned int __len = (len); \
637 unsigned int *__copied = (copied); \
638 const size_t __recsize = sizeof(*__tmp->rectype); \
639 struct __kfifo *__kfifo = &__tmp->kfifo; \
641 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
642 __kfifo_to_user(__kfifo, __to, __len, __copied); \
647 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
648 * @fifo: address of the fifo to be used
649 * @sgl: pointer to the scatterlist array
650 * @nents: number of entries in the scatterlist array
651 * @len: number of elements to transfer
653 * This macro fills a scatterlist for DMA input.
654 * It returns the number entries in the scatterlist array.
656 * Note that with only one concurrent reader and one concurrent
657 * writer, you don't need extra locking to use these macros.
659 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
661 typeof((fifo) + 1) __tmp = (fifo); \
662 struct scatterlist *__sgl = (sgl); \
663 int __nents = (nents); \
664 unsigned int __len = (len); \
665 const size_t __recsize = sizeof(*__tmp->rectype); \
666 struct __kfifo *__kfifo = &__tmp->kfifo; \
668 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
669 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
673 * kfifo_dma_in_finish - finish a DMA IN operation
674 * @fifo: address of the fifo to be used
675 * @len: number of bytes to received
677 * This macro finish a DMA IN operation. The in counter will be updated by
678 * the len parameter. No error checking will be done.
680 * Note that with only one concurrent reader and one concurrent
681 * writer, you don't need extra locking to use these macros.
683 #define kfifo_dma_in_finish(fifo, len) \
685 typeof((fifo) + 1) __tmp = (fifo); \
686 unsigned int __len = (len); \
687 const size_t __recsize = sizeof(*__tmp->rectype); \
688 struct __kfifo *__kfifo = &__tmp->kfifo; \
690 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
692 __kfifo->in += __len / sizeof(*__tmp->type); \
696 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
697 * @fifo: address of the fifo to be used
698 * @sgl: pointer to the scatterlist array
699 * @nents: number of entries in the scatterlist array
700 * @len: number of elements to transfer
702 * This macro fills a scatterlist for DMA output which at most @len bytes
704 * It returns the number entries in the scatterlist array.
705 * A zero means there is no space available and the scatterlist is not filled.
707 * Note that with only one concurrent reader and one concurrent
708 * writer, you don't need extra locking to use these macros.
710 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
712 typeof((fifo) + 1) __tmp = (fifo); \
713 struct scatterlist *__sgl = (sgl); \
714 int __nents = (nents); \
715 unsigned int __len = (len); \
716 const size_t __recsize = sizeof(*__tmp->rectype); \
717 struct __kfifo *__kfifo = &__tmp->kfifo; \
719 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
720 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
724 * kfifo_dma_out_finish - finish a DMA OUT operation
725 * @fifo: address of the fifo to be used
726 * @len: number of bytes transferred
728 * This macro finish a DMA OUT operation. The out counter will be updated by
729 * the len parameter. No error checking will be done.
731 * Note that with only one concurrent reader and one concurrent
732 * writer, you don't need extra locking to use these macros.
734 #define kfifo_dma_out_finish(fifo, len) \
736 typeof((fifo) + 1) __tmp = (fifo); \
737 unsigned int __len = (len); \
738 const size_t __recsize = sizeof(*__tmp->rectype); \
739 struct __kfifo *__kfifo = &__tmp->kfifo; \
741 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
743 __kfifo->out += __len / sizeof(*__tmp->type); \
747 * kfifo_out_peek - gets some data from the fifo
748 * @fifo: address of the fifo to be used
749 * @buf: pointer to the storage buffer
750 * @n: max. number of elements to get
752 * This macro get the data from the fifo and return the numbers of elements
753 * copied. The data is not removed from the fifo.
755 * Note that with only one concurrent reader and one concurrent
756 * writer, you don't need extra locking to use these macro.
758 #define kfifo_out_peek(fifo, buf, n) \
759 __kfifo_uint_must_check_helper( \
761 typeof((fifo) + 1) __tmp = (fifo); \
762 typeof(__tmp->ptr) __buf = (buf); \
763 unsigned long __n = (n); \
764 const size_t __recsize = sizeof(*__tmp->rectype); \
765 struct __kfifo *__kfifo = &__tmp->kfifo; \
767 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
768 __kfifo_out_peek(__kfifo, __buf, __n); \
772 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
773 size_t esize, gfp_t gfp_mask);
775 extern void __kfifo_free(struct __kfifo *fifo);
777 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
778 unsigned int size, size_t esize);
780 extern unsigned int __kfifo_in(struct __kfifo *fifo,
781 const void *buf, unsigned int len);
783 extern unsigned int __kfifo_out(struct __kfifo *fifo,
784 void *buf, unsigned int len);
786 extern int __kfifo_from_user(struct __kfifo *fifo,
787 const void __user *from, unsigned long len, unsigned int *copied);
789 extern int __kfifo_to_user(struct __kfifo *fifo,
790 void __user *to, unsigned long len, unsigned int *copied);
792 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
793 struct scatterlist *sgl, int nents, unsigned int len);
795 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
796 struct scatterlist *sgl, int nents, unsigned int len);
798 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
799 void *buf, unsigned int len);
801 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
802 const void *buf, unsigned int len, size_t recsize);
804 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
805 void *buf, unsigned int len, size_t recsize);
807 extern int __kfifo_from_user_r(struct __kfifo *fifo,
808 const void __user *from, unsigned long len, unsigned int *copied,
811 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
812 unsigned long len, unsigned int *copied, size_t recsize);
814 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
815 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
817 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
818 unsigned int len, size_t recsize);
820 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
821 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
823 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
825 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
827 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
829 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
830 void *buf, unsigned int len, size_t recsize);
832 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);