Imported Upstream version 0.155
[platform/upstream/elfutils.git] / lib / list.h
1 /* Copyright (C) 2001, 2002, 2003 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2001.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11
12    or
13
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17
18    or both in parallel, as here.
19
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28
29 #ifndef LIST_H
30 #define LIST_H  1
31
32 /* Add element to the end of a circular, double-linked list.  */
33 #define CDBL_LIST_ADD_REAR(first, newp) \
34   do {                                                                        \
35     __typeof (newp) _newp = (newp);                                           \
36     assert (_newp->next == NULL);                                             \
37     assert (_newp->previous == NULL);                                         \
38     if (unlikely ((first) == NULL))                                           \
39       (first) = _newp->next = _newp->previous = _newp;                        \
40     else                                                                      \
41       {                                                                       \
42         _newp->next = (first);                                                \
43         _newp->previous = (first)->previous;                                  \
44         _newp->previous->next = _newp->next->previous = _newp;                \
45       }                                                                       \
46   } while (0)
47
48 /* Remove element from circular, double-linked list.  */
49 #define CDBL_LIST_DEL(first, elem) \
50   do {                                                                        \
51     __typeof (elem) _elem = (elem);                                           \
52     /* Check whether the element is indeed on the list.  */                   \
53     assert (first != NULL && _elem != NULL                                    \
54             && (first != elem                                                 \
55                 || ({ __typeof (elem) _runp = first->next;                    \
56                       while (_runp != first)                                  \
57                         if (_runp == _elem)                                   \
58                           break;                                              \
59                         else                                                  \
60                           _runp = _runp->next;                                \
61                       _runp == _elem; })));                                   \
62     if (unlikely (_elem->next == _elem))                                      \
63       first = NULL;                                                           \
64     else                                                                      \
65       {                                                                       \
66         _elem->next->previous = _elem->previous;                              \
67         _elem->previous->next = _elem->next;                                  \
68         if (unlikely (first == _elem))                                        \
69           first = _elem->next;                                                \
70       }                                                                       \
71      assert ((_elem->next = _elem->previous = NULL, 1));                      \
72   } while (0)
73
74
75 /* Add element to the front of a single-linked list.  */
76 #define SNGL_LIST_PUSH(first, newp) \
77   do {                                                                        \
78     __typeof (newp) _newp = (newp);                                           \
79     assert (_newp->next == NULL);                                             \
80     _newp->next = first;                                                      \
81     first = _newp;                                                            \
82   } while (0)
83
84
85 /* Add element to the rear of a circular single-linked list.  */
86 #define CSNGL_LIST_ADD_REAR(first, newp) \
87   do {                                                                        \
88     __typeof (newp) _newp = (newp);                                           \
89     assert (_newp->next == NULL);                                             \
90     if (unlikely ((first) == NULL))                                           \
91       (first) = _newp->next = _newp;                                          \
92     else                                                                      \
93       {                                                                       \
94         _newp->next = (first)->next;                                          \
95         (first) = (first)->next = _newp;                                      \
96       }                                                                       \
97   } while (0)
98
99
100 #endif  /* list.h */