I am working on a Linux kernel module and I am using be built in linked list. I need to sort this list and I saw that there is a built in list_sort function as described below. However I'm confused about the priv
parameter. What is this parameter used for and what do I need to pass? There isn't much documentation on it anywhere.
Defined in linux/list_sort.h
:
/**
* list_sort - sort a list
* @priv: private data, opaque to list_sort(), passed to @cmp
* @head: the list to sort
* @cmp: the elements comparison function
*
* This function implements "merge sort", which has O(nlog(n))
* complexity.
*
* The comparison function @cmp must return a negative value if @a
* should sort before @b, and a positive value if @a should sort after
* @b. If @a and @b are equivalent, and their original relative
* ordering is to be preserved, @cmp must return 0.
*/
void list_sort(void *priv, struct list_head *head,
int (*cmp)(void *priv, struct list_head *a,
struct list_head *b))
EDIT So I understand I can just pass NULL for the priv parameter. Now I'm not sure how to write my cmp function because list_sort takes in struct list_head pointers but I have my own defined struct birthday which contains a list_head. My below compare function won't work because list_head does not contain members that my struct birthday has.
struct birthday {
char *name;
int month;
int day;
int year;
struct list_head list;
};
int compare(void *priv, struct list_head *a, struct list_head *b) {
if (a != NULL && b != NULL) {
struct birthday personA = a;
struct birthday personB = b;
int monthA = personA.month;
int monthB = personB.month;
int dayA = personA.day;
int dayB = personB.day;
int yearA = personA.year;
int yearB = personB.year;
if (yearA < yearB) {
return 1;
} else if (yearB < yearA) {
return -1;
} else {
if (monthA < monthB) {
return 1;
} else if (monthB < monthA) {
return -1;
} else {
if (dayA < dayB) {
return 1;
} else if (dayB < dayA) {
return -1;
} else {
return 0;
}
}
}
}
}