| 1 |
/************************************************************************** |
| 2 |
* |
| 3 |
* Copyright 2006 VMware, Inc., Bismarck, ND. USA. |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 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: |
| 13 |
* |
| 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 |
| 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 |
| 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. |
| 25 |
* |
| 26 |
**************************************************************************/ |
| 27 |
|
| 28 |
/** |
| 29 |
* \file |
| 30 |
* List macros heavily inspired by the Linux kernel |
| 31 |
* list handling. No list looping yet. |
| 32 |
* |
| 33 |
* Is not threadsafe, so common operations need to |
| 34 |
* be protected using an external mutex. |
| 35 |
*/ |
| 36 |
|
| 37 |
#ifndef _UTIL_LIST_H_ |
| 38 |
#define _UTIL_LIST_H_ |
| 39 |
|
| 40 |
|
| 41 |
#include <stdbool.h> |
| 42 |
#include <stddef.h> |
| 43 |
#include <assert.h> |
| 44 |
|
| 45 |
struct list_head |
| 46 |
{ |
| 47 |
struct list_head *prev; |
| 48 |
struct list_head *next; |
| 49 |
}; |
| 50 |
|
| 51 |
static inline void list_inithead(struct list_head *item) |
| 52 |
{ |
| 53 |
item->prev = item; |
| 54 |
item->next = item; |
| 55 |
} |
| 56 |
|
| 57 |
static inline void list_add(struct list_head *item, struct list_head *list) |
| 58 |
{ |
| 59 |
item->prev = list; |
| 60 |
item->next = list->next; |
| 61 |
list->next->prev = item; |
| 62 |
list->next = item; |
| 63 |
} |
| 64 |
|
| 65 |
static inline void list_addtail(struct list_head *item, struct list_head *list) |
| 66 |
{ |
| 67 |
item->next = list; |
| 68 |
item->prev = list->prev; |
| 69 |
list->prev->next = item; |
| 70 |
list->prev = item; |
| 71 |
} |
| 72 |
|
| 73 |
static inline bool list_is_empty(const struct list_head *list); |
| 74 |
|
| 75 |
static inline void list_replace(struct list_head *from, struct list_head *to) |
| 76 |
{ |
| 77 |
if (list_is_empty(from)) { |
| 78 |
list_inithead(to); |
| 79 |
} else { |
| 80 |
to->prev = from->prev; |
| 81 |
to->next = from->next; |
| 82 |
from->next->prev = to; |
| 83 |
from->prev->next = to; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static inline void list_del(struct list_head *item) |
| 88 |
{ |
| 89 |
item->prev->next = item->next; |
| 90 |
item->next->prev = item->prev; |
| 91 |
item->prev = item->next = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
static inline void list_delinit(struct list_head *item) |
| 95 |
{ |
| 96 |
item->prev->next = item->next; |
| 97 |
item->next->prev = item->prev; |
| 98 |
item->next = item; |
| 99 |
item->prev = item; |
| 100 |
} |
| 101 |
|
| 102 |
static inline bool list_is_empty(const struct list_head *list) |
| 103 |
{ |
| 104 |
return list->next == list; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns whether the list has exactly one element. |
| 109 |
*/ |
| 110 |
static inline bool list_is_singular(const struct list_head *list) |
| 111 |
{ |
| 112 |
return list->next != NULL && list->next != list && list->next->next == list; |
| 113 |
} |
| 114 |
|
| 115 |
static inline unsigned list_length(const struct list_head *list) |
| 116 |
{ |
| 117 |
struct list_head *node; |
| 118 |
unsigned length = 0; |
| 119 |
for (node = list->next; node != list; node = node->next) |
| 120 |
length++; |
| 121 |
return length; |
| 122 |
} |
| 123 |
|
| 124 |
static inline void list_splice(struct list_head *src, struct list_head *dst) |
| 125 |
{ |
| 126 |
if (list_is_empty(src)) |
| 127 |
return; |
| 128 |
|
| 129 |
src->next->prev = dst; |
| 130 |
src->prev->next = dst->next; |
| 131 |
dst->next->prev = src->prev; |
| 132 |
dst->next = src->next; |
| 133 |
} |
| 134 |
|
| 135 |
static inline void list_splicetail(struct list_head *src, struct list_head *dst) |
| 136 |
{ |
| 137 |
if (list_is_empty(src)) |
| 138 |
return; |
| 139 |
|
| 140 |
src->prev->next = dst; |
| 141 |
src->next->prev = dst->prev; |
| 142 |
dst->prev->next = src->next; |
| 143 |
dst->prev = src->prev; |
| 144 |
} |
| 145 |
|
| 146 |
static inline void list_validate(const struct list_head *list) |
| 147 |
{ |
| 148 |
struct list_head *node; |
| 149 |
assert(list->next->prev == list && list->prev->next == list); |
| 150 |
for (node = list->next; node != list; node = node->next) |
| 151 |
assert(node->next->prev == node && node->prev->next == node); |
| 152 |
} |
| 153 |
|
| 154 |
#define LIST_ENTRY(__type, __item, __field) \ |
| 155 |
((__type *)(((char *)(__item)) - offsetof(__type, __field))) |
| 156 |
|
| 157 |
/** |
| 158 |
* Cast from a pointer to a member of a struct back to the containing struct. |
| 159 |
* |
| 160 |
* 'sample' MUST be initialized, or else the result is undefined! |
| 161 |
*/ |
| 162 |
#ifndef container_of |
| 163 |
#define container_of(ptr, sample, member) \ |
| 164 |
(void *)((char *)(ptr) \ |
| 165 |
- ((char *)&(sample)->member - (char *)(sample))) |
| 166 |
#endif |
| 167 |
|
| 168 |
#define list_first_entry(ptr, type, member) \ |
| 169 |
LIST_ENTRY(type, (ptr)->next, member) |
| 170 |
|
| 171 |
#define list_last_entry(ptr, type, member) \ |
| 172 |
LIST_ENTRY(type, (ptr)->prev, member) |
| 173 |
|
| 174 |
|
| 175 |
#define LIST_FOR_EACH_ENTRY(pos, head, member) \ |
| 176 |
for (pos = NULL, pos = container_of((head)->next, pos, member); \ |
| 177 |
&pos->member != (head); \ |
| 178 |
pos = container_of(pos->member.next, pos, member)) |
| 179 |
|
| 180 |
#define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \ |
| 181 |
for (pos = NULL, pos = container_of((head)->next, pos, member), \ |
| 182 |
storage = container_of(pos->member.next, pos, member); \ |
| 183 |
&pos->member != (head); \ |
| 184 |
pos = storage, storage = container_of(storage->member.next, storage, member)) |
| 185 |
|
| 186 |
#define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \ |
| 187 |
for (pos = NULL, pos = container_of((head)->prev, pos, member), \ |
| 188 |
storage = container_of(pos->member.prev, pos, member); \ |
| 189 |
&pos->member != (head); \ |
| 190 |
pos = storage, storage = container_of(storage->member.prev, storage, member)) |
| 191 |
|
| 192 |
#define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \ |
| 193 |
for (pos = NULL, pos = container_of((start), pos, member); \ |
| 194 |
&pos->member != (head); \ |
| 195 |
pos = container_of(pos->member.next, pos, member)) |
| 196 |
|
| 197 |
#define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \ |
| 198 |
for (pos = NULL, pos = container_of((start), pos, member); \ |
| 199 |
&pos->member != (head); \ |
| 200 |
pos = container_of(pos->member.prev, pos, member)) |
| 201 |
|
| 202 |
#define list_for_each_entry(type, pos, head, member) \ |
| 203 |
for (type *pos = LIST_ENTRY(type, (head)->next, member), \ |
| 204 |
*__next = LIST_ENTRY(type, pos->member.next, member); \ |
| 205 |
&pos->member != (head); \ |
| 206 |
pos = LIST_ENTRY(type, pos->member.next, member), \ |
| 207 |
__next = LIST_ENTRY(type, __next->member.next, member)) |
| 208 |
|
| 209 |
#define list_for_each_entry_safe(type, pos, head, member) \ |
| 210 |
for (type *pos = LIST_ENTRY(type, (head)->next, member), \ |
| 211 |
*__next = LIST_ENTRY(type, pos->member.next, member); \ |
| 212 |
&pos->member != (head); \ |
| 213 |
pos = __next, \ |
| 214 |
__next = LIST_ENTRY(type, __next->member.next, member)) |
| 215 |
|
| 216 |
#define list_for_each_entry_rev(type, pos, head, member) \ |
| 217 |
for (type *pos = LIST_ENTRY(type, (head)->prev, member), \ |
| 218 |
*__prev = LIST_ENTRY(type, pos->member.prev, member); \ |
| 219 |
&pos->member != (head); \ |
| 220 |
pos = LIST_ENTRY(type, pos->member.prev, member), \ |
| 221 |
__prev = LIST_ENTRY(type, __prev->member.prev, member)) |
| 222 |
|
| 223 |
#define list_for_each_entry_safe_rev(type, pos, head, member) \ |
| 224 |
for (type *pos = LIST_ENTRY(type, (head)->prev, member), \ |
| 225 |
*__prev = LIST_ENTRY(type, pos->member.prev, member); \ |
| 226 |
&pos->member != (head); \ |
| 227 |
pos = __prev, \ |
| 228 |
__prev = LIST_ENTRY(type, __prev->member.prev, member)) |
| 229 |
|
| 230 |
#define list_for_each_entry_from(type, pos, start, head, member) \ |
| 231 |
for (type *pos = LIST_ENTRY(type, (start), member); \ |
| 232 |
&pos->member != (head); \ |
| 233 |
pos = LIST_ENTRY(type, pos->member.next, member)) |
| 234 |
|
| 235 |
#define list_for_each_entry_from_safe(type, pos, start, head, member) \ |
| 236 |
for (type *pos = LIST_ENTRY(type, (start), member), \ |
| 237 |
*__next = LIST_ENTRY(type, pos->member.next, member); \ |
| 238 |
&pos->member != (head); \ |
| 239 |
pos = __next, \ |
| 240 |
__next = LIST_ENTRY(type, __next->member.next, member)) |
| 241 |
|
| 242 |
#define list_for_each_entry_from_rev(type, pos, start, head, member) \ |
| 243 |
for (type *pos = LIST_ENTRY(type, (start), member); \ |
| 244 |
&pos->member != (head); \ |
| 245 |
pos = LIST_ENTRY(type, pos->member.prev, member)) |
| 246 |
|
| 247 |
#define list_pair_for_each_entry(type, pos1, pos2, head1, head2, member) \ |
| 248 |
for (type *pos1 = LIST_ENTRY(type, (head1)->next, member), \ |
| 249 |
*pos2 = LIST_ENTRY(type, (head2)->next, member); \ |
| 250 |
&pos1->member != (head1) && &pos2->member != (head2); \ |
| 251 |
pos1 = LIST_ENTRY(type, pos1->member.next, member), \ |
| 252 |
pos2 = LIST_ENTRY(type, pos2->member.next, member)) |
| 253 |
|
| 254 |
#endif /*_UTIL_LIST_H_*/ |