Tizen 2.1 base
[platform/upstream/hplip.git] / io / hpmud / list.h
1 #ifndef _LIST_H
2 #define _LIST_H
3
4 /* 
5  * This linked list implementation is the same as linux kernel implementation.
6  * Refer to the linux kernel documentation for usage.
7  */
8
9 struct list_head {
10         struct list_head *next, *prev;
11 };
12
13 #define INIT_LIST_HEAD(ptr) do { \
14         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
15 } while (0)
16
17 /*
18  * Insert a new entry between two known consecutive entries. 
19  *
20  * This is only for internal list manipulation where we know
21  * the prev/next entries already!
22  */
23 static inline void __list_add(struct list_head * new_entry,
24         struct list_head * prev,
25         struct list_head * next)
26 {
27         next->prev = new_entry;
28         new_entry->next = next;
29         new_entry->prev = prev;
30         prev->next = new_entry;
31 }
32
33 /**
34  * list_add - add a new entry
35  * @new: new entry to be added
36  * @head: list head to add it after
37  *
38  * Insert a new entry after the specified head.
39  * This is good for implementing stacks.
40  */
41 static inline void list_add(struct list_head *new_entry, struct list_head *head)
42 {
43         __list_add(new_entry, head, head->next);
44 }
45
46 /**
47  * list_add_tail - add a new entry
48  * @new: new entry to be added
49  * @head: list head to add it before
50  *
51  * Insert a new entry before the specified head.
52  * This is useful for implementing queues.
53  */
54 static inline void list_add_tail(struct list_head *new_entry, struct list_head *head)
55 {
56         __list_add(new_entry, head->prev, head);
57 }
58
59 /*
60  * Delete a list entry by making the prev/next entries
61  * point to each other.
62  *
63  * This is only for internal list manipulation where we know
64  * the prev/next entries already!
65  */
66 static inline void __list_del(struct list_head * prev,
67                                   struct list_head * next)
68 {
69         next->prev = prev;
70         prev->next = next;
71 }
72
73 /**
74  * list_del - deletes entry from list.
75  * @entry: the element to delete from the list.
76  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
77  */
78 static inline void list_del(struct list_head *entry)
79 {
80         __list_del(entry->prev, entry->next);
81         entry->next = entry->prev = 0;
82 }
83
84 /**
85  * list_del_init - deletes entry from list and reinitialize it.
86  * @entry: the element to delete from the list.
87  */
88 static inline void list_del_init(struct list_head *entry)
89 {
90         __list_del(entry->prev, entry->next);
91         INIT_LIST_HEAD(entry); 
92 }
93
94 /**
95  * list_empty - tests whether a list is empty
96  * @head: the list to test.
97  */
98 static inline int list_empty(struct list_head *head)
99 {
100         return head->next == head;
101 }
102
103 /**
104  * list_entry - get the struct for this entry
105  * @ptr:        the &struct list_head pointer.
106  * @type:       the type of the struct this is embedded in.
107  * @member:     the name of the list_struct within the struct.
108  */
109 #define list_entry(ptr, type, member) \
110         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
111
112 /**
113  * list_for_each        -       iterate over a list
114  * @pos:        the &struct list_head to use as a loop counter.
115  * @head:       the head for your list.
116  */
117 #define list_for_each(pos, head) \
118         for (pos = (head)->next; pos != (head); \
119                 pos = pos->next)
120                 
121 /**
122  * list_for_each_safe   -       iterate over a list safe against removal of list entry
123  * @pos:        the &struct list_head to use as a loop counter.
124  * @n:          another &struct list_head to use as temporary storage
125  * @head:       the head for your list.
126  */
127 #define list_for_each_safe(pos, n, head) \
128         for (pos = (head)->next, n = pos->next; pos != (head); \
129                 pos = n, n = pos->next)
130
131 #endif /* _LIST_H */