i965: Request that returns be lowered in shader main
[mesa/nouveau-pmpeg.git] / src / glsl / list.h
blob3197b03cf2832a51fd9044e72f9debb55f33cb8f
1 /*
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
13 * Software.
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.
24 /**
25 * \file list.h
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.
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
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.
51 * Therefore, if \c head->next is \c NULL or \c tail_prev->prev is \c NULL,
52 * the list is empty.
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>
63 #pragma once
64 #ifndef LIST_CONTAINER_H
65 #define LIST_CONTAINER_H
67 #ifndef __cplusplus
68 #include <stddef.h>
69 #include <talloc.h>
70 #else
71 extern "C" {
72 #include <talloc.h>
74 #endif
76 #include <assert.h>
78 struct exec_node {
79 struct exec_node *next;
80 struct exec_node *prev;
82 #ifdef __cplusplus
83 /* Callers of this talloc-based new need not call delete. It's
84 * easier to just talloc_free 'ctx' (or any of its ancestors). */
85 static void* operator new(size_t size, void *ctx)
87 void *node;
89 node = talloc_size(ctx, size);
90 assert(node != NULL);
92 return node;
95 /* If the user *does* call delete, that's OK, we will just
96 * talloc_free in that case. */
97 static void operator delete(void *node)
99 talloc_free(node);
102 exec_node() : next(NULL), prev(NULL)
104 /* empty */
107 const exec_node *get_next() const
109 return next;
112 exec_node *get_next()
114 return next;
117 const exec_node *get_prev() const
119 return prev;
122 exec_node *get_prev()
124 return prev;
127 void remove()
129 next->prev = prev;
130 prev->next = next;
131 next = NULL;
132 prev = NULL;
136 * Link a node with itself
138 * This creates a sort of degenerate list that is occasionally useful.
140 void self_link()
142 next = this;
143 prev = this;
147 * Insert a node in the list after the current node
149 void insert_after(exec_node *after)
151 after->next = this->next;
152 after->prev = this;
154 this->next->prev = after;
155 this->next = after;
158 * Insert a node in the list before the current node
160 void insert_before(exec_node *before)
162 before->next = this;
163 before->prev = this->prev;
165 this->prev->next = before;
166 this->prev = before;
170 * Insert another list in the list before the current node
172 void insert_before(struct exec_list *before);
175 * Replace the current node with the given node.
177 void replace_with(exec_node *replacement)
179 replacement->prev = this->prev;
180 replacement->next = this->next;
182 this->prev->next = replacement;
183 this->next->prev = replacement;
187 * Is this the sentinel at the tail of the list?
189 bool is_tail_sentinel() const
191 return this->next == NULL;
195 * Is this the sentinel at the head of the list?
197 bool is_head_sentinel() const
199 return this->prev == NULL;
201 #endif
205 #ifdef __cplusplus
206 /* This macro will not work correctly if `t' uses virtual inheritance. If you
207 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
209 #define exec_list_offsetof(t, f, p) \
210 (((char *) &((t *) p)->f) - ((char *) p))
211 #else
212 #define exec_list_offsetof(t, f, p) offsetof(t, f)
213 #endif
216 * Get a pointer to the structure containing an exec_node
218 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
219 * the containing structure.
221 * \param type Base type of the structure containing the node
222 * \param node Pointer to the \c exec_node
223 * \param field Name of the field in \c type that is the embedded \c exec_node
225 #define exec_node_data(type, node, field) \
226 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
228 #ifdef __cplusplus
229 struct exec_node;
231 class iterator {
232 public:
233 void next()
237 void *get()
239 return NULL;
242 bool has_next() const
244 return false;
248 class exec_list_iterator : public iterator {
249 public:
250 exec_list_iterator(exec_node *n) : node(n), _next(n->next)
252 /* empty */
255 void next()
257 node = _next;
258 _next = node->next;
261 void remove()
263 node->remove();
266 exec_node *get()
268 return node;
271 bool has_next() const
273 return _next != NULL;
276 private:
277 exec_node *node;
278 exec_node *_next;
281 #define foreach_iter(iter_type, iter, container) \
282 for (iter_type iter = (container) . iterator(); iter.has_next(); iter.next())
283 #endif
286 struct exec_list {
287 struct exec_node *head;
288 struct exec_node *tail;
289 struct exec_node *tail_pred;
291 #ifdef __cplusplus
292 /* Callers of this talloc-based new need not call delete. It's
293 * easier to just talloc_free 'ctx' (or any of its ancestors). */
294 static void* operator new(size_t size, void *ctx)
296 void *node;
298 node = talloc_size(ctx, size);
299 assert(node != NULL);
301 return node;
304 /* If the user *does* call delete, that's OK, we will just
305 * talloc_free in that case. */
306 static void operator delete(void *node)
308 talloc_free(node);
311 exec_list()
313 make_empty();
316 void make_empty()
318 head = (exec_node *) & tail;
319 tail = NULL;
320 tail_pred = (exec_node *) & head;
323 bool is_empty() const
325 /* There are three ways to test whether a list is empty or not.
327 * - Check to see if the \c head points to the \c tail.
328 * - Check to see if the \c tail_pred points to the \c head.
329 * - Check to see if the \c head is the sentinel node by test whether its
330 * \c next pointer is \c NULL.
332 * The first two methods tend to generate better code on modern systems
333 * because they save a pointer dereference.
335 return head == (exec_node *) &tail;
338 const exec_node *get_head() const
340 return !is_empty() ? head : NULL;
343 exec_node *get_head()
345 return !is_empty() ? head : NULL;
348 const exec_node *get_tail() const
350 return !is_empty() ? tail_pred : NULL;
353 exec_node *get_tail()
355 return !is_empty() ? tail_pred : NULL;
358 void push_head(exec_node *n)
360 n->next = head;
361 n->prev = (exec_node *) &head;
363 n->next->prev = n;
364 head = n;
367 void push_tail(exec_node *n)
369 n->next = (exec_node *) &tail;
370 n->prev = tail_pred;
372 n->prev->next = n;
373 tail_pred = n;
376 void push_degenerate_list_at_head(exec_node *n)
378 assert(n->prev->next == n);
380 n->prev->next = head;
381 head->prev = n->prev;
382 n->prev = (exec_node *) &head;
383 head = n;
387 * Remove the first node from a list and return it
389 * \return
390 * The first node in the list or \c NULL if the list is empty.
392 * \sa exec_list::get_head
394 exec_node *pop_head()
396 exec_node *const n = this->get_head();
397 if (n != NULL)
398 n->remove();
400 return n;
404 * Move all of the nodes from this list to the target list
406 void move_nodes_to(exec_list *target)
408 if (is_empty()) {
409 target->make_empty();
410 } else {
411 target->head = head;
412 target->tail = NULL;
413 target->tail_pred = tail_pred;
415 target->head->prev = (exec_node *) &target->head;
416 target->tail_pred->next = (exec_node *) &target->tail;
418 make_empty();
423 * Append all nodes from the source list to the target list
425 void
426 append_list(exec_list *source)
428 if (source->is_empty())
429 return;
431 /* Link the first node of the source with the last node of the target list.
433 this->tail_pred->next = source->head;
434 source->head->prev = this->tail_pred;
436 /* Make the tail of the source list be the tail of the target list.
438 this->tail_pred = source->tail_pred;
439 this->tail_pred->next = (exec_node *) &this->tail;
441 /* Make the source list empty for good measure.
443 source->make_empty();
446 exec_list_iterator iterator()
448 return exec_list_iterator(head);
451 exec_list_iterator iterator() const
453 return exec_list_iterator((exec_node *) head);
455 #endif
459 #ifdef __cplusplus
460 inline void exec_node::insert_before(exec_list *before)
462 if (before->is_empty())
463 return;
465 before->tail_pred->next = this;
466 before->head->prev = this->prev;
468 this->prev->next = before->head;
469 this->prev = before->tail_pred;
471 before->make_empty();
473 #endif
476 * This version is safe even if the current node is removed.
478 #define foreach_list_safe(__node, __list) \
479 for (exec_node * __node = (__list)->head, * __next = __node->next \
480 ; __next != NULL \
481 ; __node = __next, __next = __next->next)
483 #define foreach_list(__node, __list) \
484 for (exec_node * __node = (__list)->head \
485 ; (__node)->next != NULL \
486 ; (__node) = (__node)->next)
488 #define foreach_list_const(__node, __list) \
489 for (const exec_node * __node = (__list)->head \
490 ; (__node)->next != NULL \
491 ; (__node) = (__node)->next)
493 #define foreach_list_typed(__type, __node, __field, __list) \
494 for (__type * __node = \
495 exec_node_data(__type, (__list)->head, __field); \
496 (__node)->__field.next != NULL; \
497 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
499 #define foreach_list_typed_const(__type, __node, __field, __list) \
500 for (const __type * __node = \
501 exec_node_data(__type, (__list)->head, __field); \
502 (__node)->__field.next != NULL; \
503 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
505 #endif /* LIST_CONTAINER_H */