glsl: Pass in options to do_algebraic().
[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 target list
349 */
350 void append_list(exec_list *source);
351 #endif
352 };
353
354 static inline void
355 exec_list_make_empty(struct exec_list *list)
356 {
357 list->head = (struct exec_node *) & list->tail;
358 list->tail = NULL;
359 list->tail_pred = (struct exec_node *) & list->head;
360 }
361
362 static inline bool
363 exec_list_is_empty(const struct exec_list *list)
364 {
365 /* There are three ways to test whether a list is empty or not.
366 *
367 * - Check to see if the \c head points to the \c tail.
368 * - Check to see if the \c tail_pred points to the \c head.
369 * - Check to see if the \c head is the sentinel node by test whether its
370 * \c next pointer is \c NULL.
371 *
372 * The first two methods tend to generate better code on modern systems
373 * because they save a pointer dereference.
374 */
375 return list->head == (struct exec_node *) &list->tail;
376 }
377
378 static inline const struct exec_node *
379 exec_list_get_head_const(const struct exec_list *list)
380 {
381 return !exec_list_is_empty(list) ? list->head : NULL;
382 }
383
384 static inline struct exec_node *
385 exec_list_get_head(struct exec_list *list)
386 {
387 return !exec_list_is_empty(list) ? list->head : NULL;
388 }
389
390 static inline const struct exec_node *
391 exec_list_get_tail_const(const struct exec_list *list)
392 {
393 return !exec_list_is_empty(list) ? list->tail_pred : NULL;
394 }
395
396 static inline struct exec_node *
397 exec_list_get_tail(struct exec_list *list)
398 {
399 return !exec_list_is_empty(list) ? list->tail_pred : NULL;
400 }
401
402 static inline void
403 exec_list_push_head(struct exec_list *list, struct exec_node *n)
404 {
405 n->next = list->head;
406 n->prev = (struct exec_node *) &list->head;
407
408 n->next->prev = n;
409 list->head = n;
410 }
411
412 static inline void
413 exec_list_push_tail(struct exec_list *list, struct exec_node *n)
414 {
415 n->next = (struct exec_node *) &list->tail;
416 n->prev = list->tail_pred;
417
418 n->prev->next = n;
419 list->tail_pred = n;
420 }
421
422 static inline void
423 exec_list_push_degenerate_list_at_head(struct exec_list *list, struct exec_node *n)
424 {
425 assert(n->prev->next == n);
426
427 n->prev->next = list->head;
428 list->head->prev = n->prev;
429 n->prev = (struct exec_node *) &list->head;
430 list->head = n;
431 }
432
433 static inline struct exec_node *
434 exec_list_pop_head(struct exec_list *list)
435 {
436 struct exec_node *const n = exec_list_get_head(list);
437 if (n != NULL)
438 exec_node_remove(n);
439
440 return n;
441 }
442
443 static inline void
444 exec_list_move_nodes_to(struct exec_list *list, struct exec_list *target)
445 {
446 if (exec_list_is_empty(list)) {
447 exec_list_make_empty(target);
448 } else {
449 target->head = list->head;
450 target->tail = NULL;
451 target->tail_pred = list->tail_pred;
452
453 target->head->prev = (struct exec_node *) &target->head;
454 target->tail_pred->next = (struct exec_node *) &target->tail;
455
456 exec_list_make_empty(list);
457 }
458 }
459
460 static inline void
461 exec_list_append(struct exec_list *list, struct exec_list *source)
462 {
463 if (exec_list_is_empty(source))
464 return;
465
466 /* Link the first node of the source with the last node of the target list.
467 */
468 list->tail_pred->next = source->head;
469 source->head->prev = list->tail_pred;
470
471 /* Make the tail of the source list be the tail of the target list.
472 */
473 list->tail_pred = source->tail_pred;
474 list->tail_pred->next = (struct exec_node *) &list->tail;
475
476 /* Make the source list empty for good measure.
477 */
478 exec_list_make_empty(source);
479 }
480
481 static inline void
482 exec_node_insert_list_before(struct exec_node *n, struct exec_list *before)
483 {
484 if (exec_list_is_empty(before))
485 return;
486
487 before->tail_pred->next = n;
488 before->head->prev = n->prev;
489
490 n->prev->next = before->head;
491 n->prev = before->tail_pred;
492
493 exec_list_make_empty(before);
494 }
495
496 #ifdef __cplusplus
497 inline void exec_list::make_empty()
498 {
499 exec_list_make_empty(this);
500 }
501
502 inline bool exec_list::is_empty() const
503 {
504 return exec_list_is_empty(this);
505 }
506
507 inline const exec_node *exec_list::get_head() const
508 {
509 return exec_list_get_head_const(this);
510 }
511
512 inline exec_node *exec_list::get_head()
513 {
514 return exec_list_get_head(this);
515 }
516
517 inline const exec_node *exec_list::get_tail() const
518 {
519 return exec_list_get_tail_const(this);
520 }
521
522 inline exec_node *exec_list::get_tail()
523 {
524 return exec_list_get_tail(this);
525 }
526
527 inline void exec_list::push_head(exec_node *n)
528 {
529 exec_list_push_head(this, n);
530 }
531
532 inline void exec_list::push_tail(exec_node *n)
533 {
534 exec_list_push_tail(this, n);
535 }
536
537 inline void exec_list::push_degenerate_list_at_head(exec_node *n)
538 {
539 exec_list_push_degenerate_list_at_head(this, n);
540 }
541
542 inline exec_node *exec_list::pop_head()
543 {
544 return exec_list_pop_head(this);
545 }
546
547 inline void exec_list::move_nodes_to(exec_list *target)
548 {
549 exec_list_move_nodes_to(this, target);
550 }
551
552 inline void exec_list::append_list(exec_list *source)
553 {
554 exec_list_append(this, source);
555 }
556
557 inline void exec_node::insert_before(exec_list *before)
558 {
559 exec_node_insert_list_before(this, before);
560 }
561 #endif
562
563 /**
564 * This version is safe even if the current node is removed.
565 */
566 #define foreach_list_safe(__node, __list) \
567 for (struct exec_node * __node = (__list)->head, * __next = __node->next \
568 ; __next != NULL \
569 ; __node = __next, __next = __next->next)
570
571 #define foreach_list(__node, __list) \
572 for (struct exec_node * __node = (__list)->head \
573 ; (__node)->next != NULL \
574 ; (__node) = (__node)->next)
575
576 /**
577 * Iterate through two lists at once. Stops at the end of the shorter list.
578 *
579 * This is safe against either current node being removed or replaced.
580 */
581 #define foreach_two_lists(__node1, __list1, __node2, __list2) \
582 for (struct exec_node * __node1 = (__list1)->head, \
583 * __node2 = (__list2)->head, \
584 * __next1 = __node1->next, \
585 * __next2 = __node2->next \
586 ; __next1 != NULL && __next2 != NULL \
587 ; __node1 = __next1, \
588 __node2 = __next2, \
589 __next1 = __next1->next, \
590 __next2 = __next2->next)
591
592 #define foreach_list_const(__node, __list) \
593 for (const struct exec_node * __node = (__list)->head \
594 ; (__node)->next != NULL \
595 ; (__node) = (__node)->next)
596
597 #define foreach_list_typed(__type, __node, __field, __list) \
598 for (__type * __node = \
599 exec_node_data(__type, (__list)->head, __field); \
600 (__node)->__field.next != NULL; \
601 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
602
603 #define foreach_list_typed_const(__type, __node, __field, __list) \
604 for (const __type * __node = \
605 exec_node_data(__type, (__list)->head, __field); \
606 (__node)->__field.next != NULL; \
607 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
608
609 #endif /* LIST_CONTAINER_H */