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

[PATCH] pack-objects: use free()+xcalloc() instead of xrealloc()+memset()

From
René Scharfe <l.s.r@web.de>
Date
Jun 1, 2014, 11:07 UTC
Message-ID
<538B0969.9080409@web.de>

Whenever the hash table becomes too small then its size is increased, the original part (and the added space) is zerod out using memset(), and the table is rebuilt from scratch.

Simplify this proceess by returning the old memory using free() and allocating the new buffer using xcalloc(), which already clears the buffer for us. That way we avoid copying the old hash table contents needlessly inside xrealloc().

While at it, use the first array member with sizeof instead of a specific type. The old code used uint32_t and int, while index is actually an array of int32_t. Their sizes are the same basically everywhere, so it's not actually a problem, but the new code is cleaner and doesn't have to be touched should the type be changed.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 pack-objects.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pack-objects.c b/pack-objects.c
index d01d851..4f36c32 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -47,8 +47,8 @@ static void rehash_objects(struct packing_data *pdata)
 	if (pdata->index_size < 1024)
 		pdata->index_size = 1024;
 
-	pdata->index = xrealloc(pdata->index, sizeof(uint32_t) * pdata->index_size);
-	memset(pdata->index, 0, sizeof(int) * pdata->index_size);
+	free(pdata->index);
+	pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
 
 	entry = pdata->objects;
 
-- 
2.0.0
Next: Jeff King
Message 1 of 6 in “pack-objects: use free()+xcalloc() instead of xrealloc()+memset()”
  1. pack-objects: use free()+xcalloc() instead of xrealloc()+memset()René Scharfe, Jun 1, 2014
  2. Jeff KingJun 2, 2014
  3. David KastrupJun 2, 2014
  4. Jeff KingJun 2, 2014
  5. Junio C HamanoJun 2, 2014
  6. Jeff KingJun 3, 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.