*.c *_init(): define in terms of corresponding *_INIT macro

Change the common patter in the codebase of duplicating the
initialization logic between an *_INIT macro and a
corresponding *_init() function to use the macro as the canonical
source of truth.

Now we no longer need to keep the function up-to-date with the macro
version. This implements a suggestion by Jeff King who found that
under -O2 [1] modern compilers will init new version in place without
the extra copy[1]. The performance of a single *_init() won't matter
in most cases, but even if it does we're going to be producing
efficient machine code to perform these operations.

1. https://lore.kernel.org/git/YNyrDxUO1PlGJvCn@coredump.intra.peff.net/

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2021-07-01 12:51:26 +02:00 committed by Junio C Hamano
parent 3d97ea479f
commit 5726a6b401
6 changed files with 12 additions and 15 deletions

View File

@ -10,8 +10,8 @@
void credential_init(struct credential *c) void credential_init(struct credential *c)
{ {
memset(c, 0, sizeof(*c)); struct credential blank = CREDENTIAL_INIT;
c->helpers.strdup_strings = 1; memcpy(c, &blank, sizeof(*c));
} }
void credential_clear(struct credential *c) void credential_clear(struct credential *c)

View File

@ -3,10 +3,8 @@
void jw_init(struct json_writer *jw) void jw_init(struct json_writer *jw)
{ {
strbuf_init(&jw->json, 0); struct json_writer blank = JSON_WRITER_INIT;
strbuf_init(&jw->open_stack, 0); memcpy(jw, &blank, sizeof(*jw));;
jw->need_comma = 0;
jw->pretty = 0;
} }
void jw_release(struct json_writer *jw) void jw_release(struct json_writer *jw)

View File

@ -11,9 +11,8 @@
void child_process_init(struct child_process *child) void child_process_init(struct child_process *child)
{ {
memset(child, 0, sizeof(*child)); struct child_process blank = CHILD_PROCESS_INIT;
strvec_init(&child->args); memcpy(child, &blank, sizeof(*child));
strvec_init(&child->env_array);
} }
void child_process_clear(struct child_process *child) void child_process_clear(struct child_process *child)

View File

@ -52,8 +52,8 @@ char strbuf_slopbuf[1];
void strbuf_init(struct strbuf *sb, size_t hint) void strbuf_init(struct strbuf *sb, size_t hint)
{ {
sb->alloc = sb->len = 0; struct strbuf blank = STRBUF_INIT;
sb->buf = strbuf_slopbuf; memcpy(sb, &blank, sizeof(*sb));
if (hint) if (hint)
strbuf_grow(sb, hint); strbuf_grow(sb, hint);
} }

View File

@ -25,7 +25,8 @@ static struct strmap_entry *find_strmap_entry(struct strmap *map,
void strmap_init(struct strmap *map) void strmap_init(struct strmap *map)
{ {
strmap_init_with_options(map, NULL, 1); struct strmap blank = STRMAP_INIT;
memcpy(map, &blank, sizeof(*map));
} }
void strmap_init_with_options(struct strmap *map, void strmap_init_with_options(struct strmap *map,

View File

@ -6,9 +6,8 @@ const char *empty_strvec[] = { NULL };
void strvec_init(struct strvec *array) void strvec_init(struct strvec *array)
{ {
array->v = empty_strvec; struct strvec blank = STRVEC_INIT;
array->nr = 0; memcpy(array, &blank, sizeof(*array));
array->alloc = 0;
} }
static void strvec_push_nodup(struct strvec *array, const char *value) static void strvec_push_nodup(struct strvec *array, const char *value)