1 /* Doubly linked list macros compatible with Linux kernel's version
2 * Copyright (c) 2015 by Takashi Iwai <tiwai@suse.de>
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
21 struct list_head *next;
22 struct list_head *prev;
25 /* one-shot definition of a list head */
26 #define LIST_HEAD(x) \
27 struct list_head x = { &x, &x }
29 /* initialize a list head explicitly */
30 static inline void INIT_LIST_HEAD(struct list_head *p)
32 p->next = p->prev = p;
35 #define list_entry_offset(p, type, offset) \
36 ((type *)((char *)(p) - (offset)))
38 /* list_entry - retrieve the original struct from list_head
39 * @p: list_head pointer
41 * @member: struct field member containing the list_head
43 #define list_entry(p, type, member) \
44 list_entry_offset(p, type, offsetof(type, member))
46 /* list_for_each - iterate over the linked list
47 * @p: iterator, a list_head pointer variable
48 * @list: list_head pointer containing the list
50 #define list_for_each(p, list) \
51 for (p = (list)->next; p != (list); p = p->next)
53 /* list_for_each_safe - iterate over the linked list, safe to delete
54 * @p: iterator, a list_head pointer variable
55 * @s: a temporary variable to keep the next, a list_head pointer, too
56 * @list: list_head pointer containing the list
58 #define list_for_each_safe(p, s, list) \
59 for (p = (list)->next; s = p->next, p != (list); p = s)
61 /* list_add - prepend a list entry at the head
62 * @p: the new list entry to add
63 * @list: the list head
65 static inline void list_add(struct list_head *p, struct list_head *list)
67 struct list_head *first = list->next;
75 /* list_add_tail - append a list entry at the tail
76 * @p: the new list entry to add
77 * @list: the list head
79 static inline void list_add_tail(struct list_head *p, struct list_head *list)
81 struct list_head *last = list->prev;
89 /* list_del - delete the given list entry */
90 static inline void list_del(struct list_head *p)
92 p->prev->next = p->next;
93 p->next->prev = p->prev;
96 /* list_empty - returns 1 if the given list is empty */
97 static inline int list_empty(const struct list_head *p)