66028f649d43a24e9b74badba692f5dc110b605f
[mesa.git] / src / glsl / list.h
1 /*
2 * Copyright © 2008, 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file list.h
26 * \brief Doubly-linked list abstract container type.
27 *
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.
32 *
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.
36 *
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:
42 *
43 * - A \c head pointer that represents the \c next pointer of the
44 * head sentinel node.
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
49 * tail sentinel node.
50 *
51 * Therefore, if \c head->next is \c NULL or \c tail_prev->prev is \c NULL,
52 * the list is empty.
53 *
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.
57 *
58 * http://www.natami.net/dev/Libraries_Manual_guide/node02D7.html
59 *
60 * \author Ian Romanick <ian.d.romanick@intel.com>
61 */
62
63 #pragma once
64 #ifndef LIST_CONTAINER_H
65 #define LIST_CONTAINER_H
66
67 #ifndef __cplusplus
68 #include <stddef.h>
69 #endif
70 #include <assert.h>
71
72 #include "ralloc.h"
73
74 struct exec_node {
75 struct exec_node *next;
76 struct exec_node *prev;
77
78 #ifdef __cplusplus
79 DECLARE_RALLOC_CXX_OPERATORS(exec_node)
80
81 exec_node() : next(NULL), prev(NULL)
82 {
83 /* empty */
84 }
85
86 const exec_node *get_next() const;
87 exec_node *get_next();
88
89 const exec_node *get_prev() const;
90 exec_node *get_prev();
91
92 void remove();
93
94 /**
95 * Link a node with itself
96 *
97 * This creates a sort of degenerate list that is occasionally useful.
98 */
99 void self_link();
100
101 /**
102 * Insert a node in the list after the current node
103 */
104 void insert_after(exec_node *after);
105 /**
106 * Insert a node in the list before the current node
107 */
108 void insert_before(exec_node *before);
109
110 /**
111 * Insert another list in the list before the current node
112 */
113 void insert_before(struct exec_list *before);
114
115 /**
116 * Replace the current node with the given node.
117 */
118 void replace_with(exec_node *replacement);
119
120 /**
121 * Is this the sentinel at the tail of the list?
122 */
123 bool is_tail_sentinel() const;
124
125 /**
126 * Is this the sentinel at the head of the list?
127 */
128 bool is_head_sentinel() const;
129 #endif
130 };
131
132 static inline void
133 exec_node_init(struct exec_node *n)
134 {
135 n->next = NULL;
136 n->prev = NULL;
137 }
138
139 static inline const struct exec_node *
140 exec_node_get_next_const(const struct exec_node *n)
141 {
142 return n->next;
143 }
144
145 static inline struct exec_node *
146 exec_node_get_next(struct exec_node *n)
147 {
148 return n->next;
149 }
150
151 static inline const struct exec_node *
152 exec_node_get_prev_const(const struct exec_node *n)
153 {
154 return n->prev;
155 }
156
157 static inline struct exec_node *
158 exec_node_get_prev(struct exec_node *n)
159 {
160 return n->prev;
161 }
162
163 static inline void
164 exec_node_remove(struct exec_node *n)
165 {
166 n->next->prev = n->prev;
167 n->prev->next = n->next;
168 n->next = NULL;
169 n->prev = NULL;
170 }
171
172 static inline void
173 exec_node_self_link(struct exec_node *n)
174 {
175 n->next = n;
176 n->prev = n;
177 }
178
179 static inline void
180 exec_node_insert_after(struct exec_node *n, struct exec_node *after)
181 {
182 after->next = n->next;
183 after->prev = n;
184
185 n->next->prev = after;
186 n->next = after;
187 }
188
189 static inline void
190 exec_node_insert_node_before(struct exec_node *n, struct exec_node *before)
191 {
192 before->next = n;
193 before->prev = n->prev;
194
195 n->prev->next = before;
196 n->prev = before;
197 }
198
199 static inline void
200 exec_node_replace_with(struct exec_node *n, struct exec_node *replacement)
201 {
202 replacement->prev = n->prev;
203 replacement->next = n->next;
204
205 n->prev->next = replacement;
206 n->next->prev = replacement;
207 }
208
209 static inline bool
210 exec_node_is_tail_sentinel(const struct exec_node *n)
211 {
212 return n->next == NULL;
213 }
214
215 static inline bool
216 exec_node_is_head_sentinel(const struct exec_node *n)
217 {
218 return n->prev == NULL;
219 }
220
221 #ifdef __cplusplus
222 inline const exec_node *exec_node::get_next() const
223 {
224 return exec_node_get_next_const(this);
225 }
226
227 inline exec_node *exec_node::get_next()
228 {
229 return exec_node_get_next(this);
230 }
231
232 inline const exec_node *exec_node::get_prev() const
233 {
234 return exec_node_get_prev_const(this);
235 }
236
237 inline exec_node *exec_node::get_prev()
238 {
239 return exec_node_get_prev(this);
240 }
241
242 inline void exec_node::remove()
243 {
244 exec_node_remove(this);
245 }
246
247 inline void exec_node::self_link()
248 {
249 exec_node_self_link(this);
250 }
251
252 inline void exec_node::insert_after(exec_node *after)
253 {
254 exec_node_insert_after(this, after);
255 }
256
257 inline void exec_node::insert_before(exec_node *before)
258 {
259 exec_node_insert_node_before(this, before);
260 }
261
262 inline void exec_node::replace_with(exec_node *replacement)
263 {
264 exec_node_replace_with(this, replacement);
265 }
266
267 inline bool exec_node::is_tail_sentinel() const
268 {
269 return exec_node_is_tail_sentinel(this);
270 }
271
272 inline bool exec_node::is_head_sentinel() const
273 {
274 return exec_node_is_head_sentinel(this);
275 }
276 #endif
277
278 #ifdef __cplusplus
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!
281 */
282 #define exec_list_offsetof(t, f, p) \
283 (((char *) &((t *) p)->f) - ((char *) p))
284 #else
285 #define exec_list_offsetof(t, f, p) offsetof(t, f)
286 #endif
287
288 /**
289 * Get a pointer to the structure containing an exec_node
290 *
291 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
292 * the containing structure.
293 *
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
297 */
298 #define exec_node_data(type, node, field) \
299 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
300
301 #ifdef __cplusplus
302 struct exec_node;
303 #endif
304
305 struct exec_list {
306 struct exec_node *head;
307 struct exec_node *tail;
308 struct exec_node *tail_pred;
309
310 #ifdef __cplusplus
311 DECLARE_RALLOC_CXX_OPERATORS(exec_list)
312
313 exec_list()
314 {
315 make_empty();
316 }
317
318 void make_empty();
319
320 bool is_empty() const;
321
322 const exec_node *get_head() const;
323 exec_node *get_head();
324
325 const exec_node *get_tail() const;
326 exec_node *get_tail();
327
328 void push_head(exec_node *n);
329 void push_tail(exec_node *n);
330 void push_degenerate_list_at_head(exec_node *n);
331
332 /**
333 * Remove the first node from a list and return it
334 *
335 * \return
336 * The first node in the list or \c NULL if the list is empty.
337 *
338 * \sa exec_list::get_head
339 */
340 exec_node *pop_head();
341
342 /**
343 * Move all of the nodes from this list to the target list
344 */
345 void move_nodes_to(exec_list *target);
346
347 /**
348 * Append all nodes from the source list to the end of the target list
349 */
350 void append_list(exec_list *source);
351
352 /**
353 * Prepend all nodes from the source list to the beginning of the target
354 * list
355 */
356 void prepend_list(exec_list *source);
357 #endif
358 };
359
360 static inline void
361 exec_list_make_empty(struct exec_list *list)
362 {
363 list->head = (struct exec_node *) & list->tail;
364 list->tail = NULL;
365 list->tail_pred = (struct exec_node *) & list->head;
366 }
367
368 static inline bool
369 exec_list_is_empty(const struct exec_list *list)
370 {
371 /* There are three ways to test whether a list is empty or not.
372 *
373 * - Check to see if the \c head points to the \c tail.
374 * - Check to see if the \c tail_pred points to the \c head.
375 * - Check to see if the \c head is the sentinel node by test whether its
376 * \c next pointer is \c NULL.
377 *
378 * The first two methods tend to generate better code on modern systems
379 * because they save a pointer dereference.
380 */
381 return list->head == (struct exec_node *) &list->tail;
382 }
383
384 static inline const struct exec_node *
385 exec_list_get_head_const(const struct exec_list *list)
386 {
387 return !exec_list_is_empty(list) ? list->head : NULL;
388 }
389
390 static inline struct exec_node *
391 exec_list_get_head(struct exec_list *list)
392 {
393 return !exec_list_is_empty(list) ? list->head : NULL;
394 }
395
396 static inline const struct exec_node *
397 exec_list_get_tail_const(const struct exec_list *list)
398 {
399 return !exec_list_is_empty(list) ? list->tail_pred : NULL;
400 }
401
402 static inline struct exec_node *
403 exec_list_get_tail(struct exec_list *list)
404 {
405 return !exec_list_is_empty(list) ? list->tail_pred : NULL;
406 }
407
408 static inline void
409 exec_list_push_head(struct exec_list *list, struct exec_node *n)
410 {
411 n->next = list->head;
412 n->prev = (struct exec_node *) &list->head;
413
414 n->next->prev = n;
415 list->head = n;
416 }
417
418 static inline void
419 exec_list_push_tail(struct exec_list *list, struct exec_node *n)
420 {
421 n->next = (struct exec_node *) &list->tail;
422 n->prev = list->tail_pred;
423
424 n->prev->next = n;
425 list->tail_pred = n;
426 }
427
428 static inline void
429 exec_list_push_degenerate_list_at_head(struct exec_list *list, struct exec_node *n)
430 {
431 assert(n->prev->next == n);
432
433 n->prev->next = list->head;
434 list->head->prev = n->prev;
435 n->prev = (struct exec_node *) &list->head;
436 list->head = n;
437 }
438
439 static inline struct exec_node *
440 exec_list_pop_head(struct exec_list *list)
441 {
442 struct exec_node *const n = exec_list_get_head(list);
443 if (n != NULL)
444 exec_node_remove(n);
445
446 return n;
447 }
448
449 static inline void
450 exec_list_move_nodes_to(struct exec_list *list, struct exec_list *target)
451 {
452 if (exec_list_is_empty(list)) {
453 exec_list_make_empty(target);
454 } else {
455 target->head = list->head;
456 target->tail = NULL;
457 target->tail_pred = list->tail_pred;
458
459 target->head->prev = (struct exec_node *) &target->head;
460 target->tail_pred->next = (struct exec_node *) &target->tail;
461
462 exec_list_make_empty(list);
463 }
464 }
465
466 static inline void
467 exec_list_append(struct exec_list *list, struct exec_list *source)
468 {
469 if (exec_list_is_empty(source))
470 return;
471
472 /* Link the first node of the source with the last node of the target list.
473 */
474 list->tail_pred->next = source->head;
475 source->head->prev = list->tail_pred;
476
477 /* Make the tail of the source list be the tail of the target list.
478 */
479 list->tail_pred = source->tail_pred;
480 list->tail_pred->next = (struct exec_node *) &list->tail;
481
482 /* Make the source list empty for good measure.
483 */
484 exec_list_make_empty(source);
485 }
486
487 static inline void
488 exec_list_prepend(struct exec_list *list, struct exec_list *source)
489 {
490 exec_list_append(source, list);
491 exec_list_move_nodes_to(source, list);
492 }
493
494 static inline void
495 exec_node_insert_list_before(struct exec_node *n, struct exec_list *before)
496 {
497 if (exec_list_is_empty(before))
498 return;
499
500 before->tail_pred->next = n;
501 before->head->prev = n->prev;
502
503 n->prev->next = before->head;
504 n->prev = before->tail_pred;
505
506 exec_list_make_empty(before);
507 }
508
509 #ifdef __cplusplus
510 inline void exec_list::make_empty()
511 {
512 exec_list_make_empty(this);
513 }
514
515 inline bool exec_list::is_empty() const
516 {
517 return exec_list_is_empty(this);
518 }
519
520 inline const exec_node *exec_list::get_head() const
521 {
522 return exec_list_get_head_const(this);
523 }
524
525 inline exec_node *exec_list::get_head()
526 {
527 return exec_list_get_head(this);
528 }
529
530 inline const exec_node *exec_list::get_tail() const
531 {
532 return exec_list_get_tail_const(this);
533 }
534
535 inline exec_node *exec_list::get_tail()
536 {
537 return exec_list_get_tail(this);
538 }
539
540 inline void exec_list::push_head(exec_node *n)
541 {
542 exec_list_push_head(this, n);
543 }
544
545 inline void exec_list::push_tail(exec_node *n)
546 {
547 exec_list_push_tail(this, n);
548 }
549
550 inline void exec_list::push_degenerate_list_at_head(exec_node *n)
551 {
552 exec_list_push_degenerate_list_at_head(this, n);
553 }
554
555 inline exec_node *exec_list::pop_head()
556 {
557 return exec_list_pop_head(this);
558 }
559
560 inline void exec_list::move_nodes_to(exec_list *target)
561 {
562 exec_list_move_nodes_to(this, target);
563 }
564
565 inline void exec_list::append_list(exec_list *source)
566 {
567 exec_list_append(this, source);
568 }
569
570 inline void exec_list::prepend_list(exec_list *source)
571 {
572 exec_list_prepend(this, source);
573 }
574
575 inline void exec_node::insert_before(exec_list *before)
576 {
577 exec_node_insert_list_before(this, before);
578 }
579 #endif
580
581 #define foreach_in_list(__type, __inst, __list) \
582 for (__type *(__inst) = (__type *)(__list)->head; \
583 !(__inst)->is_tail_sentinel(); \
584 (__inst) = (__type *)(__inst)->next)
585
586 #define foreach_in_list_reverse(__type, __inst, __list) \
587 for (__type *(__inst) = (__type *)(__list)->tail_pred; \
588 !(__inst)->is_head_sentinel(); \
589 (__inst) = (__type *)(__inst)->prev)
590
591 /**
592 * This version is safe even if the current node is removed.
593 */
594 #define foreach_in_list_safe(__type, __node, __list) \
595 for (__type *__node = (__type *)(__list)->head, \
596 *__next = (__type *)__node->next; \
597 __next != NULL; \
598 __node = __next, __next = (__type *)__next->next)
599
600 #define foreach_in_list_use_after(__type, __inst, __list) \
601 __type *(__inst); \
602 for ((__inst) = (__type *)(__list)->head; \
603 !(__inst)->is_tail_sentinel(); \
604 (__inst) = (__type *)(__inst)->next)
605 /**
606 * Iterate through two lists at once. Stops at the end of the shorter list.
607 *
608 * This is safe against either current node being removed or replaced.
609 */
610 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
611 for (struct exec_node * __node1 = (__list1)->head, \
612 * __node2 = (__list2)->head, \
613 * __next1 = __node1->next, \
614 * __next2 = __node2->next \
615 ; __next1 != NULL && __next2 != NULL \
616 ; __node1 = __next1, \
617 __node2 = __next2, \
618 __next1 = __next1->next, \
619 __next2 = __next2->next)
620
621 #define foreach_list_typed(__type, __node, __field, __list) \
622 for (__type * __node = \
623 exec_node_data(__type, (__list)->head, __field); \
624 (__node)->__field.next != NULL; \
625 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
626
627 #define foreach_list_typed_safe(__type, __node, __field, __list) \
628 for (__type * __node = \
629 exec_node_data(__type, (__list)->head, __field), \
630 * __next = \
631 exec_node_data(__type, (__node)->__field.next, __field); \
632 __next != NULL; \
633 __node = __next, __next = \
634 exec_node_data(__type, (__next)->__field.next, __field))
635
636 #endif /* LIST_CONTAINER_H */