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

Re: [PATCH v2] khash: clarify that allocations never fail

From
Jeff King <peff@peff.net>
Date
Jul 4, 2021, 09:05 UTC
Message-ID
<YOF55hIKhRrwrZah@coredump.intra.peff.net>
In-Reply-To
<3778fb28-ed19-e90e-216a-d29d72305155@web.de>
On Sat, Jul 03, 2021 at 02:57:30PM +0200, René Scharfe wrote:
Show 6 quoted lines
> We use our standard allocation functions and macros (xcalloc,
> ALLOC_ARRAY, REALLOC_ARRAY) in our version of khash.h.  They terminate
> the program on error instead, so code that's using them doesn't have to
> handle allocation failures.  Make this behavior explicit by turning
> kh_resize_ into a void function and removing the related unreachable
> error handling code.
Thanks, this looks good to me.
Show 14 quoted lines
>  	SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
>  	{																	\
>  		khint_t x;														\
>  		if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
>  			if (h->n_buckets > (h->size<<1)) {							\
> -				if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
> -					*ret = -1; return h->n_buckets;						\
> -				}														\
> -			} else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
> -				*ret = -1; return h->n_buckets;							\
> +				kh_resize_##name(h, h->n_buckets - 1); /* clear "deleted" elements */ \
> +			} else { \
> +				kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \
>  			}															\

I had to read this over again carefully, because the second "else-if" makes the conversion less obvious. I think the original would have been easier to read as:

  if (something) {
    if (do_option_one())
        ...error...
  } else {
    if (do_option_two())
        ...error...
  }
instead of:
  if (something) {
    if (do_option_one())
        ...error...
  } else if (do_option_two())
        ...error...
  }

All of which is to say the conversion here is correct, and I think as a bonus the resulting code is easier to follow. ;)

-Peff
Previous: René Scharfe
Message 12 of 12 in “khash: clarify that allocations never fail”
  1. khash: clarify that allocations never failRené Scharfe, Jul 3, 2021
  2. Jeff KingJul 3, 2021
  3. Jeff KingJul 3, 2021
  4. Ævar Arnfjörð BjarmasonJul 3, 2021
  5. René ScharfeJul 3, 2021
  6. Ævar Arnfjörð BjarmasonJul 3, 2021
  7. Jeff KingJul 4, 2021
  8. René ScharfeJul 4, 2021
  9. Jeff KingJul 4, 2021
  10. René ScharfeJul 3, 2021
  11. khash: clarify that allocations never failRené Scharfe, Jul 3, 2021
  12. Jeff KingJul 4, 2021

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.