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 sentinal head and tail node. These nodes
29 * contain no data. The head sentinal can be identified by its \c prev
30 * pointer being \c NULL. The tail sentinal can be identified by its
31 * \c next pointer being \c NULL.
33 * A list is empty if either the head sentinal's \c next pointer points to the
34 * tail sentinal or the tail sentinal'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 sentinal nodes are in a single structure. Noting
39 * that each sentinal 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 * sentinal node and the \c next pointer of the tail sentinal 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
73 struct exec_node
*next
;
74 struct exec_node
*prev
;
77 exec_node() : next(NULL
), prev(NULL
)
82 const exec_node
*get_next() const
92 const exec_node
*get_prev() const
111 * Link a node with itself
113 * This creates a sort of degenerate list that is occasionally useful.
122 * Insert a node in the list after the current node
124 void insert_after(exec_node
*after
)
126 after
->next
= this->next
;
129 this->next
->prev
= after
;
133 * Insert a node in the list before the current node
135 void insert_before(exec_node
*before
)
138 before
->prev
= this->prev
;
140 this->prev
->next
= before
;
145 * Is this the sentinal at the tail of the list?
147 bool is_tail_sentinal() const
149 return this->next
== NULL
;
153 * Is this the sentinal at the head of the list?
155 bool is_head_sentinal() const
157 return this->prev
== NULL
;
164 /* This macro will not work correctly if `t' uses virtual inheritance. If you
165 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
167 #define exec_list_offsetof(t, f, p) \
168 (((char *) &((t *) p)->f) - ((char *) p))
170 #define exec_list_offsetof(t, f, p) offsetof(t, f)
174 * Get a pointer to the structure containing an exec_node
176 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
177 * the containing structure.
179 * \param type Base type of the structure containing the node
180 * \param node Pointer to the \c exec_node
181 * \param field Name of the field in \c type that is the embedded \c exec_node
183 #define exec_node_data(type, node, field) \
184 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
200 bool has_next() const
206 class exec_list_iterator
: public iterator
{
208 exec_list_iterator(exec_node
*n
) : node(n
), _next(n
->next
)
229 bool has_next() const
231 return _next
!= NULL
;
239 #define foreach_iter(iter_type, iter, container) \
240 for (iter_type iter = (container) . iterator(); iter.has_next(); iter.next())
245 struct exec_node
*head
;
246 struct exec_node
*tail
;
247 struct exec_node
*tail_pred
;
257 head
= (exec_node
*) & tail
;
259 tail_pred
= (exec_node
*) & head
;
262 bool is_empty() const
264 /* There are three ways to test whether a list is empty or not.
266 * - Check to see if the \c head points to the \c tail.
267 * - Check to see if the \c tail_pred points to the \c head.
268 * - Check to see if the \c head is the sentinal node by test whether its
269 * \c next pointer is \c NULL.
271 * The first two methods tend to generate better code on modern systems
272 * because they save a pointer dereference.
274 return head
== (exec_node
*) &tail
;
277 const exec_node
*get_head() const
279 return !is_empty() ? head
: NULL
;
282 exec_node
*get_head()
284 return !is_empty() ? head
: NULL
;
287 const exec_node
*get_tail() const
289 return !is_empty() ? tail_pred
: NULL
;
292 exec_node
*get_tail()
294 return !is_empty() ? tail_pred
: NULL
;
297 void push_head(exec_node
*n
)
300 n
->prev
= (exec_node
*) &head
;
306 void push_tail(exec_node
*n
)
308 n
->next
= (exec_node
*) &tail
;
315 void push_degenerate_list_at_head(exec_node
*n
)
317 assert(n
->prev
->next
== n
);
319 n
->prev
->next
= head
;
320 head
->prev
= n
->prev
;
321 n
->prev
= (exec_node
*) &head
;
326 * Move all of the nodes from this list to the target list
328 void move_nodes_to(exec_list
*target
)
331 target
->make_empty();
335 target
->tail_pred
= tail_pred
;
337 target
->head
->prev
= (exec_node
*) &target
->head
;
338 target
->tail_pred
->next
= (exec_node
*) &target
->tail
;
344 exec_list_iterator
iterator()
346 return exec_list_iterator(head
);
349 exec_list_iterator
iterator() const
351 return exec_list_iterator((exec_node
*) head
);
356 #define foreach_list(__node, __list) \
357 for (exec_node * __node = (__list)->head \
358 ; (__node)->next != NULL \
359 ; (__node) = (__node)->next)
361 #define foreach_list_const(__node, __list) \
362 for (const exec_node * __node = (__list)->head \
363 ; (__node)->next != NULL \
364 ; (__node) = (__node)->next)
366 #define foreach_list_typed(__type, __node, __field, __list) \
367 for (__type * __node = \
368 exec_node_data(__type, (__list)->head, __field); \
369 (__node)->__field.next != NULL; \
370 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
372 #define foreach_list_typed_const(__type, __node, __field, __list) \
373 for (const __type * __node = \
374 exec_node_data(__type, (__list)->head, __field); \
375 (__node)->__field.next != NULL; \
376 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
378 #endif /* LIST_CONTAINER_H */