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  * struct list_head - Simple doubly linked list implementation.
  27  *
  28  * Copied from Linux kernel 2.4.2-ac18 into Linux-NTFS (with minor
  29  * modifications). - AIA
  30  *
  31  * Some of the internal functions ("__xxx") are useful when
  32  * manipulating whole lists rather than single entries, as
  33  * sometimes we already know the next/prev entries and we can
  34  * generate better code by using them directly rather than
  35  * using the generic single-entry routines.
  36  */
  37 struct list_head {
  38         struct list_head *next, *prev;
  39 };
  40 
  41 #define LIST_HEAD_INIT(name) { &(name), &(name) }
  42 
  43 #define LIST_HEAD(name) \
  44         struct list_head name = LIST_HEAD_INIT(name)
  45 
  46 #define INIT_LIST_HEAD(ptr) do { \
  47         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  48 } while (0)
  49 
  50 /**
  51  * __list_add - Insert a new entry between two known consecutive entries.
  52  * @new:
  53  * @prev:
  54  * @next:
  55  *
  56  * This is only for internal list manipulation where we know the prev/next
  57  * entries already!
  58  */
  59 static __inline__ void __list_add(struct list_head * new,
  60                 struct list_head * prev, struct list_head * next)
  61 {
  62         next->prev = new;
  63         new->next = next;
  64         new->prev = prev;
  65         prev->next = new;
  66 }
  67 
  68 /**
  69  * list_add - add a new entry
  70  * @new:        new entry to be added
  71  * @head:       list head to add it after
  72  *
  73  * Insert a new entry after the specified head.
  74  * This is good for implementing stacks.
  75  */
  76 static __inline__ void list_add(struct list_head *new, struct list_head *head)
  77 {
  78         __list_add(new, head, head->next);
  79 }
  80 
  81 /**
  82  * list_add_tail - add a new entry
  83  * @new:        new entry to be added
  84  * @head:       list head to add it before
  85  *
  86  * Insert a new entry before the specified head.
  87  * This is useful for implementing queues.
  88  */
  89 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
  90 {
  91         __list_add(new, head->prev, head);
  92 }
  93 
  94 /**
  95  * __list_del -
  96  * @prev:
  97  * @next:
  98  *
  99  * Delete a list entry by making the prev/next entries point to each other.
 100  *
 101  * This is only for internal list manipulation where we know the prev/next
 102  * entries already!
 103  */
 104 static __inline__ void __list_del(struct list_head * prev,
 105                 struct list_head * next)
 106 {
 107         next->prev = prev;
 108         prev->next = next;
 109 }
 110 
 111 /**
 112  * list_del - deletes entry from list.
 113  * @entry:      the element to delete from the list.
 114  *
 115  * Note: list_empty on entry does not return true after this, the entry is in
 116  * an undefined state.
 117  */
 118 static __inline__ void list_del(struct list_head *entry)
 119 {
 120         __list_del(entry->prev, entry->next);
 121 }
 122 
 123 /**
 124  * list_del_init - deletes entry from list and reinitialize it.
 125  * @entry:      the element to delete from the list.
 126  */
 127 static __inline__ void list_del_init(struct list_head *entry)
 128 {
 129         __list_del(entry->prev, entry->next);
 130         INIT_LIST_HEAD(entry);
 131 }
 132 
 133 /**
 134  * list_empty - tests whether a list is empty
 135  * @head:       the list to test.
 136  */
 137 static __inline__ int list_empty(struct list_head *head)
 138 {
 139         return head->next == head;
 140 }
 141 
 142 /**
 143  * list_splice - join two lists
 144  * @list:       the new list to add.
 145  * @head:       the place to add it in the first list.
 146  */
 147 static __inline__ void list_splice(struct list_head *list,
 148                 struct list_head *head)
 149 {
 150         struct list_head *first = list->next;
 151 
 152         if (first != list) {
 153                 struct list_head *last = list->prev;
 154                 struct list_head *at = head->next;
 155 
 156                 first->prev = head;
 157                 head->next = first;
 158 
 159                 last->next = at;
 160                 at->prev = last;
 161         }
 162 }
 163 
 164 /**
 165  * list_entry - get the struct for this entry
 166  * @ptr:        the &struct list_head pointer.
 167  * @type:       the type of the struct this is embedded in.
 168  * @member:     the name of the list_struct within the struct.
 169  */
 170 #define list_entry(ptr, type, member) \
 171         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
 172 
 173 /**
 174  * list_for_each - iterate over a list
 175  * @pos:        the &struct list_head to use as a loop counter.
 176  * @head:       the head for your list.
 177  */
 178 #define list_for_each(pos, head) \
 179         for (pos = (head)->next; pos != (head); pos = pos->next)
 180 
 181 /**
 182  * list_for_each_safe   -       iterate over a list safe against removal of list entry
 183  * @pos:        the &struct list_head to use as a loop counter.
 184  * @n:          another &struct list_head to use as temporary storage
 185  * @head:       the head for your list.
 186  */
 187 #define list_for_each_safe(pos, n, head) \
 188         for (pos = (head)->next, n = pos->next; pos != (head); \
 189                 pos = n, n = pos->next)
 190 
 191 #endif /* defined _NTFS_LIST_H */
 192