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

Re: [PATCH v2 18/19] OFFSETOF_VAR macro to simplify hashmap iterators

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 4, 2019, 01:18 UTC
Message-ID
<xmqqr23t8g0t.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20190924010324.22619-19-e@80x24.org>
Eric Wong <e@80x24.org> writes:
> In the future, list iterator macros (e.g. list_for_each_entry)
> may also be implemented using OFFSETOF_VAR to save hackers the
> trouble of using container_of/list_entry macros and without
> relying on non-portable `__typeof__'.

Can we add something like this as a preliminary preparation step before the series?

Subject: [PATCH] treewide: initialize pointers to hashmap entries

There are not strictly necessary, but some compilers (e.g. clang 6.0.1) apparently have trouble in construct we will use in the OFFSETOF_VAR() macro, i.e.

    ((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
when the ptr is uninitialized.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 attr.c                              | 2 +-
 blame.c                             | 4 ++--
 builtin/describe.c                  | 2 +-
 builtin/difftool.c                  | 2 +-
 config.c                            | 2 +-
 merge-recursive.c                   | 6 +++---
 revision.c                          | 4 ++--
 submodule-config.c                  | 2 +-
 t/helper/test-hashmap.c             | 2 +-
 t/helper/test-lazy-init-name-hash.c | 4 ++--
 10 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/attr.c b/attr.c
index 15f0efdf60..0c367fb84a 100644
--- a/attr.c
+++ b/attr.c
@@ -161,7 +161,7 @@ static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
 	 * field and fill each entry with its corresponding git_attr.
 	 */
 	if (size != check->all_attrs_nr) {
-		struct attr_hash_entry *e;
+		struct attr_hash_entry *e = NULL;
 		struct hashmap_iter iter;
 
 		REALLOC_ARRAY(check->all_attrs, size);
diff --git a/blame.c b/blame.c
index 6384f48133..a5f8672419 100644
--- a/blame.c
+++ b/blame.c
@@ -448,7 +448,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
 {
 	int intersection = 0;
 	struct hashmap_iter iter;
-	const struct fingerprint_entry *entry_a, *entry_b;
+	const struct fingerprint_entry *entry_a, *entry_b = NULL;
 
 	hashmap_for_each_entry(&b->map, &iter, entry_b,
 				entry /* member name */) {
@@ -467,7 +467,7 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
 {
 	struct hashmap_iter iter;
 	struct fingerprint_entry *entry_a;
-	const struct fingerprint_entry *entry_b;
+	const struct fingerprint_entry *entry_b = NULL;
 
 	hashmap_iter_init(&b->map, &iter);
 
diff --git a/builtin/describe.c b/builtin/describe.c
index 1caf98f716..42e6cc3a4f 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -330,7 +330,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
 	if (!have_util) {
 		struct hashmap_iter iter;
 		struct commit *c;
-		struct commit_name *n;
+		struct commit_name *n = NULL;
 
 		init_commit_names(&commit_names);
 		hashmap_for_each_entry(&names, &iter, n,
diff --git a/builtin/difftool.c b/builtin/difftool.c
index c280e682b2..30f3bd6219 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -344,7 +344,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	FILE *fp;
 	struct hashmap working_tree_dups, submodules, symlinks2;
 	struct hashmap_iter iter;
-	struct pair_entry *entry;
+	struct pair_entry *entry = NULL;
 	struct index_state wtindex;
 	struct checkout lstate, rstate;
 	int rc, flags = RUN_GIT_CMD, err = 0;
diff --git a/config.c b/config.c
index a4fa464ed2..6ceefb7cff 100644
--- a/config.c
+++ b/config.c
@@ -1936,7 +1936,7 @@ void git_configset_init(struct config_set *cs)
 
 void git_configset_clear(struct config_set *cs)
 {
-	struct config_set_element *entry;
+	struct config_set_element *entry = NULL;
 	struct hashmap_iter iter;
 	if (!cs->hash_initialized)
 		return;
diff --git a/merge-recursive.c b/merge-recursive.c
index 8787a40b0c..bc826fb7ac 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -2127,7 +2127,7 @@ static void handle_directory_level_conflicts(struct merge_options *opt,
 					     struct tree *merge)
 {
 	struct hashmap_iter iter;
-	struct dir_rename_entry *head_ent;
+	struct dir_rename_entry *head_ent = NULL;
 	struct dir_rename_entry *merge_ent;
 
 	struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
@@ -2566,7 +2566,7 @@ static struct string_list *get_renames(struct merge_options *opt,
 	int i;
 	struct hashmap collisions;
 	struct hashmap_iter iter;
-	struct collision_entry *e;
+	struct collision_entry *e = NULL;
 	struct string_list *renames;
 
 	compute_collisions(&collisions, dir_renames, pairs);
@@ -2838,7 +2838,7 @@ static void initial_cleanup_rename(struct diff_queue_struct *pairs,
 				   struct hashmap *dir_renames)
 {
 	struct hashmap_iter iter;
-	struct dir_rename_entry *e;
+	struct dir_rename_entry *e = NULL;
 
 	hashmap_for_each_entry(dir_renames, &iter, e,
 				ent /* member name */) {
diff --git a/revision.c b/revision.c
index 6688f89d0d..70f478666b 100644
--- a/revision.c
+++ b/revision.c
@@ -127,7 +127,7 @@ static void paths_and_oids_init(struct hashmap *map)
 static void paths_and_oids_clear(struct hashmap *map)
 {
 	struct hashmap_iter iter;
-	struct path_and_oids_entry *entry;
+	struct path_and_oids_entry *entry = NULL;
 
 	hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
 		oidset_clear(&entry->trees);
@@ -210,7 +210,7 @@ void mark_trees_uninteresting_sparse(struct repository *r,
 	unsigned has_interesting = 0, has_uninteresting = 0;
 	struct hashmap map;
 	struct hashmap_iter map_iter;
-	struct path_and_oids_entry *entry;
+	struct path_and_oids_entry *entry = NULL;
 	struct object_id *oid;
 	struct oidset_iter iter;
 
diff --git a/submodule-config.c b/submodule-config.c
index 401a9b2382..4d4d3b7dd7 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -89,7 +89,7 @@ static void free_one_config(struct submodule_entry *entry)
 static void submodule_cache_clear(struct submodule_cache *cache)
 {
 	struct hashmap_iter iter;
-	struct submodule_entry *entry;
+	struct submodule_entry *entry = NULL;
 
 	if (!cache->initialized)
 		return;
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index cc577c8956..a3bed8cc70 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -161,7 +161,7 @@ int cmd__hashmap(int argc, const char **argv)
 	while (strbuf_getline(&line, stdin) != EOF) {
 		char *cmd, *p1 = NULL, *p2 = NULL;
 		unsigned int hash = 0;
-		struct test_entry *entry;
+		struct test_entry *entry = NULL;
 
 		/* break line into command and up to two parameters */
 		cmd = strtok(line.buf, DELIM);
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
index cd1b4c9736..2cb1fa3d8c 100644
--- a/t/helper/test-lazy-init-name-hash.c
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -29,8 +29,8 @@ static void dump_run(void)
 		char name[FLEX_ARRAY];
 	};
 
-	struct dir_entry *dir;
-	struct cache_entry *ce;
+	struct dir_entry *dir = NULL;
+	struct cache_entry *ce = NULL;
 
 	read_cache();
 	if (single) {
-- 
2.23.0-680-gad10c89764
Previous: Junio C HamanoNext: Eric Wong
Message 60 of 71 in “hashmap: bugfixes, safety fixes, and WIP improvements”
  1. 00/11 hashmap: bugfixes, safety fixes, and WIP improvementsEric Wong, Aug 26, 2019
  2. 01/11 diff: use hashmap_entry_init on moved_entry.entEric Wong, Aug 26, 2019
  3. Derrick StoleeAug 27, 2019
  4. 02/11 packfile: use hashmap_entry in delta_base_cache_entryEric Wong, Aug 26, 2019
  5. 03/11 hashmap_entry_init takes "struct hashmap_entry *"Eric Wong, Aug 26, 2019
  6. Derrick StoleeAug 27, 2019
  7. Johannes SchindelinAug 28, 2019
  8. Eric WongAug 30, 2019
  9. Johannes SchindelinSep 2, 2019
  10. 04/11 hashmap_entry: detect improper initializationEric Wong, Aug 26, 2019
  11. Johannes SchindelinAug 27, 2019
  12. Eric WongAug 27, 2019
  13. Junio C HamanoAug 27, 2019
  14. Johannes SchindelinAug 28, 2019
  15. Phillip WoodAug 28, 2019
  16. Eric WongAug 30, 2019
  17. 04/11 coccicheck: detect hashmap_entry.hash assignmentEric Wong, Sep 8, 2019
  18. Junio C HamanoSep 9, 2019
  19. 05/11 hashmap_get_next takes "const struct hashmap_entry *"Eric Wong, Aug 26, 2019
  20. 06/11 hashmap_add takes "struct hashmap_entry *"Eric Wong, Aug 26, 2019
  21. 07/11 hashmap_get takes "const struct hashmap_entry *"Eric Wong, Aug 26, 2019
  22. 08/11 hashmap_remove takes "const struct hashmap_entry *"Eric Wong, Aug 26, 2019
  23. 09/11 hashmap_put takes "struct hashmap_entry *"Eric Wong, Aug 26, 2019
  24. 10/11 introduce container_of macroEric Wong, Aug 26, 2019
  25. Derrick StoleeAug 27, 2019
  26. Phillip WoodAug 28, 2019
  27. Eric WongAug 30, 2019
  28. 11/11 hashmap_get_next returns "struct hashmap_entry *"Eric Wong, Aug 26, 2019
  29. Derrick StoleeAug 27, 2019
  30. Eric WongAug 30, 2019
  31. 00/19 hashmap bug/safety/ease-of-use fixesEric Wong, Sep 24, 2019
  32. 01/19 diff: use hashmap_entry_init on moved_entry.entEric Wong, Sep 24, 2019
  33. 02/19 coccicheck: detect hashmap_entry.hash assignmentEric Wong, Sep 24, 2019
  34. Derrick StoleeSep 25, 2019
  35. 03/19 packfile: use hashmap_entry in delta_base_cache_entryEric Wong, Sep 24, 2019
  36. 04/19 hashmap_entry_init takes "struct hashmap_entry *"Eric Wong, Sep 24, 2019
  37. Derrick StoleeSep 25, 2019
  38. 05/19 hashmap_get_next takes "const struct hashmap_entry *"Eric Wong, Sep 24, 2019
  39. 06/19 hashmap_add takes "struct hashmap_entry *"Eric Wong, Sep 24, 2019
  40. 07/19 hashmap_get takes "const struct hashmap_entry *"Eric Wong, Sep 24, 2019
  41. Derrick StoleeSep 25, 2019
  42. Eric WongSep 30, 2019
  43. 08/19 hashmap_remove takes "const struct hashmap_entry *"Eric Wong, Sep 24, 2019
  44. Derrick StoleeSep 25, 2019
  45. 09/19 hashmap_put takes "struct hashmap_entry *"Eric Wong, Sep 24, 2019
  46. 10/19 introduce container_of macroEric Wong, Sep 24, 2019
  47. Derrick StoleeSep 25, 2019
  48. Eric WongSep 30, 2019
  49. 11/19 hashmap_get_next returns "struct hashmap_entry *"Eric Wong, Sep 24, 2019
  50. Derrick StoleeSep 25, 2019
  51. 12/19 hashmap: use *_entry APIs to wrap container_ofEric Wong, Sep 24, 2019
  52. Derrick StoleeSep 25, 2019
  53. 13/19 hashmap_get{,_from_hash} return "struct hashmap_entry *"Eric Wong, Sep 24, 2019
  54. 14/19 hashmap_cmp_fn takes hashmap_entry paramsEric Wong, Sep 24, 2019
  55. 15/19 hashmap: use *_entry APIs for iterationEric Wong, Sep 24, 2019
  56. 16/19 hashmap: hashmap_{put,remove} return hashmap_entry *Eric Wong, Sep 24, 2019
  57. 17/19 hashmap: introduce hashmap_free_entriesEric Wong, Sep 24, 2019
  58. 18/19 OFFSETOF_VAR macro to simplify hashmap iteratorsEric Wong, Sep 24, 2019
  59. Junio C HamanoSep 30, 2019
  60. Junio C HamanoOct 4, 2019
  61. Eric WongOct 4, 2019
  62. Junio C HamanoOct 4, 2019
  63. Eric WongOct 4, 2019
  64. Junio C HamanoOct 6, 2019
  65. 19/19 hashmap: remove type arg from hashmap_{get,put,remove}_entryEric Wong, Sep 24, 2019
  66. Derrick StoleeSep 25, 2019
  67. Derrick StoleeSep 25, 2019
  68. Johannes SchindelinSep 26, 2019
  69. Eric WongSep 30, 2019
  70. Phillip WoodSep 26, 2019
  71. Junio C HamanoSep 29, 2019

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.