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.
43 #ifndef LIST_CONTAINER_H
44 #define LIST_CONTAINER_H
51 #include "util/ralloc.h"
54 struct exec_node
*next
;
55 struct exec_node
*prev
;
58 DECLARE_RZALLOC_CXX_OPERATORS(exec_node
)
60 exec_node() : next(NULL
), prev(NULL
)
65 const exec_node
*get_next() const;
66 exec_node
*get_next();
68 const exec_node
*get_prev() const;
69 exec_node
*get_prev();
74 * Link a node with itself
76 * This creates a sort of degenerate list that is occasionally useful.
81 * Insert a node in the list after the current node
83 void insert_after(exec_node
*after
);
85 * Insert a node in the list before the current node
87 void insert_before(exec_node
*before
);
90 * Insert another list in the list before the current node
92 void insert_before(struct exec_list
*before
);
95 * Replace the current node with the given node.
97 void replace_with(exec_node
*replacement
);
100 * Is this the sentinel at the tail of the list?
102 bool is_tail_sentinel() const;
105 * Is this the sentinel at the head of the list?
107 bool is_head_sentinel() const;
112 exec_node_init(struct exec_node
*n
)
118 static inline const struct exec_node
*
119 exec_node_get_next_const(const struct exec_node
*n
)
124 static inline struct exec_node
*
125 exec_node_get_next(struct exec_node
*n
)
130 static inline const struct exec_node
*
131 exec_node_get_prev_const(const struct exec_node
*n
)
136 static inline struct exec_node
*
137 exec_node_get_prev(struct exec_node
*n
)
143 exec_node_remove(struct exec_node
*n
)
145 n
->next
->prev
= n
->prev
;
146 n
->prev
->next
= n
->next
;
152 exec_node_self_link(struct exec_node
*n
)
159 exec_node_insert_after(struct exec_node
*n
, struct exec_node
*after
)
161 after
->next
= n
->next
;
164 n
->next
->prev
= after
;
169 exec_node_insert_node_before(struct exec_node
*n
, struct exec_node
*before
)
172 before
->prev
= n
->prev
;
174 n
->prev
->next
= before
;
179 exec_node_replace_with(struct exec_node
*n
, struct exec_node
*replacement
)
181 replacement
->prev
= n
->prev
;
182 replacement
->next
= n
->next
;
184 n
->prev
->next
= replacement
;
185 n
->next
->prev
= replacement
;
189 exec_node_is_tail_sentinel(const struct exec_node
*n
)
191 return n
->next
== NULL
;
195 exec_node_is_head_sentinel(const struct exec_node
*n
)
197 return n
->prev
== NULL
;
201 inline const exec_node
*exec_node::get_next() const
203 return exec_node_get_next_const(this);
206 inline exec_node
*exec_node::get_next()
208 return exec_node_get_next(this);
211 inline const exec_node
*exec_node::get_prev() const
213 return exec_node_get_prev_const(this);
216 inline exec_node
*exec_node::get_prev()
218 return exec_node_get_prev(this);
221 inline void exec_node::remove()
223 exec_node_remove(this);
226 inline void exec_node::self_link()
228 exec_node_self_link(this);
231 inline void exec_node::insert_after(exec_node
*after
)
233 exec_node_insert_after(this, after
);
236 inline void exec_node::insert_before(exec_node
*before
)
238 exec_node_insert_node_before(this, before
);
241 inline void exec_node::replace_with(exec_node
*replacement
)
243 exec_node_replace_with(this, replacement
);
246 inline bool exec_node::is_tail_sentinel() const
248 return exec_node_is_tail_sentinel(this);
251 inline bool exec_node::is_head_sentinel() const
253 return exec_node_is_head_sentinel(this);
258 /* This macro will not work correctly if `t' uses virtual inheritance. If you
259 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
261 #define exec_list_offsetof(t, f, p) \
262 (((char *) &((t *) p)->f) - ((char *) p))
264 #define exec_list_offsetof(t, f, p) offsetof(t, f)
268 * Get a pointer to the structure containing an exec_node
270 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
271 * the containing structure.
273 * \param type Base type of the structure containing the node
274 * \param node Pointer to the \c exec_node
275 * \param field Name of the field in \c type that is the embedded \c exec_node
277 #define exec_node_data(type, node, field) \
278 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
285 struct exec_node head_sentinel
;
286 struct exec_node tail_sentinel
;
289 DECLARE_RALLOC_CXX_OPERATORS(exec_list
)
298 bool is_empty() const;
300 const exec_node
*get_head() const;
301 exec_node
*get_head();
302 const exec_node
*get_head_raw() const;
303 exec_node
*get_head_raw();
305 const exec_node
*get_tail() const;
306 exec_node
*get_tail();
307 const exec_node
*get_tail_raw() const;
308 exec_node
*get_tail_raw();
310 unsigned length() const;
312 void push_head(exec_node
*n
);
313 void push_tail(exec_node
*n
);
314 void push_degenerate_list_at_head(exec_node
*n
);
317 * Remove the first node from a list and return it
320 * The first node in the list or \c NULL if the list is empty.
322 * \sa exec_list::get_head
324 exec_node
*pop_head();
327 * Move all of the nodes from this list to the target list
329 void move_nodes_to(exec_list
*target
);
332 * Append all nodes from the source list to the end of the target list
334 void append_list(exec_list
*source
);
337 * Prepend all nodes from the source list to the beginning of the target
340 void prepend_list(exec_list
*source
);
345 exec_list_make_empty(struct exec_list
*list
)
347 list
->head_sentinel
.next
= &list
->tail_sentinel
;
348 list
->head_sentinel
.prev
= NULL
;
349 list
->tail_sentinel
.next
= NULL
;
350 list
->tail_sentinel
.prev
= &list
->head_sentinel
;
354 exec_list_is_empty(const struct exec_list
*list
)
356 /* There are three ways to test whether a list is empty or not.
358 * - Check to see if the head sentinel's \c next is the tail sentinel.
359 * - Check to see if the tail sentinel's \c prev is the head sentinel.
360 * - Check to see if the head is the sentinel node by test whether its
361 * \c next pointer is \c NULL.
363 * The first two methods tend to generate better code on modern systems
364 * because they save a pointer dereference.
366 return list
->head_sentinel
.next
== &list
->tail_sentinel
;
369 static inline const struct exec_node
*
370 exec_list_get_head_const(const struct exec_list
*list
)
372 return !exec_list_is_empty(list
) ? list
->head_sentinel
.next
: NULL
;
375 static inline struct exec_node
*
376 exec_list_get_head(struct exec_list
*list
)
378 return !exec_list_is_empty(list
) ? list
->head_sentinel
.next
: NULL
;
381 static inline const struct exec_node
*
382 exec_list_get_head_raw_const(const struct exec_list
*list
)
384 return list
->head_sentinel
.next
;
387 static inline struct exec_node
*
388 exec_list_get_head_raw(struct exec_list
*list
)
390 return list
->head_sentinel
.next
;
393 static inline const struct exec_node
*
394 exec_list_get_tail_const(const struct exec_list
*list
)
396 return !exec_list_is_empty(list
) ? list
->tail_sentinel
.prev
: NULL
;
399 static inline struct exec_node
*
400 exec_list_get_tail(struct exec_list
*list
)
402 return !exec_list_is_empty(list
) ? list
->tail_sentinel
.prev
: NULL
;
405 static inline const struct exec_node
*
406 exec_list_get_tail_raw_const(const struct exec_list
*list
)
408 return list
->tail_sentinel
.prev
;
411 static inline struct exec_node
*
412 exec_list_get_tail_raw(struct exec_list
*list
)
414 return list
->tail_sentinel
.prev
;
417 static inline unsigned
418 exec_list_length(const struct exec_list
*list
)
421 struct exec_node
*node
;
423 for (node
= list
->head_sentinel
.next
; node
->next
!= NULL
; node
= node
->next
) {
431 exec_list_push_head(struct exec_list
*list
, struct exec_node
*n
)
433 n
->next
= list
->head_sentinel
.next
;
434 n
->prev
= &list
->head_sentinel
;
437 list
->head_sentinel
.next
= n
;
441 exec_list_push_tail(struct exec_list
*list
, struct exec_node
*n
)
443 n
->next
= &list
->tail_sentinel
;
444 n
->prev
= list
->tail_sentinel
.prev
;
447 list
->tail_sentinel
.prev
= n
;
451 exec_list_push_degenerate_list_at_head(struct exec_list
*list
, struct exec_node
*n
)
453 assert(n
->prev
->next
== n
);
455 n
->prev
->next
= list
->head_sentinel
.next
;
456 list
->head_sentinel
.next
->prev
= n
->prev
;
457 n
->prev
= &list
->head_sentinel
;
458 list
->head_sentinel
.next
= n
;
461 static inline struct exec_node
*
462 exec_list_pop_head(struct exec_list
*list
)
464 struct exec_node
*const n
= exec_list_get_head(list
);
472 exec_list_move_nodes_to(struct exec_list
*list
, struct exec_list
*target
)
474 if (exec_list_is_empty(list
)) {
475 exec_list_make_empty(target
);
477 target
->head_sentinel
.next
= list
->head_sentinel
.next
;
478 target
->head_sentinel
.prev
= NULL
;
479 target
->tail_sentinel
.next
= NULL
;
480 target
->tail_sentinel
.prev
= list
->tail_sentinel
.prev
;
482 target
->head_sentinel
.next
->prev
= &target
->head_sentinel
;
483 target
->tail_sentinel
.prev
->next
= &target
->tail_sentinel
;
485 exec_list_make_empty(list
);
490 exec_list_append(struct exec_list
*list
, struct exec_list
*source
)
492 if (exec_list_is_empty(source
))
495 /* Link the first node of the source with the last node of the target list.
497 list
->tail_sentinel
.prev
->next
= source
->head_sentinel
.next
;
498 source
->head_sentinel
.next
->prev
= list
->tail_sentinel
.prev
;
500 /* Make the tail of the source list be the tail of the target list.
502 list
->tail_sentinel
.prev
= source
->tail_sentinel
.prev
;
503 list
->tail_sentinel
.prev
->next
= &list
->tail_sentinel
;
505 /* Make the source list empty for good measure.
507 exec_list_make_empty(source
);
511 exec_list_prepend(struct exec_list
*list
, struct exec_list
*source
)
513 exec_list_append(source
, list
);
514 exec_list_move_nodes_to(source
, list
);
518 exec_node_insert_list_before(struct exec_node
*n
, struct exec_list
*before
)
520 if (exec_list_is_empty(before
))
523 before
->tail_sentinel
.prev
->next
= n
;
524 before
->head_sentinel
.next
->prev
= n
->prev
;
526 n
->prev
->next
= before
->head_sentinel
.next
;
527 n
->prev
= before
->tail_sentinel
.prev
;
529 exec_list_make_empty(before
);
533 exec_list_validate(const struct exec_list
*list
)
535 const struct exec_node
*node
;
537 assert(list
->head_sentinel
.next
->prev
== &list
->head_sentinel
);
538 assert(list
->head_sentinel
.prev
== NULL
);
539 assert(list
->tail_sentinel
.next
== NULL
);
540 assert(list
->tail_sentinel
.prev
->next
== &list
->tail_sentinel
);
542 /* We could try to use one of the interators below for this but they all
543 * either require C++ or assume the exec_node is embedded in a structure
544 * which is not the case for this function.
546 for (node
= list
->head_sentinel
.next
; node
->next
!= NULL
; node
= node
->next
) {
547 assert(node
->next
->prev
== node
);
548 assert(node
->prev
->next
== node
);
553 inline void exec_list::make_empty()
555 exec_list_make_empty(this);
558 inline bool exec_list::is_empty() const
560 return exec_list_is_empty(this);
563 inline const exec_node
*exec_list::get_head() const
565 return exec_list_get_head_const(this);
568 inline exec_node
*exec_list::get_head()
570 return exec_list_get_head(this);
573 inline const exec_node
*exec_list::get_head_raw() const
575 return exec_list_get_head_raw_const(this);
578 inline exec_node
*exec_list::get_head_raw()
580 return exec_list_get_head_raw(this);
583 inline const exec_node
*exec_list::get_tail() const
585 return exec_list_get_tail_const(this);
588 inline exec_node
*exec_list::get_tail()
590 return exec_list_get_tail(this);
593 inline const exec_node
*exec_list::get_tail_raw() const
595 return exec_list_get_tail_raw_const(this);
598 inline exec_node
*exec_list::get_tail_raw()
600 return exec_list_get_tail_raw(this);
603 inline unsigned exec_list::length() const
605 return exec_list_length(this);
608 inline void exec_list::push_head(exec_node
*n
)
610 exec_list_push_head(this, n
);
613 inline void exec_list::push_tail(exec_node
*n
)
615 exec_list_push_tail(this, n
);
618 inline void exec_list::push_degenerate_list_at_head(exec_node
*n
)
620 exec_list_push_degenerate_list_at_head(this, n
);
623 inline exec_node
*exec_list::pop_head()
625 return exec_list_pop_head(this);
628 inline void exec_list::move_nodes_to(exec_list
*target
)
630 exec_list_move_nodes_to(this, target
);
633 inline void exec_list::append_list(exec_list
*source
)
635 exec_list_append(this, source
);
638 inline void exec_list::prepend_list(exec_list
*source
)
640 exec_list_prepend(this, source
);
643 inline void exec_node::insert_before(exec_list
*before
)
645 exec_node_insert_list_before(this, before
);
649 #define foreach_in_list(__type, __inst, __list) \
650 for (__type *(__inst) = (__type *)(__list)->head_sentinel.next; \
651 !(__inst)->is_tail_sentinel(); \
652 (__inst) = (__type *)(__inst)->next)
654 #define foreach_in_list_reverse(__type, __inst, __list) \
655 for (__type *(__inst) = (__type *)(__list)->tail_sentinel.prev; \
656 !(__inst)->is_head_sentinel(); \
657 (__inst) = (__type *)(__inst)->prev)
660 * This version is safe even if the current node is removed.
662 #define foreach_in_list_safe(__type, __node, __list) \
663 for (__type *__node = (__type *)(__list)->head_sentinel.next, \
664 *__next = (__type *)__node->next; \
666 __node = __next, __next = (__type *)__next->next)
668 #define foreach_in_list_reverse_safe(__type, __node, __list) \
669 for (__type *__node = (__type *)(__list)->tail_sentinel.prev, \
670 *__prev = (__type *)__node->prev; \
672 __node = __prev, __prev = (__type *)__prev->prev)
674 #define foreach_in_list_use_after(__type, __inst, __list) \
676 for ((__inst) = (__type *)(__list)->head_sentinel.next; \
677 !(__inst)->is_tail_sentinel(); \
678 (__inst) = (__type *)(__inst)->next)
680 * Iterate through two lists at once. Stops at the end of the shorter list.
682 * This is safe against either current node being removed or replaced.
684 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
685 for (struct exec_node * __node1 = (__list1)->head_sentinel.next, \
686 * __node2 = (__list2)->head_sentinel.next, \
687 * __next1 = __node1->next, \
688 * __next2 = __node2->next \
689 ; __next1 != NULL && __next2 != NULL \
690 ; __node1 = __next1, \
692 __next1 = __next1->next, \
693 __next2 = __next2->next)
695 #define foreach_list_typed(__type, __node, __field, __list) \
696 for (__type * __node = \
697 exec_node_data(__type, (__list)->head_sentinel.next, __field); \
698 (__node)->__field.next != NULL; \
699 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
701 #define foreach_list_typed_from(__type, __node, __field, __list, __start) \
702 for (__type * __node = exec_node_data(__type, (__start), __field); \
703 (__node)->__field.next != NULL; \
704 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
706 #define foreach_list_typed_reverse(__type, __node, __field, __list) \
707 for (__type * __node = \
708 exec_node_data(__type, (__list)->tail_sentinel.prev, __field); \
709 (__node)->__field.prev != NULL; \
710 (__node) = exec_node_data(__type, (__node)->__field.prev, __field))
712 #define foreach_list_typed_safe(__type, __node, __field, __list) \
713 for (__type * __node = \
714 exec_node_data(__type, (__list)->head_sentinel.next, __field), \
716 exec_node_data(__type, (__node)->__field.next, __field); \
717 (__node)->__field.next != NULL; \
718 __node = __next, __next = \
719 exec_node_data(__type, (__next)->__field.next, __field))
721 #define foreach_list_typed_reverse_safe(__type, __node, __field, __list) \
722 for (__type * __node = \
723 exec_node_data(__type, (__list)->tail_sentinel.prev, __field), \
725 exec_node_data(__type, (__node)->__field.prev, __field); \
726 (__node)->__field.prev != NULL; \
727 __node = __prev, __prev = \
728 exec_node_data(__type, (__prev)->__field.prev, __field))
730 #endif /* LIST_CONTAINER_H */