Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.h 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright © 2008, 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file list.h
  25. * \brief Doubly-linked list abstract container type.
  26. *
  27. * Each doubly-linked list has a sentinal head and tail node. These nodes
  28. * contain no data. The head sentinal can be identified by its \c prev
  29. * pointer being \c NULL. The tail sentinal can be identified by its
  30. * \c next pointer being \c NULL.
  31. *
  32. * A list is empty if either the head sentinal's \c next pointer points to the
  33. * tail sentinal or the tail sentinal's \c prev poiner points to the head
  34. * sentinal.
  35. *
  36. * Instead of tracking two separate \c node structures and a \c list structure
  37. * that points to them, the sentinal nodes are in a single structure. Noting
  38. * that each sentinal node always has one \c NULL pointer, the \c NULL
  39. * pointers occupy the same memory location. In the \c list structure
  40. * contains a the following:
  41. *
  42. * - A \c head pointer that represents the \c next pointer of the
  43. * head sentinal node.
  44. * - A \c tail pointer that represents the \c prev pointer of the head
  45. * sentinal node and the \c next pointer of the tail sentinal node. This
  46. * pointer is \b always \c NULL.
  47. * - A \c tail_prev pointer that represents the \c prev pointer of the
  48. * tail sentinal node.
  49. *
  50. * Therefore, if \c head->next is \c NULL or \c tail_prev->prev is \c NULL,
  51. * the list is empty.
  52. *
  53. * To anyone familiar with "exec lists" on the Amiga, this structure should
  54. * be immediately recognizable. See the following link for the original Amiga
  55. * operating system documentation on the subject.
  56. *
  57. * http://www.natami.net/dev/Libraries_Manual_guide/node02D7.html
  58. *
  59. * \author Ian Romanick <ian.d.romanick@intel.com>
  60. */
  61. #pragma once
  62. #ifndef LIST_CONTAINER_H
  63. #define LIST_CONTAINER_H
  64. #ifndef __cplusplus
  65. #include <stddef.h>
  66. #endif
  67. #include <assert.h>
  68. struct exec_node {
  69. struct exec_node *next;
  70. struct exec_node *prev;
  71. #ifdef __cplusplus
  72. exec_node() : next(NULL), prev(NULL)
  73. {
  74. /* empty */
  75. }
  76. const exec_node *get_next() const
  77. {
  78. return next;
  79. }
  80. exec_node *get_next()
  81. {
  82. return next;
  83. }
  84. const exec_node *get_prev() const
  85. {
  86. return prev;
  87. }
  88. exec_node *get_prev()
  89. {
  90. return prev;
  91. }
  92. void remove()
  93. {
  94. next->prev = prev;
  95. prev->next = next;
  96. next = NULL;
  97. prev = NULL;
  98. }
  99. /**
  100. * Link a node with itself
  101. *
  102. * This creates a sort of degenerate list that is occasionally useful.
  103. */
  104. void self_link()
  105. {
  106. next = this;
  107. prev = this;
  108. }
  109. /**
  110. * Insert a node in the list after the current node
  111. */
  112. void insert_after(exec_node *after)
  113. {
  114. after->next = this->next;
  115. after->prev = this;
  116. this->next->prev = after;
  117. this->next = after;
  118. }
  119. /**
  120. * Insert a node in the list before the current node
  121. */
  122. void insert_before(exec_node *before)
  123. {
  124. before->next = this;
  125. before->prev = this->prev;
  126. this->prev->next = before;
  127. this->prev = before;
  128. }
  129. /**
  130. * Is this the sentinal at the tail of the list?
  131. */
  132. bool is_tail_sentinal() const
  133. {
  134. return this->next == NULL;
  135. }
  136. /**
  137. * Is this the sentinal at the head of the list?
  138. */
  139. bool is_head_sentinal() const
  140. {
  141. return this->prev == NULL;
  142. }
  143. #endif
  144. };
  145. #ifdef __cplusplus
  146. /* This macro will not work correctly if `t' uses virtual inheritance. If you
  147. * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
  148. */
  149. #define exec_list_offsetof(t, f, p) \
  150. (((char *) &((t *) p)->f) - ((char *) p))
  151. #else
  152. #define exec_list_offsetof(t, f, p) offsetof(t, f)
  153. #endif
  154. /**
  155. * Get a pointer to the structure containing an exec_node
  156. *
  157. * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
  158. * the containing structure.
  159. *
  160. * \param type Base type of the structure containing the node
  161. * \param node Pointer to the \c exec_node
  162. * \param field Name of the field in \c type that is the embedded \c exec_node
  163. */
  164. #define exec_node_data(type, node, field) \
  165. ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
  166. #ifdef __cplusplus
  167. struct exec_node;
  168. class iterator {
  169. public:
  170. void next()
  171. {
  172. }
  173. void *get()
  174. {
  175. return NULL;
  176. }
  177. bool has_next() const
  178. {
  179. return false;
  180. }
  181. };
  182. class exec_list_iterator : public iterator {
  183. public:
  184. exec_list_iterator(exec_node *n) : node(n), _next(n->next)
  185. {
  186. /* empty */
  187. }
  188. void next()
  189. {
  190. node = _next;
  191. _next = node->next;
  192. }
  193. void remove()
  194. {
  195. node->remove();
  196. }
  197. exec_node *get()
  198. {
  199. return node;
  200. }
  201. bool has_next() const
  202. {
  203. return _next != NULL;
  204. }
  205. private:
  206. exec_node *node;
  207. exec_node *_next;
  208. };
  209. #define foreach_iter(iter_type, iter, container) \
  210. for (iter_type iter = (container) . iterator(); iter.has_next(); iter.next())
  211. #endif
  212. struct exec_list {
  213. struct exec_node *head;
  214. struct exec_node *tail;
  215. struct exec_node *tail_pred;
  216. #ifdef __cplusplus
  217. exec_list()
  218. {
  219. make_empty();
  220. }
  221. void make_empty()
  222. {
  223. head = (exec_node *) & tail;
  224. tail = NULL;
  225. tail_pred = (exec_node *) & head;
  226. }
  227. bool is_empty() const
  228. {
  229. /* There are three ways to test whether a list is empty or not.
  230. *
  231. * - Check to see if the \c head points to the \c tail.
  232. * - Check to see if the \c tail_pred points to the \c head.
  233. * - Check to see if the \c head is the sentinal node by test whether its
  234. * \c next pointer is \c NULL.
  235. *
  236. * The first two methods tend to generate better code on modern systems
  237. * because they save a pointer dereference.
  238. */
  239. return head == (exec_node *) &tail;
  240. }
  241. const exec_node *get_head() const
  242. {
  243. return !is_empty() ? head : NULL;
  244. }
  245. exec_node *get_head()
  246. {
  247. return !is_empty() ? head : NULL;
  248. }
  249. const exec_node *get_tail() const
  250. {
  251. return !is_empty() ? tail_pred : NULL;
  252. }
  253. exec_node *get_tail()
  254. {
  255. return !is_empty() ? tail_pred : NULL;
  256. }
  257. void push_head(exec_node *n)
  258. {
  259. n->next = head;
  260. n->prev = (exec_node *) &head;
  261. n->next->prev = n;
  262. head = n;
  263. }
  264. void push_tail(exec_node *n)
  265. {
  266. n->next = (exec_node *) &tail;
  267. n->prev = tail_pred;
  268. n->prev->next = n;
  269. tail_pred = n;
  270. }
  271. void push_degenerate_list_at_head(exec_node *n)
  272. {
  273. assert(n->prev->next == n);
  274. n->prev->next = head;
  275. head->prev = n->prev;
  276. n->prev = (exec_node *) &head;
  277. head = n;
  278. }
  279. /**
  280. * Move all of the nodes from this list to the target list
  281. */
  282. void move_nodes_to(exec_list *target)
  283. {
  284. if (is_empty()) {
  285. target->make_empty();
  286. } else {
  287. target->head = head;
  288. target->tail = NULL;
  289. target->tail_pred = tail_pred;
  290. target->head->prev = (exec_node *) &target->head;
  291. target->tail_pred->next = (exec_node *) &target->tail;
  292. make_empty();
  293. }
  294. }
  295. exec_list_iterator iterator()
  296. {
  297. return exec_list_iterator(head);
  298. }
  299. exec_list_iterator iterator() const
  300. {
  301. return exec_list_iterator((exec_node *) head);
  302. }
  303. #endif
  304. };
  305. #define foreach_list(__node, __list) \
  306. for (exec_node * __node = (__list)->head \
  307. ; (__node)->next != NULL \
  308. ; (__node) = (__node)->next)
  309. #define foreach_list_const(__node, __list) \
  310. for (const exec_node * __node = (__list)->head \
  311. ; (__node)->next != NULL \
  312. ; (__node) = (__node)->next)
  313. #define foreach_list_typed(__type, __node, __field, __list) \
  314. for (__type * __node = \
  315. exec_node_data(__type, (__list)->head, __field); \
  316. (__node)->__field.next != NULL; \
  317. (__node) = exec_node_data(__type, (__node)->__field.next, __field))
  318. #define foreach_list_typed_const(__type, __node, __field, __list) \
  319. for (const __type * __node = \
  320. exec_node_data(__type, (__list)->head, __field); \
  321. (__node)->__field.next != NULL; \
  322. (__node) = exec_node_data(__type, (__node)->__field.next, __field))
  323. #endif /* LIST_CONTAINER_H */