1 /* GPL v2, adapted from the Linux kernel */
7 * container_of - cast a member of a structure out to the containing structure
8 * @ptr: the pointer to the member.
9 * @type: the type of the container struct this is embedded in.
10 * @member: the name of the member within the struct.
14 #define container_of(ptr, type, member) ( \
15 (type *)( (char *)ptr - offsetof(type,member) ))
20 * Simple doubly linked list implementation.
22 * Some of the internal functions ("__xxx") are useful when
23 * manipulating whole lists rather than single entries, as
24 * sometimes we already know the next/prev entries and we can
25 * generate better code by using them directly rather than
26 * using the generic single-entry routines.
30 struct list_head
*next
, *prev
;
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35 #define LIST_HEAD(name) \
36 struct list_head name = LIST_HEAD_INIT(name)
38 static inline void INIT_LIST_HEAD(struct list_head
*list
)
45 * Insert a new entry between two known consecutive entries.
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
50 static inline void __list_add(struct list_head
*new,
51 struct list_head
*prev
,
52 struct list_head
*next
)
61 * list_add - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it after
65 * Insert a new entry after the specified head.
66 * This is good for implementing stacks.
68 static inline void list_add(struct list_head
*new, struct list_head
*head
)
70 __list_add(new, head
, head
->next
);
75 * list_add_tail - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it before
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
82 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
84 __list_add(new, head
->prev
, head
);
89 * Delete a list entry by making the prev/next entries
90 * point to each other.
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
95 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
102 * list_del - deletes entry from list.
103 * @entry: the element to delete from the list.
104 * Note: list_empty() on entry does not return true after this, the entry is
105 * in an undefined state.
107 static inline void list_del(struct list_head
*entry
)
109 __list_del(entry
->prev
, entry
->next
);
115 * list_replace - replace old entry by new one
116 * @old : the element to be replaced
117 * @new : the new element to insert
119 * If @old was empty, it will be overwritten.
121 static inline void list_replace(struct list_head
*old
,
122 struct list_head
*new)
124 new->next
= old
->next
;
125 new->next
->prev
= new;
126 new->prev
= old
->prev
;
127 new->prev
->next
= new;
130 static inline void list_replace_init(struct list_head
*old
,
131 struct list_head
*new)
133 list_replace(old
, new);
138 * list_del_init - deletes entry from list and reinitialize it.
139 * @entry: the element to delete from the list.
141 static inline void list_del_init(struct list_head
*entry
)
143 __list_del(entry
->prev
, entry
->next
);
144 INIT_LIST_HEAD(entry
);
148 * list_move - delete from one list and add as another's head
149 * @list: the entry to move
150 * @head: the head that will precede our entry
152 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
154 __list_del(list
->prev
, list
->next
);
155 list_add(list
, head
);
159 * list_move_tail - delete from one list and add as another's tail
160 * @list: the entry to move
161 * @head: the head that will follow our entry
163 static inline void list_move_tail(struct list_head
*list
,
164 struct list_head
*head
)
166 __list_del(list
->prev
, list
->next
);
167 list_add_tail(list
, head
);
171 * list_is_last - tests whether @list is the last entry in list @head
172 * @list: the entry to test
173 * @head: the head of the list
175 static inline int list_is_last(const struct list_head
*list
,
176 const struct list_head
*head
)
178 return list
->next
== head
;
182 * list_empty - tests whether a list is empty
183 * @head: the list to test.
185 static inline int list_empty(const struct list_head
*head
)
187 return head
->next
== head
;
191 * list_empty_careful - tests whether a list is empty and not being modified
192 * @head: the list to test
195 * tests whether a list is empty _and_ checks that no other CPU might be
196 * in the process of modifying either member (next or prev)
198 * NOTE: using list_empty_careful() without synchronization
199 * can only be safe if the only activity that can happen
200 * to the list entry is list_del_init(). Eg. it cannot be used
201 * if another CPU could re-list_add() it.
203 static inline int list_empty_careful(const struct list_head
*head
)
205 struct list_head
*next
= head
->next
;
206 return (next
== head
) && (next
== head
->prev
);
209 static inline void __list_splice(struct list_head
*list
,
210 struct list_head
*head
)
212 struct list_head
*first
= list
->next
;
213 struct list_head
*last
= list
->prev
;
214 struct list_head
*at
= head
->next
;
224 * list_splice - join two lists
225 * @list: the new list to add.
226 * @head: the place to add it in the first list.
228 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
230 if (!list_empty(list
))
231 __list_splice(list
, head
);
235 * list_splice_init - join two lists and reinitialise the emptied list.
236 * @list: the new list to add.
237 * @head: the place to add it in the first list.
239 * The list at @list is reinitialised
241 static inline void list_splice_init(struct list_head
*list
,
242 struct list_head
*head
)
244 if (!list_empty(list
)) {
245 __list_splice(list
, head
);
246 INIT_LIST_HEAD(list
);
251 * list_entry - get the struct for this entry
252 * @ptr: the &struct list_head pointer.
253 * @type: the type of the struct this is embedded in.
254 * @member: the name of the list_struct within the struct.
256 #define list_entry(ptr, type, member) \
257 container_of(ptr, type, member)
260 * list_first_entry - get the first element from a list
261 * @ptr: the list head to take the element from.
262 * @type: the type of the struct this is embedded in.
263 * @member: the name of the list_struct within the struct.
265 * Note, that list is expected to be not empty.
267 #define list_first_entry(ptr, type, member) \
268 list_entry((ptr)->next, type, member)
271 * list_for_each - iterate over a list
272 * @pos: the &struct list_head to use as a loop cursor.
273 * @head: the head for your list.
275 #define list_for_each(pos, head) \
276 for (pos = (head)->next; pos != (head); \
280 * __list_for_each - iterate over a list
281 * @pos: the &struct list_head to use as a loop cursor.
282 * @head: the head for your list.
284 * This variant differs from list_for_each() in that it's the
285 * simplest possible list iteration code, no prefetching is done.
286 * Use this for code that knows the list to be very short (empty
287 * or 1 entry) most of the time.
289 #define __list_for_each(pos, head) \
290 for (pos = (head)->next; pos != (head); pos = pos->next)
293 * list_for_each_prev - iterate over a list backwards
294 * @pos: the &struct list_head to use as a loop cursor.
295 * @head: the head for your list.
297 #define list_for_each_prev(pos, head) \
298 for (pos = (head)->prev; pos != (head); \
302 * list_for_each_safe - iterate over a list safe against removal of list entry
303 * @pos: the &struct list_head to use as a loop cursor.
304 * @n: another &struct list_head to use as temporary storage
305 * @head: the head for your list.
307 #define list_for_each_safe(pos, n, head) \
308 for (pos = (head)->next, n = pos->next; pos != (head); \
309 pos = n, n = pos->next)
312 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
313 * @pos: the &struct list_head to use as a loop cursor.
314 * @n: another &struct list_head to use as temporary storage
315 * @head: the head for your list.
317 #define list_for_each_prev_safe(pos, n, head) \
318 for (pos = (head)->prev, n = pos->prev; \
320 pos = n, n = pos->prev)
323 * list_for_each_entry - iterate over list of given type
324 * @pos: the type * to use as a loop cursor.
325 * @head: the head for your list.
326 * @member: the name of the list_struct within the struct.
328 #define list_for_each_entry(pos, head, member) \
329 for (pos = list_entry((head)->next, typeof(*pos), member); \
330 &pos->member != (head); \
331 pos = list_entry(pos->member.next, typeof(*pos), member))
334 * list_for_each_entry_reverse - iterate backwards over list of given type.
335 * @pos: the type * to use as a loop cursor.
336 * @head: the head for your list.
337 * @member: the name of the list_struct within the struct.
339 #define list_for_each_entry_reverse(pos, head, member) \
340 for (pos = list_entry((head)->prev, typeof(*pos), member); \
341 &pos->member != (head); \
342 pos = list_entry(pos->member.prev, typeof(*pos), member))
345 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
346 * @pos: the type * to use as a start point
347 * @head: the head of the list
348 * @member: the name of the list_struct within the struct.
350 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
352 #define list_prepare_entry(pos, head, member) \
353 ((pos) ? : list_entry(head, typeof(*pos), member))
356 * list_for_each_entry_continue - continue iteration over list of given type
357 * @pos: the type * to use as a loop cursor.
358 * @head: the head for your list.
359 * @member: the name of the list_struct within the struct.
361 * Continue to iterate over list of given type, continuing after
362 * the current position.
364 #define list_for_each_entry_continue(pos, head, member) \
365 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
366 &pos->member != (head); \
367 pos = list_entry(pos->member.next, typeof(*pos), member))
370 * list_for_each_entry_continue_reverse - iterate backwards from the given point
371 * @pos: the type * to use as a loop cursor.
372 * @head: the head for your list.
373 * @member: the name of the list_struct within the struct.
375 * Start to iterate over list of given type backwards, continuing after
376 * the current position.
378 #define list_for_each_entry_continue_reverse(pos, head, member) \
379 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
380 &pos->member != (head); \
381 pos = list_entry(pos->member.prev, typeof(*pos), member))
384 * list_for_each_entry_from - iterate over list of given type from the current point
385 * @pos: the type * to use as a loop cursor.
386 * @head: the head for your list.
387 * @member: the name of the list_struct within the struct.
389 * Iterate over list of given type, continuing from current position.
391 #define list_for_each_entry_from(pos, head, member) \
392 for (; &pos->member != (head); \
393 pos = list_entry(pos->member.next, typeof(*pos), member))
396 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
397 * @pos: the type * to use as a loop cursor.
398 * @n: another type * to use as temporary storage
399 * @head: the head for your list.
400 * @member: the name of the list_struct within the struct.
402 #define list_for_each_entry_safe(pos, n, head, member) \
403 for (pos = list_entry((head)->next, typeof(*pos), member), \
404 n = list_entry(pos->member.next, typeof(*pos), member); \
405 &pos->member != (head); \
406 pos = n, n = list_entry(n->member.next, typeof(*n), member))
409 * list_for_each_entry_safe_continue
410 * @pos: the type * to use as a loop cursor.
411 * @n: another type * to use as temporary storage
412 * @head: the head for your list.
413 * @member: the name of the list_struct within the struct.
415 * Iterate over list of given type, continuing after current point,
416 * safe against removal of list entry.
418 #define list_for_each_entry_safe_continue(pos, n, head, member) \
419 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
420 n = list_entry(pos->member.next, typeof(*pos), member); \
421 &pos->member != (head); \
422 pos = n, n = list_entry(n->member.next, typeof(*n), member))
425 * list_for_each_entry_safe_from
426 * @pos: the type * to use as a loop cursor.
427 * @n: another type * to use as temporary storage
428 * @head: the head for your list.
429 * @member: the name of the list_struct within the struct.
431 * Iterate over list of given type from current point, safe against
432 * removal of list entry.
434 #define list_for_each_entry_safe_from(pos, n, head, member) \
435 for (n = list_entry(pos->member.next, typeof(*pos), member); \
436 &pos->member != (head); \
437 pos = n, n = list_entry(n->member.next, typeof(*n), member))
440 * list_for_each_entry_safe_reverse
441 * @pos: the type * to use as a loop cursor.
442 * @n: another type * to use as temporary storage
443 * @head: the head for your list.
444 * @member: the name of the list_struct within the struct.
446 * Iterate backwards over list of given type, safe against removal
449 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
450 for (pos = list_entry((head)->prev, typeof(*pos), member), \
451 n = list_entry(pos->member.prev, typeof(*pos), member); \
452 &pos->member != (head); \
453 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
456 * Double linked lists with a single pointer list head.
457 * Mostly useful for hash tables where the two pointer list head is
459 * You lose the ability to access the tail in O(1).
463 struct hlist_node
*first
;
467 struct hlist_node
*next
, **pprev
;
470 #define HLIST_HEAD_INIT { .first = NULL }
471 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
472 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
473 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
479 static inline int hlist_unhashed(const struct hlist_node
*h
)
484 static inline int hlist_empty(const struct hlist_head
*h
)
489 static inline void __hlist_del(struct hlist_node
*n
)
491 struct hlist_node
*next
= n
->next
;
492 struct hlist_node
**pprev
= n
->pprev
;
498 static inline void hlist_del(struct hlist_node
*n
)
505 static inline void hlist_del_init(struct hlist_node
*n
)
507 if (!hlist_unhashed(n
)) {
514 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
516 struct hlist_node
*first
= h
->first
;
519 first
->pprev
= &n
->next
;
521 n
->pprev
= &h
->first
;
525 /* next must be != NULL */
526 static inline void hlist_add_before(struct hlist_node
*n
,
527 struct hlist_node
*next
)
529 n
->pprev
= next
->pprev
;
531 next
->pprev
= &n
->next
;
535 static inline void hlist_add_after(struct hlist_node
*n
,
536 struct hlist_node
*next
)
538 next
->next
= n
->next
;
540 next
->pprev
= &n
->next
;
543 next
->next
->pprev
= &next
->next
;
546 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
548 #define hlist_for_each(pos, head) \
549 for (pos = (head)->first; pos; pos = pos->next)
551 #define hlist_for_each_safe(pos, n, head) \
552 for (pos = (head)->first; pos; pos = n)
555 * hlist_for_each_entry - iterate over list of given type
556 * @tpos: the type * to use as a loop cursor.
557 * @pos: the &struct hlist_node to use as a loop cursor.
558 * @head: the head for your list.
559 * @member: the name of the hlist_node within the struct.
561 #define hlist_for_each_entry(tpos, pos, head, member) \
562 for (pos = (head)->first; pos && \
563 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
567 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
568 * @tpos: the type * to use as a loop cursor.
569 * @pos: the &struct hlist_node to use as a loop cursor.
570 * @member: the name of the hlist_node within the struct.
572 #define hlist_for_each_entry_continue(tpos, pos, member) \
573 for (pos = (pos)->next; pos && \
574 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
578 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
579 * @tpos: the type * to use as a loop cursor.
580 * @pos: the &struct hlist_node to use as a loop cursor.
581 * @member: the name of the hlist_node within the struct.
583 #define hlist_for_each_entry_from(tpos, pos, member) \
585 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
589 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
590 * @tpos: the type * to use as a loop cursor.
591 * @pos: the &struct hlist_node to use as a loop cursor.
592 * @n: another &struct hlist_node to use as temporary storage
593 * @head: the head for your list.
594 * @member: the name of the hlist_node within the struct.
596 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
597 for (pos = (head)->first; \
598 pos && ({ n = pos->next; 1; }) && \
599 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
This page took 0.075732 seconds and 5 git commands to generate.