aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2019-08-30 12:57:21 +0300
committerArnold D. Robbins <arnold@skeeve.com>2019-08-30 12:57:21 +0300
commit134f9d04acffcd68c298a7f41419b0620212595e (patch)
tree8b57d66071f1312ec6bfe9236871385cec50d2e0
parent58982af68e8cbbc71b48a2ab36865ed4630b960b (diff)
downloadegawk-134f9d04acffcd68c298a7f41419b0620212595e.tar.gz
egawk-134f9d04acffcd68c298a7f41419b0620212595e.tar.bz2
egawk-134f9d04acffcd68c298a7f41419b0620212595e.zip
Restore modified use of realloc in format_tree.
-rw-r--r--ChangeLog9
-rw-r--r--builtin.c10
2 files changed, 16 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index b608e023..edf4bf57 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2019-08-29 Arnold D. Robbins <arnold@skeeve.com>
+
+ Restore a modified version of the change of
+ 2016-05-03, which used realloc to shrink the buffer.
+
+ * builtin.c (format_tree): Reduce INITIAL_OUT_SIZE to 64.
+ At the end, call realloc only if there is more
+ than (2 * INITIAL_OUT_SIZE) bytes to give back.
+
2019-08-23 Arnold D. Robbins <arnold@skeeve.com>
* main.c (main): Fix whitespace issues.
diff --git a/builtin.c b/builtin.c
index 503a386c..1c107d3b 100644
--- a/builtin.c
+++ b/builtin.c
@@ -685,7 +685,7 @@ format_tree(
int i, nc;
bool toofew = false;
char *obuf, *obufout;
- size_t osiz, ofre;
+ size_t osiz, ofre, olen_final;
const char *chbuf;
const char *s0, *s1;
int cs1;
@@ -735,7 +735,7 @@ format_tree(
static const char lchbuf[] = "0123456789abcdef";
static const char Uchbuf[] = "0123456789ABCDEF";
-#define INITIAL_OUT_SIZE 512
+#define INITIAL_OUT_SIZE 64
emalloc(obuf, char *, INITIAL_OUT_SIZE, "format_tree");
obufout = obuf;
osiz = INITIAL_OUT_SIZE;
@@ -1646,7 +1646,11 @@ mpf1:
_("too many arguments supplied for format string"));
}
bchunk(s0, s1 - s0);
- r = make_str_node(obuf, obufout - obuf, ALREADY_MALLOCED);
+ olen_final = obufout - obuf;
+#define GIVE_BACK_SIZE (INITIAL_OUT_SIZE * 2)
+ if (ofre > GIVE_BACK_SIZE)
+ erealloc(obuf, char *, olen_final + 1, "format_tree");
+ r = make_str_node(obuf, olen_final, ALREADY_MALLOCED);
obuf = NULL;
out:
{