Skip to content

Commit 0425473

Browse files
andy-shevtorvalds
authored andcommitted
list: introduce list_is_head() helper and re-use it in list.h
Introduce list_is_head() in the similar (*) way as it's done for list_entry_is_head(). Make use of it in the list.h. *) it's done as inliner and not a macro to be aligned with other list_is_*() APIs; while at it, make all three to have the same style. Link: https://lkml.kernel.org/r/20211201141824.81400-1-andriy.shevchenko@linux.intel.com Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 70ac699 commit 0425473

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

include/linux/list.h

+22-14
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ static inline void list_bulk_move_tail(struct list_head *head,
258258
* @list: the entry to test
259259
* @head: the head of the list
260260
*/
261-
static inline int list_is_first(const struct list_head *list,
262-
const struct list_head *head)
261+
static inline int list_is_first(const struct list_head *list, const struct list_head *head)
263262
{
264263
return list->prev == head;
265264
}
@@ -269,12 +268,21 @@ static inline int list_is_first(const struct list_head *list,
269268
* @list: the entry to test
270269
* @head: the head of the list
271270
*/
272-
static inline int list_is_last(const struct list_head *list,
273-
const struct list_head *head)
271+
static inline int list_is_last(const struct list_head *list, const struct list_head *head)
274272
{
275273
return list->next == head;
276274
}
277275

276+
/**
277+
* list_is_head - tests whether @list is the list @head
278+
* @list: the entry to test
279+
* @head: the head of the list
280+
*/
281+
static inline int list_is_head(const struct list_head *list, const struct list_head *head)
282+
{
283+
return list == head;
284+
}
285+
278286
/**
279287
* list_empty - tests whether a list is empty
280288
* @head: the list to test.
@@ -318,7 +326,7 @@ static inline void list_del_init_careful(struct list_head *entry)
318326
static inline int list_empty_careful(const struct list_head *head)
319327
{
320328
struct list_head *next = smp_load_acquire(&head->next);
321-
return (next == head) && (next == head->prev);
329+
return list_is_head(next, head) && (next == head->prev);
322330
}
323331

324332
/**
@@ -393,10 +401,9 @@ static inline void list_cut_position(struct list_head *list,
393401
{
394402
if (list_empty(head))
395403
return;
396-
if (list_is_singular(head) &&
397-
(head->next != entry && head != entry))
404+
if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
398405
return;
399-
if (entry == head)
406+
if (list_is_head(entry, head))
400407
INIT_LIST_HEAD(list);
401408
else
402409
__list_cut_position(list, head, entry);
@@ -570,7 +577,7 @@ static inline void list_splice_tail_init(struct list_head *list,
570577
* @head: the head for your list.
571578
*/
572579
#define list_for_each(pos, head) \
573-
for (pos = (head)->next; pos != (head); pos = pos->next)
580+
for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
574581

575582
/**
576583
* list_for_each_continue - continue iteration over a list
@@ -580,15 +587,15 @@ static inline void list_splice_tail_init(struct list_head *list,
580587
* Continue to iterate over a list, continuing after the current position.
581588
*/
582589
#define list_for_each_continue(pos, head) \
583-
for (pos = pos->next; pos != (head); pos = pos->next)
590+
for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
584591

585592
/**
586593
* list_for_each_prev - iterate over a list backwards
587594
* @pos: the &struct list_head to use as a loop cursor.
588595
* @head: the head for your list.
589596
*/
590597
#define list_for_each_prev(pos, head) \
591-
for (pos = (head)->prev; pos != (head); pos = pos->prev)
598+
for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
592599

593600
/**
594601
* list_for_each_safe - iterate over a list safe against removal of list entry
@@ -597,8 +604,9 @@ static inline void list_splice_tail_init(struct list_head *list,
597604
* @head: the head for your list.
598605
*/
599606
#define list_for_each_safe(pos, n, head) \
600-
for (pos = (head)->next, n = pos->next; pos != (head); \
601-
pos = n, n = pos->next)
607+
for (pos = (head)->next, n = pos->next; \
608+
!list_is_head(pos, (head)); \
609+
pos = n, n = pos->next)
602610

603611
/**
604612
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
@@ -608,7 +616,7 @@ static inline void list_splice_tail_init(struct list_head *list,
608616
*/
609617
#define list_for_each_prev_safe(pos, n, head) \
610618
for (pos = (head)->prev, n = pos->prev; \
611-
pos != (head); \
619+
!list_is_head(pos, (head)); \
612620
pos = n, n = pos->prev)
613621

614622
/**

0 commit comments

Comments
 (0)