[klibc] [klibc:update-dash] dash: memalloc: Add growstackto helper

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


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

[klibc] dash: memalloc: Add growstackto helper

[ dash commit a9c4e4c9fc11cf1bd17d08e166405f7ab355a9f3 ]

This patch adds the growstackto helper which repeatedly calls
growstackblock until the requested size is reached.

Signed-off-by: Herbert Xu <herbert at gondor.apana.org.au>
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

---
 usr/dash/exec.c     |  4 +---
 usr/dash/memalloc.c | 20 +++++++++-----------
 usr/dash/memalloc.h |  1 +
 usr/dash/parser.c   |  4 +---
 4 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/usr/dash/exec.c b/usr/dash/exec.c
index d7ced357..c98f14c0 100644
--- a/usr/dash/exec.c
+++ b/usr/dash/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/usr/dash/memalloc.c b/usr/dash/memalloc.c
index d8e4413d..9d1de74a 100644
--- a/usr/dash/memalloc.c
+++ b/usr/dash/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/usr/dash/memalloc.h b/usr/dash/memalloc.h
index 4b5be46c..b348d9cc 100644
--- a/usr/dash/memalloc.h
+++ b/usr/dash/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/usr/dash/parser.c b/usr/dash/parser.c
index 809c6a8a..3de977c1 100644
--- a/usr/dash/parser.c
+++ b/usr/dash/parser.c
@@ -1460,9 +1460,7 @@ done:
 	/* Ignore any pushed back tokens left from the backquote parsing. */
 	if (oldstyle)
 		tokpushback = 0;
-	while (stackblocksize() <= savelen)
-		growstackblock();
-	STARTSTACKSTR(out);
+	out = growstackto(savelen + 1);
 	if (str) {
 		memcpy(out, str, savelen);
 		STADJUST(savelen, out);


More information about the klibc mailing list