1 /*
2  * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3  * See COPYING file for license information
4  */
5 
6 #ifndef _COMMON_LIST_H
7 #define _COMMON_LIST_H
8 
9 /*
10  * Stolen from linux-2.1.131
11  * All comments from the original source unless otherwise noted
12  * Added: the CLEAR_LIST_NODE macro
13  */
14 
15 /*
16  * Simple doubly linked list implementation.
17  *
18  * Some of the internal functions ("__xxx") are useful when
19  * manipulating whole lists rather than single entries, as
20  * sometimes we already know the next/prev entries and we can
21  * generate better code by using them directly rather than
22  * using the generic single-entry routines.
23  */
24 
25 #include "inline.h"
26 
27 struct list_head {
28         struct list_head *next, *prev;
29 };
30 
31 #define LIST_HEAD(name) \
32         struct list_head name = { &name, &name }
33 
34 #define INIT_LIST_HEAD(ptr)  do { \
35         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
36 } while (0)
37 
38 #define CLEAR_LIST_NODE(ptr) do { \
39         (ptr)->next = NULL;  (ptr)->prev = NULL; \
40 } while (0)
41 
42 /*
43  * Insert a new entry between two known consecutive entries.
44  *
45  * This is only for internal list manipulation where we know
46  * the prev/next entries already!
47  */
__list_add(struct list_head * li,struct list_head * prev,struct list_head * next)48 static INLINE void __list_add(struct list_head *li,
49         struct list_head * prev,
50         struct list_head * next)
51 {
52         next->prev = li;
53         li->next = next;
54         li->prev = prev;
55         prev->next = li;
56 }
57 
58 /*
59  * Insert a new entry after the specified head..
60  */
list_add(struct list_head * li,struct list_head * head)61 static INLINE void list_add(struct list_head *li, struct list_head *head)
62 {
63         __list_add(li, head, head->next);
64 }
65 
66 /*
67  * Delete a list entry by making the prev/next entries
68  * point to each other.
69  *
70  * This is only for internal list manipulation where we know
71  * the prev/next entries already!
72  */
__list_del(struct list_head * prev,struct list_head * next)73 static INLINE void __list_del(struct list_head * prev,
74                                   struct list_head * next)
75 {
76         next->prev = prev;
77         prev->next = next;
78 }
79 
list_del(struct list_head * entry)80 static INLINE void list_del(struct list_head *entry)
81 {
82         __list_del(entry->prev, entry->next);
83 }
84 
list_empty(struct list_head * head)85 static INLINE int list_empty(struct list_head *head)
86 {
87         return head->next == head;
88 }
89 
90 /*
91  * Splice in "list" into "head"
92  */
list_splice(struct list_head * list,struct list_head * head)93 static INLINE void list_splice(struct list_head *list, struct list_head *head)
94 {
95         struct list_head *first = list->next;
96 
97         if (first != list) {
98                 struct list_head *last = list->prev;
99                 struct list_head *at = head->next;
100 
101                 first->prev = head;
102                 head->next = first;
103 
104                 last->next = at;
105                 at->prev = last;
106         }
107 }
108 
109 #define list_entry(ptr, type, member) \
110         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
111 
112 #endif /* _COMMON_LIST_H */
113