http://linux-ntfs.sourceforge.net/snapshots/ntfsprogs-200307311516.tar.bz2
[ntfsprogs.git] / include / list.h
1 /*
2  * list.h - Linked list implementation. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2000-2002 Anton Altaparmakov and others
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifndef _NTFS_LIST_H
23 #define _NTFS_LIST_H
24
25 /*
26  * Simple doubly linked list implementation. - Copied from Linux kernel
27  * 2.4.2-ac18 into Linux-NTFS (with minor modifications). - AIA
28  *
29  * Some of the internal functions ("__xxx") are useful when
30  * manipulating whole lists rather than single entries, as
31  * sometimes we already know the next/prev entries and we can
32  * generate better code by using them directly rather than
33  * using the generic single-entry routines.
34  */
35
36 struct list_head {
37         struct list_head *next, *prev;
38 };
39
40 #define LIST_HEAD_INIT(name) { &(name), &(name) }
41
42 #define LIST_HEAD(name) \
43         struct list_head name = LIST_HEAD_INIT(name)
44
45 #define INIT_LIST_HEAD(ptr) do { \
46         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
47 } while (0)
48
49 /*
50  * Insert a new entry between two known consecutive entries.
51  *
52  * This is only for internal list manipulation where we know the prev/next
53  * entries already!
54  */
55 static __inline__ void __list_add(struct list_head * new,
56                 struct list_head * prev, struct list_head * next)
57 {
58         next->prev = new;
59         new->next = next;
60         new->prev = prev;
61         prev->next = new;
62 }
63
64 /**
65  * list_add - add a new entry
66  * @new:        new entry to be added
67  * @head:       list head to add it after
68  *
69  * Insert a new entry after the specified head.
70  * This is good for implementing stacks.
71  */
72 static __inline__ void list_add(struct list_head *new, struct list_head *head)
73 {
74         __list_add(new, head, head->next);
75 }
76
77 /**
78  * list_add_tail - add a new entry
79  * @new:        new entry to be added
80  * @head:       list head to add it before
81  *
82  * Insert a new entry before the specified head.
83  * This is useful for implementing queues.
84  */
85 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
86 {
87         __list_add(new, head->prev, head);
88 }
89
90 /*
91  * Delete a list entry by making the prev/next entries point to each other.
92  *
93  * This is only for internal list manipulation where we know the prev/next
94  * entries already!
95  */
96 static __inline__ void __list_del(struct list_head * prev,
97                 struct list_head * next)
98 {
99         next->prev = prev;
100         prev->next = next;
101 }
102
103 /**
104  * list_del - deletes entry from list.
105  * @entry:      the element to delete from the list.
106  *
107  * Note: list_empty on entry does not return true after this, the entry is in
108  * an undefined state.
109  */
110 static __inline__ void list_del(struct list_head *entry)
111 {
112         __list_del(entry->prev, entry->next);
113 }
114
115 /**
116  * list_del_init - deletes entry from list and reinitialize it.
117  * @entry:      the element to delete from the list.
118  */
119 static __inline__ void list_del_init(struct list_head *entry)
120 {
121         __list_del(entry->prev, entry->next);
122         INIT_LIST_HEAD(entry);
123 }
124
125 /**
126  * list_empty - tests whether a list is empty
127  * @head:       the list to test.
128  */
129 static __inline__ int list_empty(struct list_head *head)
130 {
131         return head->next == head;
132 }
133
134 /**
135  * list_splice - join two lists
136  * @list:       the new list to add.
137  * @head:       the place to add it in the first list.
138  */
139 static __inline__ void list_splice(struct list_head *list,
140                 struct list_head *head)
141 {
142         struct list_head *first = list->next;
143
144         if (first != list) {
145                 struct list_head *last = list->prev;
146                 struct list_head *at = head->next;
147
148                 first->prev = head;
149                 head->next = first;
150
151                 last->next = at;
152                 at->prev = last;
153         }
154 }
155
156 /**
157  * list_entry - get the struct for this entry
158  * @ptr:        the &struct list_head pointer.
159  * @type:       the type of the struct this is embedded in.
160  * @member:     the name of the list_struct within the struct.
161  */
162 #define list_entry(ptr, type, member) \
163         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
164
165 /**
166  * list_for_each - iterate over a list
167  * @pos:        the &struct list_head to use as a loop counter.
168  * @head:       the head for your list.
169  */
170 #define list_for_each(pos, head) \
171         for (pos = (head)->next; pos != (head); pos = pos->next)
172
173 /**
174  * list_for_each_safe   -       iterate over a list safe against removal of list entry
175  * @pos:        the &struct list_head to use as a loop counter.
176  * @n:          another &struct list_head to use as temporary storage
177  * @head:       the head for your list.
178  */
179 #define list_for_each_safe(pos, n, head) \
180         for (pos = (head)->next, n = pos->next; pos != (head); \
181                 pos = n, n = pos->next)
182
183 #endif /* defined _NTFS_LIST_H */
184