blob: 7aca9730b2e9621349cfb27b427c3ff19f91829f [file] [log] [blame]
Jens Axboe01743ee2008-06-02 12:19:19 +02001#ifndef _LINUX_FLIST_H
2#define _LINUX_FLIST_H
3
Jens Axboe79d16312010-03-04 12:43:20 +01004#include <stdlib.h>
5
Jens Axboe01743ee2008-06-02 12:19:19 +02006#undef offsetof
7#ifdef __compiler_offsetof
8#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
9#else
10#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
11#endif
12
13#define container_of(ptr, type, member) ({ \
14 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
15 (type *)( (char *)__mptr - offsetof(type,member) );})
16
17/*
18 * Simple doubly linked list implementation.
19 *
20 * Some of the internal functions ("__xxx") are useful when
21 * manipulating whole lists rather than single entries, as
22 * sometimes we already know the next/prev entries and we can
23 * generate better code by using them directly rather than
24 * using the generic single-entry routines.
25 */
26
27struct flist_head {
28 struct flist_head *next, *prev;
29};
30
31#define FLIST_HEAD_INIT(name) { &(name), &(name) }
32
33#define FLIST_HEAD(name) \
34 struct flist_head name = FLIST_HEAD_INIT(name)
35
36#define INIT_FLIST_HEAD(ptr) do { \
37 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
38} while (0)
39
40/*
41 * Insert a new entry between two known consecutive entries.
42 *
43 * This is only for internal list manipulation where we know
44 * the prev/next entries already!
45 */
Daniel Rall2b525112008-09-10 08:56:01 +020046static inline void __flist_add(struct flist_head *new_entry,
Jens Axboe01743ee2008-06-02 12:19:19 +020047 struct flist_head *prev,
48 struct flist_head *next)
49{
Daniel Rall2b525112008-09-10 08:56:01 +020050 next->prev = new_entry;
51 new_entry->next = next;
52 new_entry->prev = prev;
53 prev->next = new_entry;
Jens Axboe01743ee2008-06-02 12:19:19 +020054}
55
56/**
57 * flist_add - add a new entry
Daniel Rall2b525112008-09-10 08:56:01 +020058 * @new_entry: new entry to be added
Jens Axboe01743ee2008-06-02 12:19:19 +020059 * @head: list head to add it after
60 *
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
63 */
Daniel Rall2b525112008-09-10 08:56:01 +020064static inline void flist_add(struct flist_head *new_entry,
65 struct flist_head *head)
Jens Axboe01743ee2008-06-02 12:19:19 +020066{
Daniel Rall2b525112008-09-10 08:56:01 +020067 __flist_add(new_entry, head, head->next);
Jens Axboe01743ee2008-06-02 12:19:19 +020068}
69
Daniel Rall2b525112008-09-10 08:56:01 +020070static inline void flist_add_tail(struct flist_head *new_entry,
Jens Axboe01743ee2008-06-02 12:19:19 +020071 struct flist_head *head)
72{
Daniel Rall2b525112008-09-10 08:56:01 +020073 __flist_add(new_entry, head->prev, head);
Jens Axboe01743ee2008-06-02 12:19:19 +020074}
75
76/*
77 * Delete a list entry by making the prev/next entries
78 * point to each other.
79 *
80 * This is only for internal list manipulation where we know
81 * the prev/next entries already!
82 */
83static inline void __flist_del(struct flist_head *prev,
84 struct flist_head * next)
85{
86 next->prev = prev;
87 prev->next = next;
88}
89
90/**
91 * flist_del - deletes entry from list.
92 * @entry: the element to delete from the list.
93 * Note: flist_empty on entry does not return true after this, the entry is
94 * in an undefined state.
95 */
96static inline void flist_del(struct flist_head *entry)
97{
98 __flist_del(entry->prev, entry->next);
99 entry->next = NULL;
100 entry->prev = NULL;
101}
102
103/**
104 * flist_del_init - deletes entry from list and reinitialize it.
105 * @entry: the element to delete from the list.
106 */
107static inline void flist_del_init(struct flist_head *entry)
108{
109 __flist_del(entry->prev, entry->next);
110 INIT_FLIST_HEAD(entry);
111}
112
113/**
114 * flist_empty - tests whether a list is empty
115 * @head: the list to test.
116 */
117static inline int flist_empty(const struct flist_head *head)
118{
119 return head->next == head;
120}
121
Jens Axboee53ab272009-07-06 13:43:46 +0200122static inline void __flist_splice(const struct flist_head *list,
123 struct flist_head *prev,
124 struct flist_head *next)
125{
126 struct flist_head *first = list->next;
127 struct flist_head *last = list->prev;
128
129 first->prev = prev;
130 prev->next = first;
131
132 last->next = next;
133 next->prev = last;
134}
135
136static inline void flist_splice(const struct flist_head *list,
137 struct flist_head *head)
138{
139 if (!flist_empty(list))
140 __flist_splice(list, head, head->next);
141}
142
143static inline void flist_splice_init(struct flist_head *list,
144 struct flist_head *head)
145{
146 if (!flist_empty(list)) {
147 __flist_splice(list, head, head->next);
148 INIT_FLIST_HEAD(list);
149 }
150}
151
Jens Axboe01743ee2008-06-02 12:19:19 +0200152/**
153 * flist_entry - get the struct for this entry
154 * @ptr: the &struct flist_head pointer.
155 * @type: the type of the struct this is embedded in.
156 * @member: the name of the flist_struct within the struct.
157 */
158#define flist_entry(ptr, type, member) \
159 container_of(ptr, type, member)
160
161/**
162 * flist_for_each - iterate over a list
163 * @pos: the &struct flist_head to use as a loop counter.
164 * @head: the head for your list.
165 */
166#define flist_for_each(pos, head) \
167 for (pos = (head)->next; pos != (head); pos = pos->next)
168
169/**
170 * flist_for_each_safe - iterate over a list safe against removal of list entry
171 * @pos: the &struct flist_head to use as a loop counter.
172 * @n: another &struct flist_head to use as temporary storage
173 * @head: the head for your list.
174 */
175#define flist_for_each_safe(pos, n, head) \
176 for (pos = (head)->next, n = pos->next; pos != (head); \
177 pos = n, n = pos->next)
178
179#endif