introduce kmod_list_last()

This gets the last element in the list, that is, the previous element
of the head.
This commit is contained in:
Gustavo Sverzut Barbieri 2011-12-16 22:32:33 -02:00 committed by Lucas De Marchi
parent 2a70a5d4e0
commit d5ec60bc0c
3 changed files with 23 additions and 0 deletions

View File

@ -310,3 +310,23 @@ KMOD_EXPORT struct kmod_list *kmod_list_next(const struct kmod_list *list,
return container_of(curr->node.next, struct kmod_list, node);
}
/**
* kmod_list_last:
* @list: the head of the list
*
* Get the last element of the @list. As @list is a circular list,
* this is a cheap operation O(1) with the last element being the
* previous element.
*
* If the list has a single element it will return the list itself (as
* expected, and this is what differentiates from kmod_list_prev()).
*
* Returns: last node at @list or NULL if the list is empty.
*/
KMOD_EXPORT struct kmod_list *kmod_list_last(const struct kmod_list *list)
{
if (list == NULL)
return NULL;
return container_of(list->node.prev, struct kmod_list, node);
}

View File

@ -62,6 +62,8 @@ struct kmod_list *kmod_list_next(const struct kmod_list *first_entry,
const struct kmod_list *list_entry);
struct kmod_list *kmod_list_prev(const struct kmod_list *first_entry,
const struct kmod_list *list_entry);
struct kmod_list *kmod_list_last(const struct kmod_list *first_entry);
#define kmod_list_foreach(list_entry, first_entry) \
for (list_entry = first_entry; \
list_entry != NULL; \

View File

@ -11,6 +11,7 @@ global:
kmod_unref;
kmod_list_next;
kmod_list_prev;
kmod_list_last;
kmod_load_resources;
kmod_unload_resources;