Efficiency improvements. Cut down on copy operations.

This commit is contained in:
W. Eric Norum
2003-07-18 12:33:44 +00:00
parent a02b3b243e
commit 6661789143
+16 -8
View File
@@ -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;
}