[klibc] [klibc:update-dash] dash: memalloc: Avoid looping in growstackto

klibc-bot for Herbert Xu herbert at gondor.apana.org.au
Sat Mar 28 14:49:29 PDT 2020


Commit-ID:  21ceb151c758eb2384962b9ee8abc33b5bd674e9
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=21ceb151c758eb2384962b9ee8abc33b5bd674e9
Author:     Herbert Xu <herbert at gondor.apana.org.au>
AuthorDate: Thu, 31 May 2018 01:51:48 +0800
Committer:  Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sat, 28 Mar 2020 21:42:55 +0000

[klibc] dash: memalloc: Avoid looping in growstackto

[ dash commit e9cb50188b1b04b6e5e8e8ccc8874b2abcff8bb1 ]

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 <herbert at gondor.apana.org.au>
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

---
 usr/dash/memalloc.c | 16 ++++++++--------
 usr/dash/memalloc.h |  1 -
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/usr/dash/memalloc.c b/usr/dash/memalloc.c
index 9d1de74a..60637da1 100644
--- a/usr/dash/memalloc.c
+++ b/usr/dash/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/usr/dash/memalloc.h b/usr/dash/memalloc.h
index b348d9cc..b9c63dac 100644
--- a/usr/dash/memalloc.h
+++ b/usr/dash/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 *);


More information about the klibc mailing list