From: Dmitry Antipov <dmantipov@yandex.ru>
To: linux-modules@vger.kernel.org
Cc: Dmitry Antipov <dmantipov@yandex.ru>
Subject: [PATCH] shared, tools: introduce and use array iterator
Date: Fri, 19 May 2023 11:54:23 +0300 [thread overview]
Message-ID: <20230519085423.429239-1-dmantipov@yandex.ru> (raw)
Introduce the convenient kernel-style array iterator and use it where
appropriate. Note this also fixes the following warnings reported by UBSan:
shared/array.c:67:23: runtime error: applying zero offset to null pointer
tools/depmod.c:1983:25: runtime error: applying zero offset to null pointer
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
shared/array.c | 11 ++++++-----
shared/array.h | 5 +++++
tools/depmod.c | 24 +++++++++++++-----------
3 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/shared/array.c b/shared/array.c
index c2e2e14..bc314c6 100644
--- a/shared/array.c
+++ b/shared/array.c
@@ -63,11 +63,12 @@ int array_append(struct array *array, const void *element)
int array_append_unique(struct array *array, const void *element)
{
- void **itr = array->array;
- void **itr_end = itr + array->count;
- for (; itr < itr_end; itr++)
- if (*itr == element)
- return -EEXIST;
+ if (array->array) {
+ void **itr, **end;
+ array_for_each(array, itr, end)
+ if (*itr == element)
+ return -EEXIST;
+ }
return array_append(array, element);
}
diff --git a/shared/array.h b/shared/array.h
index b88482f..7172a66 100644
--- a/shared/array.h
+++ b/shared/array.h
@@ -20,3 +20,8 @@ void array_pop(struct array *array);
void array_free_array(struct array *array);
void array_sort(struct array *array, int (*cmp)(const void *a, const void *b));
int array_remove_at(struct array *array, unsigned int pos);
+
+/* Convenient kernel-style array iterator */
+#define array_for_each(_array, _itr, _end) \
+ for (_itr = _array->array, _end = _itr + _array->count; \
+ _itr < _end; _itr++)
diff --git a/tools/depmod.c b/tools/depmod.c
index a2c39b3..ade33ae 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -1970,7 +1970,6 @@ static int depmod_calculate_dependencies(struct depmod *depmod)
/* topological sort (outputs modules without users first) */
while (n_roots > 0) {
- const struct mod **itr_dst, **itr_dst_end;
struct mod *src;
uint16_t src_idx = roots[--n_roots];
@@ -1979,16 +1978,19 @@ static int depmod_calculate_dependencies(struct depmod *depmod)
sorted[n_sorted] = src_idx;
n_sorted++;
- itr_dst = (const struct mod **)src->deps.array;
- itr_dst_end = itr_dst + src->deps.count;
- for (; itr_dst < itr_dst_end; itr_dst++) {
- const struct mod *dst = *itr_dst;
- uint16_t dst_idx = dst->idx;
- assert(users[dst_idx] > 0);
- users[dst_idx]--;
- if (users[dst_idx] == 0) {
- roots[n_roots] = dst_idx;
- n_roots++;
+ if (src->deps.array) {
+ void **itr, **end;
+ struct array *base = &src->deps;
+
+ array_for_each(base, itr, end) {
+ const struct mod *dst = *itr;
+ uint16_t dst_idx = dst->idx;
+ assert(users[dst_idx] > 0);
+ users[dst_idx]--;
+ if (users[dst_idx] == 0) {
+ roots[n_roots] = dst_idx;
+ n_roots++;
+ }
}
}
}
--
2.40.1
reply other threads:[~2023-05-19 8:55 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230519085423.429239-1-dmantipov@yandex.ru \
--to=dmantipov@yandex.ru \
--cc=linux-modules@vger.kernel.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).