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

Re: [PATCH 1/5] *.h: move some *_INIT to designated initializers

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Jul 1, 2021, 15:00 UTC
Message-ID
<CAN0heSpsg=bwX9gbYfeWYqUbuen-zArR69FWxNmdtcQW_RubzA@mail.gmail.com>
In-Reply-To
<patch-1.5-9a92e7c216f-20210701T104855Z-avarab@gmail.com>
On Thu, 1 Jul 2021 at 12:54, Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
Show 5 quoted lines
>
> Move *_INIT macros I'll use in a subsequent commits to designated
> initializers. This isn't required for those follow-up changes, but
> since I'm changing things in this are let's use the modern pattern
> over the old one while we're at it.
s/are/area/, maybe even s/area/area,/ on top.
> -#define CREDENTIAL_INIT { STRING_LIST_INIT_DUP }
> +#define CREDENTIAL_INIT { \
> +       .helpers = STRING_LIST_INIT_DUP, \
> +}

I've verified that all of these designated initializers assign the exact same fields before and after this commit, e.g., `helpers` actually is what comes first in the struct.

> -#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
> -#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1, NULL }
> +#define STRING_LIST_INIT_NODUP { 0 }
> +#define STRING_LIST_INIT_DUP   { .strdup_strings = 1 }

This "NODUP" one is a bit of an odd case though. You don't actually change it to use designated initializers, as the patch says. Instead you change it in a slightly different way. In a sense, you assign the literal zero not to strdup_strings, but to the first field, which is a pointer, where I would have expected NULL.

I think there's been some recent-ish "we can assign `{ 0 }` even if the first field is a pointer, because it's idiomatic and works out fine" even if assigning 0 to a pointer looks a bit odd. So it might not be wrong as such, but it doesn't match what the commit message claims, and I think it would be more clear to use `{ .strdup_strings = 0 }` here: We're explicitly interested in *not* duplicating the strings. It's not just some default "let's leave everything as zero/NULL".

Martin
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 5 of 10 in “*.[ch]: don't duplicate *_init() and *_INIT logic”
  1. 0/5 *.[ch]: don't duplicate *_init() and *_INIT logicÆvar Arnfjörð Bjarmason, Jul 1, 2021
  2. 3/5 dir.[ch]: replace dir_init() with DIR_INITÆvar Arnfjörð Bjarmason, Jul 1, 2021
  3. 2/5 *.c *_init(): define in terms of corresponding *_INIT macroÆvar Arnfjörð Bjarmason, Jul 1, 2021
  4. 1/5 *.h: move some *_INIT to designated initializersÆvar Arnfjörð Bjarmason, Jul 1, 2021
  5. Martin ÅgrenJul 1, 2021
  6. Junio C HamanoJul 1, 2021
  7. Ævar Arnfjörð BjarmasonJul 1, 2021
  8. 5/5 string-list.h users: change to use *_{nodup,dup}()Ævar Arnfjörð Bjarmason, Jul 1, 2021
  9. 4/5 string-list.[ch]: add a string_list_init_{nodup,dup}()Ævar Arnfjörð Bjarmason, Jul 1, 2021
  10. Jeff KingJul 1, 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.