diff options
author | Dave Chinner <david@fromorbit.com> | 2010-01-12 01:39:16 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-01-13 00:02:00 -0500 |
commit | 2c761270d5520dd84ab0b4e47c24d99ff8503c38 (patch) | |
tree | c23a51fdcc96641661b632d3b3c2c46ad7e53a91 /lib | |
parent | dbf004d7883b3adb058c0c1a5635bc4ec27651c0 (diff) |
lib: Introduce generic list_sort function
There are two copies of list_sort() in the tree already, one in the DRM
code, another in ubifs. Now XFS needs this as well. Create a generic
list_sort() function from the ubifs version and convert existing users
to it so we don't end up with yet another copy in the tree.
Signed-off-by: Dave Chinner <david@fromorbit.com>
Acked-by: Dave Airlie <airlied@redhat.com>
Acked-by: Artem Bityutskiy <dedekind@infradead.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/list_sort.c | 102 |
2 files changed, 103 insertions, 1 deletions
diff --git a/lib/Makefile b/lib/Makefile index 911b25aed1e7..3b0b4a696db9 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -21,7 +21,7 @@ lib-y += kobject.o kref.o klist.o | |||
21 | 21 | ||
22 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ | 22 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ |
23 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ | 23 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ |
24 | string_helpers.o gcd.o | 24 | string_helpers.o gcd.o list_sort.o |
25 | 25 | ||
26 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) | 26 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) |
27 | CFLAGS_kobject.o += -DDEBUG | 27 | CFLAGS_kobject.o += -DDEBUG |
diff --git a/lib/list_sort.c b/lib/list_sort.c new file mode 100644 index 000000000000..19d11e0bb958 --- /dev/null +++ b/lib/list_sort.c | |||
@@ -0,0 +1,102 @@ | |||
1 | #include <linux/kernel.h> | ||
2 | #include <linux/module.h> | ||
3 | #include <linux/list_sort.h> | ||
4 | #include <linux/slab.h> | ||
5 | #include <linux/list.h> | ||
6 | |||
7 | /** | ||
8 | * list_sort - sort a list. | ||
9 | * @priv: private data, passed to @cmp | ||
10 | * @head: the list to sort | ||
11 | * @cmp: the elements comparison function | ||
12 | * | ||
13 | * This function has been implemented by Mark J Roberts <mjr@znex.org>. It | ||
14 | * implements "merge sort" which has O(nlog(n)) complexity. The list is sorted | ||
15 | * in ascending order. | ||
16 | * | ||
17 | * The comparison function @cmp is supposed to return a negative value if @a is | ||
18 | * less than @b, and a positive value if @a is greater than @b. If @a and @b | ||
19 | * are equivalent, then it does not matter what this function returns. | ||
20 | */ | ||
21 | void list_sort(void *priv, struct list_head *head, | ||
22 | int (*cmp)(void *priv, struct list_head *a, | ||
23 | struct list_head *b)) | ||
24 | { | ||
25 | struct list_head *p, *q, *e, *list, *tail, *oldhead; | ||
26 | int insize, nmerges, psize, qsize, i; | ||
27 | |||
28 | if (list_empty(head)) | ||
29 | return; | ||
30 | |||
31 | list = head->next; | ||
32 | list_del(head); | ||
33 | insize = 1; | ||
34 | for (;;) { | ||
35 | p = oldhead = list; | ||
36 | list = tail = NULL; | ||
37 | nmerges = 0; | ||
38 | |||
39 | while (p) { | ||
40 | nmerges++; | ||
41 | q = p; | ||
42 | psize = 0; | ||
43 | for (i = 0; i < insize; i++) { | ||
44 | psize++; | ||
45 | q = q->next == oldhead ? NULL : q->next; | ||
46 | if (!q) | ||
47 | break; | ||
48 | } | ||
49 | |||
50 | qsize = insize; | ||
51 | while (psize > 0 || (qsize > 0 && q)) { | ||
52 | if (!psize) { | ||
53 | e = q; | ||
54 | q = q->next; | ||
55 | qsize--; | ||
56 | if (q == oldhead) | ||
57 | q = NULL; | ||
58 | } else if (!qsize || !q) { | ||
59 | e = p; | ||
60 | p = p->next; | ||
61 | psize--; | ||
62 | if (p == oldhead) | ||
63 | p = NULL; | ||
64 | } else if (cmp(priv, p, q) <= 0) { | ||
65 | e = p; | ||
66 | p = p->next; | ||
67 | psize--; | ||
68 | if (p == oldhead) | ||
69 | p = NULL; | ||
70 | } else { | ||
71 | e = q; | ||
72 | q = q->next; | ||
73 | qsize--; | ||
74 | if (q == oldhead) | ||
75 | q = NULL; | ||
76 | } | ||
77 | if (tail) | ||
78 | tail->next = e; | ||
79 | else | ||
80 | list = e; | ||
81 | e->prev = tail; | ||
82 | tail = e; | ||
83 | } | ||
84 | p = q; | ||
85 | } | ||
86 | |||
87 | tail->next = list; | ||
88 | list->prev = tail; | ||
89 | |||
90 | if (nmerges <= 1) | ||
91 | break; | ||
92 | |||
93 | insize *= 2; | ||
94 | } | ||
95 | |||
96 | head->next = list; | ||
97 | head->prev = list->prev; | ||
98 | list->prev->next = head; | ||
99 | list->prev = head; | ||
100 | } | ||
101 | |||
102 | EXPORT_SYMBOL(list_sort); | ||