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

Re: Fix up ugly open-coded "alloc_nr()" user in object.c

From
Jeff King <peff@peff.net>
Date
Jun 16, 2007, 18:21 UTC
Message-ID
<20070616182134.GA22003@coredump.intra.peff.net>
In-Reply-To
<alpine.LFD.0.98.0706161024220.14121@woody.linux-foundation.org>
On Sat, Jun 16, 2007 at 10:30:22AM -0700, Linus Torvalds wrote:
Show 6 quoted lines
> When adding objects to the object/mode array, we used to have our own 
> alloc_nr() implementation, rather than use the normal one.
> 
> And since the normal one is arguably a bit nicer (still grows the 
> allocation exponentially, just not by more-than-doubling it every time), 
> why not just use it?

How about using the new ALLOC_GROW macro to make it even shorter? I also got rid of the aliased variables, which IMO just make it harder to see what's going on.

---
 object.c |   19 +++++--------------
 1 files changed, 5 insertions(+), 14 deletions(-)
diff --git a/object.c b/object.c
index 16793d9..064e423 100644
--- a/object.c
+++ b/object.c
@@ -240,18 +240,9 @@ void add_object_array(struct object *obj, const char *name, struct object_array
 
 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
 {
-	unsigned nr = array->nr;
-	unsigned alloc = array->alloc;
-	struct object_array_entry *objects = array->objects;
-
-	if (nr >= alloc) {
-		alloc = (alloc + 32) * 2;
-		objects = xrealloc(objects, alloc * sizeof(*objects));
-		array->alloc = alloc;
-		array->objects = objects;
-	}
-	objects[nr].item = obj;
-	objects[nr].name = name;
-	objects[nr].mode = mode;
-	array->nr = ++nr;
+	ALLOC_GROW(array->objects, array->nr, array->alloc);
+	array->objects[array->nr].item = obj;
+	array->objects[array->nr].name = name;
+	array->objects[array->nr].mode = mode;
+	array->nr++;
 }
Previous: Linus TorvaldsNext: Olivier Galibert
Message 2 of 4 in “Fix up ugly open-coded "alloc_nr()" user in object.c”
  1. Linus TorvaldsJun 16, 2007
  2. Jeff KingJun 16, 2007
  3. Olivier GalibertJun 16, 2007
  4. Jeff KingJun 16, 2007

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.