{"thread":{"id":"15805","subject":"[PATCH] Replace xmalloc/memset(0) pairs with xcalloc","startedAt":"2008-10-06T23:39:10Z","lastAt":"2008-10-07T05:12:14Z","messageCount":2,"participants":["Brandon Casey","Andreas Ericsson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"92463","messageId":"48R7WYLSZ5lElWvJbfurso2ZPBQbzSCmtOgIsqTWyaA8yfW6Ndq1aA@cipher.nrlssc.navy.mil","threadId":"15805","inReplyTo":null,"subject":"[PATCH] Replace xmalloc/memset(0) pairs with xcalloc","fromName":"Brandon Casey","fromEmail":"casey@nrlssc.navy.mil","sentAt":"2008-10-06T23:39:10Z","receivedAt":"2008-10-06T23:39:10Z","isPatch":true,"sender":{"key":"drafnel@gmail.com","avatar":"https://avatars.githubusercontent.com/u/921167?v=4"},"body":"Many call sites immediately initialize allocated memory with zero after\ncalling xmalloc. A single call to xcalloc can replace this two-call\nsequence.\n\nSigned-off-by: Brandon Casey <casey@nrlssc.navy.mil>\n---\n builtin-merge.c          |    3 +--\n builtin-pack-objects.c   |    4 +---\n builtin-unpack-objects.c |    3 +--\n merge-tree.c             |    3 +--\n remote.c                 |    3 +--\n 5 files changed, 5 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin-merge.c b/builtin-merge.c\nindex 5c65a58..dcf8987 100644\n--- a/builtin-merge.c\n+++ b/builtin-merge.c\n@@ -123,8 +123,7 @@ static struct strategy *get_strategy(const char *name)\n \t\texit(1);\n \t}\n \n-\tret = xmalloc(sizeof(struct strategy));\n-\tmemset(ret, 0, sizeof(struct strategy));\n+\tret = xcalloc(1, sizeof(struct strategy));\n \tret->name = xstrdup(name);\n \treturn ret;\n }\ndiff --git a/builtin-pack-objects.c b/builtin-pack-objects.c\nindex 1158e42..59c30d1 100644\n--- a/builtin-pack-objects.c\n+++ b/builtin-pack-objects.c\n@@ -1369,12 +1369,10 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,\n \t\t\tint window, int depth, unsigned *processed)\n {\n \tuint32_t i, idx = 0, count = 0;\n-\tunsigned int array_size = window * sizeof(struct unpacked);\n \tstruct unpacked *array;\n \tunsigned long mem_usage = 0;\n \n-\tarray = xmalloc(array_size);\n-\tmemset(array, 0, array_size);\n+\tarray = xcalloc(window, sizeof(struct unpacked));\n \n \tfor (;;) {\n \t\tstruct object_entry *entry = *list++;\ndiff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c\nindex d2796b6..9f4bdd3 100644\n--- a/builtin-unpack-objects.c\n+++ b/builtin-unpack-objects.c\n@@ -477,8 +477,7 @@ static void unpack_all(void)\n \n \tif (!quiet)\n \t\tprogress = start_progress(\"Unpacking objects\", nr_objects);\n-\tobj_list = xmalloc(nr_objects * sizeof(*obj_list));\n-\tmemset(obj_list, 0, nr_objects * sizeof(*obj_list));\n+\tobj_list = xcalloc(nr_objects, sizeof(*obj_list));\n \tfor (i = 0; i < nr_objects; i++) {\n \t\tunpack_one(i);\n \t\tdisplay_progress(progress, i + 1);\ndiff --git a/merge-tree.c b/merge-tree.c\nindex 02fc10f..2d1413e 100644\n--- a/merge-tree.c\n+++ b/merge-tree.c\n@@ -158,9 +158,8 @@ static int same_entry(struct name_entry *a, struct name_entry *b)\n \n static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)\n {\n-\tstruct merge_list *res = xmalloc(sizeof(*res));\n+\tstruct merge_list *res = xcalloc(1, sizeof(*res));\n \n-\tmemset(res, 0, sizeof(*res));\n \tres->stage = stage;\n \tres->path = path;\n \tres->mode = mode;\ndiff --git a/remote.c b/remote.c\nindex c45d96e..a2d7ab1 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -751,8 +751,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n \n struct ref *alloc_ref(unsigned namelen)\n {\n-\tstruct ref *ret = xmalloc(sizeof(struct ref) + namelen);\n-\tmemset(ret, 0, sizeof(struct ref) + namelen);\n+\tstruct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);\n \treturn ret;\n }\n \n-- \n1.6.0.2.445.g1e1e0\n"},{"id":"92485","messageId":"48EAEFAE.3010504@op5.se","threadId":"15805","inReplyTo":"48R7WYLSZ5lElWvJbfurso2ZPBQbzSCmtOgIsqTWyaA8yfW6Ndq1aA@cipher.nrlssc.navy.mil","subject":"Re: [PATCH] Replace xmalloc/memset(0) pairs with xcalloc","fromName":"Andreas Ericsson","fromEmail":"ae@op5.se","sentAt":"2008-10-07T05:12:14Z","receivedAt":"2008-10-07T05:12:14Z","isPatch":true,"sender":{"key":"ae@op5.se","avatar":"https://gravatar.com/avatar/426e89595c75a8f5252dd0c989e5fabe5bcac616e68557427ad9aef6b0ca342a?d=mp&s=160"},"body":"Brandon Casey wrote:\n> Many call sites immediately initialize allocated memory with zero after\n> calling xmalloc. A single call to xcalloc can replace this two-call\n> sequence.\n> \n\nNicely done. Apart from reducing LoC count, this is also a slight\noptimization as we'll no longer bzero() memory handed to us directly\nby the kernel (which is already nul'ed out).\n\n-- \nAndreas Ericsson                   andreas.ericsson@op5.se\nOP5 AB                             www.op5.se\nTel: +46 8-230225                  Fax: +46 8-230231\n"}]}