Skip to content

Commit

Permalink
Replace xmalloc/memset(0) pairs with xcalloc
Browse files Browse the repository at this point in the history
Many call sites immediately initialize allocated memory with zero after
calling xmalloc. A single call to xcalloc can replace this two-call
sequence.

Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
  • Loading branch information
Brandon Casey authored and Shawn O. Pearce committed Oct 8, 2008
1 parent 276328f commit 19d4b41
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 11 deletions.
3 changes: 1 addition & 2 deletions builtin-merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ static struct strategy *get_strategy(const char *name)
exit(1);
}

ret = xmalloc(sizeof(struct strategy));
memset(ret, 0, sizeof(struct strategy));
ret = xcalloc(1, sizeof(struct strategy));
ret->name = xstrdup(name);
return ret;
}
Expand Down
4 changes: 1 addition & 3 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -1369,12 +1369,10 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
int window, int depth, unsigned *processed)
{
uint32_t i, idx = 0, count = 0;
unsigned int array_size = window * sizeof(struct unpacked);
struct unpacked *array;
unsigned long mem_usage = 0;

array = xmalloc(array_size);
memset(array, 0, array_size);
array = xcalloc(window, sizeof(struct unpacked));

for (;;) {
struct object_entry *entry = *list++;
Expand Down
3 changes: 1 addition & 2 deletions builtin-unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,7 @@ static void unpack_all(void)

if (!quiet)
progress = start_progress("Unpacking objects", nr_objects);
obj_list = xmalloc(nr_objects * sizeof(*obj_list));
memset(obj_list, 0, nr_objects * sizeof(*obj_list));
obj_list = xcalloc(nr_objects, sizeof(*obj_list));
for (i = 0; i < nr_objects; i++) {
unpack_one(i);
display_progress(progress, i + 1);
Expand Down
3 changes: 1 addition & 2 deletions merge-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ static int same_entry(struct name_entry *a, struct name_entry *b)

static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
{
struct merge_list *res = xmalloc(sizeof(*res));
struct merge_list *res = xcalloc(1, sizeof(*res));

memset(res, 0, sizeof(*res));
res->stage = stage;
res->path = path;
res->mode = mode;
Expand Down
3 changes: 1 addition & 2 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)

struct ref *alloc_ref(unsigned namelen)
{
struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
memset(ret, 0, sizeof(struct ref) + namelen);
struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);
return ret;
}

Expand Down

0 comments on commit 19d4b41

Please sign in to comment.