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