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 | |
| 41 | #include <stddef.h> |
| 42 | |
| 43 | |
| 44 | struct list_head |
| 45 | { |
| 46 | struct list_head *prev; |
| 47 | struct list_head *next; |
| 48 | }; |
| 49 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 50 | static inline void list_inithead(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 51 | { |
| 52 | item->prev = item; |
| 53 | item->next = item; |
| 54 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 55 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 56 | 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] | 57 | { |
| 58 | item->prev = list; |
| 59 | item->next = list->next; |
| 60 | list->next->prev = item; |
| 61 | list->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_addtail(struct list_head *item, struct list_head *list) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 65 | { |
| 66 | item->next = list; |
| 67 | item->prev = list->prev; |
| 68 | list->prev->next = item; |
| 69 | list->prev = 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_replace(struct list_head *from, struct list_head *to) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 73 | { |
| 74 | to->prev = from->prev; |
| 75 | to->next = from->next; |
| 76 | from->next->prev = to; |
| 77 | from->prev->next = to; |
| 78 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 79 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 80 | static inline void list_del(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 81 | { |
| 82 | item->prev->next = item->next; |
| 83 | item->next->prev = item->prev; |
Chris Fester | 3fffe8f | 2012-10-11 16:01:23 -0600 | [diff] [blame] | 84 | item->prev = item->next = NULL; |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 85 | } |
José Fonseca | bc56e87 | 2008-04-10 22:57:21 +0900 | [diff] [blame] | 86 | |
Jason Ekstrand | 258b419 | 2015-04-27 16:58:29 -0700 | [diff] [blame] | 87 | static inline void list_delinit(struct list_head *item) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 88 | { |
| 89 | item->prev->next = item->next; |
| 90 | item->next->prev = item->prev; |
| 91 | item->next = item; |
| 92 | item->prev = item; |
| 93 | } |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 94 | |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 95 | #define LIST_INITHEAD(__item) list_inithead(__item) |
| 96 | #define LIST_ADD(__item, __list) list_add(__item, __list) |
| 97 | #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list) |
| 98 | #define LIST_REPLACE(__from, __to) list_replace(__from, __to) |
| 99 | #define LIST_DEL(__item) list_del(__item) |
| 100 | #define LIST_DELINIT(__item) list_delinit(__item) |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 101 | |
| 102 | #define LIST_ENTRY(__type, __item, __field) \ |
| 103 | ((__type *)(((char *)(__item)) - offsetof(__type, __field))) |
| 104 | |
José Fonseca | 3708aae | 2009-03-23 12:05:07 +0000 | [diff] [blame] | 105 | #define LIST_IS_EMPTY(__list) \ |
| 106 | ((__list)->next == (__list)) |
| 107 | |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Cast from a pointer to a member of a struct back to the containing struct. |
| 110 | * |
| 111 | * 'sample' MUST be initialized, or else the result is undefined! |
| 112 | */ |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 113 | #ifndef container_of |
| 114 | #define container_of(ptr, sample, member) \ |
| 115 | (void *)((char *)(ptr) \ |
| 116 | - ((char *)&(sample)->member - (char *)(sample))) |
| 117 | #endif |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 118 | |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 119 | #define LIST_FOR_EACH_ENTRY(pos, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 120 | for (pos = NULL, pos = container_of((head)->next, pos, member); \ |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 121 | &pos->member != (head); \ |
| 122 | pos = container_of(pos->member.next, pos, member)) |
| 123 | |
| 124 | #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 125 | for (pos = NULL, pos = container_of((head)->next, pos, member), \ |
Dave Airlie | 8556b77 | 2010-07-02 15:27:17 +1000 | [diff] [blame] | 126 | storage = container_of(pos->member.next, pos, member); \ |
| 127 | &pos->member != (head); \ |
| 128 | pos = storage, storage = container_of(storage->member.next, storage, member)) |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 129 | |
| 130 | #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 131 | for (pos = NULL, pos = container_of((head)->prev, pos, member), \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 132 | storage = container_of(pos->member.prev, pos, member); \ |
| 133 | &pos->member != (head); \ |
| 134 | pos = storage, storage = container_of(storage->member.prev, storage, member)) |
| 135 | |
| 136 | #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 137 | for (pos = NULL, pos = container_of((start), pos, member); \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 138 | &pos->member != (head); \ |
| 139 | pos = container_of(pos->member.next, pos, member)) |
| 140 | |
| 141 | #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \ |
Dylan Noblesmith | ccff749 | 2012-04-01 18:21:47 +0000 | [diff] [blame] | 142 | for (pos = NULL, pos = container_of((start), pos, member); \ |
Jerome Glisse | ebe304f | 2011-03-28 17:45:31 -0400 | [diff] [blame] | 143 | &pos->member != (head); \ |
| 144 | pos = container_of(pos->member.prev, pos, member)) |
| 145 | |
Jason Ekstrand | b31d898 | 2015-04-27 18:56:02 -0700 | [diff] [blame^] | 146 | #define list_for_each_entry(type, pos, head, member) \ |
| 147 | for (type *pos = LIST_ENTRY(type, (head)->next, member); \ |
| 148 | &pos->member != (head); \ |
| 149 | pos = LIST_ENTRY(type, pos->member.next, member)) |
| 150 | |
| 151 | #define list_for_each_entry_safe(type, pos, head, member) \ |
| 152 | for (type *pos = LIST_ENTRY(type, (head)->next, member), \ |
| 153 | *__next = LIST_ENTRY(type, pos->member.next, member); \ |
| 154 | &pos->member != (head); \ |
| 155 | pos = __next, \ |
| 156 | __next = LIST_ENTRY(type, __next->member.next, member)) |
| 157 | |
| 158 | #define list_for_each_entry_rev(type, pos, head, member) \ |
| 159 | for (type *pos = LIST_ENTRY(type, (head)->prev, member); \ |
| 160 | &pos->member != (head); \ |
| 161 | pos = LIST_ENTRY(type, pos->member.prev, member)) |
| 162 | |
| 163 | #define list_for_each_entry_safe_rev(type, pos, head, member) \ |
| 164 | for (type *pos = LIST_ENTRY(type, (head)->prev, member), \ |
| 165 | *__prev = LIST_ENTRY(type, pos->member.prev, member); \ |
| 166 | &pos->member != (head); \ |
| 167 | pos = __prev, \ |
| 168 | __prev = LIST_ENTRY(type, __prev->member.prev, member)) |
| 169 | |
| 170 | #define list_for_each_entry_from(type, pos, start, head, member) \ |
| 171 | for (type *pos = LIST_ENTRY(type, (start), member); \ |
| 172 | &pos->member != (head); \ |
| 173 | pos = LIST_ENTRY(type, pos->member.next, member)) |
| 174 | |
| 175 | #define list_for_each_entry_from_rev(type, pos, start, head, member) \ |
| 176 | for (type *pos = LIST_ENTRY(type, (start), member); \ |
| 177 | &pos->member != (head); \ |
| 178 | pos = LIST_ENTRY(type, pos->member.prev, member)) |
| 179 | |
Jason Ekstrand | 7a30668 | 2015-04-27 17:41:27 -0700 | [diff] [blame] | 180 | #endif /*_UTIL_LIST_H_*/ |