aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-09 07:08:16 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-09 07:08:16 -0700
commit18f2cbf0d59c8828e9220f2a01c69bed8f450529 (patch)
tree0aa0625ff92d1bbec3c8b9d0d5117d7de8a6b20d
parent7cd5fc9a48e5fb89c02c911917c4e6a7da18607d (diff)
downloadpw-18f2cbf0d59c8828e9220f2a01c69bed8f450529.tar.gz
pw-18f2cbf0d59c8828e9220f2a01c69bed8f450529.tar.bz2
pw-18f2cbf0d59c8828e9220f2a01c69bed8f450529.zip
Avoid unnecessary realloc in resizebuf.
-rw-r--r--pw.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/pw.c b/pw.c
index acac723..6c36a90 100644
--- a/pw.c
+++ b/pw.c
@@ -1048,13 +1048,14 @@ static void sigwinch(int sig)
static char **resizebuf(char **buf, size_t nlfrom, size_t nlto)
{
- if (nlfrom > nlto)
+ if (nlfrom > nlto) {
for (size_t i = nlto; i < nlfrom; i++)
dsdrop(buf[i]);
- if ((buf = realloc(buf, sizeof *buf * nlto)) == 0)
- panic("out of memory");
- if (nlfrom < nlto)
+ } else if (nlfrom < nlto) {
+ if ((buf = realloc(buf, sizeof *buf * nlto)) == 0)
+ panic("out of memory");
memset(buf + nlfrom, 0, (nlto - nlfrom) * sizeof *buf);
+ }
return buf;
}