José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Jason Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 2 | * |
José Fonseca | 8771285 | 2014-01-17 16:27:50 +0000 | [diff] [blame] | 3 | * Copyright 2006 VMware, Inc., Bismarck, ND. USA. |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 4 | * All Rights Reserved. |
Jason Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 5 | * |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 6 | * 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 Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 13 | * |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 14 | * 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 Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 17 | * 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é Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 20 | * 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 Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 25 | * |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 26 | **************************************************************************/ |
| 27 | |
| 28 | /** |
| 29 | * \file |
| 30 | * List macros heavily inspired by the Linux kernel |
| 31 | * list handling. No list looping yet. |
Jason Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 32 | * |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 33 | * Is not threadsafe, so common operations need to |
| 34 | * be protected using an external mutex. |
| 35 | */ |
| 36 | |
Jason Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 37 | #ifndef _UTIL_LIST_H_ |
| 38 | #define _UTIL_LIST_H_ |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 39 | |
| 40 | |
Jason Ekstrand | addcf41 | 2015-04-27 20:39:37 -0700 | [diff] [blame] | 41 | #include <stdbool.h> |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 42 | #include <stddef.h> |
Jason Ekstrand | 2c2cd36 | 2015-04-27 20:40:11 -0700 | [diff] [blame] | 43 | #include <assert.h> |
Brian Paul | 960f640 | 2017-04-03 08:44:10 -0600 | [diff] [blame] | 44 | #include "c99_compat.h" |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 45 | |
Rob Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 46 | #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é Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 51 | |
| 52 | struct list_head |
| 53 | { |
| 54 | struct list_head *prev; |
| 55 | struct list_head *next; |
| 56 | }; |
| 57 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 58 | static inline void list_inithead(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 59 | { |
| 60 | item->prev = item; |
| 61 | item->next = item; |
| 62 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 63 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 64 | static inline void list_add(struct list_head *item, struct list_head *list) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 65 | { |
| 66 | item->prev = list; |
| 67 | item->next = list->next; |
| 68 | list->next->prev = item; |
| 69 | list->next = item; |
| 70 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 71 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 72 | static inline void list_addtail(struct list_head *item, struct list_head *list) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 73 | { |
| 74 | item->next = list; |
| 75 | item->prev = list->prev; |
| 76 | list->prev->next = item; |
| 77 | list->prev = item; |
| 78 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 79 | |
Timothy Arceri | 7f106a2 | 2019-10-28 21:27:52 +1100 | [diff] [blame] | 80 | static inline bool list_is_empty(const struct list_head *list); |
Rob Clark | 23dd9ea | 2016-07-02 08:02:51 -0400 | [diff] [blame] | 81 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 82 | static inline void list_replace(struct list_head *from, struct list_head *to) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 83 | { |
Timothy Arceri | 7f106a2 | 2019-10-28 21:27:52 +1100 | [diff] [blame] | 84 | if (list_is_empty(from)) { |
Rob Clark | 23dd9ea | 2016-07-02 08:02:51 -0400 | [diff] [blame] | 85 | 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 Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 92 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 93 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 94 | static inline void list_del(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 95 | { |
| 96 | item->prev->next = item->next; |
| 97 | item->next->prev = item->prev; |
Chris Fester | 3fffe8f | 2012-10-11 16:01:23 -0600 | [diff] [blame] | 98 | item->prev = item->next = NULL; |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 99 | } |
José Fonseca | bc56e87 | 2008-04-10 22:57:21 +0900 | [diff] [blame] | 100 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 101 | static inline void list_delinit(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 102 | { |
| 103 | item->prev->next = item->next; |
| 104 | item->next->prev = item->prev; |
| 105 | item->next = item; |
| 106 | item->prev = item; |
| 107 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 108 | |
Timothy Arceri | 7f106a2 | 2019-10-28 21:27:52 +1100 | [diff] [blame] | 109 | static inline bool list_is_empty(const struct list_head *list) |
Jason Ekstrand | addcf41 | 2015-04-27 20:39:37 -0700 | [diff] [blame] | 110 | { |
| 111 | return list->next == list; |
| 112 | } |
| 113 | |
Eduardo Lima Mitev | fb3b566 | 2015-10-23 16:31:41 +0200 | [diff] [blame] | 114 | /** |
| 115 | * Returns whether the list has exactly one element. |
| 116 | */ |
| 117 | static inline bool list_is_singular(const struct list_head *list) |
| 118 | { |
Timothy Arceri | 0252ba2 | 2017-01-11 15:13:35 +1100 | [diff] [blame] | 119 | return list->next != NULL && list->next != list && list->next->next == list; |
Eduardo Lima Mitev | fb3b566 | 2015-10-23 16:31:41 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Jason Ekstrand | 5c1c693 | 2018-07-25 10:37:53 -0700 | [diff] [blame] | 122 | static inline unsigned list_length(const struct list_head *list) |
Jason Ekstrand | addcf41 | 2015-04-27 20:39:37 -0700 | [diff] [blame] | 123 | { |
| 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 Ekstrand | 7dac4a2 | 2015-07-30 11:28:22 -0700 | [diff] [blame] | 131 | static inline void list_splice(struct list_head *src, struct list_head *dst) |
| 132 | { |
Timothy Arceri | 7f106a2 | 2019-10-28 21:27:52 +1100 | [diff] [blame] | 133 | if (list_is_empty(src)) |
Jason Ekstrand | 7dac4a2 | 2015-07-30 11:28:22 -0700 | [diff] [blame] | 134 | 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 | |
| 142 | static inline void list_splicetail(struct list_head *src, struct list_head *dst) |
| 143 | { |
Timothy Arceri | 7f106a2 | 2019-10-28 21:27:52 +1100 | [diff] [blame] | 144 | if (list_is_empty(src)) |
Jason Ekstrand | 7dac4a2 | 2015-07-30 11:28:22 -0700 | [diff] [blame] | 145 | 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 Ekstrand | 5c1c693 | 2018-07-25 10:37:53 -0700 | [diff] [blame] | 153 | static inline void list_validate(const struct list_head *list) |
Jason Ekstrand | 2c2cd36 | 2015-04-27 20:40:11 -0700 | [diff] [blame] | 154 | { |
| 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é Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 161 | #define LIST_ENTRY(__type, __item, __field) \ |
| 162 | ((__type *)(((char *)(__item)) - offsetof(__type, __field))) |
| 163 | |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 164 | /** |
| 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 Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 169 | #ifndef container_of |
| 170 | #define container_of(ptr, sample, member) \ |
| 171 | (void *)((char *)(ptr) \ |
| 172 | - ((char *)&(sample)->member - (char *)(sample))) |
| 173 | #endif |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 174 | |
Rob Clark | c79b2e6 | 2015-04-29 08:38:45 -0400 | [diff] [blame] | 175 | #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 Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 182 | #define LIST_FOR_EACH_ENTRY(pos, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 183 | for (pos = NULL, pos = container_of((head)->next, pos, member); \ |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 184 | &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 Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 188 | for (pos = NULL, pos = container_of((head)->next, pos, member), \ |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 189 | storage = container_of(pos->member.next, pos, member); \ |
| 190 | &pos->member != (head); \ |
| 191 | pos = storage, storage = container_of(storage->member.next, storage, member)) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 192 | |
| 193 | #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 194 | for (pos = NULL, pos = container_of((head)->prev, pos, member), \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 195 | 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 Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 200 | for (pos = NULL, pos = container_of((start), pos, member); \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 201 | &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 Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 205 | for (pos = NULL, pos = container_of((start), pos, member); \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 206 | &pos->member != (head); \ |
| 207 | pos = container_of(pos->member.prev, pos, member)) |
| 208 | |
Jason Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 209 | #define list_for_each_entry(type, pos, head, member) \ |
Rob Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 210 | for (type *pos = LIST_ENTRY(type, (head)->next, member), \ |
| 211 | *__next = LIST_ENTRY(type, pos->member.next, member); \ |
Jason Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 212 | &pos->member != (head); \ |
Rob Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 213 | 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 Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 216 | |
| 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 Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 222 | __next = LIST_ENTRY(type, __next->member.next, member)) |
Jason Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 223 | |
| 224 | #define list_for_each_entry_rev(type, pos, head, member) \ |
Rob Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 225 | for (type *pos = LIST_ENTRY(type, (head)->prev, member), \ |
| 226 | *__prev = LIST_ENTRY(type, pos->member.prev, member); \ |
Jason Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 227 | &pos->member != (head); \ |
Rob Clark | 372e83b | 2019-05-25 10:50:41 -0700 | [diff] [blame] | 228 | 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 Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 231 | |
| 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 Filho | f40f8f6 | 2019-09-16 18:21:06 -0700 | [diff] [blame] | 244 | #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 Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame] | 251 | #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 Ekstrand | aeb95fd | 2019-08-05 16:19:06 -0500 | [diff] [blame] | 256 | #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 Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 263 | #endif /*_UTIL_LIST_H_*/ |