2 * list.h List Utilities
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
16 struct list_head *next;
17 struct list_head *prev;
20 static inline void list_add(struct list_head *obj,
21 struct list_head *prev,
22 struct list_head *next)
30 static inline void list_add_tail(struct list_head *obj,
31 struct list_head *head)
33 list_add(obj, head->prev, head);
36 static inline void list_add_head(struct list_head *obj,
37 struct list_head *head)
39 list_add(obj, head, head->next);
42 static inline void list_del(struct list_head *obj)
44 obj->prev->next = obj->next;
45 obj->next->prev = obj->prev;
46 obj->next = obj->prev = obj;
49 static inline void list_replace(struct list_head *old,
50 struct list_head *obj)
52 obj->next = old->next;
53 obj->next->prev = obj;
54 obj->prev = old->prev;
55 obj->prev->next = obj;
58 static inline int list_empty(struct list_head *head)
60 return head->next == head;
63 #define list_entry(ptr, type, member) \
64 container_of(ptr, type, member)
66 #define list_first_entry(ptr, type, member) \
67 container_of((ptr)->next, type, member)
69 #define list_at_tail(pos, head, member) \
70 ((pos)->member.next == (head))
72 #define list_at_head(pos, head, member) \
73 ((pos)->member.prev == (head))
75 #define LIST_HEAD(name) \
76 struct list_head name = { &(name), &(name) }
78 #define list_for_each_entry(pos, head, member) \
79 for (pos = list_entry((head)->next, typeof(*pos), member); \
80 &(pos)->member != (head); \
81 (pos) = list_entry((pos)->member.next, typeof(*(pos)), member))
83 #define list_for_each_entry_safe(pos, n, head, member) \
84 for (pos = list_entry((head)->next, typeof(*pos), member), \
85 n = list_entry(pos->member.next, typeof(*pos), member); \
86 &(pos)->member != (head); \
87 pos = n, n = list_entry(n->member.next, typeof(*n), member))
89 #define init_list_head(head) \
90 do { (head)->next = (head); (head)->prev = (head); } while (0)