glsl: Put `sample`-qualified varyings in their own packing classes
[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 {
88 return next;
89 }
90
91 exec_node *get_next()
92 {
93 return next;
94 }
95
96 const exec_node *get_prev() const
97 {
98 return prev;
99 }
100
101 exec_node *get_prev()
102 {
103 return prev;
104 }
105
106 void remove()
107 {
108 next->prev = prev;
109 prev->next = next;
110 next = NULL;
111 prev = NULL;
112 }
113
114 /**
115 * Link a node with itself
116 *
117 * This creates a sort of degenerate list that is occasionally useful.
118 */
119 void self_link()
120 {
121 next = this;
122 prev = this;
123 }
124
125 /**
126 * Insert a node in the list after the current node
127 */
128 void insert_after(exec_node *after)
129 {
130 after->next = this->next;
131 after->prev = this;
132
133 this->next->prev = after;
134 this->next = after;
135 }
136 /**
137 * Insert a node in the list before the current node
138 */
139 void insert_before(exec_node *before)
140 {
141 before->next = this;
142 before->prev = this->prev;
143
144 this->prev->next = before;
145 this->prev = before;
146 }
147
148 /**
149 * Insert another list in the list before the current node
150 */
151 void insert_before(struct exec_list *before);
152
153 /**
154 * Replace the current node with the given node.
155 */
156 void replace_with(exec_node *replacement)
157 {
158 replacement->prev = this->prev;
159 replacement->next = this->next;
160
161 this->prev->next = replacement;
162 this->next->prev = replacement;
163 }
164
165 /**
166 * Is this the sentinel at the tail of the list?
167 */
168 bool is_tail_sentinel() const
169 {
170 return this->next == NULL;
171 }
172
173 /**
174 * Is this the sentinel at the head of the list?
175 */
176 bool is_head_sentinel() const
177 {
178 return this->prev == NULL;
179 }
180 #endif
181 };
182
183
184 #ifdef __cplusplus
185 /* This macro will not work correctly if `t' uses virtual inheritance. If you
186 * are using virtual inheritance, you deserve a slow and painful death. Enjoy!
187 */
188 #define exec_list_offsetof(t, f, p) \
189 (((char *) &((t *) p)->f) - ((char *) p))
190 #else
191 #define exec_list_offsetof(t, f, p) offsetof(t, f)
192 #endif
193
194 /**
195 * Get a pointer to the structure containing an exec_node
196 *
197 * Given a pointer to an \c exec_node embedded in a structure, get a pointer to
198 * the containing structure.
199 *
200 * \param type Base type of the structure containing the node
201 * \param node Pointer to the \c exec_node
202 * \param field Name of the field in \c type that is the embedded \c exec_node
203 */
204 #define exec_node_data(type, node, field) \
205 ((type *) (((char *) node) - exec_list_offsetof(type, field, node)))
206
207 #ifdef __cplusplus
208 struct exec_node;
209
210 class iterator {
211 public:
212 void next()
213 {
214 }
215
216 void *get()
217 {
218 return NULL;
219 }
220
221 bool has_next() const
222 {
223 return false;
224 }
225 };
226
227 class exec_list_iterator : public iterator {
228 public:
229 exec_list_iterator(exec_node *n) : node(n), _next(n->next)
230 {
231 /* empty */
232 }
233
234 void next()
235 {
236 node = _next;
237 _next = node->next;
238 }
239
240 void remove()
241 {
242 node->remove();
243 }
244
245 exec_node *get()
246 {
247 return node;
248 }
249
250 bool has_next() const
251 {
252 return _next != NULL;
253 }
254
255 private:
256 exec_node *node;
257 exec_node *_next;
258 };
259
260 #define foreach_iter(iter_type, iter, container) \
261 for (iter_type iter = (container) . iterator(); iter.has_next(); iter.next())
262 #endif
263
264
265 struct exec_list {
266 struct exec_node *head;
267 struct exec_node *tail;
268 struct exec_node *tail_pred;
269
270 #ifdef __cplusplus
271 DECLARE_RALLOC_CXX_OPERATORS(exec_list)
272
273 exec_list()
274 {
275 make_empty();
276 }
277
278 void make_empty()
279 {
280 head = (exec_node *) & tail;
281 tail = NULL;
282 tail_pred = (exec_node *) & head;
283 }
284
285 bool is_empty() const
286 {
287 /* There are three ways to test whether a list is empty or not.
288 *
289 * - Check to see if the \c head points to the \c tail.
290 * - Check to see if the \c tail_pred points to the \c head.
291 * - Check to see if the \c head is the sentinel node by test whether its
292 * \c next pointer is \c NULL.
293 *
294 * The first two methods tend to generate better code on modern systems
295 * because they save a pointer dereference.
296 */
297 return head == (exec_node *) &tail;
298 }
299
300 const exec_node *get_head() const
301 {
302 return !is_empty() ? head : NULL;
303 }
304
305 exec_node *get_head()
306 {
307 return !is_empty() ? head : NULL;
308 }
309
310 const exec_node *get_tail() const
311 {
312 return !is_empty() ? tail_pred : NULL;
313 }
314
315 exec_node *get_tail()
316 {
317 return !is_empty() ? tail_pred : NULL;
318 }
319
320 void push_head(exec_node *n)
321 {
322 n->next = head;
323 n->prev = (exec_node *) &head;
324
325 n->next->prev = n;
326 head = n;
327 }
328
329 void push_tail(exec_node *n)
330 {
331 n->next = (exec_node *) &tail;
332 n->prev = tail_pred;
333
334 n->prev->next = n;
335 tail_pred = n;
336 }
337
338 void push_degenerate_list_at_head(exec_node *n)
339 {
340 assert(n->prev->next == n);
341
342 n->prev->next = head;
343 head->prev = n->prev;
344 n->prev = (exec_node *) &head;
345 head = n;
346 }
347
348 /**
349 * Remove the first node from a list and return it
350 *
351 * \return
352 * The first node in the list or \c NULL if the list is empty.
353 *
354 * \sa exec_list::get_head
355 */
356 exec_node *pop_head()
357 {
358 exec_node *const n = this->get_head();
359 if (n != NULL)
360 n->remove();
361
362 return n;
363 }
364
365 /**
366 * Move all of the nodes from this list to the target list
367 */
368 void move_nodes_to(exec_list *target)
369 {
370 if (is_empty()) {
371 target->make_empty();
372 } else {
373 target->head = head;
374 target->tail = NULL;
375 target->tail_pred = tail_pred;
376
377 target->head->prev = (exec_node *) &target->head;
378 target->tail_pred->next = (exec_node *) &target->tail;
379
380 make_empty();
381 }
382 }
383
384 /**
385 * Append all nodes from the source list to the target list
386 */
387 void
388 append_list(exec_list *source)
389 {
390 if (source->is_empty())
391 return;
392
393 /* Link the first node of the source with the last node of the target list.
394 */
395 this->tail_pred->next = source->head;
396 source->head->prev = this->tail_pred;
397
398 /* Make the tail of the source list be the tail of the target list.
399 */
400 this->tail_pred = source->tail_pred;
401 this->tail_pred->next = (exec_node *) &this->tail;
402
403 /* Make the source list empty for good measure.
404 */
405 source->make_empty();
406 }
407
408 exec_list_iterator iterator()
409 {
410 return exec_list_iterator(head);
411 }
412
413 exec_list_iterator iterator() const
414 {
415 return exec_list_iterator((exec_node *) head);
416 }
417 #endif
418 };
419
420
421 #ifdef __cplusplus
422 inline void exec_node::insert_before(exec_list *before)
423 {
424 if (before->is_empty())
425 return;
426
427 before->tail_pred->next = this;
428 before->head->prev = this->prev;
429
430 this->prev->next = before->head;
431 this->prev = before->tail_pred;
432
433 before->make_empty();
434 }
435 #endif
436
437 /**
438 * This version is safe even if the current node is removed.
439 */
440 #define foreach_list_safe(__node, __list) \
441 for (exec_node * __node = (__list)->head, * __next = __node->next \
442 ; __next != NULL \
443 ; __node = __next, __next = __next->next)
444
445 #define foreach_list(__node, __list) \
446 for (exec_node * __node = (__list)->head \
447 ; (__node)->next != NULL \
448 ; (__node) = (__node)->next)
449
450 #define foreach_list_const(__node, __list) \
451 for (const exec_node * __node = (__list)->head \
452 ; (__node)->next != NULL \
453 ; (__node) = (__node)->next)
454
455 #define foreach_list_typed(__type, __node, __field, __list) \
456 for (__type * __node = \
457 exec_node_data(__type, (__list)->head, __field); \
458 (__node)->__field.next != NULL; \
459 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
460
461 #define foreach_list_typed_const(__type, __node, __field, __list) \
462 for (const __type * __node = \
463 exec_node_data(__type, (__list)->head, __field); \
464 (__node)->__field.next != NULL; \
465 (__node) = exec_node_data(__type, (__node)->__field.next, __field))
466
467 #endif /* LIST_CONTAINER_H */