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

[PATCH v2 1/5] daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macro

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Sep 27, 2021, 12:58 UTC
Message-ID
<patch-v2-1.5-3130693b416-20210927T125715Z-avarab@gmail.com>
In-Reply-To
<cover-v2-0.5-00000000000-20210927T125715Z-avarab@gmail.com>

Remove the hostinfo_init() function added in 01cec54e135 (daemon: deglobalize hostname information, 2015-03-07) and instead initialize the "struct hostinfo" with a macro.

This is the more idiomatic pattern in the codebase, and doesn't leave us wondering when we see the *_init() function if this struct needs more complex initialization than a macro can provide.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 daemon.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/daemon.c b/daemon.c
index 5c4cbad62d0..d80d009d1a1 100644
--- a/daemon.c
+++ b/daemon.c
@@ -63,6 +63,12 @@ struct hostinfo {
 	unsigned int hostname_lookup_done:1;
 	unsigned int saw_extended_args:1;
 };
+#define HOSTINFO_INIT { \
+	.hostname = STRBUF_INIT, \
+	.canon_hostname = STRBUF_INIT, \
+	.ip_address = STRBUF_INIT, \
+	.tcp_port = STRBUF_INIT, \
+}
 
 static void lookup_hostname(struct hostinfo *hi);
 
@@ -727,15 +733,6 @@ static void lookup_hostname(struct hostinfo *hi)
 	}
 }
 
-static void hostinfo_init(struct hostinfo *hi)
-{
-	memset(hi, 0, sizeof(*hi));
-	strbuf_init(&hi->hostname, 0);
-	strbuf_init(&hi->canon_hostname, 0);
-	strbuf_init(&hi->ip_address, 0);
-	strbuf_init(&hi->tcp_port, 0);
-}
-
 static void hostinfo_clear(struct hostinfo *hi)
 {
 	strbuf_release(&hi->hostname);
@@ -760,11 +757,9 @@ static int execute(void)
 	char *line = packet_buffer;
 	int pktlen, len, i;
 	char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
-	struct hostinfo hi;
+	struct hostinfo hi = HOSTINFO_INIT;
 	struct strvec env = STRVEC_INIT;
 
-	hostinfo_init(&hi);
-
 	if (addr)
 		loginfo("Connection from %s:%s", addr, port);
 
-- 
2.33.0.1316.gb2e9b3ba3ae
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 11 of 33 in “Non-trivial designated initializer conversion”
  1. 0/6 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Sep 27, 2021
  2. 1/6 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  3. 2/6 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  4. 3/6 shortlog: use designated initializer for "struct shortlog"Ævar Arnfjörð Bjarmason, Sep 27, 2021
  5. Phillip WoodSep 27, 2021
  6. Ævar Arnfjörð BjarmasonSep 27, 2021
  7. 4/6 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  8. 5/6 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  9. 6/6 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  10. 0/5 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Sep 27, 2021
  11. 1/5 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  12. 2/5 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  13. 3/5 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  14. Junio C HamanoSep 27, 2021
  15. 4/5 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  16. Junio C HamanoSep 27, 2021
  17. Ævar Arnfjörð BjarmasonSep 27, 2021
  18. Junio C HamanoSep 27, 2021
  19. 5/5 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  20. 0/6 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Oct 1, 2021
  21. 1/6 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 1, 2021
  22. 2/6 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 1, 2021
  23. 3/6 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  24. 4/6 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  25. 5/6 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  26. 6/6 unpack-trees.[ch]: define and use a UNPACK_TREES_OPTIONS_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  27. Junio C HamanoOct 1, 2021
  28. 0/5 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Oct 2, 2021
  29. 2/5 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 2, 2021
  30. 1/5 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 2, 2021
  31. 4/5 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Oct 2, 2021
  32. 5/5 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Oct 2, 2021
  33. 3/5 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Oct 2, 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.