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

[PATCH] alloc.c: remove alloc_raw_commit_node() function

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Jun 18, 2014, 19:52 UTC
Message-ID
<53A1EE0E.6040000@ramsay1.demon.co.uk>

In order to encapsulate the setting of the unique commit index, commit 969eba63 ("commit: push commit_index update into alloc_commit_node", 10-06-2014) introduced a (logically private) intermediary allocator function. However, this function (alloc_raw_commit_node()) was declared as a public function, which undermines its entire purpose.

Remove the alloc_raw_commit_node() function and inline its code into the (public) alloc_commit_node() function.

Noticed by sparse ("symbol 'alloc_raw_commit_node' was not declared. Should it be static?").

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Hi Jeff,

I noticed this while it was still in 'pu', but got distracted and didn't send this in time ... sorry about that! :(

My first attempt at fixing this involved changing the DEFINE_ALLOCATOR macro to include a 'scope' parameter so that I could declare the raw_commit allocator 'static'. Unfortunately, I could not pass the extern keyword as the scope parameter to all the other allocators, because that made sparse even more upset - you can't use extern on a function _definition_. That meant passing an empty argument (or a comment token) to the scope parameter. This worked for gcc 4.8.2 and clang 3.4, but I was a little concerned about portability.

This seems a better solution to me. Having said that ... as I'm typing this I realized that I could have removed the 'commit_count' variable and used 'commit_allocs' to set c->index instead! :-P Oh well ...

ATB, Ramsay Jones

 alloc.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/alloc.c b/alloc.c
index eb22a45..124d710 100644
--- a/alloc.c
+++ b/alloc.c
@@ -47,16 +47,30 @@ union any_object {
 
 DEFINE_ALLOCATOR(blob, struct blob)
 DEFINE_ALLOCATOR(tree, struct tree)
-DEFINE_ALLOCATOR(raw_commit, struct commit)
 DEFINE_ALLOCATOR(tag, struct tag)
 DEFINE_ALLOCATOR(object, union any_object)
 
+static unsigned int commit_allocs;
+
 void *alloc_commit_node(void)
 {
 	static int commit_count;
-	struct commit *c = alloc_raw_commit_node();
+	static int nr;
+	static struct commit *block;
+	struct commit *c;
+	void *ret;
+
+	if (!nr) {
+		nr = BLOCKING;
+		block = xmalloc(BLOCKING * sizeof(struct commit));
+	}
+	nr--;
+	commit_allocs++;
+	ret = block++;
+	memset(ret, 0, sizeof(struct commit));
+	c = (struct commit *) ret;
 	c->index = commit_count++;
-	return c;
+	return ret;
 }
 
 static void report(const char *name, unsigned int count, size_t size)
@@ -72,7 +86,7 @@ void alloc_report(void)
 {
 	REPORT(blob, struct blob);
 	REPORT(tree, struct tree);
-	REPORT(raw_commit, struct commit);
+	REPORT(commit, struct commit);
 	REPORT(tag, struct tag);
 	REPORT(object, union any_object);
 }
-- 
2.0.0
Next: Jeff King
Message 1 of 6 in “alloc.c: remove alloc_raw_commit_node() function”
  1. alloc.c: remove alloc_raw_commit_node() functionRamsay Jones, Jun 18, 2014
  2. Jeff KingJun 18, 2014
  3. Ramsay JonesJun 18, 2014
  4. Jeff KingJun 19, 2014
  5. Ramsay JonesJun 19, 2014
  6. Junio C HamanoJun 19, 2014

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.