From 50886382bfec3ceb043f8966f7ca07fecdc2277b Mon Sep 17 00:00:00 2001 From: CTCaer Date: Sat, 25 Jun 2022 05:59:53 +0300 Subject: [PATCH] bdk: list: add LIST_FOREACH_INVERSE and LIST_FOREACH_ENTRY_INVERSE --- bdk/utils/list.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bdk/utils/list.h b/bdk/utils/list.h index ece5402..06e4605 100644 --- a/bdk/utils/list.h +++ b/bdk/utils/list.h @@ -30,6 +30,10 @@ #define LIST_FOREACH(iter, list) \ for(link_t *iter = (list)->next; iter != (list); iter = iter->next) +/*! Iterate over all list links backwards. */ +#define LIST_FOREACH_INVERSE(iter, list) \ + for(link_t *iter = (list)->prev; iter != (list); iter = iter->prev) + /*! Safely iterate over all list links. */ #define LIST_FOREACH_SAFE(iter, list) \ for(link_t *iter = (list)->next, *safe = iter->next; iter != (list); iter = safe, safe = iter->next) @@ -39,6 +43,11 @@ if ((list)->next != (list)) \ for(etype *iter = CONTAINER_OF((list)->next, etype, mn); &iter->mn != (list); iter = CONTAINER_OF(iter->mn.next, etype, mn)) +/* Iterate over all list members backwards and make sure that the list has at least one entry. */ +#define LIST_FOREACH_ENTRY_INVERSE(type, iter, list, mn) \ + if ((list)->prev != (list)) \ + for(type *iter = CONTAINER_OF((list)->prev, type, mn); &iter->mn != (list); iter = CONTAINER_OF(iter->mn.prev, type, mn)) + typedef struct _link_t { struct _link_t *prev;