From: Ole Schuerks <ole0811sch@gmail.com>
To: linux-kbuild@vger.kernel.org
Cc: ole0811sch@gmail.com, jude.gyimah@rub.de, thorsten.berger@rub.de,
deltaone@debian.org, jan.sollmann@rub.de, mcgrof@kernel.org,
masahiroy@kernel.org, linux-kernel@vger.kernel.org,
nathan@kernel.org, nicolas@fjasle.eu
Subject: [PATCH v5 02/11] kbuild: Add list_is_{first,last}, list_size, list_at_index, list_for_each_from
Date: Fri, 20 Sep 2024 10:56:19 +0200 [thread overview]
Message-ID: <20240920085628.51863-3-ole0811sch@gmail.com> (raw)
In-Reply-To: <20240920085628.51863-1-ole0811sch@gmail.com>
list_is_first and list_is_last respectively check whether an entry is
the first or last entry of the list. list_size counts the number of
entries. list_at_index retrieves the entry at an index.
list_for_each_from iterates over a list from some entry to the end.
Signed-off-by: Ole Schuerks <ole0811sch@gmail.com>
---
scripts/include/list.h | 71 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)
diff --git a/scripts/include/list.h b/scripts/include/list.h
index fea1e2b79063..f7aff1749a0b 100644
--- a/scripts/include/list.h
+++ b/scripts/include/list.h
@@ -169,6 +169,43 @@ static inline int list_empty(const struct list_head *head)
return head->next == head;
}
+/**
+ * list_is_first - tests whether @list belongs to the first entry
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_first(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list == head->next;
+}
+
+/**
+ * list_is_last - tests whether @list belongs to the last entry
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_last(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list == head->prev;
+}
+
+/**
+ * list_size - counts the number of entries in a list
+ * @head: the list whose entries are counted
+ */
+static inline size_t list_size(const struct list_head *head)
+{
+ size_t ret = 0;
+
+ for (struct list_head *curr = head->next; curr != head;
+ curr = curr->next)
+ ++ret;
+
+ return ret;
+}
+
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
@@ -260,6 +297,40 @@ static inline int list_empty(const struct list_head *head)
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
+/**
+ * list_for_each_entry_from - iterate over list of given type starting at a given node
+ * @pos: the type * to use as a loop cursor.
+ * @start: the node to start iterating at
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_for_each_entry_from(pos, start, head, member) \
+ for (pos = list_entry(start, typeof(*pos), member); \
+ !list_entry_is_head(pos, head, member); \
+ pos = list_next_entry(pos, member))
+
+/**
+ * list_at_index - retrieve the entry at index i in O(n)
+ * @i: index of entry to retrieve.
+ * @head: the head for your list.
+ * @type: the type of the struct the entries are embedded in.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_at_index(i, head, type, member) \
+ ({ \
+ type *__pos; \
+ size_t __counter = 0; \
+ list_for_each_entry(__pos, head, member) { \
+ if (__counter++ == i) \
+ break; \
+ if (__pos->member.next == head) { \
+ __pos = NULL; \
+ break; \
+ } \
+ } \
+ __pos; \
+ })
+
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
--
2.39.2
next prev parent reply other threads:[~2024-09-20 8:56 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-20 8:56 [PATCH v5 00/11] kbuild, kconfig: Add support for conflict resolution Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 01/11] kconfig: Add PicoSAT interface Ole Schuerks
2024-09-20 11:11 ` Luis Chamberlain
2024-09-20 8:56 ` Ole Schuerks [this message]
2024-09-20 8:56 ` [PATCH v5 03/11] kconfig: Add definitions Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 04/11] kconfig: Add files for building constraints Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 05/11] kconfig: Add files for handling expressions Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 06/11] kconfig: Add files for RangeFix Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 07/11] kconfig: Add files with utility functions Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 08/11] kconfig: Add tools Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 09/11] kconfig: Add xconfig-modifications Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 10/11] kconfig: Add loader.gif Ole Schuerks
2024-09-20 8:56 ` [PATCH v5 11/11] kconfig: Add documentation for the conflict resolver Ole Schuerks
2025-01-13 16:35 ` Brendan Jackman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240920085628.51863-3-ole0811sch@gmail.com \
--to=ole0811sch@gmail.com \
--cc=deltaone@debian.org \
--cc=jan.sollmann@rub.de \
--cc=jude.gyimah@rub.de \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=mcgrof@kernel.org \
--cc=nathan@kernel.org \
--cc=nicolas@fjasle.eu \
--cc=thorsten.berger@rub.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox