blob: d50eb1ea5dde481711d1f726d47323d8544b4d3a [file] [log] [blame]
José Fonsecab4783622007-11-23 17:22:54 +00001/**************************************************************************
Jason Ekstrand7a306682015-04-27 17:41:27 -07002 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2006 VMware, Inc., Bismarck, ND. USA.
José Fonsecab4783622007-11-23 17:22:54 +00004 * All Rights Reserved.
Jason Ekstrand7a306682015-04-27 17:41:27 -07005 *
José Fonsecab4783622007-11-23 17:22:54 +00006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Jason Ekstrand7a306682015-04-27 17:41:27 -070013 *
José Fonsecab4783622007-11-23 17:22:54 +000014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
Jason Ekstrand7a306682015-04-27 17:41:27 -070017 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
José Fonsecab4783622007-11-23 17:22:54 +000020 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
Jason Ekstrand7a306682015-04-27 17:41:27 -070025 *
José Fonsecab4783622007-11-23 17:22:54 +000026 **************************************************************************/
27
28/**
29 * \file
30 * List macros heavily inspired by the Linux kernel
31 * list handling. No list looping yet.
Jason Ekstrand7a306682015-04-27 17:41:27 -070032 *
José Fonsecab4783622007-11-23 17:22:54 +000033 * Is not threadsafe, so common operations need to
34 * be protected using an external mutex.
35 */
36
Jason Ekstrand7a306682015-04-27 17:41:27 -070037#ifndef _UTIL_LIST_H_
38#define _UTIL_LIST_H_
José Fonsecab4783622007-11-23 17:22:54 +000039
40
Jason Ekstrandaddcf412015-04-27 20:39:37 -070041#include <stdbool.h>
José Fonsecab4783622007-11-23 17:22:54 +000042#include <stddef.h>
Jason Ekstrand2c2cd362015-04-27 20:40:11 -070043#include <assert.h>
Brian Paul960f6402017-04-03 08:44:10 -060044#include "c99_compat.h"
José Fonsecab4783622007-11-23 17:22:54 +000045
Rob Clark372e83b2019-05-25 10:50:41 -070046#ifdef DEBUG
47# define list_assert(cond, msg) assert(cond && msg)
48#else
49# define list_assert(cond, msg) (void)(0 && (cond))
50#endif
José Fonsecab4783622007-11-23 17:22:54 +000051
52struct list_head
53{
54 struct list_head *prev;
55 struct list_head *next;
56};
57
Jason Ekstrand258b4192015-04-27 16:58:29 -070058static inline void list_inithead(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -040059{
60 item->prev = item;
61 item->next = item;
62}
José Fonsecab4783622007-11-23 17:22:54 +000063
Jason Ekstrand258b4192015-04-27 16:58:29 -070064static inline void list_add(struct list_head *item, struct list_head *list)
Jerome Glisseebe304f2011-03-28 17:45:31 -040065{
66 item->prev = list;
67 item->next = list->next;
68 list->next->prev = item;
69 list->next = item;
70}
José Fonsecab4783622007-11-23 17:22:54 +000071
Jason Ekstrand258b4192015-04-27 16:58:29 -070072static inline void list_addtail(struct list_head *item, struct list_head *list)
Jerome Glisseebe304f2011-03-28 17:45:31 -040073{
74 item->next = list;
75 item->prev = list->prev;
76 list->prev->next = item;
77 list->prev = item;
78}
José Fonsecab4783622007-11-23 17:22:54 +000079
Timothy Arceri7f106a22019-10-28 21:27:52 +110080static inline bool list_is_empty(const struct list_head *list);
Rob Clark23dd9ea2016-07-02 08:02:51 -040081
Jason Ekstrand258b4192015-04-27 16:58:29 -070082static inline void list_replace(struct list_head *from, struct list_head *to)
Jerome Glisseebe304f2011-03-28 17:45:31 -040083{
Timothy Arceri7f106a22019-10-28 21:27:52 +110084 if (list_is_empty(from)) {
Rob Clark23dd9ea2016-07-02 08:02:51 -040085 list_inithead(to);
86 } else {
87 to->prev = from->prev;
88 to->next = from->next;
89 from->next->prev = to;
90 from->prev->next = to;
91 }
Jerome Glisseebe304f2011-03-28 17:45:31 -040092}
José Fonsecab4783622007-11-23 17:22:54 +000093
Jason Ekstrand258b4192015-04-27 16:58:29 -070094static inline void list_del(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -040095{
96 item->prev->next = item->next;
97 item->next->prev = item->prev;
Chris Fester3fffe8f2012-10-11 16:01:23 -060098 item->prev = item->next = NULL;
Jerome Glisseebe304f2011-03-28 17:45:31 -040099}
José Fonsecabc56e872008-04-10 22:57:21 +0900100
Jason Ekstrand258b4192015-04-27 16:58:29 -0700101static inline void list_delinit(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -0400102{
103 item->prev->next = item->next;
104 item->next->prev = item->prev;
105 item->next = item;
106 item->prev = item;
107}
José Fonsecab4783622007-11-23 17:22:54 +0000108
Timothy Arceri7f106a22019-10-28 21:27:52 +1100109static inline bool list_is_empty(const struct list_head *list)
Jason Ekstrandaddcf412015-04-27 20:39:37 -0700110{
111 return list->next == list;
112}
113
Eduardo Lima Mitevfb3b5662015-10-23 16:31:41 +0200114/**
115 * Returns whether the list has exactly one element.
116 */
117static inline bool list_is_singular(const struct list_head *list)
118{
Timothy Arceri0252ba22017-01-11 15:13:35 +1100119 return list->next != NULL && list->next != list && list->next->next == list;
Eduardo Lima Mitevfb3b5662015-10-23 16:31:41 +0200120}
121
Jason Ekstrand5c1c6932018-07-25 10:37:53 -0700122static inline unsigned list_length(const struct list_head *list)
Jason Ekstrandaddcf412015-04-27 20:39:37 -0700123{
124 struct list_head *node;
125 unsigned length = 0;
126 for (node = list->next; node != list; node = node->next)
127 length++;
128 return length;
129}
130
Jason Ekstrand7dac4a22015-07-30 11:28:22 -0700131static inline void list_splice(struct list_head *src, struct list_head *dst)
132{
Timothy Arceri7f106a22019-10-28 21:27:52 +1100133 if (list_is_empty(src))
Jason Ekstrand7dac4a22015-07-30 11:28:22 -0700134 return;
135
136 src->next->prev = dst;
137 src->prev->next = dst->next;
138 dst->next->prev = src->prev;
139 dst->next = src->next;
140}
141
142static inline void list_splicetail(struct list_head *src, struct list_head *dst)
143{
Timothy Arceri7f106a22019-10-28 21:27:52 +1100144 if (list_is_empty(src))
Jason Ekstrand7dac4a22015-07-30 11:28:22 -0700145 return;
146
147 src->prev->next = dst;
148 src->next->prev = dst->prev;
149 dst->prev->next = src->next;
150 dst->prev = src->prev;
151}
152
Jason Ekstrand5c1c6932018-07-25 10:37:53 -0700153static inline void list_validate(const struct list_head *list)
Jason Ekstrand2c2cd362015-04-27 20:40:11 -0700154{
155 struct list_head *node;
156 assert(list->next->prev == list && list->prev->next == list);
157 for (node = list->next; node != list; node = node->next)
158 assert(node->next->prev == node && node->prev->next == node);
159}
160
José Fonsecab4783622007-11-23 17:22:54 +0000161#define LIST_ENTRY(__type, __item, __field) \
162 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
163
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000164/**
165 * Cast from a pointer to a member of a struct back to the containing struct.
166 *
167 * 'sample' MUST be initialized, or else the result is undefined!
168 */
Dave Airlie8556b772010-07-02 15:27:17 +1000169#ifndef container_of
170#define container_of(ptr, sample, member) \
171 (void *)((char *)(ptr) \
172 - ((char *)&(sample)->member - (char *)(sample)))
173#endif
José Fonsecab4783622007-11-23 17:22:54 +0000174
Rob Clarkc79b2e62015-04-29 08:38:45 -0400175#define list_first_entry(ptr, type, member) \
176 LIST_ENTRY(type, (ptr)->next, member)
177
178#define list_last_entry(ptr, type, member) \
179 LIST_ENTRY(type, (ptr)->prev, member)
180
181
Dave Airlie8556b772010-07-02 15:27:17 +1000182#define LIST_FOR_EACH_ENTRY(pos, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000183 for (pos = NULL, pos = container_of((head)->next, pos, member); \
Dave Airlie8556b772010-07-02 15:27:17 +1000184 &pos->member != (head); \
185 pos = container_of(pos->member.next, pos, member))
186
187#define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000188 for (pos = NULL, pos = container_of((head)->next, pos, member), \
Dave Airlie8556b772010-07-02 15:27:17 +1000189 storage = container_of(pos->member.next, pos, member); \
190 &pos->member != (head); \
191 pos = storage, storage = container_of(storage->member.next, storage, member))
Jerome Glisseebe304f2011-03-28 17:45:31 -0400192
193#define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000194 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400195 storage = container_of(pos->member.prev, pos, member); \
196 &pos->member != (head); \
197 pos = storage, storage = container_of(storage->member.prev, storage, member))
198
199#define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000200 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400201 &pos->member != (head); \
202 pos = container_of(pos->member.next, pos, member))
203
204#define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000205 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400206 &pos->member != (head); \
207 pos = container_of(pos->member.prev, pos, member))
208
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700209#define list_for_each_entry(type, pos, head, member) \
Rob Clark372e83b2019-05-25 10:50:41 -0700210 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
211 *__next = LIST_ENTRY(type, pos->member.next, member); \
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700212 &pos->member != (head); \
Rob Clark372e83b2019-05-25 10:50:41 -0700213 pos = LIST_ENTRY(type, pos->member.next, member), \
214 list_assert(pos == __next, "use _safe iterator"), \
215 __next = LIST_ENTRY(type, __next->member.next, member))
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700216
217#define list_for_each_entry_safe(type, pos, head, member) \
218 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
219 *__next = LIST_ENTRY(type, pos->member.next, member); \
220 &pos->member != (head); \
221 pos = __next, \
Rob Clark372e83b2019-05-25 10:50:41 -0700222 __next = LIST_ENTRY(type, __next->member.next, member))
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700223
224#define list_for_each_entry_rev(type, pos, head, member) \
Rob Clark372e83b2019-05-25 10:50:41 -0700225 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
226 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700227 &pos->member != (head); \
Rob Clark372e83b2019-05-25 10:50:41 -0700228 pos = LIST_ENTRY(type, pos->member.prev, member), \
229 list_assert(pos == __prev, "use _safe iterator"), \
230 __prev = LIST_ENTRY(type, __prev->member.prev, member))
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700231
232#define list_for_each_entry_safe_rev(type, pos, head, member) \
233 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
234 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
235 &pos->member != (head); \
236 pos = __prev, \
237 __prev = LIST_ENTRY(type, __prev->member.prev, member))
238
239#define list_for_each_entry_from(type, pos, start, head, member) \
240 for (type *pos = LIST_ENTRY(type, (start), member); \
241 &pos->member != (head); \
242 pos = LIST_ENTRY(type, pos->member.next, member))
243
Caio Marcelo de Oliveira Filhof40f8f62019-09-16 18:21:06 -0700244#define list_for_each_entry_from_safe(type, pos, start, head, member) \
245 for (type *pos = LIST_ENTRY(type, (start), member), \
246 *__next = LIST_ENTRY(type, pos->member.next, member); \
247 &pos->member != (head); \
248 pos = __next, \
249 __next = LIST_ENTRY(type, __next->member.next, member))
250
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700251#define list_for_each_entry_from_rev(type, pos, start, head, member) \
252 for (type *pos = LIST_ENTRY(type, (start), member); \
253 &pos->member != (head); \
254 pos = LIST_ENTRY(type, pos->member.prev, member))
255
Jason Ekstrandaeb95fd2019-08-05 16:19:06 -0500256#define list_pair_for_each_entry(type, pos1, pos2, head1, head2, member) \
257 for (type *pos1 = LIST_ENTRY(type, (head1)->next, member), \
258 *pos2 = LIST_ENTRY(type, (head2)->next, member); \
259 &pos1->member != (head1) && &pos2->member != (head2); \
260 pos1 = LIST_ENTRY(type, pos1->member.next, member), \
261 pos2 = LIST_ENTRY(type, pos2->member.next, member))
262
Jason Ekstrand7a306682015-04-27 17:41:27 -0700263#endif /*_UTIL_LIST_H_*/