From: Jeff King <email@example.com>
To: Abhishek Kumar <firstname.lastname@example.org>
Cc: Junio C Hamano <email@example.com>, firstname.lastname@example.org
Subject: Re: [PATCH v2 2/2] oidmap: rework iterators to return typed pointer
Date: Wed, 8 Apr 2020 18:08:40 -0400 [thread overview]
Message-ID: <20200408220840.GB3468797@coredump.intra.peff.net> (raw)
On Wed, Apr 08, 2020 at 12:33:46PM +0530, Abhishek Kumar wrote:
> 87571c3f (hashmap: use *_entry APIs for iteration, 2019-10-06) modified
> hashmap_iter_next() to return a hashmap_entry pointer instead of void
> However, oidmap_iter_next() is unaware of the struct type containing
> oidmap_entry and explicitly returns a void pointer.
> Rework oidmap_iter_next() to include struct type and return appropriate
> pointer. This allows for compile-time type checks.
Yes, I think returning a pointer to an oidmap_entry makes sense. And
then we get type safety, and anybody who wants embed an oidmap_entry can
use container_of() to get back to their original struct.
> + * Returns the next entry, or NULL if there are no more entries.
> + *
> + * The entry is of @type (e.g. "struct foo") and has a member of type struct
> + * oidmap_entry.
> + */
> +#define oidmap_iter_next(iter, type) \
> + (type *) hashmap_iter_next(&(iter)->h_iter)
This cast is turning a hashmap_entry into whatever type the caller
passed in. But it's doing it with a straight cast. We know that
hashmap_entry and oidmap_entry pointers are equivalent, but we don't
know where the oidmap_entry is with respect to the user's type.
I think oidmap_iter_next() should continue to be a function that returns
an oidmap_entry pointer (and use container_of_or_null() to get to it
from the hashmap_entry, even though we know the offset is 0).
And then the caller can either use container_of() to get to their
original struct, or we can provide a helper macro. See the difference
between hashmap_iter_first() and hashmap_iter_first_entry().
There's no hashmap_iter_next_entry(). There could be, but instead it
skipped straight to hashmap_for_each_entry(), which uses an offset
within the variable rather than the type. But likewise, we could add
> diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
> index 0acf99931e..a28bf007a8 100644
> --- a/t/helper/test-oidmap.c
> +++ b/t/helper/test-oidmap.c
> @@ -96,7 +96,7 @@ int cmd__oidmap(int argc, const char **argv)
> struct oidmap_iter iter;
> oidmap_iter_init(&map, &iter);
> - while ((entry = oidmap_iter_next(&iter)))
> + while ((entry = oidmap_iter_next(&iter, struct test_entry)))
> printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
This works because "test_entry" has the oidmap_entry at the start.
But it wouldn't work with a struct where that wasn't the case, nor would
it provide any compile-time safety (because of the cast).
Note that if we do want to support that and get type safety (and I think
it is worth doing), oidmap_get() would need similar treatment (it
returns a void pointer, but it is really a pointer to an oidmap_entry).
And I guess oidmap_put() and oidmap_remove(), which returns pointers to
next prev parent reply other threads:[~2020-04-08 22:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-08 4:06 [PATCH 1/2] oidmap: make oidmap_free independent of struct layout Abhishek Kumar
2020-04-08 4:06 ` [PATCH 2/2] oidmap: rework iterators to return typed pointer Abhishek Kumar
2020-04-08 6:02 ` [PATCH 1/2] oidmap: make oidmap_free independent of struct layout Junio C Hamano
2020-04-08 7:03 ` [PATCH v2 " Abhishek Kumar
2020-04-08 7:03 ` [PATCH v2 2/2] oidmap: rework iterators to return typed pointer Abhishek Kumar
2020-04-08 22:08 ` Jeff King [this message]
2020-04-08 21:54 ` [PATCH 1/2] oidmap: make oidmap_free independent of struct layout Jeff King
2020-04-25 2:59 ` [PATCH v3 1/3] " Abhishek Kumar
2020-04-25 2:59 ` [PATCH v3 2/3] oidmap: rework iterators to return typed pointer Abhishek Kumar
2020-04-25 2:59 ` [PATCH v3 3/3] oidmap: return oidmap_entry pointers Abhishek Kumar
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:
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
* 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.
Code repositories for project(s) associated with this public inbox
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).