From: tedu Date: Mon, 5 Jan 2015 13:14:24 +0000 (+0000) Subject: simplify realloc loop by not using realloc. we don't care about old data, X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=737e3caaa961e1b84f86d85b65c435a25af1ba12;p=openbsd simplify realloc loop by not using realloc. we don't care about old data, so don't make realloc copy it. just allocate what we need. ok deraadt guenther --- diff --git a/bin/ps/fmt.c b/bin/ps/fmt.c index fc1fa4df573..c55971c18a1 100644 --- a/bin/ps/fmt.c +++ b/bin/ps/fmt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fmt.c,v 1.12 2014/05/06 20:50:42 tedu Exp $ */ +/* $OpenBSD: fmt.c,v 1.13 2015/01/05 13:14:24 tedu Exp $ */ /*- * Copyright (c) 1990, 1993, 1994 @@ -41,29 +41,22 @@ void fmt_puts(const char *s, int *leftp) { - static char *v = NULL, *nv; - static int maxlen = 0; - int len; + static char *v = NULL; + static size_t maxlen = 0; + size_t len; if (*leftp == 0) return; len = strlen(s) * 4 + 1; if (len > maxlen) { - size_t newmaxlen = maxlen; - - if (newmaxlen == 0) - newmaxlen = getpagesize(); - while (len > newmaxlen) - newmaxlen *= 2; - nv = realloc(v, newmaxlen); - if (nv == NULL) { - free(v); - v = NULL; - maxlen = 0; + free(v); + maxlen = 0; + if (len < getpagesize()) + len = getpagesize(); + v = malloc(len); + if (v == NULL) return; - } - maxlen = newmaxlen; - v = nv; + maxlen = len; } strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); if (*leftp != -1) {