list.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _LIST_H
  16. #define _LIST_H 1
  17. /* The definitions of this file are adopted from those which can be
  18. found in the Linux kernel headers to enable people familiar with
  19. the latter find their way in these sources as well. */
  20. /* Basic type for the double-link list. */
  21. typedef struct list_head
  22. {
  23. struct list_head *next;
  24. struct list_head *prev;
  25. } list_t;
  26. /* Define a variable with the head and tail of the list. */
  27. #define LIST_HEAD(name) \
  28. list_t name = { &(name), &(name) }
  29. /* Initialize a new list head. */
  30. #define INIT_LIST_HEAD(ptr) \
  31. (ptr)->next = (ptr)->prev = (ptr)
  32. /* Add new element at the head of the list. */
  33. static __inline__ void
  34. list_add (list_t *newp, list_t *head)
  35. {
  36. head->next->prev = newp;
  37. newp->next = head->next;
  38. newp->prev = head;
  39. head->next = newp;
  40. }
  41. /* Add new element at the tail of the list. */
  42. static __inline__ void
  43. list_add_tail (list_t *newp, list_t *head)
  44. {
  45. head->prev->next = newp;
  46. newp->next = head;
  47. newp->prev = head->prev;
  48. head->prev = newp;
  49. }
  50. /* Remove element from list. */
  51. static __inline__ void
  52. list_del (list_t *elem)
  53. {
  54. elem->next->prev = elem->prev;
  55. elem->prev->next = elem->next;
  56. }
  57. /* Join two lists. */
  58. static __inline__ void
  59. list_splice (list_t *add, list_t *head)
  60. {
  61. /* Do nothing if the list which gets added is empty. */
  62. if (add != add->next)
  63. {
  64. add->next->prev = head;
  65. add->prev->next = head->next;
  66. head->next->prev = add->prev;
  67. head->next = add->next;
  68. }
  69. }
  70. /* Get typed element from list at a given position. */
  71. #define list_entry(ptr, type, member) \
  72. ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
  73. /* Iterate forward over the elements of the list. */
  74. #define list_for_each(pos, head) \
  75. for (pos = (head)->next; pos != (head); pos = pos->next)
  76. /* Iterate forward over the elements of the list. */
  77. #define list_for_each_prev(pos, head) \
  78. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  79. /* Iterate backwards over the elements list. The list elements can be
  80. removed from the list while doing this. */
  81. #define list_for_each_prev_safe(pos, p, head) \
  82. for (pos = (head)->prev, p = pos->prev; \
  83. pos != (head); \
  84. pos = p, p = pos->prev)
  85. #endif /* list.h */