Skip to content

Commit fb79947

Browse files
rscharfegitster
authored andcommitted
pack-objects: use free()+xcalloc() instead of xrealloc()+memset()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bce14aa commit fb79947

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ static void rehash_objects(struct packing_data *pdata)
4747
if (pdata->index_size < 1024)
4848
pdata->index_size = 1024;
4949

50-
pdata->index = xrealloc(pdata->index, sizeof(uint32_t) * pdata->index_size);
51-
memset(pdata->index, 0, sizeof(int) * pdata->index_size);
50+
free(pdata->index);
51+
pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
5252

5353
entry = pdata->objects;
5454

0 commit comments

Comments
 (0)