OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / pthread / list.h
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #ifndef _LIST_H
20 #define _LIST_H 1
21
22 /* The definitions of this file are adopted from those which can be
23    found in the Linux kernel headers to enable people familiar with
24    the latter find their way in these sources as well.  */
25
26
27 /* Basic type for the double-link list.  */
28 typedef struct list_head
29 {
30   struct list_head *next;
31   struct list_head *prev;
32 } list_t;
33
34
35 /* Define a variable with the head and tail of the list.  */
36 #define LIST_HEAD(name) \
37   list_t name = { &(name), &(name) }
38
39 /* Initialize a new list head.  */
40 #define INIT_LIST_HEAD(ptr) \
41   (ptr)->next = (ptr)->prev = (ptr)
42
43
44 /* Add new element at the head of the list.  */
45 static __inline__ void
46 list_add (list_t *newp, list_t *head)
47 {
48   head->next->prev = newp;
49   newp->next = head->next;
50   newp->prev = head;
51   head->next = newp;
52 }
53
54
55 /* Add new element at the tail of the list.  */
56 static __inline__ void
57 list_add_tail (list_t *newp, list_t *head)
58 {
59   head->prev->next = newp;
60   newp->next = head;
61   newp->prev = head->prev;
62   head->prev = newp;
63 }
64
65
66 /* Remove element from list.  */
67 static __inline__ void
68 list_del (list_t *elem)
69 {
70   elem->next->prev = elem->prev;
71   elem->prev->next = elem->next;
72 }
73
74
75 /* Join two lists.  */
76 static __inline__ void
77 list_splice (list_t *add, list_t *head)
78 {
79   /* Do nothing if the list which gets added is empty.  */
80   if (add != add->next)
81     {
82       add->next->prev = head;
83       add->prev->next = head->next;
84       head->next->prev = add->prev;
85       head->next = add->next;
86     }
87 }
88
89
90 /* Get typed element from list at a given position.  */
91 #define list_entry(ptr, type, member) \
92   ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
93
94
95
96 /* Iterate forward over the elements of the list.  */
97 #define list_for_each(pos, head) \
98   for (pos = (head)->next; pos != (head); pos = pos->next)
99
100
101 /* Iterate forward over the elements of the list.  */
102 #define list_for_each_prev(pos, head) \
103   for (pos = (head)->prev; pos != (head); pos = pos->prev)
104
105
106 /* Iterate backwards over the elements list.  The list elements can be
107    removed from the list while doing this.  */
108 #define list_for_each_prev_safe(pos, p, head) \
109   for (pos = (head)->prev, p = pos->prev; \
110        pos != (head); \
111        pos = p, p = pos->prev)
112
113 #endif  /* list.h */