diff options
author | David Reveman <davidr@novell.com> | 2007-07-11 13:02:19 -0400 |
---|---|---|
committer | David Reveman <davidr@novell.com> | 2007-07-11 13:02:19 -0400 |
commit | a5fbfa5647e709786ca5d81575905990d37b1287 (patch) | |
tree | f3a76a79da1d3af59fbafc31fc5703dd1329a817 /src/fragment.c | |
parent | 8af1b5a06eb8fff1b5fcb87790c60c7b04df9c03 (diff) |
Clean up and avoid some unnecessary heap allocation in
addDataOpToFunctionData and addBlendOpToFunctionData.
Diffstat (limited to 'src/fragment.c')
-rw-r--r-- | src/fragment.c | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/src/fragment.c b/src/fragment.c index 2b11df34..a1c9e6e4 100644 --- a/src/fragment.c +++ b/src/fragment.c @@ -34,8 +34,6 @@ #define COMP_FUNCTION_ARB_MASK (1 << 0) #define COMP_FUNCTION_MASK (COMP_FUNCTION_ARB_MASK) -#define BUFFER_SIZE 1024 - struct _CompProgram { struct _CompProgram *next; @@ -1037,7 +1035,7 @@ addDataOpToFunctionData (CompFunctionData *data, ...) { int index = data->nBody; - int size = BUFFER_SIZE; + int size = strlen (str) + 1; int n; char *fStr; char *tmp; @@ -1046,39 +1044,41 @@ addDataOpToFunctionData (CompFunctionData *data, if (!allocBodyOpInFunctionData (data)) return FALSE; - if ((fStr = malloc (size)) == NULL) + fStr = malloc (size); + if (!fStr) return FALSE; while (1) { /* Try to print in the allocated space. */ - va_start(ap, str); + va_start (ap, str); n = vsnprintf (fStr, size, str, ap); - va_end(ap); - + va_end (ap); + /* If that worked, leave the loop. */ if (n > -1 && n < size) break; - + /* Else try again with more space. */ - if (n > -1) /* glibc 2.1 */ - size = n+1; /* precisely what is needed */ - else /* glibc 2.0 */ - size *= 2; /* twice the old size */ - - if ((tmp = realloc (fStr, size)) == NULL) + if (n > -1) /* glibc 2.1 */ + size = n + 1; /* precisely what is needed */ + else /* glibc 2.0 */ + size++; /* one more than the old size */ + + tmp = realloc (fStr, size); + if (!tmp) { - free(fStr); + free (fStr); return FALSE; - } else { + } + else + { fStr = tmp; } } data->body[index].type = CompOpTypeData; - data->body[index].data.data = strdup (fStr); - - free (fStr); + data->body[index].data.data = fStr; return TRUE; } @@ -1089,7 +1089,7 @@ addBlendOpToFunctionData (CompFunctionData *data, ...) { int index = data->nBody; - int size = BUFFER_SIZE; + int size = strlen (str) + 1; int n; char *fStr; char *tmp; @@ -1098,39 +1098,41 @@ addBlendOpToFunctionData (CompFunctionData *data, if (!allocBodyOpInFunctionData (data)) return FALSE; - if ((fStr = malloc (size)) == NULL) + fStr = malloc (size); + if (!fStr) return FALSE; while (1) { /* Try to print in the allocated space. */ - va_start(ap, str); + va_start (ap, str); n = vsnprintf (fStr, size, str, ap); - va_end(ap); - + va_end (ap); + /* If that worked, leave the loop. */ if (n > -1 && n < size) break; - + /* Else try again with more space. */ - if (n > -1) /* glibc 2.1 */ - size = n+1; /* precisely what is needed */ - else /* glibc 2.0 */ - size *= 2; /* twice the old size */ - - if ((tmp = realloc (fStr, size)) == NULL) + if (n > -1) /* glibc 2.1 */ + size = n + 1; /* precisely what is needed */ + else /* glibc 2.0 */ + size++; /* one more than the old size */ + + tmp = realloc (fStr, size); + if (!tmp) { - free(fStr); + free (fStr); return FALSE; - } else { + } + else + { fStr = tmp; } } data->body[index].type = CompOpTypeDataBlend; - data->body[index].data.data = strdup (fStr); - - free (fStr); + data->body[index].data.data = fStr; return TRUE; } |