]> arthur.barton.de Git - netatalk.git/blob - include/atalk/list.h
replace remaining %m printf glibc extension with %s strerror(errno)
[netatalk.git] / include / atalk / list.h
1 /* This code has been stolen from Linux kernel :) */
2 /* distributed under GNU GPL version 2. */
3
4 #ifndef _ATALK_LIST_H
5 #define _ATALK_LIST_H
6
7 /* test for inline */
8 #ifndef __inline__
9 #define __inline__
10 #endif
11
12 /*
13  * Simple doubly linked list implementation.
14  *
15  * Some of the internal functions ("__xxx") are useful when
16  * manipulating whole lists rather than single entries, as
17  * sometimes we already know the next/prev entries and we can
18  * generate better code by using them directly rather than
19  * using the generic single-entry routines.
20  */
21
22 struct list_head {
23     struct list_head *next, *prev;
24 };
25
26 #define ATALK_LIST_HEAD_INIT(name) { &(name), &(name) }
27
28 #define ATALK_LIST_HEAD(name) \
29     struct list_head name = ATALK_LIST_HEAD_INIT(name)
30
31 #define ATALK_INIT_LIST_HEAD(ptr) do { \
32     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
33 } while (0)
34
35 #ifdef USE_LIST
36 /*
37  * Insert a new entry between two known consecutive entries. 
38  *
39  * This is only for internal list manipulation where we know
40  * the prev/next entries already!
41  */
42 static __inline__ void __list_add(struct list_head * new,
43                                   struct list_head * prev,
44                                   struct list_head * next)
45 {
46     next->prev = new;
47     new->next = next;
48     new->prev = prev;
49     prev->next = new;
50 }
51
52 /**
53  * list_add - add a new entry
54  * @new: new entry to be added
55  * @head: list head to add it after
56  *
57  * Insert a new entry after the specified head.
58  * This is good for implementing stacks.
59  */
60 static __inline__ void list_add(struct list_head *new, struct list_head *head)
61 {
62     __list_add(new, head, head->next);
63 }
64
65 /**
66  * list_add_tail - add a new entry
67  * @new: new entry to be added
68  * @head: list head to add it before
69  *
70  * Insert a new entry before the specified head.
71  * This is useful for implementing queues.
72  */
73 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
74 {
75     __list_add(new, head->prev, head);
76 }
77
78 /*
79  * Delete a list entry by making the prev/next entries
80  * point to each other.
81  *
82  * This is only for internal list manipulation where we know
83  * the prev/next entries already!
84  */
85 static __inline__ void __list_del(struct list_head * prev,
86                                   struct list_head * next)
87 {
88     next->prev = prev;
89     prev->next = next;
90 }
91
92 /**
93  * list_del - deletes entry from list.
94  * @entry: the element to delete from the list.
95  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
96  */
97 static __inline__ void list_del(struct list_head *entry)
98 {
99     __list_del(entry->prev, entry->next);
100 }
101
102 /**
103  * list_del_init - deletes entry from list and reinitialize it.
104  * @entry: the element to delete from the list.
105  */
106 static __inline__ void list_del_init(struct list_head *entry)
107 {
108     __list_del(entry->prev, entry->next);
109     ATALK_INIT_LIST_HEAD(entry);
110 }
111
112 /**
113  * list_empty - tests whether a list is empty
114  * @head: the list to test.
115  */
116 static __inline__ int list_empty(struct list_head *head)
117 {
118     return head->next == head;
119 }
120
121 /**
122  * list_splice - join two lists
123  * @list: the new list to add.
124  * @head: the place to add it in the first list.
125  */
126 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
127 {
128     struct list_head *first = list->next;
129
130     if (first != list) {
131         struct list_head *last = list->prev;
132         struct list_head *at = head->next;
133
134         first->prev = head;
135         head->next = first;
136
137         last->next = at;
138         at->prev = last;
139     }
140 }
141
142 #endif
143 /**
144  * list_entry - get the struct for this entry
145  * @ptr:        the &struct list_head pointer.
146  * @type:       the type of the struct this is embedded in.
147  * @member:     the name of the list_struct within the struct.
148  */
149 #define list_entry(ptr, type, member) \
150     ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
151
152 /**
153  * list_for_each        -       iterate over a list
154  * @pos:        the &struct list_head to use as a loop counter.
155  * @n:          another &struct list_head to use as temporary storage
156  * @head:       the head for your list.
157  */
158 #define list_for_each(pos, head) \
159     for (pos = (head)->next; pos != (head); \
160         pos = pos->next)
161
162 /**
163  * list_for_each_prev   -       iterate over a list in reverse order
164  * @pos:        the &struct list_head to use as a loop counter.
165  * @head:       the head for your list.
166  */
167 #define list_for_each_prev(pos, head) \
168     for (pos = (head)->prev; pos != (head); \
169         pos = pos->prev)
170 #endif