Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / libcfs / include / libcfs / list.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __LIBCFS_LIST_H__
38 #define __LIBCFS_LIST_H__
39
40 #if defined (__linux__) && defined(__KERNEL__)
41
42 #include <linux/list.h>
43
44 #define CFS_LIST_HEAD_INIT(n)           LIST_HEAD_INIT(n)
45 #define CFS_LIST_HEAD(n)                LIST_HEAD(n)
46 #define CFS_INIT_LIST_HEAD(p)           INIT_LIST_HEAD(p)
47
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
49 #define CFS_HLIST_HEAD_INIT             HLIST_HEAD_INIT
50 #define CFS_HLIST_HEAD(n)               HLIST_HEAD(n)
51 #define CFS_INIT_HLIST_HEAD(p)          INIT_HLIST_HEAD(p)
52 #define CFS_INIT_HLIST_NODE(p)          INIT_HLIST_NODE(p)
53 #endif
54
55 #else /* !defined (__linux__) || !defined(__KERNEL__) */
56
57 /*
58  * Simple doubly linked list implementation.
59  *
60  * Some of the internal functions ("__xxx") are useful when
61  * manipulating whole lists rather than single entries, as
62  * sometimes we already know the next/prev entries and we can
63  * generate better code by using them directly rather than
64  * using the generic single-entry routines.
65  */
66
67 #ifndef __WINNT__
68 #define prefetch(a) ((void)a)
69 #else
70 #define prefetch(a) ((void *)a)
71 #endif
72
73 struct list_head {
74         struct list_head *next, *prev;
75 };
76
77 typedef struct list_head list_t;
78
79 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
80
81 #define CFS_LIST_HEAD(name) \
82         struct list_head name = CFS_LIST_HEAD_INIT(name)
83
84 #define CFS_INIT_LIST_HEAD(ptr) do { \
85         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
86 } while (0)
87
88 /*
89  * Insert a new entry between two known consecutive entries.
90  *
91  * This is only for internal list manipulation where we know
92  * the prev/next entries already!
93  */
94 static inline void __list_add(struct list_head * new,
95                               struct list_head * prev,
96                               struct list_head * next)
97 {
98         next->prev = new;
99         new->next = next;
100         new->prev = prev;
101         prev->next = new;
102 }
103
104 /**
105  * list_add - add a new entry
106  * @new: new entry to be added
107  * @head: list head to add it after
108  *
109  * Insert a new entry after the specified head.
110  * This is good for implementing stacks.
111  */
112 static inline void list_add(struct list_head *new, struct list_head *head)
113 {
114         __list_add(new, head, head->next);
115 }
116
117 /**
118  * list_add_tail - add a new entry
119  * @new: new entry to be added
120  * @head: list head to add it before
121  *
122  * Insert a new entry before the specified head.
123  * This is useful for implementing queues.
124  */
125 static inline void list_add_tail(struct list_head *new, struct list_head *head)
126 {
127         __list_add(new, head->prev, head);
128 }
129
130 /*
131  * Delete a list entry by making the prev/next entries
132  * point to each other.
133  *
134  * This is only for internal list manipulation where we know
135  * the prev/next entries already!
136  */
137 static inline void __list_del(struct list_head * prev, struct list_head * next)
138 {
139         next->prev = prev;
140         prev->next = next;
141 }
142
143 /**
144  * list_del - deletes entry from list.
145  * @entry: the element to delete from the list.
146  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
147  */
148 static inline void list_del(struct list_head *entry)
149 {
150         __list_del(entry->prev, entry->next);
151 }
152
153 /**
154  * list_del_init - deletes entry from list and reinitialize it.
155  * @entry: the element to delete from the list.
156  */
157 static inline void list_del_init(struct list_head *entry)
158 {
159         __list_del(entry->prev, entry->next);
160         CFS_INIT_LIST_HEAD(entry);
161 }
162
163 /**
164  * list_move - delete from one list and add as another's head
165  * @list: the entry to move
166  * @head: the head that will precede our entry
167  *
168  * This is not safe to use if @list is already on the same list as @head.
169  */
170 static inline void list_move(struct list_head *list, struct list_head *head)
171 {
172         __list_del(list->prev, list->next);
173         list_add(list, head);
174 }
175
176 /**
177  * list_move_tail - delete from one list and add as another's tail
178  * @list: the entry to move
179  * @head: the head that will follow our entry
180  *
181  * This is not safe to use if @list is already on the same list as @head.
182  */
183 static inline void list_move_tail(struct list_head *list,
184                                   struct list_head *head)
185 {
186         __list_del(list->prev, list->next);
187         list_add_tail(list, head);
188 }
189
190 /**
191  * list_empty - tests whether a list is empty
192  * @head: the list to test.
193  */
194 static inline int list_empty(struct list_head *head)
195 {
196         return head->next == head;
197 }
198
199 static inline void __list_splice(struct list_head *list,
200                                  struct list_head *head)
201 {
202         struct list_head *first = list->next;
203         struct list_head *last = list->prev;
204         struct list_head *at = head->next;
205
206         first->prev = head;
207         head->next = first;
208
209         last->next = at;
210         at->prev = last;
211 }
212
213 /**
214  * list_splice - join two lists
215  * @list: the new list to add.
216  * @head: the place to add it in the first list.
217  */
218 static inline void list_splice(struct list_head *list, struct list_head *head)
219 {
220         if (!list_empty(list))
221                 __list_splice(list, head);
222 }
223
224 /**
225  * list_splice_init - join two lists and reinitialise the emptied list.
226  * @list: the new list to add.
227  * @head: the place to add it in the first list.
228  *
229  * The list at @list is reinitialised
230  */
231 static inline void list_splice_init(struct list_head *list,
232                                     struct list_head *head)
233 {
234         if (!list_empty(list)) {
235                 __list_splice(list, head);
236                 CFS_INIT_LIST_HEAD(list);
237         }
238 }
239
240 /**
241  * list_entry - get the struct for this entry
242  * @ptr:        the &struct list_head pointer.
243  * @type:       the type of the struct this is embedded in.
244  * @member:     the name of the list_struct within the struct.
245  */
246 #define list_entry(ptr, type, member) \
247         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
248
249 /**
250  * list_for_each        -       iterate over a list
251  * @pos:        the &struct list_head to use as a loop counter.
252  * @head:       the head for your list.
253  */
254 #define list_for_each(pos, head) \
255         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
256                 pos = pos->next, prefetch(pos->next))
257
258 /**
259  * list_for_each_safe   -       iterate over a list safe against removal of list entry
260  * @pos:        the &struct list_head to use as a loop counter.
261  * @n:          another &struct list_head to use as temporary storage
262  * @head:       the head for your list.
263  */
264 #define list_for_each_safe(pos, n, head) \
265         for (pos = (head)->next, n = pos->next; pos != (head); \
266                 pos = n, n = pos->next)
267
268 /*
269  * Double linked lists with a single pointer list head.
270  * Mostly useful for hash tables where the two pointer list head is
271  * too wasteful.
272  * You lose the ability to access the tail in O(1).
273  */
274
275 struct hlist_head {
276         struct hlist_node *first;
277 };
278
279 struct hlist_node {
280         struct hlist_node *next, **pprev;
281 };
282
283 /*
284  * "NULL" might not be defined at this point
285  */
286 #ifdef NULL
287 #define NULL_P NULL
288 #else
289 #define NULL_P ((void *)0)
290 #endif
291
292 #define CFS_HLIST_HEAD_INIT { .first = NULL_P }
293 #define CFS_HLIST_HEAD(name) struct hlist_head name = {  .first = NULL_P }
294 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
295 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
296
297 #define HLIST_HEAD_INIT         CFS_HLIST_HEAD_INIT
298 #define HLIST_HEAD(n)           CFS_HLIST_HEAD(n)
299 #define INIT_HLIST_HEAD(p)      CFS_INIT_HLIST_HEAD(p)
300 #define INIT_HLIST_NODE(p)      CFS_INIT_HLIST_NODE(p)
301
302 static inline int hlist_unhashed(const struct hlist_node *h)
303 {
304         return !h->pprev;
305 }
306
307 static inline int hlist_empty(const struct hlist_head *h)
308 {
309         return !h->first;
310 }
311
312 static inline void __hlist_del(struct hlist_node *n)
313 {
314         struct hlist_node *next = n->next;
315         struct hlist_node **pprev = n->pprev;
316         *pprev = next;
317         if (next)
318                 next->pprev = pprev;
319 }
320
321 static inline void hlist_del(struct hlist_node *n)
322 {
323         __hlist_del(n);
324 }
325
326 static inline void hlist_del_init(struct hlist_node *n)
327 {
328         if (n->pprev)  {
329                 __hlist_del(n);
330                 INIT_HLIST_NODE(n);
331         }
332 }
333
334 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
335 {
336         struct hlist_node *first = h->first;
337         n->next = first;
338         if (first)
339                 first->pprev = &n->next;
340         h->first = n;
341         n->pprev = &h->first;
342 }
343
344 /* next must be != NULL */
345 static inline void hlist_add_before(struct hlist_node *n,
346                                         struct hlist_node *next)
347 {
348         n->pprev = next->pprev;
349         n->next = next;
350         next->pprev = &n->next;
351         *(n->pprev) = n;
352 }
353
354 static inline void hlist_add_after(struct hlist_node *n,
355                                         struct hlist_node *next)
356 {
357         next->next = n->next;
358         n->next = next;
359         next->pprev = &n->next;
360
361         if(next->next)
362                 next->next->pprev  = &next->next;
363 }
364
365 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
366
367 #define hlist_for_each(pos, head) \
368         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
369              pos = pos->next)
370
371 #define hlist_for_each_safe(pos, n, head) \
372         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
373              pos = n)
374
375 /**
376  * hlist_for_each_entry - iterate over list of given type
377  * @tpos:       the type * to use as a loop counter.
378  * @pos:        the &struct hlist_node to use as a loop counter.
379  * @head:       the head for your list.
380  * @member:     the name of the hlist_node within the struct.
381  */
382 #define hlist_for_each_entry(tpos, pos, head, member)                    \
383         for (pos = (head)->first;                                        \
384              pos && ({ prefetch(pos->next); 1;}) &&                      \
385                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
386              pos = pos->next)
387
388 /**
389  * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
390  * @tpos:       the type * to use as a loop counter.
391  * @pos:        the &struct hlist_node to use as a loop counter.
392  * @member:     the name of the hlist_node within the struct.
393  */
394 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
395         for (pos = (pos)->next;                                          \
396              pos && ({ prefetch(pos->next); 1;}) &&                      \
397                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
398              pos = pos->next)
399
400 /**
401  * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
402  * @tpos:       the type * to use as a loop counter.
403  * @pos:        the &struct hlist_node to use as a loop counter.
404  * @member:     the name of the hlist_node within the struct.
405  */
406 #define hlist_for_each_entry_from(tpos, pos, member)                     \
407         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
408                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
409              pos = pos->next)
410
411 /**
412  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
413  * @tpos:       the type * to use as a loop counter.
414  * @pos:        the &struct hlist_node to use as a loop counter.
415  * @n:          another &struct hlist_node to use as temporary storage
416  * @head:       the head for your list.
417  * @member:     the name of the hlist_node within the struct.
418  */
419 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
420         for (pos = (head)->first;                                        \
421              pos && ({ n = pos->next; 1; }) &&                           \
422                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
423              pos = n)
424
425 #endif /* __linux__ && __KERNEL__ */
426
427 #ifndef list_for_each_prev
428 /**
429  * list_for_each_prev   -       iterate over a list in reverse order
430  * @pos:        the &struct list_head to use as a loop counter.
431  * @head:       the head for your list.
432  */
433 #define list_for_each_prev(pos, head) \
434         for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
435                 pos = pos->prev, prefetch(pos->prev))
436
437 #endif /* list_for_each_prev */
438
439 #ifndef list_for_each_entry
440 /**
441  * list_for_each_entry  -       iterate over list of given type
442  * @pos:        the type * to use as a loop counter.
443  * @head:       the head for your list.
444  * @member:     the name of the list_struct within the struct.
445  */
446 #define list_for_each_entry(pos, head, member)                          \
447         for (pos = list_entry((head)->next, typeof(*pos), member),      \
448                      prefetch(pos->member.next);                        \
449              &pos->member != (head);                                    \
450              pos = list_entry(pos->member.next, typeof(*pos), member),  \
451              prefetch(pos->member.next))
452 #endif /* list_for_each_entry */
453
454 #ifndef list_for_each_entry_reverse
455 /**
456  * list_for_each_entry_reverse - iterate backwards over list of given type.
457  * @pos:        the type * to use as a loop counter.
458  * @head:       the head for your list.
459  * @member:     the name of the list_struct within the struct.
460  */
461 #define list_for_each_entry_reverse(pos, head, member)                  \
462         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
463              prefetch(pos->member.prev), &pos->member != (head);        \
464              pos = list_entry(pos->member.prev, typeof(*pos), member))
465 #endif /* list_for_each_entry_reverse */
466
467 #ifndef list_for_each_entry_safe
468 /**
469  * list_for_each_entry_safe  -       iterate over list of given type safe against removal of list entry
470  * @pos:        the type * to use as a loop counter.
471  * @n:          another type * to use as temporary storage
472  * @head:       the head for your list.
473  * @member:     the name of the list_struct within the struct.
474  */
475 #define list_for_each_entry_safe(pos, n, head, member)                  \
476         for (pos = list_entry((head)->next, typeof(*pos), member),      \
477                 n = list_entry(pos->member.next, typeof(*pos), member); \
478              &pos->member != (head);                                    \
479              pos = n, n = list_entry(n->member.next, typeof(*n), member))
480 #endif /* list_for_each_entry_safe */
481
482 #ifndef list_for_each_entry_safe_from
483 /**
484  * list_for_each_entry_safe_from
485  * @pos:        the type * to use as a loop cursor.
486  * @n:          another type * to use as temporary storage
487  * @head:       the head for your list.
488  * @member:     the name of the list_struct within the struct.
489  *
490  * Iterate over list of given type from current point, safe against
491  * removal of list entry.
492  */
493 #define list_for_each_entry_safe_from(pos, n, head, member)                 \
494         for (n = list_entry(pos->member.next, typeof(*pos), member);        \
495              &pos->member != (head);                                        \
496              pos = n, n = list_entry(n->member.next, typeof(*n), member))
497 #endif /* list_for_each_entry_safe_from */
498
499 #endif /* __LIBCFS_LUSTRE_LIST_H__ */