From patchwork Wed May 30 17:51:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 10439493 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 10D62602CC for ; Wed, 30 May 2018 17:51:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E55BC290DE for ; Wed, 30 May 2018 17:51:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D9D5529154; Wed, 30 May 2018 17:51:50 +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 50C53290DE for ; Wed, 30 May 2018 17:51:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753474AbeE3Rvu (ORCPT ); Wed, 30 May 2018 13:51:50 -0400 Received: from orcrist.hmeau.com ([104.223.48.154]:54696 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753442AbeE3Rvt (ORCPT ); Wed, 30 May 2018 13:51:49 -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 1fO5GG-0002c3-Hi; Thu, 31 May 2018 01:51:48 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1fO5GG-0007fy-E4; Thu, 31 May 2018 01:51:48 +0800 Date: Thu, 31 May 2018 01:51:48 +0800 From: Herbert Xu To: DASH Mailing List Subject: memalloc: Avoid looping in growstackto Message-ID: <20180530175148.q2blfarneijc6vwh@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 Currently growstackto will repeatedly call growstackblock until the requisite size is obtained. This is wasteful. This patch changes growstackblock to take a minimum size instead. Signed-off-by: Herbert Xu diff --git a/src/memalloc.c b/src/memalloc.c index 9d1de74..60637da 100644 --- a/src/memalloc.c +++ b/src/memalloc.c @@ -201,16 +201,16 @@ popstackmark(struct stackmark *mark) * part of the block that has been used. */ -void -growstackblock(void) +static void growstackblock(size_t min) { size_t newlen; newlen = stacknleft * 2; if (newlen < stacknleft) sh_error("Out of space"); - if (newlen < 128) - newlen += 128; + min = SHELL_ALIGN(min | 128); + if (newlen < min) + newlen += min; if (stacknxt == stackp->space && stackp != &stackbase) { struct stack_block *sp; @@ -261,15 +261,15 @@ void * growstackstr(void) { size_t len = stackblocksize(); - growstackblock(); + + growstackblock(0); return stackblock() + len; } char *growstackto(size_t len) { - while (stackblocksize() < len) - growstackblock(); - + if (stackblocksize() < len) + growstackblock(len); return stackblock(); } diff --git a/src/memalloc.h b/src/memalloc.h index b348d9c..b9c63da 100644 --- a/src/memalloc.h +++ b/src/memalloc.h @@ -55,7 +55,6 @@ void stunalloc(pointer); void pushstackmark(struct stackmark *mark, size_t len); void setstackmark(struct stackmark *); void popstackmark(struct stackmark *); -void growstackblock(void); void *growstackstr(void); char *growstackto(size_t len); char *makestrspace(size_t, char *);