803aab56be2b3fda4a19e1d647c785cc99e79152
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;
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;
132 static inline const struct exec_node
*
133 exec_node_get_next_const(const struct exec_node
*n
)
138 static inline struct exec_node
*
139 exec_node_get_next(struct exec_node
*n
)
144 static inline const struct exec_node
*
145 exec_node_get_prev_const(const struct exec_node
*n
)
150 static inline struct exec_node
*
151 exec_node_get_prev(struct exec_node
*n
)
157 exec_node_remove(struct exec_node
*n
)
159 n
->next
->prev
= n
->prev
;
160 n
->prev
->next
= n
->next
;
166 exec_node_self_link(struct exec_node
*n
)
173 exec_node_insert_after(struct exec_node
*n
, struct exec_node
*after
)
175 after
->next
= n
->next
;
178 n
->next
->prev
= after
;
183 exec_node_insert_node_before(struct exec_node
*n
, struct exec_node
*before
)
186 before
->prev
= n
->prev
;
188 n
->prev
->next
= before
;
193 exec_node_replace_with(struct exec_node
*n
, struct exec_node
*replacement
)
195 replacement
->prev
= n
->prev
;
196 replacement
->next
= n
->next
;
198 n
->prev
->next
= replacement
;
199 n
->next
->prev
= replacement
;
203 exec_node_is_tail_sentinel(const struct exec_node
*n
)
205 return n
->next
== NULL
;
209 exec_node_is_head_sentinel(const struct exec_node
*n
)
211 return n
->prev
== NULL
;
215 inline const exec_node
*exec_node::get_next() const
217 return exec_node_get_next_const(this);
220 inline exec_node
*exec_node::get_next()
222 return exec_node_get_next(this);
225 inline const exec_node
*exec_node::get_prev() const
227 return exec_node_get_prev_const(this);
230 inline exec_node
*exec_node::get_prev()
232 return exec_node_get_prev(this);
235 inline void exec_node::remove()
237 exec_node_remove(this);
240 inline void exec_node::self_link()
242 exec_node_self_link(this);
245 inline void exec_node::insert_after(exec_node
*after
)
247 exec_node_insert_after(this, after
);
250 inline void exec_node::insert_before(exec_node
*before
)
252 exec_node_insert_node_before(this, before
);
255 inline void exec_node::replace_with(exec_node
*replacement
)
257 exec_node_replace_with(this, replacement
);
260 inline bool exec_node::is_tail_sentinel() const
262 return exec_node_is_tail_sentinel(this);
265 inline bool exec_node::is_head_sentinel() const
267 return exec_node_is_head_sentinel(this);
272 /* This macro will not work correctly if `t' uses virtual inheritance. If you
273 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
275 #define exec_list_offsetof(t, f, p) \
276 (((char *) &((t *) p)->f) - ((char *) p))
278 #define exec_list_offsetof(t, f, p) offsetof(t, f)
282 * Get a pointer to the structure containing an exec_node
284 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
285 * the containing structure.
287 * \param type Base type of the structure containing the node
288 * \param node Pointer to the \c exec_node
289 * \param field Name of the field in \c type that is the embedded \c exec_node
291 #define exec_node_data(type, node, field) \
292 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
299 struct exec_node
*head
;
300 struct exec_node
*tail
;
301 struct exec_node
*tail_pred
;
304 DECLARE_RALLOC_CXX_OPERATORS(exec_list
)
313 bool is_empty() const;
315 const exec_node
*get_head() const;
316 exec_node
*get_head();
318 const exec_node
*get_tail() const;
319 exec_node
*get_tail();
321 void push_head(exec_node
*n
);
322 void push_tail(exec_node
*n
);
323 void push_degenerate_list_at_head(exec_node
*n
);
326 * Remove the first node from a list and return it
329 * The first node in the list or \c NULL if the list is empty.
331 * \sa exec_list::get_head
333 exec_node
*pop_head();
336 * Move all of the nodes from this list to the target list
338 void move_nodes_to(exec_list
*target
);
341 * Append all nodes from the source list to the target list
343 void append_list(exec_list
*source
);
348 exec_list_make_empty(struct exec_list
*list
)
350 list
->head
= (struct exec_node
*) & list
->tail
;
352 list
->tail_pred
= (struct exec_node
*) & list
->head
;
356 exec_list_is_empty(const struct exec_list
*list
)
358 /* There are three ways to test whether a list is empty or not.
360 * - Check to see if the \c head points to the \c tail.
361 * - Check to see if the \c tail_pred points to the \c head.
362 * - Check to see if the \c head is the sentinel node by test whether its
363 * \c next pointer is \c NULL.
365 * The first two methods tend to generate better code on modern systems
366 * because they save a pointer dereference.
368 return list
->head
== (struct exec_node
*) &list
->tail
;
371 static inline const struct exec_node
*
372 exec_list_get_head_const(const struct exec_list
*list
)
374 return !exec_list_is_empty(list
) ? list
->head
: NULL
;
377 static inline struct exec_node
*
378 exec_list_get_head(struct exec_list
*list
)
380 return !exec_list_is_empty(list
) ? list
->head
: NULL
;
383 static inline const struct exec_node
*
384 exec_list_get_tail_const(const struct exec_list
*list
)
386 return !exec_list_is_empty(list
) ? list
->tail_pred
: NULL
;
389 static inline struct exec_node
*
390 exec_list_get_tail(struct exec_list
*list
)
392 return !exec_list_is_empty(list
) ? list
->tail_pred
: NULL
;
396 exec_list_push_head(struct exec_list
*list
, struct exec_node
*n
)
398 n
->next
= list
->head
;
399 n
->prev
= (struct exec_node
*) &list
->head
;
406 exec_list_push_tail(struct exec_list
*list
, struct exec_node
*n
)
408 n
->next
= (struct exec_node
*) &list
->tail
;
409 n
->prev
= list
->tail_pred
;
416 exec_list_push_degenerate_list_at_head(struct exec_list
*list
, struct exec_node
*n
)
418 assert(n
->prev
->next
== n
);
420 n
->prev
->next
= list
->head
;
421 list
->head
->prev
= n
->prev
;
422 n
->prev
= (struct exec_node
*) &list
->head
;
426 static inline struct exec_node
*
427 exec_list_pop_head(struct exec_list
*list
)
429 struct exec_node
*const n
= exec_list_get_head(list
);
437 exec_list_move_nodes_to(struct exec_list
*list
, struct exec_list
*target
)
439 if (exec_list_is_empty(list
)) {
440 exec_list_make_empty(target
);
442 target
->head
= list
->head
;
444 target
->tail_pred
= list
->tail_pred
;
446 target
->head
->prev
= (struct exec_node
*) &target
->head
;
447 target
->tail_pred
->next
= (struct exec_node
*) &target
->tail
;
449 exec_list_make_empty(list
);
454 exec_list_append(struct exec_list
*list
, struct exec_list
*source
)
456 if (exec_list_is_empty(source
))
459 /* Link the first node of the source with the last node of the target list.
461 list
->tail_pred
->next
= source
->head
;
462 source
->head
->prev
= list
->tail_pred
;
464 /* Make the tail of the source list be the tail of the target list.
466 list
->tail_pred
= source
->tail_pred
;
467 list
->tail_pred
->next
= (struct exec_node
*) &list
->tail
;
469 /* Make the source list empty for good measure.
471 exec_list_make_empty(source
);
475 exec_node_insert_list_before(struct exec_node
*n
, struct exec_list
*before
)
477 if (exec_list_is_empty(before
))
480 before
->tail_pred
->next
= n
;
481 before
->head
->prev
= n
->prev
;
483 n
->prev
->next
= before
->head
;
484 n
->prev
= before
->tail_pred
;
486 exec_list_make_empty(before
);
490 inline void exec_list::make_empty()
492 exec_list_make_empty(this);
495 inline bool exec_list::is_empty() const
497 return exec_list_is_empty(this);
500 inline const exec_node
*exec_list::get_head() const
502 return exec_list_get_head_const(this);
505 inline exec_node
*exec_list::get_head()
507 return exec_list_get_head(this);
510 inline const exec_node
*exec_list::get_tail() const
512 return exec_list_get_tail_const(this);
515 inline exec_node
*exec_list::get_tail()
517 return exec_list_get_tail(this);
520 inline void exec_list::push_head(exec_node
*n
)
522 exec_list_push_head(this, n
);
525 inline void exec_list::push_tail(exec_node
*n
)
527 exec_list_push_tail(this, n
);
530 inline void exec_list::push_degenerate_list_at_head(exec_node
*n
)
532 exec_list_push_degenerate_list_at_head(this, n
);
535 inline exec_node
*exec_list::pop_head()
537 return exec_list_pop_head(this);
540 inline void exec_list::move_nodes_to(exec_list
*target
)
542 exec_list_move_nodes_to(this, target
);
545 inline void exec_list::append_list(exec_list
*source
)
547 exec_list_append(this, source
);
550 inline void exec_node::insert_before(exec_list
*before
)
552 exec_node_insert_list_before(this, before
);
557 * This version is safe even if the current node is removed.
559 #define foreach_list_safe(__node, __list) \
560 for (struct exec_node * __node = (__list)->head, * __next = __node->next \
562 ; __node = __next, __next = __next->next)
564 #define foreach_list(__node, __list) \
565 for (struct exec_node * __node = (__list)->head \
566 ; (__node)->next != NULL \
567 ; (__node) = (__node)->next)
570 * Iterate through two lists at once. Stops at the end of the shorter list.
572 * This is safe against either current node being removed or replaced.
574 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
575 for (struct exec_node * __node1 = (__list1)->head, \
576 * __node2 = (__list2)->head, \
577 * __next1 = __node1->next, \
578 * __next2 = __node2->next \
579 ; __next1 != NULL && __next2 != NULL \
580 ; __node1 = __next1, \
582 __next1 = __next1->next, \
583 __next2 = __next2->next)
585 #define foreach_list_const(__node, __list) \
586 for (const struct exec_node * __node = (__list)->head \
587 ; (__node)->next != NULL \
588 ; (__node) = (__node)->next)
590 #define foreach_list_typed(__type, __node, __field, __list) \
591 for (__type * __node = \
592 exec_node_data(__type, (__list)->head, __field); \
593 (__node)->__field.next != NULL; \
594 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
596 #define foreach_list_typed_const(__type, __node, __field, __list) \
597 for (const __type * __node = \
598 exec_node_data(__type, (__list)->head, __field); \
599 (__node)->__field.next != NULL; \
600 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
602 #endif /* LIST_CONTAINER_H */