2 * Copyright © 2008, 2010 Intel Corporation
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:
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
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.
26 * \brief Doubly-linked list abstract container type.
28 * Each doubly-linked list has a sentinel head and tail node. These nodes
29 * contain no data. The head sentinel can be identified by its \c prev
30 * pointer being \c NULL. The tail sentinel can be identified by its
31 * \c next pointer being \c NULL.
33 * A list is empty if either the head sentinel's \c next pointer points to the
34 * tail sentinel or the tail sentinel's \c prev poiner points to the head
37 * Instead of tracking two separate \c node structures and a \c list structure
38 * that points to them, the sentinel nodes are in a single structure. Noting
39 * that each sentinel node always has one \c NULL pointer, the \c NULL
40 * pointers occupy the same memory location. In the \c list structure
41 * contains a the following:
43 * - A \c head pointer that represents the \c next pointer of the
45 * - A \c tail pointer that represents the \c prev pointer of the head
46 * sentinel node and the \c next pointer of the tail sentinel node. This
47 * pointer is \b always \c NULL.
48 * - A \c tail_prev pointer that represents the \c prev pointer of the
51 * Therefore, if \c head->next is \c NULL or \c tail_prev->prev is \c NULL,
54 * To anyone familiar with "exec lists" on the Amiga, this structure should
55 * be immediately recognizable. See the following link for the original Amiga
56 * operating system documentation on the subject.
58 * http://www.natami.net/dev/Libraries_Manual_guide/node02D7.html
60 * \author Ian Romanick <ian.d.romanick@intel.com>
64 #ifndef LIST_CONTAINER_H
65 #define LIST_CONTAINER_H
75 struct exec_node
*next
;
76 struct exec_node
*prev
;
79 DECLARE_RALLOC_CXX_OPERATORS(exec_node
)
81 exec_node() : next(NULL
), prev(NULL
)
86 const exec_node
*get_next() const
96 const exec_node
*get_prev() const
101 exec_node
*get_prev()
115 * Link a node with itself
117 * This creates a sort of degenerate list that is occasionally useful.
126 * Insert a node in the list after the current node
128 void insert_after(exec_node
*after
)
130 after
->next
= this->next
;
133 this->next
->prev
= after
;
137 * Insert a node in the list before the current node
139 void insert_before(exec_node
*before
)
142 before
->prev
= this->prev
;
144 this->prev
->next
= before
;
149 * Insert another list in the list before the current node
151 void insert_before(struct exec_list
*before
);
154 * Replace the current node with the given node.
156 void replace_with(exec_node
*replacement
)
158 replacement
->prev
= this->prev
;
159 replacement
->next
= this->next
;
161 this->prev
->next
= replacement
;
162 this->next
->prev
= replacement
;
166 * Is this the sentinel at the tail of the list?
168 bool is_tail_sentinel() const
170 return this->next
== NULL
;
174 * Is this the sentinel at the head of the list?
176 bool is_head_sentinel() const
178 return this->prev
== NULL
;
185 /* This macro will not work correctly if `t' uses virtual inheritance. If you
186 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
188 #define exec_list_offsetof(t, f, p) \
189 (((char *) &((t *) p)->f) - ((char *) p))
191 #define exec_list_offsetof(t, f, p) offsetof(t, f)
195 * Get a pointer to the structure containing an exec_node
197 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
198 * the containing structure.
200 * \param type Base type of the structure containing the node
201 * \param node Pointer to the \c exec_node
202 * \param field Name of the field in \c type that is the embedded \c exec_node
204 #define exec_node_data(type, node, field) \
205 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
212 struct exec_node
*head
;
213 struct exec_node
*tail
;
214 struct exec_node
*tail_pred
;
217 DECLARE_RALLOC_CXX_OPERATORS(exec_list
)
226 head
= (exec_node
*) & tail
;
228 tail_pred
= (exec_node
*) & head
;
231 bool is_empty() const
233 /* There are three ways to test whether a list is empty or not.
235 * - Check to see if the \c head points to the \c tail.
236 * - Check to see if the \c tail_pred points to the \c head.
237 * - Check to see if the \c head is the sentinel node by test whether its
238 * \c next pointer is \c NULL.
240 * The first two methods tend to generate better code on modern systems
241 * because they save a pointer dereference.
243 return head
== (exec_node
*) &tail
;
246 const exec_node
*get_head() const
248 return !is_empty() ? head
: NULL
;
251 exec_node
*get_head()
253 return !is_empty() ? head
: NULL
;
256 const exec_node
*get_tail() const
258 return !is_empty() ? tail_pred
: NULL
;
261 exec_node
*get_tail()
263 return !is_empty() ? tail_pred
: NULL
;
266 void push_head(exec_node
*n
)
269 n
->prev
= (exec_node
*) &head
;
275 void push_tail(exec_node
*n
)
277 n
->next
= (exec_node
*) &tail
;
284 void push_degenerate_list_at_head(exec_node
*n
)
286 assert(n
->prev
->next
== n
);
288 n
->prev
->next
= head
;
289 head
->prev
= n
->prev
;
290 n
->prev
= (exec_node
*) &head
;
295 * Remove the first node from a list and return it
298 * The first node in the list or \c NULL if the list is empty.
300 * \sa exec_list::get_head
302 exec_node
*pop_head()
304 exec_node
*const n
= this->get_head();
312 * Move all of the nodes from this list to the target list
314 void move_nodes_to(exec_list
*target
)
317 target
->make_empty();
321 target
->tail_pred
= tail_pred
;
323 target
->head
->prev
= (exec_node
*) &target
->head
;
324 target
->tail_pred
->next
= (exec_node
*) &target
->tail
;
331 * Append all nodes from the source list to the target list
334 append_list(exec_list
*source
)
336 if (source
->is_empty())
339 /* Link the first node of the source with the last node of the target list.
341 this->tail_pred
->next
= source
->head
;
342 source
->head
->prev
= this->tail_pred
;
344 /* Make the tail of the source list be the tail of the target list.
346 this->tail_pred
= source
->tail_pred
;
347 this->tail_pred
->next
= (exec_node
*) &this->tail
;
349 /* Make the source list empty for good measure.
351 source
->make_empty();
358 inline void exec_node::insert_before(exec_list
*before
)
360 if (before
->is_empty())
363 before
->tail_pred
->next
= this;
364 before
->head
->prev
= this->prev
;
366 this->prev
->next
= before
->head
;
367 this->prev
= before
->tail_pred
;
369 before
->make_empty();
374 * This version is safe even if the current node is removed.
376 #define foreach_list_safe(__node, __list) \
377 for (exec_node * __node = (__list)->head, * __next = __node->next \
379 ; __node = __next, __next = __next->next)
381 #define foreach_list(__node, __list) \
382 for (exec_node * __node = (__list)->head \
383 ; (__node)->next != NULL \
384 ; (__node) = (__node)->next)
387 * Iterate through two lists at once. Stops at the end of the shorter list.
389 * This is safe against either current node being removed or replaced.
391 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
392 for (exec_node * __node1 = (__list1)->head, \
393 * __node2 = (__list2)->head, \
394 * __next1 = __node1->next, \
395 * __next2 = __node2->next \
396 ; __next1 != NULL && __next2 != NULL \
397 ; __node1 = __next1, \
399 __next1 = __next1->next, \
400 __next2 = __next2->next)
402 #define foreach_list_const(__node, __list) \
403 for (const exec_node * __node = (__list)->head \
404 ; (__node)->next != NULL \
405 ; (__node) = (__node)->next)
407 #define foreach_list_typed(__type, __node, __field, __list) \
408 for (__type * __node = \
409 exec_node_data(__type, (__list)->head, __field); \
410 (__node)->__field.next != NULL; \
411 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
413 #define foreach_list_typed_const(__type, __node, __field, __list) \
414 for (const __type * __node = \
415 exec_node_data(__type, (__list)->head, __field); \
416 (__node)->__field.next != NULL; \
417 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
419 #endif /* LIST_CONTAINER_H */