blob: f0dec5da60874c7cf9e6028589e9d1f03697816b [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>
José Fonsecab4783622007-11-23 17:22:54 +000044
45
46struct list_head
47{
48 struct list_head *prev;
49 struct list_head *next;
50};
51
Jason Ekstrand258b4192015-04-27 16:58:29 -070052static inline void list_inithead(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -040053{
54 item->prev = item;
55 item->next = item;
56}
José Fonsecab4783622007-11-23 17:22:54 +000057
Jason Ekstrand258b4192015-04-27 16:58:29 -070058static inline void list_add(struct list_head *item, struct list_head *list)
Jerome Glisseebe304f2011-03-28 17:45:31 -040059{
60 item->prev = list;
61 item->next = list->next;
62 list->next->prev = item;
63 list->next = item;
64}
José Fonsecab4783622007-11-23 17:22:54 +000065
Jason Ekstrand258b4192015-04-27 16:58:29 -070066static inline void list_addtail(struct list_head *item, struct list_head *list)
Jerome Glisseebe304f2011-03-28 17:45:31 -040067{
68 item->next = list;
69 item->prev = list->prev;
70 list->prev->next = item;
71 list->prev = item;
72}
José Fonsecab4783622007-11-23 17:22:54 +000073
Jason Ekstrand258b4192015-04-27 16:58:29 -070074static inline void list_replace(struct list_head *from, struct list_head *to)
Jerome Glisseebe304f2011-03-28 17:45:31 -040075{
76 to->prev = from->prev;
77 to->next = from->next;
78 from->next->prev = to;
79 from->prev->next = to;
80}
José Fonsecab4783622007-11-23 17:22:54 +000081
Jason Ekstrand258b4192015-04-27 16:58:29 -070082static inline void list_del(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -040083{
84 item->prev->next = item->next;
85 item->next->prev = item->prev;
Chris Fester3fffe8f2012-10-11 16:01:23 -060086 item->prev = item->next = NULL;
Jerome Glisseebe304f2011-03-28 17:45:31 -040087}
José Fonsecabc56e872008-04-10 22:57:21 +090088
Jason Ekstrand258b4192015-04-27 16:58:29 -070089static inline void list_delinit(struct list_head *item)
Jerome Glisseebe304f2011-03-28 17:45:31 -040090{
91 item->prev->next = item->next;
92 item->next->prev = item->prev;
93 item->next = item;
94 item->prev = item;
95}
José Fonsecab4783622007-11-23 17:22:54 +000096
Jason Ekstrandaddcf412015-04-27 20:39:37 -070097static inline bool list_empty(struct list_head *list)
98{
99 return list->next == list;
100}
101
Eduardo Lima Mitevfb3b5662015-10-23 16:31:41 +0200102/**
103 * Returns whether the list has exactly one element.
104 */
105static inline bool list_is_singular(const struct list_head *list)
106{
107 return list->next != NULL && list->next->next == list;
108}
109
Jason Ekstrandaddcf412015-04-27 20:39:37 -0700110static inline unsigned list_length(struct list_head *list)
111{
112 struct list_head *node;
113 unsigned length = 0;
114 for (node = list->next; node != list; node = node->next)
115 length++;
116 return length;
117}
118
Jason Ekstrand7dac4a22015-07-30 11:28:22 -0700119static inline void list_splice(struct list_head *src, struct list_head *dst)
120{
121 if (list_empty(src))
122 return;
123
124 src->next->prev = dst;
125 src->prev->next = dst->next;
126 dst->next->prev = src->prev;
127 dst->next = src->next;
128}
129
130static inline void list_splicetail(struct list_head *src, struct list_head *dst)
131{
132 if (list_empty(src))
133 return;
134
135 src->prev->next = dst;
136 src->next->prev = dst->prev;
137 dst->prev->next = src->next;
138 dst->prev = src->prev;
139}
140
Jason Ekstrand2c2cd362015-04-27 20:40:11 -0700141static inline void list_validate(struct list_head *list)
142{
143 struct list_head *node;
144 assert(list->next->prev == list && list->prev->next == list);
145 for (node = list->next; node != list; node = node->next)
146 assert(node->next->prev == node && node->prev->next == node);
147}
148
Jerome Glisseebe304f2011-03-28 17:45:31 -0400149#define LIST_INITHEAD(__item) list_inithead(__item)
150#define LIST_ADD(__item, __list) list_add(__item, __list)
151#define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
152#define LIST_REPLACE(__from, __to) list_replace(__from, __to)
153#define LIST_DEL(__item) list_del(__item)
154#define LIST_DELINIT(__item) list_delinit(__item)
José Fonsecab4783622007-11-23 17:22:54 +0000155
156#define LIST_ENTRY(__type, __item, __field) \
157 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
158
José Fonseca3708aae2009-03-23 12:05:07 +0000159#define LIST_IS_EMPTY(__list) \
160 ((__list)->next == (__list))
161
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000162/**
163 * Cast from a pointer to a member of a struct back to the containing struct.
164 *
165 * 'sample' MUST be initialized, or else the result is undefined!
166 */
Dave Airlie8556b772010-07-02 15:27:17 +1000167#ifndef container_of
168#define container_of(ptr, sample, member) \
169 (void *)((char *)(ptr) \
170 - ((char *)&(sample)->member - (char *)(sample)))
171#endif
José Fonsecab4783622007-11-23 17:22:54 +0000172
Rob Clarkc79b2e62015-04-29 08:38:45 -0400173#define list_first_entry(ptr, type, member) \
174 LIST_ENTRY(type, (ptr)->next, member)
175
176#define list_last_entry(ptr, type, member) \
177 LIST_ENTRY(type, (ptr)->prev, member)
178
179
Dave Airlie8556b772010-07-02 15:27:17 +1000180#define LIST_FOR_EACH_ENTRY(pos, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000181 for (pos = NULL, pos = container_of((head)->next, pos, member); \
Dave Airlie8556b772010-07-02 15:27:17 +1000182 &pos->member != (head); \
183 pos = container_of(pos->member.next, pos, member))
184
185#define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000186 for (pos = NULL, pos = container_of((head)->next, pos, member), \
Dave Airlie8556b772010-07-02 15:27:17 +1000187 storage = container_of(pos->member.next, pos, member); \
188 &pos->member != (head); \
189 pos = storage, storage = container_of(storage->member.next, storage, member))
Jerome Glisseebe304f2011-03-28 17:45:31 -0400190
191#define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000192 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400193 storage = container_of(pos->member.prev, pos, member); \
194 &pos->member != (head); \
195 pos = storage, storage = container_of(storage->member.prev, storage, member))
196
197#define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000198 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400199 &pos->member != (head); \
200 pos = container_of(pos->member.next, pos, member))
201
202#define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000203 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400204 &pos->member != (head); \
205 pos = container_of(pos->member.prev, pos, member))
206
Jason Ekstrandb31d8982015-04-27 18:56:02 -0700207#define list_for_each_entry(type, pos, head, member) \
208 for (type *pos = LIST_ENTRY(type, (head)->next, member); \
209 &pos->member != (head); \
210 pos = LIST_ENTRY(type, pos->member.next, member))
211
212#define list_for_each_entry_safe(type, pos, head, member) \
213 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
214 *__next = LIST_ENTRY(type, pos->member.next, member); \
215 &pos->member != (head); \
216 pos = __next, \
217 __next = LIST_ENTRY(type, __next->member.next, member))
218
219#define list_for_each_entry_rev(type, pos, head, member) \
220 for (type *pos = LIST_ENTRY(type, (head)->prev, member); \
221 &pos->member != (head); \
222 pos = LIST_ENTRY(type, pos->member.prev, member))
223
224#define list_for_each_entry_safe_rev(type, pos, head, member) \
225 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
226 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
227 &pos->member != (head); \
228 pos = __prev, \
229 __prev = LIST_ENTRY(type, __prev->member.prev, member))
230
231#define list_for_each_entry_from(type, pos, start, head, member) \
232 for (type *pos = LIST_ENTRY(type, (start), member); \
233 &pos->member != (head); \
234 pos = LIST_ENTRY(type, pos->member.next, member))
235
236#define list_for_each_entry_from_rev(type, pos, start, head, member) \
237 for (type *pos = LIST_ENTRY(type, (start), member); \
238 &pos->member != (head); \
239 pos = LIST_ENTRY(type, pos->member.prev, member))
240
Jason Ekstrand7a306682015-04-27 17:41:27 -0700241#endif /*_UTIL_LIST_H_*/