list.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) HighPoint Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD: src/sys/dev/hptrr/list.h,v 1.2.2.1.4.1 2010/06/14 02:09:06 kensmith Exp $
  27. */
  28. /*
  29. * $Id: list.h,v 1.6 2006/10/31 06:25:28 gmm Exp $
  30. * Copyright (C) 2004-2005 HighPoint Technologies, Inc. All rights reserved.
  31. */
  32. #ifndef _HPT_LIST_H_
  33. #define _HPT_LIST_H_
  34. #ifndef _LINUX_LIST_H
  35. #ifndef _LINUX_TYPES_H
  36. #ifndef HPT_INLINE
  37. #define HPT_INLINE __inline
  38. #endif
  39. typedef unsigned long HPT_UPTR;
  40. struct list_head {
  41. struct list_head *next, *prev;
  42. };
  43. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  44. #define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)
  45. static HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
  46. {
  47. next->prev = _new;
  48. _new->next = next;
  49. _new->prev = prev;
  50. prev->next = _new;
  51. }
  52. static HPT_INLINE void list_add(struct list_head *_new, struct list_head *head)
  53. {
  54. __list_add(_new, head, head->next);
  55. }
  56. static HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head)
  57. {
  58. __list_add(_new, head->prev, head);
  59. }
  60. static HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next)
  61. {
  62. next->prev = prev;
  63. prev->next = next;
  64. }
  65. static HPT_INLINE void list_del(struct list_head *entry)
  66. {
  67. __list_del(entry->prev, entry->next);
  68. }
  69. static HPT_INLINE void list_del_init(struct list_head *entry)
  70. {
  71. __list_del(entry->prev, entry->next);
  72. INIT_LIST_HEAD(entry);
  73. }
  74. static inline void list_move(struct list_head *list, struct list_head *head)
  75. {
  76. __list_del(list->prev, list->next);
  77. list_add(list, head);
  78. }
  79. static inline void list_move_tail(struct list_head *list,
  80. struct list_head *head)
  81. {
  82. __list_del(list->prev, list->next);
  83. list_add_tail(list, head);
  84. }
  85. static HPT_INLINE int list_empty(struct list_head *head)
  86. {
  87. return head->next == head;
  88. }
  89. static HPT_INLINE void __list_splice(struct list_head *list,
  90. struct list_head *head)
  91. {
  92. struct list_head *first = list->next;
  93. struct list_head *last = list->prev;
  94. struct list_head *at = head->next;
  95. first->prev = head;
  96. head->next = first;
  97. last->next = at;
  98. at->prev = last;
  99. }
  100. static HPT_INLINE void list_splice(struct list_head *list, struct list_head *head)
  101. {
  102. if (!list_empty(list))
  103. __list_splice(list, head);
  104. }
  105. static HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head)
  106. {
  107. if (!list_empty(list)) {
  108. __list_splice(list, head);
  109. INIT_LIST_HEAD(list);
  110. }
  111. }
  112. /*#define list_entry(ptr, type, member) \
  113. ((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member))) */
  114. #define list_entry(ptr, type, member) \
  115. ((type *)((unsigned long)(ptr)-((unsigned long)(&((type *)1)->member) - 1)))
  116. #define list_for_each(pos, head) \
  117. for (pos = (head)->next; pos != (head); pos = pos->next)
  118. #define list_for_each_safe(pos, n, head) \
  119. for (pos = (head)->next, n = pos->next; pos != (head); \
  120. pos = n, n = pos->next)
  121. #define get_first_item(attached, type, member) \
  122. ((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member)))
  123. #endif
  124. #endif
  125. #endif