Skip to content

Commit 5726a6b

Browse files
avargitster
authored andcommitted
*.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/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d97ea4 commit 5726a6b

File tree

6 files changed

+12
-15
lines changed

6 files changed

+12
-15
lines changed

credential.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
void credential_init(struct credential *c)
1212
{
13-
memset(c, 0, sizeof(*c));
14-
c->helpers.strdup_strings = 1;
13+
struct credential blank = CREDENTIAL_INIT;
14+
memcpy(c, &blank, sizeof(*c));
1515
}
1616

1717
void credential_clear(struct credential *c)

json-writer.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
void jw_init(struct json_writer *jw)
55
{
6-
strbuf_init(&jw->json, 0);
7-
strbuf_init(&jw->open_stack, 0);
8-
jw->need_comma = 0;
9-
jw->pretty = 0;
6+
struct json_writer blank = JSON_WRITER_INIT;
7+
memcpy(jw, &blank, sizeof(*jw));;
108
}
119

1210
void jw_release(struct json_writer *jw)

run-command.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
void child_process_init(struct child_process *child)
1313
{
14-
memset(child, 0, sizeof(*child));
15-
strvec_init(&child->args);
16-
strvec_init(&child->env_array);
14+
struct child_process blank = CHILD_PROCESS_INIT;
15+
memcpy(child, &blank, sizeof(*child));
1716
}
1817

1918
void child_process_clear(struct child_process *child)

strbuf.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ char strbuf_slopbuf[1];
5252

5353
void strbuf_init(struct strbuf *sb, size_t hint)
5454
{
55-
sb->alloc = sb->len = 0;
56-
sb->buf = strbuf_slopbuf;
55+
struct strbuf blank = STRBUF_INIT;
56+
memcpy(sb, &blank, sizeof(*sb));
5757
if (hint)
5858
strbuf_grow(sb, hint);
5959
}

strmap.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ static struct strmap_entry *find_strmap_entry(struct strmap *map,
2525

2626
void strmap_init(struct strmap *map)
2727
{
28-
strmap_init_with_options(map, NULL, 1);
28+
struct strmap blank = STRMAP_INIT;
29+
memcpy(map, &blank, sizeof(*map));
2930
}
3031

3132
void strmap_init_with_options(struct strmap *map,

strvec.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ const char *empty_strvec[] = { NULL };
66

77
void strvec_init(struct strvec *array)
88
{
9-
array->v = empty_strvec;
10-
array->nr = 0;
11-
array->alloc = 0;
9+
struct strvec blank = STRVEC_INIT;
10+
memcpy(array, &blank, sizeof(*array));
1211
}
1312

1413
static void strvec_push_nodup(struct strvec *array, const char *value)

0 commit comments

Comments
 (0)