From 6661789143eae5465ac2871dc34fb00d16789478 Mon Sep 17 00:00:00 2001 From: "W. Eric Norum" Date: Fri, 18 Jul 2003 12:33:44 +0000 Subject: [PATCH] Efficiency improvements. Cut down on copy operations. --- src/libCom/macLib/macEnv.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/libCom/macLib/macEnv.c b/src/libCom/macLib/macEnv.c index 6a096b753..a1cc10410 100644 --- a/src/libCom/macLib/macEnv.c +++ b/src/libCom/macLib/macEnv.c @@ -27,19 +27,27 @@ macEnvExpand(char *str) int destCapacity = 200; char *dest = NULL; int n; - char *ret; assert(macCreateHandle(&handle, pairs) == 0); do { destCapacity *= 2; - assert((dest = realloc(dest, destCapacity)) != 0); + /* + * Use free/malloc rather than realloc since there's no need to + * bother copying the contents if realloc needs to move the buffer + */ + free(dest); + assert((dest = malloc(destCapacity)) != 0); n = macExpandString(handle, str, dest, destCapacity); } while (n >= (destCapacity - 1)); - if (n < 0) - ret = NULL; - else - ret = epicsStrDup(dest); - free(dest); + if (n < 0) { + free(dest); + dest = NULL; + } + else { + int unused = destCapacity - ++n; + if (unused >= 20) + dest = realloc(dest, n); + } assert(macDeleteHandle(handle) == 0); - return ret; + return dest; }