xref: /freebsd-src/sys/compat/linuxkpi/common/include/linux/kfifo.h (revision 4a7e8c7bd40bc39b3e247df069fa913f0197ea01)
1088b746cSEmmanuel Vadot /*-
2088b746cSEmmanuel Vadot  * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
3*4a7e8c7bSBjoern A. Zeeb  * Copyright (c) 2022 Bjoern A. Zeeb
4088b746cSEmmanuel Vadot  *
5088b746cSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6088b746cSEmmanuel Vadot  * modification, are permitted provided that the following conditions
7088b746cSEmmanuel Vadot  * are met:
8088b746cSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9088b746cSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10088b746cSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11088b746cSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12088b746cSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13088b746cSEmmanuel Vadot  *
14088b746cSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15088b746cSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16088b746cSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17088b746cSEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18088b746cSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19088b746cSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20088b746cSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21088b746cSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22088b746cSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23088b746cSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24088b746cSEmmanuel Vadot  * SUCH DAMAGE.
25088b746cSEmmanuel Vadot  *
26088b746cSEmmanuel Vadot  */
27088b746cSEmmanuel Vadot 
28088b746cSEmmanuel Vadot #ifndef _LINUXKPI_LINUX_KFIFO_H_
29088b746cSEmmanuel Vadot #define	_LINUXKPI_LINUX_KFIFO_H_
30088b746cSEmmanuel Vadot 
31*4a7e8c7bSBjoern A. Zeeb #include <sys/types.h>
32*4a7e8c7bSBjoern A. Zeeb 
33*4a7e8c7bSBjoern A. Zeeb #include <linux/slab.h>
34*4a7e8c7bSBjoern A. Zeeb #include <linux/gfp.h>
35*4a7e8c7bSBjoern A. Zeeb 
36088b746cSEmmanuel Vadot #define	INIT_KFIFO(x)	0
37088b746cSEmmanuel Vadot #define	DECLARE_KFIFO(x, y, z)
38088b746cSEmmanuel Vadot 
39*4a7e8c7bSBjoern A. Zeeb #define	DECLARE_KFIFO_PTR(_name, _type)					\
40*4a7e8c7bSBjoern A. Zeeb 	struct kfifo_ ## _name {					\
41*4a7e8c7bSBjoern A. Zeeb 		size_t		total;					\
42*4a7e8c7bSBjoern A. Zeeb 		size_t		count;					\
43*4a7e8c7bSBjoern A. Zeeb 		size_t		first;					\
44*4a7e8c7bSBjoern A. Zeeb 		size_t		last;					\
45*4a7e8c7bSBjoern A. Zeeb 		_type		*head;					\
46*4a7e8c7bSBjoern A. Zeeb 	} _name
47*4a7e8c7bSBjoern A. Zeeb 
48*4a7e8c7bSBjoern A. Zeeb #define	kfifo_len(_kf)							\
49*4a7e8c7bSBjoern A. Zeeb ({									\
50*4a7e8c7bSBjoern A. Zeeb 	(_kf)->count;							\
51*4a7e8c7bSBjoern A. Zeeb })
52*4a7e8c7bSBjoern A. Zeeb 
53*4a7e8c7bSBjoern A. Zeeb #define	kfifo_is_empty(_kf)						\
54*4a7e8c7bSBjoern A. Zeeb ({									\
55*4a7e8c7bSBjoern A. Zeeb 	((_kf)->count == 0) ? true : false;				\
56*4a7e8c7bSBjoern A. Zeeb })
57*4a7e8c7bSBjoern A. Zeeb 
58*4a7e8c7bSBjoern A. Zeeb #define	kfifo_is_full(_kf)						\
59*4a7e8c7bSBjoern A. Zeeb ({									\
60*4a7e8c7bSBjoern A. Zeeb 	((_kf)->count == (_kf)->total) ? true : false;			\
61*4a7e8c7bSBjoern A. Zeeb })
62*4a7e8c7bSBjoern A. Zeeb 
63*4a7e8c7bSBjoern A. Zeeb #define	kfifo_put(_kf, _e)						\
64*4a7e8c7bSBjoern A. Zeeb ({									\
65*4a7e8c7bSBjoern A. Zeeb 	bool _rc;							\
66*4a7e8c7bSBjoern A. Zeeb 									\
67*4a7e8c7bSBjoern A. Zeeb 	/* Would overflow. */						\
68*4a7e8c7bSBjoern A. Zeeb 	if (kfifo_is_full(_kf)) {					\
69*4a7e8c7bSBjoern A. Zeeb 		_rc = false;						\
70*4a7e8c7bSBjoern A. Zeeb 	} else {							\
71*4a7e8c7bSBjoern A. Zeeb 		(_kf)->head[(_kf)->last] = (_e);			\
72*4a7e8c7bSBjoern A. Zeeb 		(_kf)->count++;						\
73*4a7e8c7bSBjoern A. Zeeb 		(_kf)->last++;						\
74*4a7e8c7bSBjoern A. Zeeb 		if ((_kf)->last > (_kf)->total)				\
75*4a7e8c7bSBjoern A. Zeeb 			(_kf)->last = 0;				\
76*4a7e8c7bSBjoern A. Zeeb 		_rc = true;						\
77*4a7e8c7bSBjoern A. Zeeb 	}								\
78*4a7e8c7bSBjoern A. Zeeb 									\
79*4a7e8c7bSBjoern A. Zeeb 	_rc;								\
80*4a7e8c7bSBjoern A. Zeeb })
81*4a7e8c7bSBjoern A. Zeeb 
82*4a7e8c7bSBjoern A. Zeeb #define	kfifo_get(_kf, _e)						\
83*4a7e8c7bSBjoern A. Zeeb ({									\
84*4a7e8c7bSBjoern A. Zeeb 	bool _rc;							\
85*4a7e8c7bSBjoern A. Zeeb 									\
86*4a7e8c7bSBjoern A. Zeeb 	if (kfifo_is_empty(_kf)) {					\
87*4a7e8c7bSBjoern A. Zeeb 		_rc = false;						\
88*4a7e8c7bSBjoern A. Zeeb 	} else {							\
89*4a7e8c7bSBjoern A. Zeeb 		*(_e) = (_kf)->head[(_kf)->first];			\
90*4a7e8c7bSBjoern A. Zeeb 		(_kf)->count--;						\
91*4a7e8c7bSBjoern A. Zeeb 		(_kf)->first++;						\
92*4a7e8c7bSBjoern A. Zeeb 		if ((_kf)->first > (_kf)->total)			\
93*4a7e8c7bSBjoern A. Zeeb 			(_kf)->first = 0;				\
94*4a7e8c7bSBjoern A. Zeeb 		_rc = true;						\
95*4a7e8c7bSBjoern A. Zeeb 	}								\
96*4a7e8c7bSBjoern A. Zeeb 									\
97*4a7e8c7bSBjoern A. Zeeb 	_rc;								\
98*4a7e8c7bSBjoern A. Zeeb })
99*4a7e8c7bSBjoern A. Zeeb 
100*4a7e8c7bSBjoern A. Zeeb #define	kfifo_alloc(_kf, _s, _gfp)					\
101*4a7e8c7bSBjoern A. Zeeb ({									\
102*4a7e8c7bSBjoern A. Zeeb 	int _error;							\
103*4a7e8c7bSBjoern A. Zeeb 									\
104*4a7e8c7bSBjoern A. Zeeb 	(_kf)->head = kmalloc(sizeof(__typeof(*(_kf)->head)) * (_s), _gfp); \
105*4a7e8c7bSBjoern A. Zeeb 	if ((_kf)->head == NULL)					\
106*4a7e8c7bSBjoern A. Zeeb 		_error = ENOMEM;					\
107*4a7e8c7bSBjoern A. Zeeb 	else {								\
108*4a7e8c7bSBjoern A. Zeeb 		(_kf)->total = (_s);					\
109*4a7e8c7bSBjoern A. Zeeb 		_error = 0;						\
110*4a7e8c7bSBjoern A. Zeeb 	}								\
111*4a7e8c7bSBjoern A. Zeeb 									\
112*4a7e8c7bSBjoern A. Zeeb 	_error;								\
113*4a7e8c7bSBjoern A. Zeeb })
114*4a7e8c7bSBjoern A. Zeeb 
115*4a7e8c7bSBjoern A. Zeeb #define	kfifo_free(_kf)							\
116*4a7e8c7bSBjoern A. Zeeb ({									\
117*4a7e8c7bSBjoern A. Zeeb 	kfree((_kf)->head);						\
118*4a7e8c7bSBjoern A. Zeeb 	(_kf)->head = NULL;						\
119*4a7e8c7bSBjoern A. Zeeb 	(_kf)->total = (_kf)->count = (_kf)->first = (_kf)->last = 0;	\
120*4a7e8c7bSBjoern A. Zeeb })
121*4a7e8c7bSBjoern A. Zeeb 
122088b746cSEmmanuel Vadot #endif	/* _LINUXKPI_LINUX_KFIFO_H_*/
123