summaryrefslogtreecommitdiff
path: root/amavutils/include/list.h (plain)
blob: e54a1cf7a51a06aeef407af8fe6f5e146006c6d2
1/*
2 * Copyright (c) 2014 Amlogic, Inc. All rights reserved.
3 *
4 * This source code is subject to the terms and conditions defined in the
5 * file 'LICENSE' which is part of this source code package.
6 *
7 * Description:
8 */
9
10
11#ifndef LIST_HEADERS_H
12#define LIST_HEADERS_H
13
14#define LIST_POISON1 ((void *) 0x00100100)
15#define LIST_POISON2 ((void *) 0x00200200)
16
17struct list_head {
18 struct list_head *next;
19 struct list_head *prev;
20};
21
22typedef struct list_head list_t;
23
24static inline void INIT_LIST_HEAD(struct list_head *list)
25{
26 list->next = list;
27 list->prev = list;
28}
29static inline void __list_add(struct list_head *newh,
30 struct list_head *prev,
31 struct list_head *next)
32{
33 next->prev = newh;
34 newh->next = next;
35 newh->prev = prev;
36 prev->next = newh;
37}
38
39static inline void __list_del(struct list_head *prev,
40 struct list_head *next)
41{
42 next->prev = prev;
43 prev->next = next;
44}
45static inline void list_add(struct list_head *newh, struct list_head *head)
46{
47 __list_add(newh, head, head->next);
48}
49
50static inline void list_add_tail(struct list_head *newh, struct list_head *head)
51{
52 __list_add(newh, head->prev, head);
53}
54
55static inline void list_del(struct list_head *entry)
56{
57 __list_del(entry->prev, entry->next);
58 entry->next = (struct list_head *)LIST_POISON1;
59 entry->prev = (struct list_head *)LIST_POISON2;
60}
61static inline void list_del_init(struct list_head *entry)
62{
63 __list_del(entry->prev, entry->next);
64 INIT_LIST_HEAD(entry);
65}
66
67/**
68 * list_empty - tests whether a list is empty
69 * @head: the list to test.
70 */
71static inline int list_empty(const struct list_head *head)
72{
73 return head->next == head;
74}
75
76#ifndef offsetof
77#define offsetof(TYPE, MEMBER) ((size_t) (&((TYPE *)0)->MEMBER))
78#endif
79#define prefetch(x) (x)
80
81#ifndef typeof
82#define typeof(T) __typeof__(T)
83#endif
84/**
85 * container_of - cast a member of a structure out to the containing structure
86 * @ptr: the pointer to the member.
87 * @type: the type of the container struct this is embedded in.
88 * @member: the name of the member within the struct.
89 *
90 */
91#define container_of(ptr, type, member) ({ \
92 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
93 (type *)( (char *)__mptr - offsetof(type,member) );})
94
95
96/**
97 * list_entry - get the struct for this entry
98 * @ptr: the &struct list_head pointer.
99 * @type: the type of the struct this is embedded in.
100 * @member: the name of the list_struct within the struct.
101 */
102#define list_entry(ptr, type, member) \
103 container_of(ptr, type, member)
104
105
106/**
107* list_entry - get the struct for this entry
108* @ptr: the &struct list_head pointer.
109* @type: the type of the struct this is embedded in.
110* @member: the name of the list_struct within the struct.
111*/
112#define list_entry(ptr, type, member) \
113 container_of(ptr, type, member)
114
115/**
116 * list_first_entry - get the first element from a list
117 * @ptr: the list head to take the element from.
118 * @type: the type of the struct this is embedded in.
119 * @member: the name of the list_struct within the struct.
120 *
121 * Note, that list is expected to be not empty.
122 */
123#define list_first_entry(ptr, type, member) \
124 list_entry((ptr)->next, type, member)
125
126/**
127 * list_for_each - iterate over a list
128 * @pos: the &struct list_head to use as a loop cursor.
129 * @head: the head for your list.
130 */
131#define list_for_each(pos, head) \
132 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
133 pos = pos->next)
134
135/**
136 * list_for_each_prev - iterate over a list backwards
137 * @pos: the &struct list_head to use as a loop cursor.
138 * @head: the head for your list.
139 */
140#define list_for_each_prev(pos, head) \
141 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
142 pos = pos->prev)
143
144/**
145 * list_for_each_safe - iterate over a list safe against removal of list entry
146 * @pos: the &struct list_head to use as a loop cursor.
147 * @n: another &struct list_head to use as temporary storage
148 * @head: the head for your list.
149 */
150#define list_for_each_safe(pos, n, head) \
151 for (pos = (head)->next, n = pos->next; pos != (head); \
152 pos = n, n = pos->next)
153
154/**
155 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
156 * @pos: the &struct list_head to use as a loop cursor.
157 * @n: another &struct list_head to use as temporary storage
158 * @head: the head for your list.
159 */
160#define list_for_each_prev_safe(pos, n, head) \
161 for (pos = (head)->prev, n = pos->prev; \
162 prefetch(pos->prev), pos != (head); \
163 pos = n, n = pos->prev)
164
165/**
166 * list_for_each_entry - iterate over list of given type
167 * @pos: the type * to use as a loop cursor.
168 * @head: the head for your list.
169 * @member: the name of the list_struct within the struct.
170 */
171#define list_for_each_entry(pos, head, member) \
172 for (pos = list_entry((head)->next, typeof(*pos), member); \
173 prefetch(pos->member.next), &pos->member != (head); \
174 pos = list_entry(pos->member.next, typeof(*pos), member))
175
176/**
177 * list_for_each_entry_reverse - iterate backwards over list of given type.
178 * @pos: the type * to use as a loop cursor.
179 * @head: the head for your list.
180 * @member: the name of the list_struct within the struct.
181 */
182#define list_for_each_entry_reverse(pos, head, member) \
183 for (pos = list_entry((head)->prev, typeof(*pos), member); \
184 prefetch(pos->member.prev), &pos->member != (head); \
185 pos = list_entry(pos->member.prev, typeof(*pos), member))
186
187/**
188 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
189 * @pos: the type * to use as a loop cursor.
190 * @n: another type * to use as temporary storage
191 * @head: the head for your list.
192 * @member: the name of the list_struct within the struct.
193 */
194#define list_for_each_entry_safe(pos, n, head, member) \
195 for (pos = list_entry((head)->next, typeof(*pos), member), \
196 n = list_entry(pos->member.next, typeof(*pos), member); \
197 &pos->member != (head); \
198 pos = n, n = list_entry(n->member.next, typeof(*n), member))
199
200#endif
201
202