PageRenderTime 140ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/cddl/contrib/opensolaris/uts/common/os/list.c

https://bitbucket.org/freebsd/freebsd-head/
C | 245 lines | 177 code | 37 blank | 31 comment | 24 complexity | 58e48d94f253a711cdbc99d67c100740 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  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 2008 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. #define list_remove_node(node) \
  52. (node)->list_prev->list_next = (node)->list_next; \
  53. (node)->list_next->list_prev = (node)->list_prev; \
  54. (node)->list_next = (node)->list_prev = NULL
  55. void
  56. list_create(list_t *list, size_t size, size_t offset)
  57. {
  58. ASSERT(list);
  59. ASSERT(size > 0);
  60. ASSERT(size >= offset + sizeof (list_node_t));
  61. list->list_size = size;
  62. list->list_offset = offset;
  63. list->list_head.list_next = list->list_head.list_prev =
  64. &list->list_head;
  65. }
  66. void
  67. list_destroy(list_t *list)
  68. {
  69. list_node_t *node = &list->list_head;
  70. ASSERT(list);
  71. ASSERT(list->list_head.list_next == node);
  72. ASSERT(list->list_head.list_prev == node);
  73. node->list_next = node->list_prev = NULL;
  74. }
  75. void
  76. list_insert_after(list_t *list, void *object, void *nobject)
  77. {
  78. if (object == NULL) {
  79. list_insert_head(list, nobject);
  80. } else {
  81. list_node_t *lold = list_d2l(list, object);
  82. list_insert_after_node(list, lold, nobject);
  83. }
  84. }
  85. void
  86. list_insert_before(list_t *list, void *object, void *nobject)
  87. {
  88. if (object == NULL) {
  89. list_insert_tail(list, nobject);
  90. } else {
  91. list_node_t *lold = list_d2l(list, object);
  92. list_insert_before_node(list, lold, nobject);
  93. }
  94. }
  95. void
  96. list_insert_head(list_t *list, void *object)
  97. {
  98. list_node_t *lold = &list->list_head;
  99. list_insert_after_node(list, lold, object);
  100. }
  101. void
  102. list_insert_tail(list_t *list, void *object)
  103. {
  104. list_node_t *lold = &list->list_head;
  105. list_insert_before_node(list, lold, object);
  106. }
  107. void
  108. list_remove(list_t *list, void *object)
  109. {
  110. list_node_t *lold = list_d2l(list, object);
  111. ASSERT(!list_empty(list));
  112. ASSERT(lold->list_next != NULL);
  113. list_remove_node(lold);
  114. }
  115. void *
  116. list_remove_head(list_t *list)
  117. {
  118. list_node_t *head = list->list_head.list_next;
  119. if (head == &list->list_head)
  120. return (NULL);
  121. list_remove_node(head);
  122. return (list_object(list, head));
  123. }
  124. void *
  125. list_remove_tail(list_t *list)
  126. {
  127. list_node_t *tail = list->list_head.list_prev;
  128. if (tail == &list->list_head)
  129. return (NULL);
  130. list_remove_node(tail);
  131. return (list_object(list, tail));
  132. }
  133. void *
  134. list_head(list_t *list)
  135. {
  136. if (list_empty(list))
  137. return (NULL);
  138. return (list_object(list, list->list_head.list_next));
  139. }
  140. void *
  141. list_tail(list_t *list)
  142. {
  143. if (list_empty(list))
  144. return (NULL);
  145. return (list_object(list, list->list_head.list_prev));
  146. }
  147. void *
  148. list_next(list_t *list, void *object)
  149. {
  150. list_node_t *node = list_d2l(list, object);
  151. if (node->list_next != &list->list_head)
  152. return (list_object(list, node->list_next));
  153. return (NULL);
  154. }
  155. void *
  156. list_prev(list_t *list, void *object)
  157. {
  158. list_node_t *node = list_d2l(list, object);
  159. if (node->list_prev != &list->list_head)
  160. return (list_object(list, node->list_prev));
  161. return (NULL);
  162. }
  163. /*
  164. * Insert src list after dst list. Empty src list thereafter.
  165. */
  166. void
  167. list_move_tail(list_t *dst, list_t *src)
  168. {
  169. list_node_t *dstnode = &dst->list_head;
  170. list_node_t *srcnode = &src->list_head;
  171. ASSERT(dst->list_size == src->list_size);
  172. ASSERT(dst->list_offset == src->list_offset);
  173. if (list_empty(src))
  174. return;
  175. dstnode->list_prev->list_next = srcnode->list_next;
  176. srcnode->list_next->list_prev = dstnode->list_prev;
  177. dstnode->list_prev = srcnode->list_prev;
  178. srcnode->list_prev->list_next = dstnode;
  179. /* empty src list */
  180. srcnode->list_next = srcnode->list_prev = srcnode;
  181. }
  182. void
  183. list_link_replace(list_node_t *lold, list_node_t *lnew)
  184. {
  185. ASSERT(list_link_active(lold));
  186. ASSERT(!list_link_active(lnew));
  187. lnew->list_next = lold->list_next;
  188. lnew->list_prev = lold->list_prev;
  189. lold->list_prev->list_next = lnew;
  190. lold->list_next->list_prev = lnew;
  191. lold->list_next = lold->list_prev = NULL;
  192. }
  193. void
  194. list_link_init(list_node_t *link)
  195. {
  196. link->list_next = NULL;
  197. link->list_prev = NULL;
  198. }
  199. int
  200. list_link_active(list_node_t *link)
  201. {
  202. return (link->list_next != NULL);
  203. }
  204. int
  205. list_is_empty(list_t *list)
  206. {
  207. return (list_empty(list));
  208. }