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

[PATCH] repository: use FREE_AND_NULL

From
René Scharfe <l.s.r@web.de>
Date
Oct 1, 2017, 14:44 UTC
Message-ID
<503c157c-70c8-7902-6170-3847c39ab0e0@web.de>

Use the macro FREE_AND_NULL to release allocated objects and clear their pointers. This is shorter and documents the intent better by combining the two related operations into one.

Patch generated with Coccinelle and contrib/coccinelle/free.cocci.
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 repository.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
diff --git a/repository.c b/repository.c
index 97c732bd48..bb2fae5446 100644
--- a/repository.c
+++ b/repository.c
@@ -200,25 +200,17 @@ int repo_submodule_init(struct repository *submodule,
 
 void repo_clear(struct repository *repo)
 {
-	free(repo->gitdir);
-	repo->gitdir = NULL;
-	free(repo->commondir);
-	repo->commondir = NULL;
-	free(repo->objectdir);
-	repo->objectdir = NULL;
-	free(repo->graft_file);
-	repo->graft_file = NULL;
-	free(repo->index_file);
-	repo->index_file = NULL;
-	free(repo->worktree);
-	repo->worktree = NULL;
-	free(repo->submodule_prefix);
-	repo->submodule_prefix = NULL;
+	FREE_AND_NULL(repo->gitdir);
+	FREE_AND_NULL(repo->commondir);
+	FREE_AND_NULL(repo->objectdir);
+	FREE_AND_NULL(repo->graft_file);
+	FREE_AND_NULL(repo->index_file);
+	FREE_AND_NULL(repo->worktree);
+	FREE_AND_NULL(repo->submodule_prefix);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
-		free(repo->config);
-		repo->config = NULL;
+		FREE_AND_NULL(repo->config);
 	}
 
 	if (repo->submodule_cache) {
@@ -228,8 +220,7 @@ void repo_clear(struct repository *repo)
 
 	if (repo->index) {
 		discard_index(repo->index);
-		free(repo->index);
-		repo->index = NULL;
+		FREE_AND_NULL(repo->index);
 	}
 }
 
-- 
2.14.2
Message 1 of 1 in “repository: use FREE_AND_NULL”
  1. repository: use FREE_AND_NULLRené Scharfe, Oct 1, 2017

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.