Skip to content

Commit cb86408

Browse files
committed
list: add list_last_entry_or_null()
Add an equivalent of list_first_entry_or_null() to obtain the last element of a list. Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://lore.kernel.org/r/20250801-drm-bridge-alloc-getput-drm_bridge_get_next_bridge-v2-1-888912b0be13@bootlin.com Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
1 parent 2a06126 commit cb86408

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

include/linux/list.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,20 @@ static inline void list_splice_tail_init(struct list_head *list,
636636
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
637637
})
638638

639+
/**
640+
* list_last_entry_or_null - get the last element from a list
641+
* @ptr: the list head to take the element from.
642+
* @type: the type of the struct this is embedded in.
643+
* @member: the name of the list_head within the struct.
644+
*
645+
* Note that if the list is empty, it returns NULL.
646+
*/
647+
#define list_last_entry_or_null(ptr, type, member) ({ \
648+
struct list_head *head__ = (ptr); \
649+
struct list_head *pos__ = READ_ONCE(head__->prev); \
650+
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
651+
})
652+
639653
/**
640654
* list_next_entry - get the next element in list
641655
* @pos: the type * to cursor

0 commit comments

Comments
 (0)