Replace xmalloc/memset(0) pairs with xcalloc
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>
Brandon Casey committed
Oct 6, 2008 at 18:39 UTC
19d4b416f429ac2d3f4c225aaf1af8761bcb03dd
5 files changed
+5
-11
builtin-merge.c
+1
-2
@@ -123,8 +123,7 @@ static struct strategy *get_strategy(const char *name)
123
exit(1);
124
}
125
126
- ret = xmalloc(sizeof(struct strategy));
127
- memset(ret, 0, sizeof(struct strategy));
126
+ ret = xcalloc(1, sizeof(struct strategy));
127
ret->name = xstrdup(name);
128
return ret;
129
}
builtin-pack-objects.c
+1
-3
@@ -1369,12 +1369,10 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
1369
int window, int depth, unsigned *processed)
1370
{
1371
uint32_t i, idx = 0, count = 0;
1372
- unsigned int array_size = window * sizeof(struct unpacked);
1372
struct unpacked *array;
1373
unsigned long mem_usage = 0;
1374
1376
- array = xmalloc(array_size);
1377
- memset(array, 0, array_size);
1375
+ array = xcalloc(window, sizeof(struct unpacked));
1376
1377
for (;;) {
1378
struct object_entry *entry = *list++;
builtin-unpack-objects.c
+1
-2
@@ -477,8 +477,7 @@ static void unpack_all(void)
477
478
if (!quiet)
479
progress = start_progress("Unpacking objects", nr_objects);
480
- obj_list = xmalloc(nr_objects * sizeof(*obj_list));
481
- memset(obj_list, 0, nr_objects * sizeof(*obj_list));
480
+ obj_list = xcalloc(nr_objects, sizeof(*obj_list));
481
for (i = 0; i < nr_objects; i++) {
482
unpack_one(i);
483
display_progress(progress, i + 1);
merge-tree.c
+1
-2
@@ -158,9 +158,8 @@ static int same_entry(struct name_entry *a, struct name_entry *b)
158
159
static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
160
{
161
- struct merge_list *res = xmalloc(sizeof(*res));
161
+ struct merge_list *res = xcalloc(1, sizeof(*res));
162
163
- memset(res, 0, sizeof(*res));
163
res->stage = stage;
164
res->path = path;
165
res->mode = mode;
remote.c
+1
-2
@@ -751,8 +751,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
751
752
struct ref *alloc_ref(unsigned namelen)
753
{
754
- struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
755
- memset(ret, 0, sizeof(struct ref) + namelen);
754
+ struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);
755
return ret;
756
}
757