list.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* vi: set sw=4 ts=4: */
  2. #if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
  3. #define BLKID_LIST_H 1
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /*
  8. * Simple doubly linked list implementation.
  9. *
  10. * Some of the internal functions ("__xxx") are useful when
  11. * manipulating whole lists rather than single entries, as
  12. * sometimes we already know the next/prev entries and we can
  13. * generate better code by using them directly rather than
  14. * using the generic single-entry routines.
  15. */
  16. struct list_head {
  17. struct list_head *next, *prev;
  18. };
  19. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  20. #define LIST_HEAD(name) \
  21. struct list_head name = LIST_HEAD_INIT(name)
  22. #define INIT_LIST_HEAD(ptr) do { \
  23. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  24. } while (0)
  25. void __list_add(struct list_head * add, struct list_head * prev, struct list_head * next);
  26. void list_add(struct list_head *add, struct list_head *head);
  27. void list_add_tail(struct list_head *add, struct list_head *head);
  28. void __list_del(struct list_head * prev, struct list_head * next);
  29. void list_del(struct list_head *entry);
  30. void list_del_init(struct list_head *entry);
  31. int list_empty(struct list_head *head);
  32. void list_splice(struct list_head *list, struct list_head *head);
  33. /**
  34. * list_entry - get the struct for this entry
  35. * @ptr: the &struct list_head pointer.
  36. * @type: the type of the struct this is embedded in.
  37. * @member: the name of the list_struct within the struct.
  38. */
  39. #define list_entry(ptr, type, member) \
  40. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  41. /**
  42. * list_for_each - iterate over elements in a list
  43. * @pos: the &struct list_head to use as a loop counter.
  44. * @head: the head for your list.
  45. */
  46. #define list_for_each(pos, head) \
  47. for (pos = (head)->next; pos != (head); pos = pos->next)
  48. /**
  49. * list_for_each_safe - iterate over elements in a list, but don't dereference
  50. * pos after the body is done (in case it is freed)
  51. * @pos: the &struct list_head to use as a loop counter.
  52. * @pnext: the &struct list_head to use as a pointer to the next item.
  53. * @head: the head for your list (not included in iteration).
  54. */
  55. #define list_for_each_safe(pos, pnext, head) \
  56. for (pos = (head)->next, pnext = pos->next; pos != (head); \
  57. pos = pnext, pnext = pos->next)
  58. #ifdef __cplusplus
  59. }
  60. #endif
  61. #endif