bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Karel Zak <kzak@redhat.com> |
| 3 | * Copyright (C) 1999-2008 by Theodore Ts'o |
| 4 | * |
| 5 | * This file may be redistributed under the terms of the |
| 6 | * GNU Lesser General Public License. |
| 7 | * |
| 8 | * (based on list.h from e2fsprogs) |
| 9 | * Merge sort based on kernel's implementation. |
| 10 | */ |
| 11 | |
| 12 | #ifndef UTIL_LINUX_LIST_H |
| 13 | #define UTIL_LINUX_LIST_H |
| 14 | |
| 15 | /* TODO: use AC_C_INLINE */ |
| 16 | #ifdef __GNUC__ |
| 17 | #define _INLINE_ static __inline__ |
| 18 | #else /* For Watcom C */ |
| 19 | #define _INLINE_ static inline |
| 20 | #endif |
| 21 | |
| 22 | /* |
| 23 | * Simple doubly linked list implementation. |
| 24 | * |
| 25 | * Some of the internal functions ("__xxx") are useful when |
| 26 | * manipulating whole lists rather than single entries, as |
| 27 | * sometimes we already know the next/prev entries and we can |
| 28 | * generate better code by using them directly rather than |
| 29 | * using the generic single-entry routines. |
| 30 | */ |
| 31 | |
| 32 | struct list_head { |
| 33 | struct list_head *next, *prev; |
| 34 | }; |
| 35 | |
| 36 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 37 | |
| 38 | #define LIST_HEAD(name) \ |
| 39 | struct list_head name = LIST_HEAD_INIT(name) |
| 40 | |
| 41 | #define INIT_LIST_HEAD(ptr) do { \ |
| 42 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
| 43 | } while (0) |
| 44 | |
| 45 | /* |
| 46 | * Insert a new entry between two known consecutive entries. |
| 47 | * |
| 48 | * This is only for internal list manipulation where we know |
| 49 | * the prev/next entries already! |
| 50 | */ |
| 51 | _INLINE_ void __list_add(struct list_head * add, |
| 52 | struct list_head * prev, |
| 53 | struct list_head * next) |
| 54 | { |
| 55 | next->prev = add; |
| 56 | add->next = next; |
| 57 | add->prev = prev; |
| 58 | prev->next = add; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * list_add - add a new entry |
| 63 | * @add: new entry to be added |
| 64 | * @head: list head to add it after |
| 65 | * |
| 66 | * Insert a new entry after the specified head. |
| 67 | * This is good for implementing stacks. |
| 68 | */ |
| 69 | _INLINE_ void list_add(struct list_head *add, struct list_head *head) |
| 70 | { |
| 71 | __list_add(add, head, head->next); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * list_add_tail - add a new entry |
| 76 | * @add: new entry to be added |
| 77 | * @head: list head to add it before |
| 78 | * |
| 79 | * Insert a new entry before the specified head. |
| 80 | * This is useful for implementing queues. |
| 81 | */ |
| 82 | _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head) |
| 83 | { |
| 84 | __list_add(add, head->prev, head); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Delete a list entry by making the prev/next entries |
| 89 | * point to each other. |
| 90 | * |
| 91 | * This is only for internal list manipulation where we know |
| 92 | * the prev/next entries already! |
| 93 | */ |
| 94 | _INLINE_ void __list_del(struct list_head * prev, |
| 95 | struct list_head * next) |
| 96 | { |
| 97 | next->prev = prev; |
| 98 | prev->next = next; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * list_del - deletes entry from list. |
| 103 | * @entry: the element to delete from the list. |
| 104 | * |
| 105 | * list_empty() on @entry does not return true after this, @entry is |
| 106 | * in an undefined state. |
| 107 | */ |
| 108 | _INLINE_ void list_del(struct list_head *entry) |
| 109 | { |
| 110 | __list_del(entry->prev, entry->next); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * list_del_init - deletes entry from list and reinitialize it. |
| 115 | * @entry: the element to delete from the list. |
| 116 | */ |
| 117 | _INLINE_ void list_del_init(struct list_head *entry) |
| 118 | { |
| 119 | __list_del(entry->prev, entry->next); |
| 120 | INIT_LIST_HEAD(entry); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * list_empty - tests whether a list is empty |
| 125 | * @head: the list to test. |
| 126 | */ |
| 127 | _INLINE_ int list_empty(struct list_head *head) |
| 128 | { |
| 129 | return head->next == head; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * list_entry_is_last - tests whether is entry last in the list |
| 134 | * @entry: the entry to test. |
| 135 | * @head: the list to test. |
| 136 | */ |
| 137 | _INLINE_ int list_entry_is_last(struct list_head *entry, struct list_head *head) |
| 138 | { |
| 139 | return head->prev == entry; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * list_splice - join two lists |
| 144 | * @list: the new list to add. |
| 145 | * @head: the place to add it in the first list. |
| 146 | */ |
| 147 | _INLINE_ void list_splice(struct list_head *list, struct list_head *head) |
| 148 | { |
| 149 | struct list_head *first = list->next; |
| 150 | |
| 151 | if (first != list) { |
| 152 | struct list_head *last = list->prev; |
| 153 | struct list_head *at = head->next; |
| 154 | |
| 155 | first->prev = head; |
| 156 | head->next = first; |
| 157 | |
| 158 | last->next = at; |
| 159 | at->prev = last; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * list_entry - get the struct for this entry |
| 165 | * @ptr: the &struct list_head pointer. |
| 166 | * @type: the type of the struct this is embedded in. |
| 167 | * @member: the name of the list_struct within the struct. |
| 168 | */ |
| 169 | #define list_entry(ptr, type, member) ({ \ |
| 170 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 171 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 172 | |
| 173 | |
| 174 | #define list_first_entry(head, type, member) \ |
| 175 | ((head) && (head)->next != (head) ? list_entry((head)->next, type, member) : NULL) |
| 176 | |
| 177 | #define list_last_entry(head, type, member) \ |
| 178 | ((head) && (head)->prev != (head) ? list_entry((head)->prev, type, member) : NULL) |
| 179 | |
| 180 | /** |
| 181 | * list_for_each - iterate over elements in a list |
| 182 | * @pos: the &struct list_head to use as a loop counter. |
| 183 | * @head: the head for your list. |
| 184 | */ |
| 185 | #define list_for_each(pos, head) \ |
| 186 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 187 | |
| 188 | /** |
| 189 | * list_for_each_backwardly - iterate over elements in a list in reverse |
| 190 | * @pos: the &struct list_head to use as a loop counter. |
| 191 | * @head: the head for your list. |
| 192 | */ |
| 193 | #define list_for_each_backwardly(pos, head) \ |
| 194 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
| 195 | |
| 196 | /** |
| 197 | * list_for_each_safe - iterate over elements in a list, but don't dereference |
| 198 | * pos after the body is done (in case it is freed) |
| 199 | * @pos: the &struct list_head to use as a loop counter. |
| 200 | * @pnext: the &struct list_head to use as a pointer to the next item. |
| 201 | * @head: the head for your list (not included in iteration). |
| 202 | */ |
| 203 | #define list_for_each_safe(pos, pnext, head) \ |
| 204 | for (pos = (head)->next, pnext = pos->next; pos != (head); \ |
| 205 | pos = pnext, pnext = pos->next) |
| 206 | |
| 207 | #define MAX_LIST_LENGTH_BITS 20 |
| 208 | |
| 209 | /* |
| 210 | * Returns a list organized in an intermediate format suited |
| 211 | * to chaining of merge() calls: null-terminated, no reserved or |
| 212 | * sentinel head node, "prev" links not maintained. |
| 213 | */ |
| 214 | _INLINE_ struct list_head *merge(int (*cmp)(struct list_head *a, |
| 215 | struct list_head *b), |
| 216 | struct list_head *a, struct list_head *b) |
| 217 | { |
| 218 | struct list_head head, *tail = &head; |
| 219 | |
| 220 | while (a && b) { |
| 221 | /* if equal, take 'a' -- important for sort stability */ |
| 222 | if ((*cmp)(a, b) <= 0) { |
| 223 | tail->next = a; |
| 224 | a = a->next; |
| 225 | } else { |
| 226 | tail->next = b; |
| 227 | b = b->next; |
| 228 | } |
| 229 | tail = tail->next; |
| 230 | } |
| 231 | tail->next = a ? a : b; |
| 232 | return head.next; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Combine final list merge with restoration of standard doubly-linked |
| 237 | * list structure. This approach duplicates code from merge(), but |
| 238 | * runs faster than the tidier alternatives of either a separate final |
| 239 | * prev-link restoration pass, or maintaining the prev links |
| 240 | * throughout. |
| 241 | */ |
| 242 | _INLINE_ void merge_and_restore_back_links(int (*cmp)(struct list_head *a, |
| 243 | struct list_head *b), |
| 244 | struct list_head *head, |
| 245 | struct list_head *a, struct list_head *b) |
| 246 | { |
| 247 | struct list_head *tail = head; |
| 248 | |
| 249 | while (a && b) { |
| 250 | /* if equal, take 'a' -- important for sort stability */ |
| 251 | if ((*cmp)(a, b) <= 0) { |
| 252 | tail->next = a; |
| 253 | a->prev = tail; |
| 254 | a = a->next; |
| 255 | } else { |
| 256 | tail->next = b; |
| 257 | b->prev = tail; |
| 258 | b = b->next; |
| 259 | } |
| 260 | tail = tail->next; |
| 261 | } |
| 262 | tail->next = a ? a : b; |
| 263 | |
| 264 | do { |
| 265 | /* |
| 266 | * In worst cases this loop may run many iterations. |
| 267 | * Continue callbacks to the client even though no |
| 268 | * element comparison is needed, so the client's cmp() |
| 269 | * routine can invoke cond_resched() periodically. |
| 270 | */ |
| 271 | (*cmp)(tail->next, tail->next); |
| 272 | |
| 273 | tail->next->prev = tail; |
| 274 | tail = tail->next; |
| 275 | } while (tail->next); |
| 276 | |
| 277 | tail->next = head; |
| 278 | head->prev = tail; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * list_sort - sort a list |
| 284 | * @head: the list to sort |
| 285 | * @cmp: the elements comparison function |
| 286 | * |
| 287 | * This function implements "merge sort", which has O(nlog(n)) |
| 288 | * complexity. |
| 289 | * |
| 290 | * The comparison function @cmp must return a negative value if @a |
| 291 | * should sort before @b, and a positive value if @a should sort after |
| 292 | * @b. If @a and @b are equivalent, and their original relative |
| 293 | * ordering is to be preserved, @cmp must return 0. |
| 294 | */ |
| 295 | _INLINE_ void list_sort(struct list_head *head, |
| 296 | int (*cmp)(struct list_head *a, |
| 297 | struct list_head *b)) |
| 298 | { |
| 299 | struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists |
| 300 | -- last slot is a sentinel */ |
| 301 | size_t lev; /* index into part[] */ |
| 302 | size_t max_lev = 0; |
| 303 | struct list_head *list; |
| 304 | |
| 305 | if (list_empty(head)) |
| 306 | return; |
| 307 | |
| 308 | memset(part, 0, sizeof(part)); |
| 309 | |
| 310 | head->prev->next = NULL; |
| 311 | list = head->next; |
| 312 | |
| 313 | while (list) { |
| 314 | struct list_head *cur = list; |
| 315 | list = list->next; |
| 316 | cur->next = NULL; |
| 317 | |
| 318 | for (lev = 0; part[lev]; lev++) { |
| 319 | cur = merge(cmp, part[lev], cur); |
| 320 | part[lev] = NULL; |
| 321 | } |
| 322 | if (lev > max_lev) { |
| 323 | /* list passed to list_sort() too long for efficiency */ |
| 324 | if (lev >= ARRAY_SIZE(part) - 1) |
| 325 | lev--; |
| 326 | max_lev = lev; |
| 327 | } |
| 328 | part[lev] = cur; |
| 329 | } |
| 330 | |
| 331 | for (lev = 0; lev < max_lev; lev++) |
| 332 | if (part[lev]) |
| 333 | list = merge(cmp, part[lev], list); |
| 334 | |
| 335 | merge_and_restore_back_links(cmp, head, part[max_lev], list); |
| 336 | } |
| 337 | |
| 338 | #undef _INLINE_ |
| 339 | |
| 340 | #endif /* UTIL_LINUX_LIST_H */ |