]> git.ipfire.org Git - thirdparty/git.git/commitdiff
*.c *_init(): define in terms of corresponding *_INIT macro
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 1 Jul 2021 10:51:26 +0000 (12:51 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Jul 2021 19:32:22 +0000 (12:32 -0700)
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>
credential.c
json-writer.c
run-command.c
strbuf.c
strmap.c
strvec.c

index e5202fbef261cce258019fbbaa98e687d0d044d3..3c05c7c669166f3ececfaa7fb9ffaac8dbe6ffbe 100644 (file)
@@ -10,8 +10,8 @@
 
 void credential_init(struct credential *c)
 {
-       memset(c, 0, sizeof(*c));
-       c->helpers.strdup_strings = 1;
+       struct credential blank = CREDENTIAL_INIT;
+       memcpy(c, &blank, sizeof(*c));
 }
 
 void credential_clear(struct credential *c)
index aadb9dbddc33ef38a45342defd5fcccfd9c07d57..f1cfd8fa8c664bfca0dcaada59e59af8dd2db38a 100644 (file)
@@ -3,10 +3,8 @@
 
 void jw_init(struct json_writer *jw)
 {
-       strbuf_init(&jw->json, 0);
-       strbuf_init(&jw->open_stack, 0);
-       jw->need_comma = 0;
-       jw->pretty = 0;
+       struct json_writer blank = JSON_WRITER_INIT;
+       memcpy(jw, &blank, sizeof(*jw));;
 }
 
 void jw_release(struct json_writer *jw)
index be6bc128cd9df2f61211cb7b8f9552b2d745cc0a..8750df16d89e8cc7bf97bf877940dcb2d0b270ae 100644 (file)
@@ -11,9 +11,8 @@
 
 void child_process_init(struct child_process *child)
 {
-       memset(child, 0, sizeof(*child));
-       strvec_init(&child->args);
-       strvec_init(&child->env_array);
+       struct child_process blank = CHILD_PROCESS_INIT;
+       memcpy(child, &blank, sizeof(*child));
 }
 
 void child_process_clear(struct child_process *child)
index 4df30b45494de819825fa32dca66040ff0882129..c8a5789694cf805799a288b66e517bedf786e479 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -52,8 +52,8 @@ char strbuf_slopbuf[1];
 
 void strbuf_init(struct strbuf *sb, size_t hint)
 {
-       sb->alloc = sb->len = 0;
-       sb->buf = strbuf_slopbuf;
+       struct strbuf blank = STRBUF_INIT;
+       memcpy(sb, &blank, sizeof(*sb));
        if (hint)
                strbuf_grow(sb, hint);
 }
index 4fb9f6100eccb7b4b3bcdd18d2801cc7b979807f..ee48635708219e66bf03bad6f5e9100c766d2fb7 100644 (file)
--- a/strmap.c
+++ b/strmap.c
@@ -25,7 +25,8 @@ static struct strmap_entry *find_strmap_entry(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,
index 21dce0a7a4d211f9810a95682fcd7cfe4635b6a2..61a76ce6cb920f33744e82111c800da8439ed975 100644 (file)
--- a/strvec.c
+++ b/strvec.c
@@ -6,9 +6,8 @@ const char *empty_strvec[] = { NULL };
 
 void strvec_init(struct strvec *array)
 {
-       array->v = empty_strvec;
-       array->nr = 0;
-       array->alloc = 0;
+       struct strvec blank = STRVEC_INIT;
+       memcpy(array, &blank, sizeof(*array));
 }
 
 static void strvec_push_nodup(struct strvec *array, const char *value)