blob: 3c08aa5f2c2a7f2f3a1403ca932179a1f3f7f461 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3 * Copyright (C) 1999-2008 by Theodore Ts'o
4 *
5 * This file may be redistributed under the terms of the
6 * GNU Lesser General Public License.
7 *
8 * (based on list.h from e2fsprogs)
9 * Merge sort based on kernel's implementation.
10 */
11
12#ifndef UTIL_LINUX_LIST_H
13#define UTIL_LINUX_LIST_H
14
15/* TODO: use AC_C_INLINE */
16#ifdef __GNUC__
17#define _INLINE_ static __inline__
18#else /* For Watcom C */
19#define _INLINE_ static inline
20#endif
21
22/*
23 * Simple doubly linked list implementation.
24 *
25 * Some of the internal functions ("__xxx") are useful when
26 * manipulating whole lists rather than single entries, as
27 * sometimes we already know the next/prev entries and we can
28 * generate better code by using them directly rather than
29 * using the generic single-entry routines.
30 */
31
32struct list_head {
33 struct list_head *next, *prev;
34};
35
36#define LIST_HEAD_INIT(name) { &(name), &(name) }
37
38#define LIST_HEAD(name) \
39 struct list_head name = LIST_HEAD_INIT(name)
40
41#define INIT_LIST_HEAD(ptr) do { \
42 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
43} while (0)
44
45/*
46 * Insert a new entry between two known consecutive entries.
47 *
48 * This is only for internal list manipulation where we know
49 * the prev/next entries already!
50 */
51_INLINE_ void __list_add(struct list_head * add,
52 struct list_head * prev,
53 struct list_head * next)
54{
55 next->prev = add;
56 add->next = next;
57 add->prev = prev;
58 prev->next = add;
59}
60
61/**
62 * list_add - add a new entry
63 * @add: new entry to be added
64 * @head: list head to add it after
65 *
66 * Insert a new entry after the specified head.
67 * This is good for implementing stacks.
68 */
69_INLINE_ void list_add(struct list_head *add, struct list_head *head)
70{
71 __list_add(add, head, head->next);
72}
73
74/**
75 * list_add_tail - add a new entry
76 * @add: new entry to be added
77 * @head: list head to add it before
78 *
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
81 */
82_INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)
83{
84 __list_add(add, head->prev, head);
85}
86
87/*
88 * Delete a list entry by making the prev/next entries
89 * point to each other.
90 *
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
93 */
94_INLINE_ void __list_del(struct list_head * prev,
95 struct list_head * next)
96{
97 next->prev = prev;
98 prev->next = next;
99}
100
101/**
102 * list_del - deletes entry from list.
103 * @entry: the element to delete from the list.
104 *
105 * list_empty() on @entry does not return true after this, @entry is
106 * in an undefined state.
107 */
108_INLINE_ void list_del(struct list_head *entry)
109{
110 __list_del(entry->prev, entry->next);
111}
112
113/**
114 * list_del_init - deletes entry from list and reinitialize it.
115 * @entry: the element to delete from the list.
116 */
117_INLINE_ void list_del_init(struct list_head *entry)
118{
119 __list_del(entry->prev, entry->next);
120 INIT_LIST_HEAD(entry);
121}
122
123/**
124 * list_empty - tests whether a list is empty
125 * @head: the list to test.
126 */
127_INLINE_ int list_empty(struct list_head *head)
128{
129 return head->next == head;
130}
131
132/**
133 * list_entry_is_last - tests whether is entry last in the list
134 * @entry: the entry to test.
135 * @head: the list to test.
136 */
137_INLINE_ int list_entry_is_last(struct list_head *entry, struct list_head *head)
138{
139 return head->prev == entry;
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_INLINE_ void list_splice(struct list_head *list, struct list_head *head)
148{
149 struct list_head *first = list->next;
150
151 if (first != list) {
152 struct list_head *last = list->prev;
153 struct list_head *at = head->next;
154
155 first->prev = head;
156 head->next = first;
157
158 last->next = at;
159 at->prev = last;
160 }
161}
162
163/**
164 * list_entry - get the struct for this entry
165 * @ptr: the &struct list_head pointer.
166 * @type: the type of the struct this is embedded in.
167 * @member: the name of the list_struct within the struct.
168 */
bigbiff7b4c7a62015-01-01 19:44:14 -0500169#define list_entry(ptr, type, member) __extension__ ({ \
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500170 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
171 (type *)( (char *)__mptr - offsetof(type,member) );})
172
173
174#define list_first_entry(head, type, member) \
175 ((head) && (head)->next != (head) ? list_entry((head)->next, type, member) : NULL)
176
177#define list_last_entry(head, type, member) \
178 ((head) && (head)->prev != (head) ? list_entry((head)->prev, type, member) : NULL)
179
180/**
181 * list_for_each - iterate over elements in a list
182 * @pos: the &struct list_head to use as a loop counter.
183 * @head: the head for your list.
184 */
185#define list_for_each(pos, head) \
186 for (pos = (head)->next; pos != (head); pos = pos->next)
187
188/**
189 * list_for_each_backwardly - iterate over elements in a list in reverse
190 * @pos: the &struct list_head to use as a loop counter.
191 * @head: the head for your list.
192 */
193#define list_for_each_backwardly(pos, head) \
194 for (pos = (head)->prev; pos != (head); pos = pos->prev)
195
196/**
197 * list_for_each_safe - iterate over elements in a list, but don't dereference
198 * pos after the body is done (in case it is freed)
199 * @pos: the &struct list_head to use as a loop counter.
200 * @pnext: the &struct list_head to use as a pointer to the next item.
201 * @head: the head for your list (not included in iteration).
202 */
203#define list_for_each_safe(pos, pnext, head) \
204 for (pos = (head)->next, pnext = pos->next; pos != (head); \
205 pos = pnext, pnext = pos->next)
206
207#define MAX_LIST_LENGTH_BITS 20
208
209/*
210 * Returns a list organized in an intermediate format suited
211 * to chaining of merge() calls: null-terminated, no reserved or
212 * sentinel head node, "prev" links not maintained.
213 */
214_INLINE_ struct list_head *merge(int (*cmp)(struct list_head *a,
bigbiff7b4c7a62015-01-01 19:44:14 -0500215 struct list_head *b,
216 void *data),
217 void *data,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500218 struct list_head *a, struct list_head *b)
219{
220 struct list_head head, *tail = &head;
221
222 while (a && b) {
223 /* if equal, take 'a' -- important for sort stability */
bigbiff7b4c7a62015-01-01 19:44:14 -0500224 if ((*cmp)(a, b, data) <= 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500225 tail->next = a;
226 a = a->next;
227 } else {
228 tail->next = b;
229 b = b->next;
230 }
231 tail = tail->next;
232 }
233 tail->next = a ? a : b;
234 return head.next;
235}
236
237/*
238 * Combine final list merge with restoration of standard doubly-linked
239 * list structure. This approach duplicates code from merge(), but
240 * runs faster than the tidier alternatives of either a separate final
241 * prev-link restoration pass, or maintaining the prev links
242 * throughout.
243 */
244_INLINE_ void merge_and_restore_back_links(int (*cmp)(struct list_head *a,
bigbiff7b4c7a62015-01-01 19:44:14 -0500245 struct list_head *b,
246 void *data),
247 void *data,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500248 struct list_head *head,
249 struct list_head *a, struct list_head *b)
250{
251 struct list_head *tail = head;
252
253 while (a && b) {
254 /* if equal, take 'a' -- important for sort stability */
bigbiff7b4c7a62015-01-01 19:44:14 -0500255 if ((*cmp)(a, b, data) <= 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500256 tail->next = a;
257 a->prev = tail;
258 a = a->next;
259 } else {
260 tail->next = b;
261 b->prev = tail;
262 b = b->next;
263 }
264 tail = tail->next;
265 }
266 tail->next = a ? a : b;
267
268 do {
269 /*
270 * In worst cases this loop may run many iterations.
271 * Continue callbacks to the client even though no
272 * element comparison is needed, so the client's cmp()
273 * routine can invoke cond_resched() periodically.
274 */
bigbiff7b4c7a62015-01-01 19:44:14 -0500275 (*cmp)(tail->next, tail->next, data);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500276
277 tail->next->prev = tail;
278 tail = tail->next;
279 } while (tail->next);
280
281 tail->next = head;
282 head->prev = tail;
283}
284
285
286/**
287 * list_sort - sort a list
288 * @head: the list to sort
289 * @cmp: the elements comparison function
290 *
291 * This function implements "merge sort", which has O(nlog(n))
292 * complexity.
293 *
294 * The comparison function @cmp must return a negative value if @a
295 * should sort before @b, and a positive value if @a should sort after
296 * @b. If @a and @b are equivalent, and their original relative
297 * ordering is to be preserved, @cmp must return 0.
298 */
299_INLINE_ void list_sort(struct list_head *head,
300 int (*cmp)(struct list_head *a,
bigbiff7b4c7a62015-01-01 19:44:14 -0500301 struct list_head *b,
302 void *data),
303 void *data)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500304{
305 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
306 -- last slot is a sentinel */
307 size_t lev; /* index into part[] */
308 size_t max_lev = 0;
309 struct list_head *list;
310
311 if (list_empty(head))
312 return;
313
314 memset(part, 0, sizeof(part));
315
316 head->prev->next = NULL;
317 list = head->next;
318
319 while (list) {
320 struct list_head *cur = list;
321 list = list->next;
322 cur->next = NULL;
323
324 for (lev = 0; part[lev]; lev++) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500325 cur = merge(cmp, data, part[lev], cur);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500326 part[lev] = NULL;
327 }
328 if (lev > max_lev) {
329 /* list passed to list_sort() too long for efficiency */
330 if (lev >= ARRAY_SIZE(part) - 1)
331 lev--;
332 max_lev = lev;
333 }
334 part[lev] = cur;
335 }
336
337 for (lev = 0; lev < max_lev; lev++)
338 if (part[lev])
bigbiff7b4c7a62015-01-01 19:44:14 -0500339 list = merge(cmp, data, part[lev], list);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500340
bigbiff7b4c7a62015-01-01 19:44:14 -0500341 merge_and_restore_back_links(cmp, data, head, part[max_lev], list);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500342}
343
344#undef _INLINE_
345
346#endif /* UTIL_LINUX_LIST_H */