]>
Commit | Line | Data |
---|---|---|
7a775e6b | 1 | /* Copyright (C) 2002, 2009, 2011 Free Software Foundation, Inc. |
76a50749 UD |
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, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
20 | #ifndef _LIST_H | |
7a775e6b AS |
21 | |
22 | #ifndef __need_list_t | |
23 | # define _LIST_H 1 | |
24 | #endif | |
76a50749 | 25 | |
76a50749 UD |
26 | /* The definitions of this file are adopted from those which can be |
27 | found in the Linux kernel headers to enable people familiar with | |
28 | the latter find their way in these sources as well. */ | |
29 | ||
30 | ||
7a775e6b AS |
31 | #if defined __need_list_t || defined _LIST_H |
32 | # ifndef __list_t_defined | |
33 | # define __list_t_defined | |
76a50749 UD |
34 | /* Basic type for the double-link list. */ |
35 | typedef struct list_head | |
36 | { | |
37 | struct list_head *next; | |
38 | struct list_head *prev; | |
39 | } list_t; | |
7a775e6b AS |
40 | # endif |
41 | # undef __need_list_t | |
42 | #endif | |
43 | ||
44 | #ifdef _LIST_H | |
76a50749 | 45 | |
7a775e6b | 46 | # include <atomic.h> |
76a50749 UD |
47 | |
48 | /* Define a variable with the head and tail of the list. */ | |
7a775e6b | 49 | # define LIST_HEAD(name) \ |
76a50749 UD |
50 | list_t name = { &(name), &(name) } |
51 | ||
52 | /* Initialize a new list head. */ | |
7a775e6b | 53 | # define INIT_LIST_HEAD(ptr) \ |
76a50749 UD |
54 | (ptr)->next = (ptr)->prev = (ptr) |
55 | ||
56 | ||
57 | /* Add new element at the head of the list. */ | |
58 | static inline void | |
59 | list_add (list_t *newp, list_t *head) | |
60 | { | |
76a50749 UD |
61 | newp->next = head->next; |
62 | newp->prev = head; | |
f25c7b08 | 63 | head->next->prev = newp; |
7a775e6b | 64 | atomic_write_barrier (); |
76a50749 UD |
65 | head->next = newp; |
66 | } | |
67 | ||
68 | ||
76a50749 UD |
69 | /* Remove element from list. */ |
70 | static inline void | |
71 | list_del (list_t *elem) | |
72 | { | |
73 | elem->next->prev = elem->prev; | |
74 | elem->prev->next = elem->next; | |
75 | } | |
76 | ||
77 | ||
78 | /* Join two lists. */ | |
79 | static inline void | |
80 | list_splice (list_t *add, list_t *head) | |
81 | { | |
82 | /* Do nothing if the list which gets added is empty. */ | |
83 | if (add != add->next) | |
84 | { | |
85 | add->next->prev = head; | |
86 | add->prev->next = head->next; | |
87 | head->next->prev = add->prev; | |
88 | head->next = add->next; | |
89 | } | |
90 | } | |
91 | ||
92 | ||
93 | /* Get typed element from list at a given position. */ | |
7a775e6b | 94 | # define list_entry(ptr, type, member) \ |
76a50749 UD |
95 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
96 | ||
97 | ||
98 | ||
99 | /* Iterate forward over the elements of the list. */ | |
7a775e6b | 100 | # define list_for_each(pos, head) \ |
76a50749 UD |
101 | for (pos = (head)->next; pos != (head); pos = pos->next) |
102 | ||
103 | ||
104 | /* Iterate forward over the elements of the list. */ | |
7a775e6b | 105 | # define list_for_each_prev(pos, head) \ |
76a50749 UD |
106 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
107 | ||
108 | ||
109 | /* Iterate backwards over the elements list. The list elements can be | |
110 | removed from the list while doing this. */ | |
7a775e6b | 111 | # define list_for_each_prev_safe(pos, p, head) \ |
76a50749 UD |
112 | for (pos = (head)->prev, p = pos->prev; \ |
113 | pos != (head); \ | |
114 | pos = p, p = pos->prev) | |
115 | ||
7a775e6b AS |
116 | #endif /* _LIST_H */ |
117 | ||
76a50749 | 118 | #endif /* list.h */ |