PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/uts/common/os/list.c

http://github.com/dustin/mac-zfs
C | 194 lines | 132 code | 31 blank | 31 comment | 14 complexity | c09e5c49a24a9bec430f60a41474df2f MD5 | raw file
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. #pragma ident "%Z%%M% %I% %E% SMI"
  26. /*
  27. * Generic doubly-linked list implementation
  28. */
  29. #include <sys/list.h>
  30. #include <sys/list_impl.h>
  31. #include <sys/types.h>
  32. #include <sys/sysmacros.h>
  33. #include <sys/debug.h>
  34. #define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
  35. #define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
  36. #define list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
  37. #define list_insert_after_node(list, node, object) { \
  38. list_node_t *lnew = list_d2l(list, object); \
  39. lnew->list_prev = node; \
  40. lnew->list_next = node->list_next; \
  41. node->list_next->list_prev = lnew; \
  42. node->list_next = lnew; \
  43. }
  44. #define list_insert_before_node(list, node, object) { \
  45. list_node_t *lnew = list_d2l(list, object); \
  46. lnew->list_next = node; \
  47. lnew->list_prev = node->list_prev; \
  48. node->list_prev->list_next = lnew; \
  49. node->list_prev = lnew; \
  50. }
  51. void
  52. list_create(list_t *list, size_t size, size_t offset)
  53. {
  54. ASSERT(list);
  55. ASSERT(size > 0);
  56. ASSERT(size >= offset + sizeof (list_node_t));
  57. list->list_size = size;
  58. list->list_offset = offset;
  59. list->list_head.list_next = list->list_head.list_prev =
  60. &list->list_head;
  61. }
  62. void
  63. list_destroy(list_t *list)
  64. {
  65. list_node_t *node = &list->list_head;
  66. ASSERT(list);
  67. ASSERT(list->list_head.list_next == node);
  68. ASSERT(list->list_head.list_prev == node);
  69. node->list_next = node->list_prev = NULL;
  70. }
  71. void
  72. list_insert_after(list_t *list, void *object, void *nobject)
  73. {
  74. list_node_t *lold = list_d2l(list, object);
  75. list_insert_after_node(list, lold, nobject);
  76. }
  77. void
  78. list_insert_before(list_t *list, void *object, void *nobject)
  79. {
  80. list_node_t *lold = list_d2l(list, object);
  81. list_insert_before_node(list, lold, nobject)
  82. }
  83. void
  84. list_insert_head(list_t *list, void *object)
  85. {
  86. list_node_t *lold = &list->list_head;
  87. list_insert_after_node(list, lold, object);
  88. }
  89. void
  90. list_insert_tail(list_t *list, void *object)
  91. {
  92. list_node_t *lold = &list->list_head;
  93. list_insert_before_node(list, lold, object);
  94. }
  95. void
  96. list_remove(list_t *list, void *object)
  97. {
  98. list_node_t *lold = list_d2l(list, object);
  99. ASSERT(!list_empty(list));
  100. ASSERT(lold->list_next != NULL);
  101. lold->list_prev->list_next = lold->list_next;
  102. lold->list_next->list_prev = lold->list_prev;
  103. lold->list_next = lold->list_prev = NULL;
  104. }
  105. void *
  106. list_head(list_t *list)
  107. {
  108. if (list_empty(list))
  109. return (NULL);
  110. return (list_object(list, list->list_head.list_next));
  111. }
  112. void *
  113. list_tail(list_t *list)
  114. {
  115. if (list_empty(list))
  116. return (NULL);
  117. return (list_object(list, list->list_head.list_prev));
  118. }
  119. void *
  120. list_next(list_t *list, void *object)
  121. {
  122. list_node_t *node = list_d2l(list, object);
  123. if (node->list_next != &list->list_head)
  124. return (list_object(list, node->list_next));
  125. return (NULL);
  126. }
  127. void *
  128. list_prev(list_t *list, void *object)
  129. {
  130. list_node_t *node = list_d2l(list, object);
  131. if (node->list_prev != &list->list_head)
  132. return (list_object(list, node->list_prev));
  133. return (NULL);
  134. }
  135. /*
  136. * Insert src list after dst list. Empty src list thereafter.
  137. */
  138. void
  139. list_move_tail(list_t *dst, list_t *src)
  140. {
  141. list_node_t *dstnode = &dst->list_head;
  142. list_node_t *srcnode = &src->list_head;
  143. ASSERT(dst->list_size == src->list_size);
  144. ASSERT(dst->list_offset == src->list_offset);
  145. if (list_empty(src))
  146. return;
  147. dstnode->list_prev->list_next = srcnode->list_next;
  148. srcnode->list_next->list_prev = dstnode->list_prev;
  149. dstnode->list_prev = srcnode->list_prev;
  150. srcnode->list_prev->list_next = dstnode;
  151. /* empty src list */
  152. srcnode->list_next = srcnode->list_prev = srcnode;
  153. }
  154. int
  155. list_link_active(list_node_t *link)
  156. {
  157. return (link->list_next != NULL);
  158. }
  159. int
  160. list_is_empty(list_t *list)
  161. {
  162. return (list_empty(list));
  163. }