diff mbox

memalloc: Add growstackto helper

Message ID 20180514155907.5oxblqfmuuk6wd4y@gondor.apana.org.au (mailing list archive)
State Superseded
Delegated to: Herbert Xu
Headers show

Commit Message

Herbert Xu May 14, 2018, 3:59 p.m. UTC
This patch adds the growstackto helper which repeatedly calls
growstackblock until the requested size is reached.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
diff mbox

Patch

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);