From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4747 invoked by alias); 3 Dec 2015 23:31:54 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 37296 Received: (qmail 19125 invoked from network); 3 Dec 2015 23:31:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= daniel.shahaf.name; h=content-type:date:from:message-id :mime-version:subject:to:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=cGV WJBNN5zyjjGeuZvgYho+dn1E=; b=XzISA7xuB0Qx8zORaLeix8iGmiry/RQV9Kx AZRYpTezBnVRycQNctNNXzoQcRdLtPLq0MRek9sR/KgSZhtE9JwiYFL/DdEZMbyF mP7ZQpf3VLNPGyBO3uX59RqOxLZLgkO4Qr/SfyzGNKbr4QmlQKYlhq+LljBM+Tp9 d1cf3C2o= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=cG VWJBNN5zyjjGeuZvgYho+dn1E=; b=b8SHyp5ITK6a6FzllvqdcrMzWKcRuCG4mX UjFjaUvEuAPMnOkiW6yvAMsnPl6DUUcgn03RtVfRNKf7RulUGW0p0edQDjbzr2Ga q4anUlYMnBqbxGiuSq6c42l0IHh/mC6UG020AHKbCtv6Et5ghn0zwaA9XrfFe3Xs zKtROy31I= X-Sasl-enc: qqhzWuugINR6NfEBiRLaJzV8IqzcsP1nWyaTOCcrwm/o 1449185511 Date: Thu, 3 Dec 2015 23:31:49 +0000 From: Daniel Shahaf To: zsh-workers@zsh.org Subject: [PATCH] Avoid needless calloc(). Message-ID: <20151203233149.GC1955@tarsus.local2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) --- All these places calloc() N bytes and then overwrite all of them. Src/utils.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Src/utils.c b/Src/utils.c index 4640970..ca810de 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -2861,11 +2861,12 @@ spckword(char **s, int hist, int cmd, int ask) if (strncmp(guess, best, preflen)) return; /* replace the temporarily expanded prefix with the original */ - u = (char *) hcalloc(t - *s + strlen(best + preflen) + 1); + u = (char *) zhalloc(t - *s + strlen(best + preflen) + 1); strncpy(u, *s, t - *s); strcpy(u + (t - *s), best + preflen); } else { - u = (char *) hcalloc(strlen(best) + 2); + u = (char *) zhalloc(strlen(best) + 2); + *u = '\0'; strcpy(u + 1, best); } best = u; @@ -3204,7 +3205,7 @@ zjoin(char **arr, int delim, int heap) len += strlen(*s) + 1 + (imeta(delim) ? 1 : 0); if (!len) return heap? "" : ztrdup(""); - ptr = ret = (heap ? (char *) hcalloc(len) : (char *) zshcalloc(len)); + ptr = ret = (char *) (heap ? zhalloc(len) : zalloc(len)); for (s = arr; *s; s++) { strucpy(&ptr, *s); if (imeta(delim)) { @@ -3290,7 +3291,8 @@ spacesplit(char *s, int allownull, int heap, int quote) int l = sizeof(*ret) * (wordcount(s, NULL, -!allownull) + 1); char *(*dup)(const char *) = (heap ? dupstring : ztrdup); - ptr = ret = (heap ? (char **) hcalloc(l) : (char **) zshcalloc(l)); + /* ### TODO: s/calloc/alloc/ */ + ptr = ret = (char **) (heap ? hcalloc(l) : zshcalloc(l)); if (quote) { /* @@ -3320,8 +3322,8 @@ spacesplit(char *s, int allownull, int heap, int quote) t = s; (void)findsep(&s, NULL, quote); if (s > t || allownull) { - *ptr = (heap ? (char *) hcalloc((s - t) + 1) : - (char *) zshcalloc((s - t) + 1)); + *ptr = (char *) (heap ? zhalloc((s - t) + 1) : + zalloc((s - t) + 1)); ztrncpy(*ptr++, t, s - t); } else *ptr++ = dup(nulstring); @@ -3511,7 +3513,7 @@ sepjoin(char **s, char *sep, int heap) } sl = strlen(sep); for (t = s, l = 1 - sl; *t; l += strlen(*t) + sl, t++); - r = p = (heap ? (char *) hcalloc(l) : (char *) zshcalloc(l)); + r = p = (char *) (heap ? zhalloc(l) : zalloc(l)); t = s; while (*t) { strucpy(&p, *t); @@ -3538,14 +3540,14 @@ sepsplit(char *s, char *sep, int allownull, int heap) sl = strlen(sep); n = wordcount(s, sep, 1); - r = p = (heap ? (char **) hcalloc((n + 1) * sizeof(char *)) : - (char **) zshcalloc((n + 1) * sizeof(char *))); + r = p = (char **) (heap ? zhalloc((n + 1) * sizeof(char *)) : + zalloc((n + 1) * sizeof(char *))); for (t = s; n--;) { tt = t; (void)findsep(&t, sep, 0); - *p = (heap ? (char *) hcalloc(t - tt + 1) : - (char *) zshcalloc(t - tt + 1)); + *p = (char *) (heap ? zhalloc(t - tt + 1) : + zalloc(t - tt + 1)); strncpy(*p, tt, t - tt); (*p)[t - tt] = '\0'; p++; -- 2.1.4