diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2008-07-16 10:52:33 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2008-07-16 10:52:33 +0300 |
commit | 1d9b4d00d7f1a0ca396624411b53d4fb714ccdf8 (patch) | |
tree | 0d10e8dd276455821efb8040f9c6f56004a20ab5 /rpmio | |
parent | 889f9f5b41d3819c52aad568e75bd0ec5568c0f6 (diff) | |
download | rpm-1d9b4d00d7f1a0ca396624411b53d4fb714ccdf8.tar.gz rpm-1d9b4d00d7f1a0ca396624411b53d4fb714ccdf8.tar.bz2 rpm-1d9b4d00d7f1a0ca396624411b53d4fb714ccdf8.zip |
Make rpmExpand() smarter wrt memory allocations
- precalculate unexpanded size and allocate enough for that plus MACROBUFSIZ
for expansion
- typical allocation is way smaller than what gets allocated "just in case",
calculate expanded size and realloc to actual size to avoid wasting
memory
Diffstat (limited to 'rpmio')
-rw-r--r-- | rpmio/macro.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/rpmio/macro.c b/rpmio/macro.c index 198056f3b..ad1f94a44 100644 --- a/rpmio/macro.c +++ b/rpmio/macro.c @@ -1583,15 +1583,26 @@ rpmExpand(const char *arg, ...) goto exit; } - buf = xmalloc(blen); + /* precalculate unexpanded size on top of MACROBUFSIZ */ + va_start(ap, arg); + for (s = arg; s != NULL; s = va_arg(ap, const char *)) + blen += strlen(s); + va_end(ap); + + buf = xmalloc(blen + 1); buf[0] = '\0'; va_start(ap, arg); for (pe = buf, s = arg; s != NULL; s = va_arg(ap, const char *)) pe = stpcpy(pe, s); va_end(ap); + (void) expandMacros(NULL, NULL, buf, blen); + /* expanded output is usually much less than alloced buffer, downsize */ + blen = strlen(buf); + buf = xrealloc(buf, blen + 1); + exit: return buf; } |