git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] hashmap: adjust documentation to reflect reality

From
Jeff King <peff@peff.net>
Date
Nov 30, 2017, 03:07 UTC
Message-ID
<20171130030727.GA24732@sigill.intra.peff.net>
In-Reply-To
<466dd5331907754ca5c25cc83173ca895220ca81.1511999045.git.johannes.schindelin@gmx.de>
On Thu, Nov 30, 2017 at 12:51:41AM +0100, Johannes Schindelin wrote:
Show 10 quoted lines
> The hashmap API is just complicated enough that even at least one
> long-time Git contributor has to look up how to use it every time he
> finds a new use case. When that happens, it is really useful if the
> provided example code is correct...
> 
> While at it, "fix a memory leak", avoid statements before variable
> declarations, fix a const -> no-const cast, several %l specifiers (which
> want to be %ld), avoid using an undefined constant, call scanf()
> correctly, use FLEX_ALLOC_STR() where appropriate, and adjust the style
> here and there.

Heh, that's quite a list of faults for what's supposed to be simple example code. ;)

Your improvements all look good to me, and I'd be happy to see this applied as-is. But here are two possible suggestions:

Show 14 quoted lines
> diff --git a/hashmap.h b/hashmap.h
> index 7cb29a6aede..7ce79f3f72c 100644
> --- a/hashmap.h
> +++ b/hashmap.h
> @@ -18,75 +18,71 @@
>   *
>   * #define COMPARE_VALUE 1
>   *
> - * static int long2string_cmp(const struct long2string *e1,
> + * static int long2string_cmp(const void *hashmap_cmp_fn_data,
> + *                            const struct long2string *e1,
>   *                            const struct long2string *e2,
> - *                            const void *keydata, const void *userdata)
> + *                            const void *keydata)

If these struct pointers became "const void *", then we would not need to cast the function pointer here:

>   *     hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);

which would mean that the original problem you are fixing would have been caught by the compiler, rather than probably segfaulting at runtime.

My second suggestion (which I'm on the fence about) is: would it better to just say "see t/helper/test-hashmap.c for a representative example?"

I'm all for code examples in documentation, but this one is quite complex. The code in test-hashmap.c is not much more complex, and is at least guaranteed to compile and run. It doesn't show off how to combine a flex-array with a hashmap as well, but I'm not sure how important that is. So I could go either way.

-Peff
Previous: Jonathan NiederNext: Junio C Hamano
Message 3 of 7 in “hashmap: adjust documentation to reflect reality”
  1. hashmap: adjust documentation to reflect realityJohannes Schindelin, Nov 29, 2017
  2. Jonathan NiederNov 30, 2017
  3. Jeff KingNov 30, 2017
  4. Junio C HamanoDec 3, 2017
  5. Stefan BellerDec 4, 2017
  6. Johannes SchindelinDec 7, 2017
  7. Jeff KingDec 8, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.