diff --git a/include/list.h b/include/list.h index 17d3576..1e8bc51 100644 --- a/include/list.h +++ b/include/list.h @@ -429,6 +429,30 @@ &pos->member != (head); \ pos = n, n = list_entry(n->member.prev, typeof(*n), member)) +/** + * list_add_sort - add a new entry to a sorted list + * @new: new entry to be added + * @head: list head to add it in + * @compare: Compare function to compare two list entries + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static inline void list_add_sort(struct list_head *new, struct list_head *head, + int (*compare)(struct list_head *a, struct list_head *b)) +{ + struct list_head *pos, *insert = head; + + list_for_each(pos, head) { + if (compare(pos, new) < 0) + continue; + insert = pos; + break; + } + + list_add_tail(new, insert); +} + /* * Double linked lists with a single pointer list head. * Mostly useful for hash tables where the two pointer list head is