list.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2002, 2009 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. newp->next = head->next;
  37. newp->prev = head;
  38. head->next->prev = newp;
  39. head->next = newp;
  40. }
  41. /* Remove element from list. */
  42. static inline void
  43. list_del (list_t *elem)
  44. {
  45. elem->next->prev = elem->prev;
  46. elem->prev->next = elem->next;
  47. }
  48. /* Join two lists. */
  49. static inline void
  50. list_splice (list_t *add, list_t *head)
  51. {
  52. /* Do nothing if the list which gets added is empty. */
  53. if (add != add->next)
  54. {
  55. add->next->prev = head;
  56. add->prev->next = head->next;
  57. head->next->prev = add->prev;
  58. head->next = add->next;
  59. }
  60. }
  61. /* Get typed element from list at a given position. */
  62. #define list_entry(ptr, type, member) \
  63. ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
  64. /* Iterate forward over the elements of the list. */
  65. #define list_for_each(pos, head) \
  66. for (pos = (head)->next; pos != (head); pos = pos->next)
  67. /* Iterate forward over the elements of the list. */
  68. #define list_for_each_prev(pos, head) \
  69. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  70. /* Iterate backwards over the elements list. The list elements can be
  71. removed from the list while doing this. */
  72. #define list_for_each_prev_safe(pos, p, head) \
  73. for (pos = (head)->prev, p = pos->prev; \
  74. pos != (head); \
  75. pos = p, p = pos->prev)
  76. #endif /* list.h */