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
72 #include "util/ralloc.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;
87 exec_node
*get_next();
89 const exec_node
*get_prev() const;
90 exec_node
*get_prev();
95 * Link a node with itself
97 * This creates a sort of degenerate list that is occasionally useful.
102 * Insert a node in the list after the current node
104 void insert_after(exec_node
*after
);
106 * Insert a node in the list before the current node
108 void insert_before(exec_node
*before
);
111 * Insert another list in the list before the current node
113 void insert_before(struct exec_list
*before
);
116 * Replace the current node with the given node.
118 void replace_with(exec_node
*replacement
);
121 * Is this the sentinel at the tail of the list?
123 bool is_tail_sentinel() const;
126 * Is this the sentinel at the head of the list?
128 bool is_head_sentinel() const;
133 exec_node_init(struct exec_node
*n
)
139 static inline const struct exec_node
*
140 exec_node_get_next_const(const struct exec_node
*n
)
145 static inline struct exec_node
*
146 exec_node_get_next(struct exec_node
*n
)
151 static inline const struct exec_node
*
152 exec_node_get_prev_const(const struct exec_node
*n
)
157 static inline struct exec_node
*
158 exec_node_get_prev(struct exec_node
*n
)
164 exec_node_remove(struct exec_node
*n
)
166 n
->next
->prev
= n
->prev
;
167 n
->prev
->next
= n
->next
;
173 exec_node_self_link(struct exec_node
*n
)
180 exec_node_insert_after(struct exec_node
*n
, struct exec_node
*after
)
182 after
->next
= n
->next
;
185 n
->next
->prev
= after
;
190 exec_node_insert_node_before(struct exec_node
*n
, struct exec_node
*before
)
193 before
->prev
= n
->prev
;
195 n
->prev
->next
= before
;
200 exec_node_replace_with(struct exec_node
*n
, struct exec_node
*replacement
)
202 replacement
->prev
= n
->prev
;
203 replacement
->next
= n
->next
;
205 n
->prev
->next
= replacement
;
206 n
->next
->prev
= replacement
;
210 exec_node_is_tail_sentinel(const struct exec_node
*n
)
212 return n
->next
== NULL
;
216 exec_node_is_head_sentinel(const struct exec_node
*n
)
218 return n
->prev
== NULL
;
222 inline const exec_node
*exec_node::get_next() const
224 return exec_node_get_next_const(this);
227 inline exec_node
*exec_node::get_next()
229 return exec_node_get_next(this);
232 inline const exec_node
*exec_node::get_prev() const
234 return exec_node_get_prev_const(this);
237 inline exec_node
*exec_node::get_prev()
239 return exec_node_get_prev(this);
242 inline void exec_node::remove()
244 exec_node_remove(this);
247 inline void exec_node::self_link()
249 exec_node_self_link(this);
252 inline void exec_node::insert_after(exec_node
*after
)
254 exec_node_insert_after(this, after
);
257 inline void exec_node::insert_before(exec_node
*before
)
259 exec_node_insert_node_before(this, before
);
262 inline void exec_node::replace_with(exec_node
*replacement
)
264 exec_node_replace_with(this, replacement
);
267 inline bool exec_node::is_tail_sentinel() const
269 return exec_node_is_tail_sentinel(this);
272 inline bool exec_node::is_head_sentinel() const
274 return exec_node_is_head_sentinel(this);
279 /* This macro will not work correctly if `t' uses virtual inheritance. If you
280 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
282 #define exec_list_offsetof(t, f, p) \
283 (((char *) &((t *) p)->f) - ((char *) p))
285 #define exec_list_offsetof(t, f, p) offsetof(t, f)
289 * Get a pointer to the structure containing an exec_node
291 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
292 * the containing structure.
294 * \param type Base type of the structure containing the node
295 * \param node Pointer to the \c exec_node
296 * \param field Name of the field in \c type that is the embedded \c exec_node
298 #define exec_node_data(type, node, field) \
299 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
306 struct exec_node
*head
;
307 struct exec_node
*tail
;
308 struct exec_node
*tail_pred
;
311 DECLARE_RALLOC_CXX_OPERATORS(exec_list
)
320 bool is_empty() const;
322 const exec_node
*get_head() const;
323 exec_node
*get_head();
325 const exec_node
*get_tail() const;
326 exec_node
*get_tail();
328 unsigned length() const;
330 void push_head(exec_node
*n
);
331 void push_tail(exec_node
*n
);
332 void push_degenerate_list_at_head(exec_node
*n
);
335 * Remove the first node from a list and return it
338 * The first node in the list or \c NULL if the list is empty.
340 * \sa exec_list::get_head
342 exec_node
*pop_head();
345 * Move all of the nodes from this list to the target list
347 void move_nodes_to(exec_list
*target
);
350 * Append all nodes from the source list to the end of the target list
352 void append_list(exec_list
*source
);
355 * Prepend all nodes from the source list to the beginning of the target
358 void prepend_list(exec_list
*source
);
363 exec_list_make_empty(struct exec_list
*list
)
365 list
->head
= (struct exec_node
*) & list
->tail
;
367 list
->tail_pred
= (struct exec_node
*) & list
->head
;
371 exec_list_is_empty(const struct exec_list
*list
)
373 /* There are three ways to test whether a list is empty or not.
375 * - Check to see if the \c head points to the \c tail.
376 * - Check to see if the \c tail_pred points to the \c head.
377 * - Check to see if the \c head is the sentinel node by test whether its
378 * \c next pointer is \c NULL.
380 * The first two methods tend to generate better code on modern systems
381 * because they save a pointer dereference.
383 return list
->head
== (struct exec_node
*) &list
->tail
;
386 static inline const struct exec_node
*
387 exec_list_get_head_const(const struct exec_list
*list
)
389 return !exec_list_is_empty(list
) ? list
->head
: NULL
;
392 static inline struct exec_node
*
393 exec_list_get_head(struct exec_list
*list
)
395 return !exec_list_is_empty(list
) ? list
->head
: NULL
;
398 static inline const struct exec_node
*
399 exec_list_get_tail_const(const struct exec_list
*list
)
401 return !exec_list_is_empty(list
) ? list
->tail_pred
: NULL
;
404 static inline struct exec_node
*
405 exec_list_get_tail(struct exec_list
*list
)
407 return !exec_list_is_empty(list
) ? list
->tail_pred
: NULL
;
410 static inline unsigned
411 exec_list_length(const struct exec_list
*list
)
414 struct exec_node
*node
;
416 for (node
= list
->head
; node
->next
!= NULL
; node
= node
->next
) {
424 exec_list_push_head(struct exec_list
*list
, struct exec_node
*n
)
426 n
->next
= list
->head
;
427 n
->prev
= (struct exec_node
*) &list
->head
;
434 exec_list_push_tail(struct exec_list
*list
, struct exec_node
*n
)
436 n
->next
= (struct exec_node
*) &list
->tail
;
437 n
->prev
= list
->tail_pred
;
444 exec_list_push_degenerate_list_at_head(struct exec_list
*list
, struct exec_node
*n
)
446 assert(n
->prev
->next
== n
);
448 n
->prev
->next
= list
->head
;
449 list
->head
->prev
= n
->prev
;
450 n
->prev
= (struct exec_node
*) &list
->head
;
454 static inline struct exec_node
*
455 exec_list_pop_head(struct exec_list
*list
)
457 struct exec_node
*const n
= exec_list_get_head(list
);
465 exec_list_move_nodes_to(struct exec_list
*list
, struct exec_list
*target
)
467 if (exec_list_is_empty(list
)) {
468 exec_list_make_empty(target
);
470 target
->head
= list
->head
;
472 target
->tail_pred
= list
->tail_pred
;
474 target
->head
->prev
= (struct exec_node
*) &target
->head
;
475 target
->tail_pred
->next
= (struct exec_node
*) &target
->tail
;
477 exec_list_make_empty(list
);
482 exec_list_append(struct exec_list
*list
, struct exec_list
*source
)
484 if (exec_list_is_empty(source
))
487 /* Link the first node of the source with the last node of the target list.
489 list
->tail_pred
->next
= source
->head
;
490 source
->head
->prev
= list
->tail_pred
;
492 /* Make the tail of the source list be the tail of the target list.
494 list
->tail_pred
= source
->tail_pred
;
495 list
->tail_pred
->next
= (struct exec_node
*) &list
->tail
;
497 /* Make the source list empty for good measure.
499 exec_list_make_empty(source
);
503 exec_list_prepend(struct exec_list
*list
, struct exec_list
*source
)
505 exec_list_append(source
, list
);
506 exec_list_move_nodes_to(source
, list
);
510 exec_node_insert_list_before(struct exec_node
*n
, struct exec_list
*before
)
512 if (exec_list_is_empty(before
))
515 before
->tail_pred
->next
= n
;
516 before
->head
->prev
= n
->prev
;
518 n
->prev
->next
= before
->head
;
519 n
->prev
= before
->tail_pred
;
521 exec_list_make_empty(before
);
525 inline void exec_list::make_empty()
527 exec_list_make_empty(this);
530 inline bool exec_list::is_empty() const
532 return exec_list_is_empty(this);
535 inline const exec_node
*exec_list::get_head() const
537 return exec_list_get_head_const(this);
540 inline exec_node
*exec_list::get_head()
542 return exec_list_get_head(this);
545 inline const exec_node
*exec_list::get_tail() const
547 return exec_list_get_tail_const(this);
550 inline exec_node
*exec_list::get_tail()
552 return exec_list_get_tail(this);
555 inline unsigned exec_list::length() const
557 return exec_list_length(this);
560 inline void exec_list::push_head(exec_node
*n
)
562 exec_list_push_head(this, n
);
565 inline void exec_list::push_tail(exec_node
*n
)
567 exec_list_push_tail(this, n
);
570 inline void exec_list::push_degenerate_list_at_head(exec_node
*n
)
572 exec_list_push_degenerate_list_at_head(this, n
);
575 inline exec_node
*exec_list::pop_head()
577 return exec_list_pop_head(this);
580 inline void exec_list::move_nodes_to(exec_list
*target
)
582 exec_list_move_nodes_to(this, target
);
585 inline void exec_list::append_list(exec_list
*source
)
587 exec_list_append(this, source
);
590 inline void exec_list::prepend_list(exec_list
*source
)
592 exec_list_prepend(this, source
);
595 inline void exec_node::insert_before(exec_list
*before
)
597 exec_node_insert_list_before(this, before
);
601 #define foreach_in_list(__type, __inst, __list) \
602 for (__type *(__inst) = (__type *)(__list)->head; \
603 !(__inst)->is_tail_sentinel(); \
604 (__inst) = (__type *)(__inst)->next)
606 #define foreach_in_list_reverse(__type, __inst, __list) \
607 for (__type *(__inst) = (__type *)(__list)->tail_pred; \
608 !(__inst)->is_head_sentinel(); \
609 (__inst) = (__type *)(__inst)->prev)
612 * This version is safe even if the current node is removed.
614 #define foreach_in_list_safe(__type, __node, __list) \
615 for (__type *__node = (__type *)(__list)->head, \
616 *__next = (__type *)__node->next; \
618 __node = __next, __next = (__type *)__next->next)
620 #define foreach_in_list_use_after(__type, __inst, __list) \
622 for ((__inst) = (__type *)(__list)->head; \
623 !(__inst)->is_tail_sentinel(); \
624 (__inst) = (__type *)(__inst)->next)
626 * Iterate through two lists at once. Stops at the end of the shorter list.
628 * This is safe against either current node being removed or replaced.
630 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
631 for (struct exec_node * __node1 = (__list1)->head, \
632 * __node2 = (__list2)->head, \
633 * __next1 = __node1->next, \
634 * __next2 = __node2->next \
635 ; __next1 != NULL && __next2 != NULL \
636 ; __node1 = __next1, \
638 __next1 = __next1->next, \
639 __next2 = __next2->next)
641 #define foreach_list_typed(__type, __node, __field, __list) \
642 for (__type * __node = \
643 exec_node_data(__type, (__list)->head, __field); \
644 (__node)->__field.next != NULL; \
645 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
647 #define foreach_list_typed_safe(__type, __node, __field, __list) \
648 for (__type * __node = \
649 exec_node_data(__type, (__list)->head, __field), \
651 exec_node_data(__type, (__node)->__field.next, __field); \
653 __node = __next, __next = \
654 exec_node_data(__type, (__next)->__field.next, __field))
656 #endif /* LIST_CONTAINER_H */