xref: /wlan-driver/qca-wifi-host-cmn/qdf/inc/qdf_slist.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name  * Copyright (c) 2019 The Linux Foundation. All rights reserved.
3*5113495bSYour Name  *
4*5113495bSYour Name  * Permission to use, copy, modify, and/or distribute this software for
5*5113495bSYour Name  * any purpose with or without fee is hereby granted, provided that the
6*5113495bSYour Name  * above copyright notice and this permission notice appear in all
7*5113495bSYour Name  * copies.
8*5113495bSYour Name  *
9*5113495bSYour Name  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10*5113495bSYour Name  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11*5113495bSYour Name  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12*5113495bSYour Name  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13*5113495bSYour Name  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14*5113495bSYour Name  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15*5113495bSYour Name  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16*5113495bSYour Name  * PERFORMANCE OF THIS SOFTWARE.
17*5113495bSYour Name  */
18*5113495bSYour Name 
19*5113495bSYour Name /**
20*5113495bSYour Name  * DOC: qdf_slist.h
21*5113495bSYour Name  *
22*5113495bSYour Name  * A minimal, singly linked list implementation, with push front, pop front, and
23*5113495bSYour Name  * remove capabilities. These are all O(1) operations.
24*5113495bSYour Name  *
25*5113495bSYour Name  * In order to remove an item, a pointer to the previous item must be known.
26*5113495bSYour Name  * Thus, removing an item is most efficient when combined with
27*5113495bSYour Name  * qdf_slist_for_each_del(). For cases where you need efficient removal of an
28*5113495bSYour Name  * arbitrary list node without iteration, consider using the doubly linked list
29*5113495bSYour Name  * qdf_list instead.
30*5113495bSYour Name  */
31*5113495bSYour Name 
32*5113495bSYour Name #ifndef __QDF_SLIST_H
33*5113495bSYour Name #define __QDF_SLIST_H
34*5113495bSYour Name 
35*5113495bSYour Name #include "qdf_trace.h"
36*5113495bSYour Name #include "qdf_util.h"
37*5113495bSYour Name 
38*5113495bSYour Name #define __qdf_slist_poison ((void *)0xdeaddeaddeaddeadull)
39*5113495bSYour Name 
40*5113495bSYour Name /**
41*5113495bSYour Name  * struct qdf_slist - a singly linked list
42*5113495bSYour Name  * @head: pointer to the head of the list
43*5113495bSYour Name  */
44*5113495bSYour Name struct qdf_slist {
45*5113495bSYour Name 	struct qdf_slist_node *head;
46*5113495bSYour Name };
47*5113495bSYour Name 
48*5113495bSYour Name /**
49*5113495bSYour Name  * struct qdf_slist_node - a singly linked list node
50*5113495bSYour Name  * @next: pointer to the next node in the list, NULL if there is none
51*5113495bSYour Name  */
52*5113495bSYour Name struct qdf_slist_node {
53*5113495bSYour Name 	struct qdf_slist_node *next;
54*5113495bSYour Name };
55*5113495bSYour Name 
56*5113495bSYour Name #define __qdf_slist_item(node, cursor, node_field) ({ \
57*5113495bSYour Name 	struct qdf_slist_node *__n = (node); \
58*5113495bSYour Name 	(__n ? qdf_container_of(__n, typeof(*(cursor)), node_field) : NULL); })
59*5113495bSYour Name 
60*5113495bSYour Name #define __qdf_slist_next_item(slist, cursor, node_field) \
61*5113495bSYour Name 	__qdf_slist_item(cursor ? (cursor)->node_field.next : \
62*5113495bSYour Name 			 (slist)->head, cursor, node_field)
63*5113495bSYour Name 
64*5113495bSYour Name /**
65*5113495bSYour Name  * qdf_slist_for_each - iterate over all of the items in @slist
66*5113495bSYour Name  * @slist: pointer to the qdf_slist to iterate over
67*5113495bSYour Name  * @cursor: cursor pointer of the list's item type, populated for each item
68*5113495bSYour Name  * @node_field: name of the qdf_slist_node field in the item's type
69*5113495bSYour Name  */
70*5113495bSYour Name #define qdf_slist_for_each(slist, cursor, node_field) \
71*5113495bSYour Name 	for (cursor = __qdf_slist_item((slist)->head, cursor, node_field); \
72*5113495bSYour Name 	     cursor; \
73*5113495bSYour Name 	     cursor = __qdf_slist_item((cursor)->node_field.next, \
74*5113495bSYour Name 				       cursor, node_field))
75*5113495bSYour Name 
76*5113495bSYour Name /**
77*5113495bSYour Name  * qdf_slist_for_each_del - iterate over all of the items in @slist,
78*5113495bSYour Name  *	allowing for the safe deletion of nodes during iteration
79*5113495bSYour Name  * @slist: pointer to the qdf_slist to iterate over
80*5113495bSYour Name  * @prev: cursor pointer, populated with the previous item
81*5113495bSYour Name  * @cursor: cursor pointer of the list's item type, populated for each item
82*5113495bSYour Name  * @node_field: name of the qdf_slist_node field in the item's type
83*5113495bSYour Name  */
84*5113495bSYour Name #define qdf_slist_for_each_del(slist, prev, cursor, node_field) \
85*5113495bSYour Name 	for (prev = NULL, \
86*5113495bSYour Name 	     cursor = __qdf_slist_item((slist)->head, cursor, node_field); \
87*5113495bSYour Name 	     cursor; \
88*5113495bSYour Name 	     prev = __qdf_slist_next_item(slist, prev, node_field) == \
89*5113495bSYour Name 		cursor ? cursor : prev, \
90*5113495bSYour Name 	     cursor = __qdf_slist_next_item(slist, prev, node_field))
91*5113495bSYour Name 
92*5113495bSYour Name /**
93*5113495bSYour Name  * qdf_slist_init() - initialize a qdf_slist
94*5113495bSYour Name  * @slist: the list to initialize
95*5113495bSYour Name  *
96*5113495bSYour Name  * Return: None
97*5113495bSYour Name  */
qdf_slist_init(struct qdf_slist * slist)98*5113495bSYour Name static inline void qdf_slist_init(struct qdf_slist *slist)
99*5113495bSYour Name {
100*5113495bSYour Name 	slist->head = NULL;
101*5113495bSYour Name }
102*5113495bSYour Name 
103*5113495bSYour Name /**
104*5113495bSYour Name  * qdf_slist_deinit() - deinitialize a qdf_slist
105*5113495bSYour Name  * @slist: the list to deinitialize
106*5113495bSYour Name  *
107*5113495bSYour Name  * Return: None
108*5113495bSYour Name  */
qdf_slist_deinit(struct qdf_slist * slist)109*5113495bSYour Name static inline void qdf_slist_deinit(struct qdf_slist *slist)
110*5113495bSYour Name {
111*5113495bSYour Name 	QDF_BUG(!slist->head);
112*5113495bSYour Name 	slist->head = __qdf_slist_poison;
113*5113495bSYour Name }
114*5113495bSYour Name 
115*5113495bSYour Name /**
116*5113495bSYour Name  * qdf_slist_empty() - check if a qdf_slist is empty
117*5113495bSYour Name  * @slist: the list to check
118*5113495bSYour Name  *
119*5113495bSYour Name  * Return: true if @slist contains zero items
120*5113495bSYour Name  */
qdf_slist_empty(struct qdf_slist * slist)121*5113495bSYour Name static inline bool qdf_slist_empty(struct qdf_slist *slist)
122*5113495bSYour Name {
123*5113495bSYour Name 	return !slist->head;
124*5113495bSYour Name }
125*5113495bSYour Name 
126*5113495bSYour Name /**
127*5113495bSYour Name  * qdf_slist_push() - push an item into the front of a qdf_slist
128*5113495bSYour Name  * @slist: the list to push into
129*5113495bSYour Name  * @cursor: the item to push
130*5113495bSYour Name  * @node_field: name of the qdf_slist_node field in the item's type
131*5113495bSYour Name  *
132*5113495bSYour Name  * Return: None
133*5113495bSYour Name  */
134*5113495bSYour Name #define qdf_slist_push(slist, cursor, node_field) \
135*5113495bSYour Name 	__qdf_slist_push(slist, &(cursor)->node_field)
136*5113495bSYour Name 
137*5113495bSYour Name static inline void
__qdf_slist_push(struct qdf_slist * slist,struct qdf_slist_node * node)138*5113495bSYour Name __qdf_slist_push(struct qdf_slist *slist, struct qdf_slist_node *node)
139*5113495bSYour Name {
140*5113495bSYour Name 	node->next = slist->head;
141*5113495bSYour Name 	slist->head = node;
142*5113495bSYour Name }
143*5113495bSYour Name 
144*5113495bSYour Name /**
145*5113495bSYour Name  * qdf_slist_pop() - pop an item from the front of a qdf_slist
146*5113495bSYour Name  * @slist: the list to pop from
147*5113495bSYour Name  * @cursor: cursor pointer of the list's item type, not populated
148*5113495bSYour Name  * @node_field: name of the qdf_slist_node field in the item's type
149*5113495bSYour Name  *
150*5113495bSYour Name  * Return: pointer to the popped item, NULL if @slist was empty
151*5113495bSYour Name  */
152*5113495bSYour Name #define qdf_slist_pop(slist, cursor, node_field) \
153*5113495bSYour Name 	__qdf_slist_item(__qdf_slist_pop(slist), cursor, node_field)
154*5113495bSYour Name 
__qdf_slist_pop(struct qdf_slist * slist)155*5113495bSYour Name static inline struct qdf_slist_node *__qdf_slist_pop(struct qdf_slist *slist)
156*5113495bSYour Name {
157*5113495bSYour Name 	struct qdf_slist_node *node = slist->head;
158*5113495bSYour Name 
159*5113495bSYour Name 	if (!node)
160*5113495bSYour Name 		return NULL;
161*5113495bSYour Name 
162*5113495bSYour Name 	slist->head = node->next;
163*5113495bSYour Name 	node->next = __qdf_slist_poison;
164*5113495bSYour Name 
165*5113495bSYour Name 	return node;
166*5113495bSYour Name }
167*5113495bSYour Name 
168*5113495bSYour Name /**
169*5113495bSYour Name  * qdf_slist_remove() - remove an item from a qdf_slist
170*5113495bSYour Name  * @slist: the list to remove from
171*5113495bSYour Name  * @prev: pointer to the item previous to the item to remove, NULL removes head
172*5113495bSYour Name  * @node_field: name of the qdf_slist_node field in the item's type
173*5113495bSYour Name  *
174*5113495bSYour Name  * Return: pointer to the removed item, NULL if none was removed
175*5113495bSYour Name  */
176*5113495bSYour Name #define qdf_slist_remove(slist, prev, node_field) \
177*5113495bSYour Name 	__qdf_slist_item(__qdf_slist_remove(slist, \
178*5113495bSYour Name 			 prev ? &(prev)->node_field : NULL), prev, node_field)
179*5113495bSYour Name 
180*5113495bSYour Name static inline struct qdf_slist_node *
__qdf_slist_remove(struct qdf_slist * slist,struct qdf_slist_node * prev)181*5113495bSYour Name __qdf_slist_remove(struct qdf_slist *slist, struct qdf_slist_node *prev)
182*5113495bSYour Name {
183*5113495bSYour Name 	struct qdf_slist_node *node;
184*5113495bSYour Name 
185*5113495bSYour Name 	if (!prev)
186*5113495bSYour Name 		return __qdf_slist_pop(slist);
187*5113495bSYour Name 
188*5113495bSYour Name 	if (!prev->next)
189*5113495bSYour Name 		return NULL;
190*5113495bSYour Name 
191*5113495bSYour Name 	node = prev->next;
192*5113495bSYour Name 	prev->next = node->next;
193*5113495bSYour Name 	node->next = __qdf_slist_poison;
194*5113495bSYour Name 
195*5113495bSYour Name 	return node;
196*5113495bSYour Name }
197*5113495bSYour Name 
198*5113495bSYour Name #endif /* __QDF_SLIST_H */
199*5113495bSYour Name 
200