From patchwork Mon May 14 15:59:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 10398839 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 959C8601E7 for ; Mon, 14 May 2018 15:59:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 835DE282E8 for ; Mon, 14 May 2018 15:59:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 747CC28358; Mon, 14 May 2018 15:59:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B1F0282E8 for ; Mon, 14 May 2018 15:59:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753246AbeENP7M (ORCPT ); Mon, 14 May 2018 11:59:12 -0400 Received: from orcrist.hmeau.com ([104.223.48.154]:45252 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752678AbeENP7M (ORCPT ); Mon, 14 May 2018 11:59:12 -0400 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1fIFsU-0006az-HF; Mon, 14 May 2018 23:59:10 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1fIFsR-0008O1-Po; Mon, 14 May 2018 23:59:07 +0800 Date: Mon, 14 May 2018 23:59:07 +0800 From: Herbert Xu To: DASH Mailing List Subject: memalloc: Add growstackto helper Message-ID: <20180514155907.5oxblqfmuuk6wd4y@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds the growstackto helper which repeatedly calls growstackblock until the requested size is reached. Signed-off-by: Herbert Xu diff --git a/src/exec.c b/src/exec.c index e9e29b7..3c0c3b6 100644 --- a/src/exec.c +++ b/src/exec.c @@ -195,9 +195,7 @@ padvance(const char **path, const char *name) start = *path; for (p = start ; *p && *p != ':' && *p != '%' ; p++); len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ - while (stackblocksize() < len) - growstackblock(); - q = stackblock(); + q = growstackto(len); if (p != start) { memcpy(q, start, p - start); q += p - start; diff --git a/src/memalloc.c b/src/memalloc.c index d8e4413..92a57fd 100644 --- a/src/memalloc.c +++ b/src/memalloc.c @@ -265,6 +265,14 @@ growstackstr(void) return stackblock() + len; } +char *growstackto(size_t len) +{ + while (stackblocksize() < len) + growstackblock(); + + return stackblock(); +} + /* * Called from CHECKSTRSPACE. */ @@ -273,18 +281,8 @@ char * makestrspace(size_t newlen, char *p) { size_t len = p - stacknxt; - size_t size; - - for (;;) { - size_t nleft; - - size = stackblocksize(); - nleft = size - len; - if (nleft >= newlen) - break; - growstackblock(); - } - return stackblock() + len; + + return growstackto(len + newlen) + len; } char * diff --git a/src/memalloc.h b/src/memalloc.h index 4b5be46..b348d9c 100644 --- a/src/memalloc.h +++ b/src/memalloc.h @@ -57,6 +57,7 @@ void setstackmark(struct stackmark *); void popstackmark(struct stackmark *); void growstackblock(void); void *growstackstr(void); +char *growstackto(size_t len); char *makestrspace(size_t, char *); char *stnputs(const char *, size_t, char *); char *stputs(const char *, char *); diff --git a/src/parser.c b/src/parser.c index 8e40781..2b6e044 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1450,9 +1450,7 @@ done: popfile(); tokpushback = 0; } - while (stackblocksize() <= savelen) - growstackblock(); - STARTSTACKSTR(out); + out = growstackto(savelen + 1); if (str) { memcpy(out, str, savelen); STADJUST(savelen, out);