| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #ifndef LLIST_H |
|---|
| 20 | #define LLIST_H |
|---|
| 21 | |
|---|
| 22 | #include <typedefs.h> |
|---|
| 23 | |
|---|
| 24 | struct ListHead |
|---|
| 25 | { |
|---|
| 26 | struct ListHead* next, *prev; |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | #define INIT_LIST_HEAD(ptr) do { (ptr)->next=(ptr); (ptr)->prev=(ptr); } while(0) |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | #define LIST_HEAD(name) struct ListHead name=LIST_HEAD_INIT(name) |
|---|
| 71 | |
|---|
| 72 | static inline void DoListAdd(struct ListHead* new,struct ListHead* prev,struct ListHead* next) |
|---|
| 73 | { |
|---|
| 74 | next->prev=new; |
|---|
| 75 | new->next=next; |
|---|
| 76 | new->prev=prev; |
|---|
| 77 | prev->next=new; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static inline void ListAdd(struct ListHead* new,struct ListHead* head) |
|---|
| 81 | { |
|---|
| 82 | DoListAdd(new,head,head->next); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | static inline void ListAddTail(struct ListHead* new,struct ListHead* head) |
|---|
| 88 | { |
|---|
| 89 | DoListAdd(new,head->prev,head); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | static inline void DoListRemove(struct ListHead* prev,struct ListHead* next) |
|---|
| 93 | { |
|---|
| 94 | next->prev=prev; |
|---|
| 95 | prev->next=next; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | static inline void ListRemove(struct ListHead* entry) |
|---|
| 99 | { |
|---|
| 100 | DoListRemove(entry->prev,entry->next); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | static inline void ListRemoveInit(struct ListHead* entry) |
|---|
| 104 | { |
|---|
| 105 | DoListRemove(entry->prev,entry->next); |
|---|
| 106 | INIT_LIST_HEAD(entry); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | static inline int ListEmpty(struct ListHead* head) |
|---|
| 110 | { |
|---|
| 111 | return (head->next == head); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | static inline void DoListSpliceTail(struct ListHead* list, struct ListHead* head) |
|---|
| 115 | { |
|---|
| 116 | struct ListHead* first = list->next; |
|---|
| 117 | struct ListHead* last = list->prev; |
|---|
| 118 | struct ListHead* at = head->prev; |
|---|
| 119 | |
|---|
| 120 | first->prev = at; |
|---|
| 121 | at->next = first; |
|---|
| 122 | |
|---|
| 123 | last->next = head; |
|---|
| 124 | head->prev = last; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | static inline void ListSpliceTail(struct ListHead* list, struct ListHead* head) |
|---|
| 128 | { |
|---|
| 129 | if (!ListEmpty(list)) |
|---|
| 130 | DoListSpliceTail(list, head); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | static inline void ListSpliceTailInit(struct ListHead* list, struct ListHead* head) |
|---|
| 134 | { |
|---|
| 135 | if (!ListEmpty(list)) |
|---|
| 136 | { |
|---|
| 137 | DoListSpliceTail(list, head); |
|---|
| 138 | INIT_LIST_HEAD(list); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | static inline void ListMove(struct ListHead* list, struct ListHead* head) |
|---|
| 143 | { |
|---|
| 144 | DoListRemove(list->prev, list->next); |
|---|
| 145 | ListAdd(list, head); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static inline void ListMoveTail(struct ListHead* list, struct ListHead* head) |
|---|
| 149 | { |
|---|
| 150 | DoListRemove(list->prev, list->next); |
|---|
| 151 | ListAddTail(list, head); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | #define ListEntry(ptr,type,member) \ |
|---|
| 156 | ((type*)(((char*)ptr) - OffsetOf(type,member))) |
|---|
| 157 | |
|---|
| 158 | #define ListForEachEntry(pos,head,member) \ |
|---|
| 159 | for (pos=ListEntry((head)->next,typeof(*pos),member); \ |
|---|
| 160 | &(pos->member) != head; \ |
|---|
| 161 | pos=ListEntry(pos->member.next, typeof(*pos), member)) |
|---|
| 162 | |
|---|
| 163 | #define ListForEachEntryReverse(pos, head, member) \ |
|---|
| 164 | for (pos=ListEntry((head)->prev, typeof(*pos), member); \ |
|---|
| 165 | &(pos->member) != head; \ |
|---|
| 166 | pos = ListEntry(pos->member.prev, typeof(*pos), member)) |
|---|
| 167 | |
|---|
| 168 | #define ListForEachEntrySafe(pos,n,head,member) \ |
|---|
| 169 | for (pos=ListEntry((head)->next,typeof(*pos),member), \ |
|---|
| 170 | n=ListEntry(pos->member.next,typeof(*pos),member); \ |
|---|
| 171 | &pos->member!=(head); \ |
|---|
| 172 | pos=n,n=ListEntry(n->member.next,typeof(*n),member)) |
|---|
| 173 | |
|---|
| 174 | #define ListForEach(pos,head) \ |
|---|
| 175 | for (pos=head; pos != head; pos=pos->next) |
|---|
| 176 | |
|---|
| 177 | #endif |
|---|