* [PATCH] Replace xmalloc/memset(0) pairs with xcalloc
@ 2008-10-06 23:39 Brandon Casey
2008-10-07 5:12 ` Andreas Ericsson
0 siblings, 1 reply; 2+ messages in thread
From: Brandon Casey @ 2008-10-06 23:39 UTC (permalink / raw)
To: Git Mailing List
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>
---
builtin-merge.c | 3 +--
builtin-pack-objects.c | 4 +---
builtin-unpack-objects.c | 3 +--
merge-tree.c | 3 +--
remote.c | 3 +--
5 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/builtin-merge.c b/builtin-merge.c
index 5c65a58..dcf8987 100644
--- a/builtin-merge.c
+++ b/builtin-merge.c
@@ -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;
}
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 1158e42..59c30d1 100644
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
@@ -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++;
diff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c
index d2796b6..9f4bdd3 100644
--- a/builtin-unpack-objects.c
+++ b/builtin-unpack-objects.c
@@ -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);
diff --git a/merge-tree.c b/merge-tree.c
index 02fc10f..2d1413e 100644
--- a/merge-tree.c
+++ b/merge-tree.c
@@ -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;
diff --git a/remote.c b/remote.c
index c45d96e..a2d7ab1 100644
--- a/remote.c
+++ b/remote.c
@@ -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;
}
--
1.6.0.2.445.g1e1e0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] Replace xmalloc/memset(0) pairs with xcalloc
2008-10-06 23:39 [PATCH] Replace xmalloc/memset(0) pairs with xcalloc Brandon Casey
@ 2008-10-07 5:12 ` Andreas Ericsson
0 siblings, 0 replies; 2+ messages in thread
From: Andreas Ericsson @ 2008-10-07 5:12 UTC (permalink / raw)
To: Brandon Casey; +Cc: Git Mailing List
Brandon Casey wrote:
> Many call sites immediately initialize allocated memory with zero after
> calling xmalloc. A single call to xcalloc can replace this two-call
> sequence.
>
Nicely done. Apart from reducing LoC count, this is also a slight
optimization as we'll no longer bzero() memory handed to us directly
by the kernel (which is already nul'ed out).
--
Andreas Ericsson andreas.ericsson@op5.se
OP5 AB www.op5.se
Tel: +46 8-230225 Fax: +46 8-230231
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-10-07 5:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-06 23:39 [PATCH] Replace xmalloc/memset(0) pairs with xcalloc Brandon Casey
2008-10-07 5:12 ` Andreas Ericsson
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.