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
35 * sentinel. The head sentinel and tail sentinel nodes are allocated within the
38 * Do note that this means that the list nodes will contain pointers into the
39 * list structure itself and as a result you may not \c realloc() an \c
40 * exec_list or any structure in which an \c exec_list is embedded.
44 #ifndef LIST_CONTAINER_H
45 #define LIST_CONTAINER_H
52 #include "util/ralloc.h"
55 struct exec_node
*next
;
56 struct exec_node
*prev
;
59 DECLARE_RZALLOC_CXX_OPERATORS(exec_node
)
61 exec_node() : next(NULL
), prev(NULL
)
66 const exec_node
*get_next() const;
67 exec_node
*get_next();
69 const exec_node
*get_prev() const;
70 exec_node
*get_prev();
75 * Link a node with itself
77 * This creates a sort of degenerate list that is occasionally useful.
82 * Insert a node in the list after the current node
84 void insert_after(exec_node
*after
);
86 * Insert a node in the list before the current node
88 void insert_before(exec_node
*before
);
91 * Insert another list in the list before the current node
93 void insert_before(struct exec_list
*before
);
96 * Replace the current node with the given node.
98 void replace_with(exec_node
*replacement
);
101 * Is this the sentinel at the tail of the list?
103 bool is_tail_sentinel() const;
106 * Is this the sentinel at the head of the list?
108 bool is_head_sentinel() const;
113 exec_node_init(struct exec_node
*n
)
119 static inline const struct exec_node
*
120 exec_node_get_next_const(const struct exec_node
*n
)
125 static inline struct exec_node
*
126 exec_node_get_next(struct exec_node
*n
)
131 static inline const struct exec_node
*
132 exec_node_get_prev_const(const struct exec_node
*n
)
137 static inline struct exec_node
*
138 exec_node_get_prev(struct exec_node
*n
)
144 exec_node_remove(struct exec_node
*n
)
146 n
->next
->prev
= n
->prev
;
147 n
->prev
->next
= n
->next
;
153 exec_node_self_link(struct exec_node
*n
)
160 exec_node_insert_after(struct exec_node
*n
, struct exec_node
*after
)
162 after
->next
= n
->next
;
165 n
->next
->prev
= after
;
170 exec_node_insert_node_before(struct exec_node
*n
, struct exec_node
*before
)
173 before
->prev
= n
->prev
;
175 n
->prev
->next
= before
;
180 exec_node_replace_with(struct exec_node
*n
, struct exec_node
*replacement
)
182 replacement
->prev
= n
->prev
;
183 replacement
->next
= n
->next
;
185 n
->prev
->next
= replacement
;
186 n
->next
->prev
= replacement
;
190 exec_node_is_tail_sentinel(const struct exec_node
*n
)
192 return n
->next
== NULL
;
196 exec_node_is_head_sentinel(const struct exec_node
*n
)
198 return n
->prev
== NULL
;
202 inline const exec_node
*exec_node::get_next() const
204 return exec_node_get_next_const(this);
207 inline exec_node
*exec_node::get_next()
209 return exec_node_get_next(this);
212 inline const exec_node
*exec_node::get_prev() const
214 return exec_node_get_prev_const(this);
217 inline exec_node
*exec_node::get_prev()
219 return exec_node_get_prev(this);
222 inline void exec_node::remove()
224 exec_node_remove(this);
227 inline void exec_node::self_link()
229 exec_node_self_link(this);
232 inline void exec_node::insert_after(exec_node
*after
)
234 exec_node_insert_after(this, after
);
237 inline void exec_node::insert_before(exec_node
*before
)
239 exec_node_insert_node_before(this, before
);
242 inline void exec_node::replace_with(exec_node
*replacement
)
244 exec_node_replace_with(this, replacement
);
247 inline bool exec_node::is_tail_sentinel() const
249 return exec_node_is_tail_sentinel(this);
252 inline bool exec_node::is_head_sentinel() const
254 return exec_node_is_head_sentinel(this);
259 /* This macro will not work correctly if `t' uses virtual inheritance. If you
260 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
262 #define exec_list_offsetof(t, f, p) \
263 (((char *) &((t *) p)->f) - ((char *) p))
265 #define exec_list_offsetof(t, f, p) offsetof(t, f)
269 * Get a pointer to the structure containing an exec_node
271 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
272 * the containing structure.
274 * \param type Base type of the structure containing the node
275 * \param node Pointer to the \c exec_node
276 * \param field Name of the field in \c type that is the embedded \c exec_node
278 #define exec_node_data(type, node, field) \
279 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
286 struct exec_node head_sentinel
;
287 struct exec_node tail_sentinel
;
290 DECLARE_RALLOC_CXX_OPERATORS(exec_list
)
299 bool is_empty() const;
301 const exec_node
*get_head() const;
302 exec_node
*get_head();
303 const exec_node
*get_head_raw() const;
304 exec_node
*get_head_raw();
306 const exec_node
*get_tail() const;
307 exec_node
*get_tail();
308 const exec_node
*get_tail_raw() const;
309 exec_node
*get_tail_raw();
311 unsigned length() const;
313 void push_head(exec_node
*n
);
314 void push_tail(exec_node
*n
);
315 void push_degenerate_list_at_head(exec_node
*n
);
318 * Remove the first node from a list and return it
321 * The first node in the list or \c NULL if the list is empty.
323 * \sa exec_list::get_head
325 exec_node
*pop_head();
328 * Move all of the nodes from this list to the target list
330 void move_nodes_to(exec_list
*target
);
333 * Append all nodes from the source list to the end of the target list
335 void append_list(exec_list
*source
);
338 * Prepend all nodes from the source list to the beginning of the target
341 void prepend_list(exec_list
*source
);
346 exec_list_make_empty(struct exec_list
*list
)
348 list
->head_sentinel
.next
= &list
->tail_sentinel
;
349 list
->head_sentinel
.prev
= NULL
;
350 list
->tail_sentinel
.next
= NULL
;
351 list
->tail_sentinel
.prev
= &list
->head_sentinel
;
355 exec_list_is_empty(const struct exec_list
*list
)
357 /* There are three ways to test whether a list is empty or not.
359 * - Check to see if the head sentinel's \c next is the tail sentinel.
360 * - Check to see if the tail sentinel's \c prev is the head sentinel.
361 * - Check to see if the head is the sentinel node by test whether its
362 * \c next pointer is \c NULL.
364 * The first two methods tend to generate better code on modern systems
365 * because they save a pointer dereference.
367 return list
->head_sentinel
.next
== &list
->tail_sentinel
;
370 static inline const struct exec_node
*
371 exec_list_get_head_const(const struct exec_list
*list
)
373 return !exec_list_is_empty(list
) ? list
->head_sentinel
.next
: NULL
;
376 static inline struct exec_node
*
377 exec_list_get_head(struct exec_list
*list
)
379 return !exec_list_is_empty(list
) ? list
->head_sentinel
.next
: NULL
;
382 static inline const struct exec_node
*
383 exec_list_get_head_raw_const(const struct exec_list
*list
)
385 return list
->head_sentinel
.next
;
388 static inline struct exec_node
*
389 exec_list_get_head_raw(struct exec_list
*list
)
391 return list
->head_sentinel
.next
;
394 static inline const struct exec_node
*
395 exec_list_get_tail_const(const struct exec_list
*list
)
397 return !exec_list_is_empty(list
) ? list
->tail_sentinel
.prev
: NULL
;
400 static inline struct exec_node
*
401 exec_list_get_tail(struct exec_list
*list
)
403 return !exec_list_is_empty(list
) ? list
->tail_sentinel
.prev
: NULL
;
406 static inline const struct exec_node
*
407 exec_list_get_tail_raw_const(const struct exec_list
*list
)
409 return list
->tail_sentinel
.prev
;
412 static inline struct exec_node
*
413 exec_list_get_tail_raw(struct exec_list
*list
)
415 return list
->tail_sentinel
.prev
;
418 static inline unsigned
419 exec_list_length(const struct exec_list
*list
)
422 struct exec_node
*node
;
424 for (node
= list
->head_sentinel
.next
; node
->next
!= NULL
; node
= node
->next
) {
432 exec_list_push_head(struct exec_list
*list
, struct exec_node
*n
)
434 n
->next
= list
->head_sentinel
.next
;
435 n
->prev
= &list
->head_sentinel
;
438 list
->head_sentinel
.next
= n
;
442 exec_list_push_tail(struct exec_list
*list
, struct exec_node
*n
)
444 n
->next
= &list
->tail_sentinel
;
445 n
->prev
= list
->tail_sentinel
.prev
;
448 list
->tail_sentinel
.prev
= n
;
452 exec_list_push_degenerate_list_at_head(struct exec_list
*list
, struct exec_node
*n
)
454 assert(n
->prev
->next
== n
);
456 n
->prev
->next
= list
->head_sentinel
.next
;
457 list
->head_sentinel
.next
->prev
= n
->prev
;
458 n
->prev
= &list
->head_sentinel
;
459 list
->head_sentinel
.next
= n
;
462 static inline struct exec_node
*
463 exec_list_pop_head(struct exec_list
*list
)
465 struct exec_node
*const n
= exec_list_get_head(list
);
473 exec_list_move_nodes_to(struct exec_list
*list
, struct exec_list
*target
)
475 if (exec_list_is_empty(list
)) {
476 exec_list_make_empty(target
);
478 target
->head_sentinel
.next
= list
->head_sentinel
.next
;
479 target
->head_sentinel
.prev
= NULL
;
480 target
->tail_sentinel
.next
= NULL
;
481 target
->tail_sentinel
.prev
= list
->tail_sentinel
.prev
;
483 target
->head_sentinel
.next
->prev
= &target
->head_sentinel
;
484 target
->tail_sentinel
.prev
->next
= &target
->tail_sentinel
;
486 exec_list_make_empty(list
);
491 exec_list_append(struct exec_list
*list
, struct exec_list
*source
)
493 if (exec_list_is_empty(source
))
496 /* Link the first node of the source with the last node of the target list.
498 list
->tail_sentinel
.prev
->next
= source
->head_sentinel
.next
;
499 source
->head_sentinel
.next
->prev
= list
->tail_sentinel
.prev
;
501 /* Make the tail of the source list be the tail of the target list.
503 list
->tail_sentinel
.prev
= source
->tail_sentinel
.prev
;
504 list
->tail_sentinel
.prev
->next
= &list
->tail_sentinel
;
506 /* Make the source list empty for good measure.
508 exec_list_make_empty(source
);
512 exec_list_prepend(struct exec_list
*list
, struct exec_list
*source
)
514 exec_list_append(source
, list
);
515 exec_list_move_nodes_to(source
, list
);
519 exec_node_insert_list_before(struct exec_node
*n
, struct exec_list
*before
)
521 if (exec_list_is_empty(before
))
524 before
->tail_sentinel
.prev
->next
= n
;
525 before
->head_sentinel
.next
->prev
= n
->prev
;
527 n
->prev
->next
= before
->head_sentinel
.next
;
528 n
->prev
= before
->tail_sentinel
.prev
;
530 exec_list_make_empty(before
);
534 exec_list_validate(const struct exec_list
*list
)
536 const struct exec_node
*node
;
538 assert(list
->head_sentinel
.next
->prev
== &list
->head_sentinel
);
539 assert(list
->head_sentinel
.prev
== NULL
);
540 assert(list
->tail_sentinel
.next
== NULL
);
541 assert(list
->tail_sentinel
.prev
->next
== &list
->tail_sentinel
);
543 /* We could try to use one of the interators below for this but they all
544 * either require C++ or assume the exec_node is embedded in a structure
545 * which is not the case for this function.
547 for (node
= list
->head_sentinel
.next
; node
->next
!= NULL
; node
= node
->next
) {
548 assert(node
->next
->prev
== node
);
549 assert(node
->prev
->next
== node
);
554 inline void exec_list::make_empty()
556 exec_list_make_empty(this);
559 inline bool exec_list::is_empty() const
561 return exec_list_is_empty(this);
564 inline const exec_node
*exec_list::get_head() const
566 return exec_list_get_head_const(this);
569 inline exec_node
*exec_list::get_head()
571 return exec_list_get_head(this);
574 inline const exec_node
*exec_list::get_head_raw() const
576 return exec_list_get_head_raw_const(this);
579 inline exec_node
*exec_list::get_head_raw()
581 return exec_list_get_head_raw(this);
584 inline const exec_node
*exec_list::get_tail() const
586 return exec_list_get_tail_const(this);
589 inline exec_node
*exec_list::get_tail()
591 return exec_list_get_tail(this);
594 inline const exec_node
*exec_list::get_tail_raw() const
596 return exec_list_get_tail_raw_const(this);
599 inline exec_node
*exec_list::get_tail_raw()
601 return exec_list_get_tail_raw(this);
604 inline unsigned exec_list::length() const
606 return exec_list_length(this);
609 inline void exec_list::push_head(exec_node
*n
)
611 exec_list_push_head(this, n
);
614 inline void exec_list::push_tail(exec_node
*n
)
616 exec_list_push_tail(this, n
);
619 inline void exec_list::push_degenerate_list_at_head(exec_node
*n
)
621 exec_list_push_degenerate_list_at_head(this, n
);
624 inline exec_node
*exec_list::pop_head()
626 return exec_list_pop_head(this);
629 inline void exec_list::move_nodes_to(exec_list
*target
)
631 exec_list_move_nodes_to(this, target
);
634 inline void exec_list::append_list(exec_list
*source
)
636 exec_list_append(this, source
);
639 inline void exec_list::prepend_list(exec_list
*source
)
641 exec_list_prepend(this, source
);
644 inline void exec_node::insert_before(exec_list
*before
)
646 exec_node_insert_list_before(this, before
);
650 #define foreach_in_list(__type, __inst, __list) \
651 for (__type *(__inst) = (__type *)(__list)->head_sentinel.next; \
652 !(__inst)->is_tail_sentinel(); \
653 (__inst) = (__type *)(__inst)->next)
655 #define foreach_in_list_reverse(__type, __inst, __list) \
656 for (__type *(__inst) = (__type *)(__list)->tail_sentinel.prev; \
657 !(__inst)->is_head_sentinel(); \
658 (__inst) = (__type *)(__inst)->prev)
661 * This version is safe even if the current node is removed.
663 #define foreach_in_list_safe(__type, __node, __list) \
664 for (__type *__node = (__type *)(__list)->head_sentinel.next, \
665 *__next = (__type *)__node->next; \
667 __node = __next, __next = (__type *)__next->next)
669 #define foreach_in_list_reverse_safe(__type, __node, __list) \
670 for (__type *__node = (__type *)(__list)->tail_sentinel.prev, \
671 *__prev = (__type *)__node->prev; \
673 __node = __prev, __prev = (__type *)__prev->prev)
675 #define foreach_in_list_use_after(__type, __inst, __list) \
677 for ((__inst) = (__type *)(__list)->head_sentinel.next; \
678 !(__inst)->is_tail_sentinel(); \
679 (__inst) = (__type *)(__inst)->next)
681 * Iterate through two lists at once. Stops at the end of the shorter list.
683 * This is safe against either current node being removed or replaced.
685 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
686 for (struct exec_node * __node1 = (__list1)->head_sentinel.next, \
687 * __node2 = (__list2)->head_sentinel.next, \
688 * __next1 = __node1->next, \
689 * __next2 = __node2->next \
690 ; __next1 != NULL && __next2 != NULL \
691 ; __node1 = __next1, \
693 __next1 = __next1->next, \
694 __next2 = __next2->next)
696 #define foreach_list_typed(__type, __node, __field, __list) \
697 for (__type * __node = \
698 exec_node_data(__type, (__list)->head_sentinel.next, __field); \
699 (__node)->__field.next != NULL; \
700 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
702 #define foreach_list_typed_from(__type, __node, __field, __list, __start) \
703 for (__type * __node = exec_node_data(__type, (__start), __field); \
704 (__node)->__field.next != NULL; \
705 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
707 #define foreach_list_typed_reverse(__type, __node, __field, __list) \
708 for (__type * __node = \
709 exec_node_data(__type, (__list)->tail_sentinel.prev, __field); \
710 (__node)->__field.prev != NULL; \
711 (__node) = exec_node_data(__type, (__node)->__field.prev, __field))
713 #define foreach_list_typed_safe(__type, __node, __field, __list) \
714 for (__type * __node = \
715 exec_node_data(__type, (__list)->head_sentinel.next, __field), \
717 exec_node_data(__type, (__node)->__field.next, __field); \
718 (__node)->__field.next != NULL; \
719 __node = __next, __next = \
720 exec_node_data(__type, (__next)->__field.next, __field))
722 #define foreach_list_typed_reverse_safe(__type, __node, __field, __list) \
723 for (__type * __node = \
724 exec_node_data(__type, (__list)->tail_sentinel.prev, __field), \
726 exec_node_data(__type, (__node)->__field.prev, __field); \
727 (__node)->__field.prev != NULL; \
728 __node = __prev, __prev = \
729 exec_node_data(__type, (__prev)->__field.prev, __field))
731 #endif /* LIST_CONTAINER_H */