list.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C 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; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _LIST_H
  17. #define _LIST_H 1
  18. /* The definitions of this file are adopted from those which can be
  19. found in the Linux kernel headers to enable people familiar with
  20. the latter find their way in these sources as well. */
  21. /* Basic type for the double-link list. */
  22. typedef struct list_head
  23. {
  24. struct list_head *next;
  25. struct list_head *prev;
  26. } list_t;
  27. /* Define a variable with the head and tail of the list. */
  28. #define LIST_HEAD(name) \
  29. list_t name = { &(name), &(name) }
  30. /* Initialize a new list head. */
  31. #define INIT_LIST_HEAD(ptr) \
  32. (ptr)->next = (ptr)->prev = (ptr)
  33. /* Add new element at the head of the list. */
  34. static inline void
  35. list_add (list_t *newp, list_t *head)
  36. {
  37. head->next->prev = newp;
  38. newp->next = head->next;
  39. newp->prev = head;
  40. head->next = newp;
  41. }
  42. /* Add new element at the tail of the list. */
  43. static inline void
  44. list_add_tail (list_t *newp, list_t *head)
  45. {
  46. head->prev->next = newp;
  47. newp->next = head;
  48. newp->prev = head->prev;
  49. head->prev = newp;
  50. }
  51. /* Remove element from list. */
  52. static inline void
  53. list_del (list_t *elem)
  54. {
  55. elem->next->prev = elem->prev;
  56. elem->prev->next = elem->next;
  57. }
  58. /* Join two lists. */
  59. static inline void
  60. list_splice (list_t *add, list_t *head)
  61. {
  62. /* Do nothing if the list which gets added is empty. */
  63. if (add != add->next)
  64. {
  65. add->next->prev = head;
  66. add->prev->next = head->next;
  67. head->next->prev = add->prev;
  68. head->next = add->next;
  69. }
  70. }
  71. /* Get typed element from list at a given position. */
  72. #define list_entry(ptr, type, member) \
  73. ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
  74. /* Iterate forward over the elements of the list. */
  75. #define list_for_each(pos, head) \
  76. for (pos = (head)->next; pos != (head); pos = pos->next)
  77. /* Iterate forward over the elements of the list. */
  78. #define list_for_each_prev(pos, head) \
  79. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  80. /* Iterate backwards over the elements list. The list elements can be
  81. removed from the list while doing this. */
  82. #define list_for_each_prev_safe(pos, p, head) \
  83. for (pos = (head)->prev, p = pos->prev; \
  84. pos != (head); \
  85. pos = p, p = pos->prev)
  86. #endif /* list.h */