This source file includes following definitions.
- __kfifo_uint_must_check_helper
- __kfifo_int_must_check_helper
1
2
3
4
5
6
7
8 #ifndef _LINUX_KFIFO_H
9 #define _LINUX_KFIFO_H
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #include <linux/kernel.h>
40 #include <linux/spinlock.h>
41 #include <linux/stddef.h>
42 #include <linux/scatterlist.h>
43
44 struct __kfifo {
45 unsigned int in;
46 unsigned int out;
47 unsigned int mask;
48 unsigned int esize;
49 void *data;
50 };
51
52 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
53 union { \
54 struct __kfifo kfifo; \
55 datatype *type; \
56 const datatype *const_type; \
57 char (*rectype)[recsize]; \
58 ptrtype *ptr; \
59 ptrtype const *ptr_const; \
60 }
61
62 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
63 { \
64 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
65 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
66 }
67
68 #define STRUCT_KFIFO(type, size) \
69 struct __STRUCT_KFIFO(type, size, 0, type)
70
71 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
72 { \
73 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
74 type buf[0]; \
75 }
76
77 #define STRUCT_KFIFO_PTR(type) \
78 struct __STRUCT_KFIFO_PTR(type, 0, type)
79
80
81
82
83 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
84
85 #define STRUCT_KFIFO_REC_1(size) \
86 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
87
88 #define STRUCT_KFIFO_REC_2(size) \
89 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
90
91
92
93
94 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
95 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
96
97
98
99
100
101
102 #define __is_kfifo_ptr(fifo) \
103 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
104
105
106
107
108
109
110 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
111
112
113
114
115
116
117
118 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
119
120
121
122
123
124 #define INIT_KFIFO(fifo) \
125 (void)({ \
126 typeof(&(fifo)) __tmp = &(fifo); \
127 struct __kfifo *__kfifo = &__tmp->kfifo; \
128 __kfifo->in = 0; \
129 __kfifo->out = 0; \
130 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
131 __kfifo->esize = sizeof(*__tmp->buf); \
132 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
133 })
134
135
136
137
138
139
140
141
142
143 #define DEFINE_KFIFO(fifo, type, size) \
144 DECLARE_KFIFO(fifo, type, size) = \
145 (typeof(fifo)) { \
146 { \
147 { \
148 .in = 0, \
149 .out = 0, \
150 .mask = __is_kfifo_ptr(&(fifo)) ? \
151 0 : \
152 ARRAY_SIZE((fifo).buf) - 1, \
153 .esize = sizeof(*(fifo).buf), \
154 .data = __is_kfifo_ptr(&(fifo)) ? \
155 NULL : \
156 (fifo).buf, \
157 } \
158 } \
159 }
160
161
162 static inline unsigned int __must_check
163 __kfifo_uint_must_check_helper(unsigned int val)
164 {
165 return val;
166 }
167
168 static inline int __must_check
169 __kfifo_int_must_check_helper(int val)
170 {
171 return val;
172 }
173
174
175
176
177
178
179
180
181 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
182
183
184
185
186
187 #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
188
189
190
191
192
193 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
194
195
196
197
198
199 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
200
201
202
203
204
205
206
207
208
209 #define kfifo_reset(fifo) \
210 (void)({ \
211 typeof((fifo) + 1) __tmp = (fifo); \
212 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
213 })
214
215
216
217
218
219
220
221
222
223 #define kfifo_reset_out(fifo) \
224 (void)({ \
225 typeof((fifo) + 1) __tmp = (fifo); \
226 __tmp->kfifo.out = __tmp->kfifo.in; \
227 })
228
229
230
231
232
233 #define kfifo_len(fifo) \
234 ({ \
235 typeof((fifo) + 1) __tmpl = (fifo); \
236 __tmpl->kfifo.in - __tmpl->kfifo.out; \
237 })
238
239
240
241
242
243 #define kfifo_is_empty(fifo) \
244 ({ \
245 typeof((fifo) + 1) __tmpq = (fifo); \
246 __tmpq->kfifo.in == __tmpq->kfifo.out; \
247 })
248
249
250
251
252
253 #define kfifo_is_full(fifo) \
254 ({ \
255 typeof((fifo) + 1) __tmpq = (fifo); \
256 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
257 })
258
259
260
261
262
263 #define kfifo_avail(fifo) \
264 __kfifo_uint_must_check_helper( \
265 ({ \
266 typeof((fifo) + 1) __tmpq = (fifo); \
267 const size_t __recsize = sizeof(*__tmpq->rectype); \
268 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
269 (__recsize) ? ((__avail <= __recsize) ? 0 : \
270 __kfifo_max_r(__avail - __recsize, __recsize)) : \
271 __avail; \
272 }) \
273 )
274
275
276
277
278
279 #define kfifo_skip(fifo) \
280 (void)({ \
281 typeof((fifo) + 1) __tmp = (fifo); \
282 const size_t __recsize = sizeof(*__tmp->rectype); \
283 struct __kfifo *__kfifo = &__tmp->kfifo; \
284 if (__recsize) \
285 __kfifo_skip_r(__kfifo, __recsize); \
286 else \
287 __kfifo->out++; \
288 })
289
290
291
292
293
294
295
296 #define kfifo_peek_len(fifo) \
297 __kfifo_uint_must_check_helper( \
298 ({ \
299 typeof((fifo) + 1) __tmp = (fifo); \
300 const size_t __recsize = sizeof(*__tmp->rectype); \
301 struct __kfifo *__kfifo = &__tmp->kfifo; \
302 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
303 __kfifo_len_r(__kfifo, __recsize); \
304 }) \
305 )
306
307
308
309
310
311
312
313
314
315
316
317
318
319 #define kfifo_alloc(fifo, size, gfp_mask) \
320 __kfifo_int_must_check_helper( \
321 ({ \
322 typeof((fifo) + 1) __tmp = (fifo); \
323 struct __kfifo *__kfifo = &__tmp->kfifo; \
324 __is_kfifo_ptr(__tmp) ? \
325 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
326 -EINVAL; \
327 }) \
328 )
329
330
331
332
333
334 #define kfifo_free(fifo) \
335 ({ \
336 typeof((fifo) + 1) __tmp = (fifo); \
337 struct __kfifo *__kfifo = &__tmp->kfifo; \
338 if (__is_kfifo_ptr(__tmp)) \
339 __kfifo_free(__kfifo); \
340 })
341
342
343
344
345
346
347
348
349
350
351
352
353 #define kfifo_init(fifo, buffer, size) \
354 ({ \
355 typeof((fifo) + 1) __tmp = (fifo); \
356 struct __kfifo *__kfifo = &__tmp->kfifo; \
357 __is_kfifo_ptr(__tmp) ? \
358 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
359 -EINVAL; \
360 })
361
362
363
364
365
366
367
368
369
370
371
372
373
374 #define kfifo_put(fifo, val) \
375 ({ \
376 typeof((fifo) + 1) __tmp = (fifo); \
377 typeof(*__tmp->const_type) __val = (val); \
378 unsigned int __ret; \
379 size_t __recsize = sizeof(*__tmp->rectype); \
380 struct __kfifo *__kfifo = &__tmp->kfifo; \
381 if (__recsize) \
382 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
383 __recsize); \
384 else { \
385 __ret = !kfifo_is_full(__tmp); \
386 if (__ret) { \
387 (__is_kfifo_ptr(__tmp) ? \
388 ((typeof(__tmp->type))__kfifo->data) : \
389 (__tmp->buf) \
390 )[__kfifo->in & __tmp->kfifo.mask] = \
391 *(typeof(__tmp->type))&__val; \
392 smp_wmb(); \
393 __kfifo->in++; \
394 } \
395 } \
396 __ret; \
397 })
398
399
400
401
402
403
404
405
406
407
408
409
410
411 #define kfifo_get(fifo, val) \
412 __kfifo_uint_must_check_helper( \
413 ({ \
414 typeof((fifo) + 1) __tmp = (fifo); \
415 typeof(__tmp->ptr) __val = (val); \
416 unsigned int __ret; \
417 const size_t __recsize = sizeof(*__tmp->rectype); \
418 struct __kfifo *__kfifo = &__tmp->kfifo; \
419 if (__recsize) \
420 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
421 __recsize); \
422 else { \
423 __ret = !kfifo_is_empty(__tmp); \
424 if (__ret) { \
425 *(typeof(__tmp->type))__val = \
426 (__is_kfifo_ptr(__tmp) ? \
427 ((typeof(__tmp->type))__kfifo->data) : \
428 (__tmp->buf) \
429 )[__kfifo->out & __tmp->kfifo.mask]; \
430 smp_wmb(); \
431 __kfifo->out++; \
432 } \
433 } \
434 __ret; \
435 }) \
436 )
437
438
439
440
441
442
443
444
445
446
447
448
449
450 #define kfifo_peek(fifo, val) \
451 __kfifo_uint_must_check_helper( \
452 ({ \
453 typeof((fifo) + 1) __tmp = (fifo); \
454 typeof(__tmp->ptr) __val = (val); \
455 unsigned int __ret; \
456 const size_t __recsize = sizeof(*__tmp->rectype); \
457 struct __kfifo *__kfifo = &__tmp->kfifo; \
458 if (__recsize) \
459 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
460 __recsize); \
461 else { \
462 __ret = !kfifo_is_empty(__tmp); \
463 if (__ret) { \
464 *(typeof(__tmp->type))__val = \
465 (__is_kfifo_ptr(__tmp) ? \
466 ((typeof(__tmp->type))__kfifo->data) : \
467 (__tmp->buf) \
468 )[__kfifo->out & __tmp->kfifo.mask]; \
469 smp_wmb(); \
470 } \
471 } \
472 __ret; \
473 }) \
474 )
475
476
477
478
479
480
481
482
483
484
485
486
487
488 #define kfifo_in(fifo, buf, n) \
489 ({ \
490 typeof((fifo) + 1) __tmp = (fifo); \
491 typeof(__tmp->ptr_const) __buf = (buf); \
492 unsigned long __n = (n); \
493 const size_t __recsize = sizeof(*__tmp->rectype); \
494 struct __kfifo *__kfifo = &__tmp->kfifo; \
495 (__recsize) ?\
496 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
497 __kfifo_in(__kfifo, __buf, __n); \
498 })
499
500
501
502
503
504
505
506
507
508
509
510 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
511 ({ \
512 unsigned long __flags; \
513 unsigned int __ret; \
514 spin_lock_irqsave(lock, __flags); \
515 __ret = kfifo_in(fifo, buf, n); \
516 spin_unlock_irqrestore(lock, __flags); \
517 __ret; \
518 })
519
520
521 #define kfifo_in_locked(fifo, buf, n, lock) \
522 kfifo_in_spinlocked(fifo, buf, n, lock)
523
524
525
526
527
528
529
530
531
532
533
534
535
536 #define kfifo_out(fifo, buf, n) \
537 __kfifo_uint_must_check_helper( \
538 ({ \
539 typeof((fifo) + 1) __tmp = (fifo); \
540 typeof(__tmp->ptr) __buf = (buf); \
541 unsigned long __n = (n); \
542 const size_t __recsize = sizeof(*__tmp->rectype); \
543 struct __kfifo *__kfifo = &__tmp->kfifo; \
544 (__recsize) ?\
545 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
546 __kfifo_out(__kfifo, __buf, __n); \
547 }) \
548 )
549
550
551
552
553
554
555
556
557
558
559
560 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
561 __kfifo_uint_must_check_helper( \
562 ({ \
563 unsigned long __flags; \
564 unsigned int __ret; \
565 spin_lock_irqsave(lock, __flags); \
566 __ret = kfifo_out(fifo, buf, n); \
567 spin_unlock_irqrestore(lock, __flags); \
568 __ret; \
569 }) \
570 )
571
572
573 #define kfifo_out_locked(fifo, buf, n, lock) \
574 kfifo_out_spinlocked(fifo, buf, n, lock)
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589 #define kfifo_from_user(fifo, from, len, copied) \
590 __kfifo_uint_must_check_helper( \
591 ({ \
592 typeof((fifo) + 1) __tmp = (fifo); \
593 const void __user *__from = (from); \
594 unsigned int __len = (len); \
595 unsigned int *__copied = (copied); \
596 const size_t __recsize = sizeof(*__tmp->rectype); \
597 struct __kfifo *__kfifo = &__tmp->kfifo; \
598 (__recsize) ? \
599 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
600 __kfifo_from_user(__kfifo, __from, __len, __copied); \
601 }) \
602 )
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617 #define kfifo_to_user(fifo, to, len, copied) \
618 __kfifo_uint_must_check_helper( \
619 ({ \
620 typeof((fifo) + 1) __tmp = (fifo); \
621 void __user *__to = (to); \
622 unsigned int __len = (len); \
623 unsigned int *__copied = (copied); \
624 const size_t __recsize = sizeof(*__tmp->rectype); \
625 struct __kfifo *__kfifo = &__tmp->kfifo; \
626 (__recsize) ? \
627 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
628 __kfifo_to_user(__kfifo, __to, __len, __copied); \
629 }) \
630 )
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
646 ({ \
647 typeof((fifo) + 1) __tmp = (fifo); \
648 struct scatterlist *__sgl = (sgl); \
649 int __nents = (nents); \
650 unsigned int __len = (len); \
651 const size_t __recsize = sizeof(*__tmp->rectype); \
652 struct __kfifo *__kfifo = &__tmp->kfifo; \
653 (__recsize) ? \
654 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
655 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
656 })
657
658
659
660
661
662
663
664
665
666
667
668
669 #define kfifo_dma_in_finish(fifo, len) \
670 (void)({ \
671 typeof((fifo) + 1) __tmp = (fifo); \
672 unsigned int __len = (len); \
673 const size_t __recsize = sizeof(*__tmp->rectype); \
674 struct __kfifo *__kfifo = &__tmp->kfifo; \
675 if (__recsize) \
676 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
677 else \
678 __kfifo->in += __len / sizeof(*__tmp->type); \
679 })
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
697 ({ \
698 typeof((fifo) + 1) __tmp = (fifo); \
699 struct scatterlist *__sgl = (sgl); \
700 int __nents = (nents); \
701 unsigned int __len = (len); \
702 const size_t __recsize = sizeof(*__tmp->rectype); \
703 struct __kfifo *__kfifo = &__tmp->kfifo; \
704 (__recsize) ? \
705 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
706 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
707 })
708
709
710
711
712
713
714
715
716
717
718
719
720 #define kfifo_dma_out_finish(fifo, len) \
721 (void)({ \
722 typeof((fifo) + 1) __tmp = (fifo); \
723 unsigned int __len = (len); \
724 const size_t __recsize = sizeof(*__tmp->rectype); \
725 struct __kfifo *__kfifo = &__tmp->kfifo; \
726 if (__recsize) \
727 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
728 else \
729 __kfifo->out += __len / sizeof(*__tmp->type); \
730 })
731
732
733
734
735
736
737
738
739
740
741
742
743
744 #define kfifo_out_peek(fifo, buf, n) \
745 __kfifo_uint_must_check_helper( \
746 ({ \
747 typeof((fifo) + 1) __tmp = (fifo); \
748 typeof(__tmp->ptr) __buf = (buf); \
749 unsigned long __n = (n); \
750 const size_t __recsize = sizeof(*__tmp->rectype); \
751 struct __kfifo *__kfifo = &__tmp->kfifo; \
752 (__recsize) ? \
753 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
754 __kfifo_out_peek(__kfifo, __buf, __n); \
755 }) \
756 )
757
758 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
759 size_t esize, gfp_t gfp_mask);
760
761 extern void __kfifo_free(struct __kfifo *fifo);
762
763 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
764 unsigned int size, size_t esize);
765
766 extern unsigned int __kfifo_in(struct __kfifo *fifo,
767 const void *buf, unsigned int len);
768
769 extern unsigned int __kfifo_out(struct __kfifo *fifo,
770 void *buf, unsigned int len);
771
772 extern int __kfifo_from_user(struct __kfifo *fifo,
773 const void __user *from, unsigned long len, unsigned int *copied);
774
775 extern int __kfifo_to_user(struct __kfifo *fifo,
776 void __user *to, unsigned long len, unsigned int *copied);
777
778 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
779 struct scatterlist *sgl, int nents, unsigned int len);
780
781 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
782 struct scatterlist *sgl, int nents, unsigned int len);
783
784 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
785 void *buf, unsigned int len);
786
787 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
788 const void *buf, unsigned int len, size_t recsize);
789
790 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
791 void *buf, unsigned int len, size_t recsize);
792
793 extern int __kfifo_from_user_r(struct __kfifo *fifo,
794 const void __user *from, unsigned long len, unsigned int *copied,
795 size_t recsize);
796
797 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
798 unsigned long len, unsigned int *copied, size_t recsize);
799
800 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
801 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
802
803 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
804 unsigned int len, size_t recsize);
805
806 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
807 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
808
809 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
810
811 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
812
813 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
814
815 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
816 void *buf, unsigned int len, size_t recsize);
817
818 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
819
820 #endif