[PATCH] bisect: save heap memory. allocate only the required amount
- From
Arjun Sreedharan <arjun024@gmail.com>
- Date
- Aug 24, 2014, 21:55 UTC
- Message-ID
- <1408917358-2372-1-git-send-email-arjun024@gmail.com>
- In-Reply-To
- <53FA057C.3070206@ramsay1.demon.co.uk>
find and allocate the required amount instead of allocating extra 100 bytes
Signed-off-by: Arjun Sreedharan <arjun024@gmail.com> --- bisect.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/bisect.c b/bisect.c index d6e851d..a52631e 100644 --- a/bisect.c +++ b/bisect.c @@ -215,12 +215,16 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n } qsort(array, cnt, sizeof(*array), compare_commit_dist); for (p = list, i = 0; i < cnt; i++) { - struct name_decoration *r = xmalloc(sizeof(*r) + 100); + struct strbuf name = STRBUF_INIT; + struct name_decoration *r; struct object *obj = &(array[i].commit->object); - sprintf(r->name, "dist=%d", array[i].distance); + strbuf_addf(&name, "dist=%d", array[i].distance); + r = xmalloc(sizeof(*r) + name.len); + memcpy(r->name, name.buf, name.len + 1); r->next = add_decoration(&name_decoration, obj, r); p->item = array[i].commit; + strbuf_release(&name); p = p->next; } if (p)
-- 1.7.11.7