forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
5 changed files
with
119 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef _LINUX_LIST_SORT_H | ||
#define _LINUX_LIST_SORT_H | ||
|
||
#include <linux/types.h> | ||
|
||
struct list_head; | ||
|
||
void list_sort(void *priv, struct list_head *head, | ||
int (*cmp)(void *priv, struct list_head *a, | ||
struct list_head *b)); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include <linux/kernel.h> | ||
#include <linux/module.h> | ||
#include <linux/list_sort.h> | ||
#include <linux/slab.h> | ||
#include <linux/list.h> | ||
|
||
/** | ||
* list_sort - sort a list. | ||
* @priv: private data, passed to @cmp | ||
* @head: the list to sort | ||
* @cmp: the elements comparison function | ||
* | ||
* This function has been implemented by Mark J Roberts <mjr@znex.org>. It | ||
* implements "merge sort" which has O(nlog(n)) complexity. The list is sorted | ||
* in ascending order. | ||
* | ||
* The comparison function @cmp is supposed to return a negative value if @a is | ||
* less than @b, and a positive value if @a is greater than @b. If @a and @b | ||
* are equivalent, then it does not matter what this function returns. | ||
*/ | ||
void list_sort(void *priv, struct list_head *head, | ||
int (*cmp)(void *priv, struct list_head *a, | ||
struct list_head *b)) | ||
{ | ||
struct list_head *p, *q, *e, *list, *tail, *oldhead; | ||
int insize, nmerges, psize, qsize, i; | ||
|
||
if (list_empty(head)) | ||
return; | ||
|
||
list = head->next; | ||
list_del(head); | ||
insize = 1; | ||
for (;;) { | ||
p = oldhead = list; | ||
list = tail = NULL; | ||
nmerges = 0; | ||
|
||
while (p) { | ||
nmerges++; | ||
q = p; | ||
psize = 0; | ||
for (i = 0; i < insize; i++) { | ||
psize++; | ||
q = q->next == oldhead ? NULL : q->next; | ||
if (!q) | ||
break; | ||
} | ||
|
||
qsize = insize; | ||
while (psize > 0 || (qsize > 0 && q)) { | ||
if (!psize) { | ||
e = q; | ||
q = q->next; | ||
qsize--; | ||
if (q == oldhead) | ||
q = NULL; | ||
} else if (!qsize || !q) { | ||
e = p; | ||
p = p->next; | ||
psize--; | ||
if (p == oldhead) | ||
p = NULL; | ||
} else if (cmp(priv, p, q) <= 0) { | ||
e = p; | ||
p = p->next; | ||
psize--; | ||
if (p == oldhead) | ||
p = NULL; | ||
} else { | ||
e = q; | ||
q = q->next; | ||
qsize--; | ||
if (q == oldhead) | ||
q = NULL; | ||
} | ||
if (tail) | ||
tail->next = e; | ||
else | ||
list = e; | ||
e->prev = tail; | ||
tail = e; | ||
} | ||
p = q; | ||
} | ||
|
||
tail->next = list; | ||
list->prev = tail; | ||
|
||
if (nmerges <= 1) | ||
break; | ||
|
||
insize *= 2; | ||
} | ||
|
||
head->next = list; | ||
head->prev = list->prev; | ||
list->prev->next = head; | ||
list->prev = head; | ||
} | ||
|
||
EXPORT_SYMBOL(list_sort); |