[fcstr,fcxml] Don't copy FcStrBuf contents when we would free it soon

We can simply NUL-terminate the buffer and use it.  Reduces number of
mallocs called from FcStrBufDone in my small test from 631 down to 66.
This commit is contained in:
Behdad Esfahbod 2009-03-11 14:07:15 -04:00
parent 7d35c11b33
commit 3ed70071cd
3 changed files with 30 additions and 14 deletions

View File

@ -1018,6 +1018,9 @@ FcStrBufDestroy (FcStrBuf *buf);
FcPrivate FcChar8 * FcPrivate FcChar8 *
FcStrBufDone (FcStrBuf *buf); FcStrBufDone (FcStrBuf *buf);
FcPrivate FcChar8 *
FcStrBufDoneStatic (FcStrBuf *buf);
FcPrivate FcBool FcPrivate FcBool
FcStrBufChar (FcStrBuf *buf, FcChar8 c); FcStrBufChar (FcStrBuf *buf, FcChar8 c);

View File

@ -754,6 +754,19 @@ FcStrBufDone (FcStrBuf *buf)
return ret; return ret;
} }
FcChar8 *
FcStrBufDoneStatic (FcStrBuf *buf)
{
FcChar8 *ret;
FcStrBufChar (buf, '\0');
if (buf->failed)
return NULL;
return buf->buf;
}
FcBool FcBool
FcStrBufChar (FcStrBuf *buf, FcChar8 c) FcStrBufChar (FcStrBuf *buf, FcChar8 c)
{ {

View File

@ -1114,7 +1114,7 @@ FcParseInt (FcConfigParse *parse)
if (!parse->pstack) if (!parse->pstack)
return; return;
s = FcStrBufDone (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
@ -1126,7 +1126,7 @@ FcParseInt (FcConfigParse *parse)
FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s); FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s);
else else
FcVStackPushInteger (parse, l); FcVStackPushInteger (parse, l);
FcStrFree (s); FcStrBufDestroy (&parse->pstack->str);
} }
/* /*
@ -1195,7 +1195,7 @@ FcParseDouble (FcConfigParse *parse)
if (!parse->pstack) if (!parse->pstack)
return; return;
s = FcStrBufDone (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
@ -1207,7 +1207,7 @@ FcParseDouble (FcConfigParse *parse)
FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s); FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s);
else else
FcVStackPushDouble (parse, d); FcVStackPushDouble (parse, d);
FcStrFree (s); FcStrBufDestroy (&parse->pstack->str);
} }
static void static void
@ -1283,14 +1283,14 @@ FcParseBool (FcConfigParse *parse)
if (!parse->pstack) if (!parse->pstack)
return; return;
s = FcStrBufDone (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
return; return;
} }
FcVStackPushBool (parse, FcConfigLexBool (parse, s)); FcVStackPushBool (parse, FcConfigLexBool (parse, s));
FcStrFree (s); FcStrBufDestroy (&parse->pstack->str);
} }
static FcBool static FcBool
@ -1370,14 +1370,14 @@ FcParseFamily (FcConfigParse *parse)
if (!parse->pstack) if (!parse->pstack)
return; return;
s = FcStrBufDone (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
return; return;
} }
expr = FcExprCreateString (s); expr = FcExprCreateString (s);
FcStrFree (s); FcStrBufDestroy (&parse->pstack->str);
if (expr) if (expr)
FcVStackPushExpr (parse, FcVStackFamily, expr); FcVStackPushExpr (parse, FcVStackFamily, expr);
} }
@ -1636,7 +1636,7 @@ FcParseInclude (FcConfigParse *parse)
const FcChar8 *i; const FcChar8 *i;
FcBool ignore_missing = FcFalse; FcBool ignore_missing = FcFalse;
s = FcStrBufDone (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
@ -1647,7 +1647,7 @@ FcParseInclude (FcConfigParse *parse)
ignore_missing = FcTrue; ignore_missing = FcTrue;
if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing)) if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing))
parse->error = FcTrue; parse->error = FcTrue;
FcStrFree (s); FcStrBufDestroy (&parse->pstack->str);
} }
typedef struct _FcOpMap { typedef struct _FcOpMap {
@ -2053,7 +2053,7 @@ FcEndElement(void *userData, const XML_Char *name)
case FcElementFontconfig: case FcElementFontconfig:
break; break;
case FcElementDir: case FcElementDir:
data = FcStrBufDone (&parse->pstack->str); data = FcStrBufDoneStatic (&parse->pstack->str);
if (!data) if (!data)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
@ -2132,7 +2132,7 @@ FcEndElement(void *userData, const XML_Char *name)
if (!FcConfigAddDir (parse->config, data)) if (!FcConfigAddDir (parse->config, data))
FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data); FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data);
} }
FcStrFree (data); FcStrBufDestroy (&parse->pstack->str);
break; break;
case FcElementCacheDir: case FcElementCacheDir:
data = FcStrBufDone (&parse->pstack->str); data = FcStrBufDone (&parse->pstack->str);
@ -2174,14 +2174,14 @@ FcEndElement(void *userData, const XML_Char *name)
break; break;
case FcElementCache: case FcElementCache:
data = FcStrBufDone (&parse->pstack->str); data = FcStrBufDoneStatic (&parse->pstack->str);
if (!data) if (!data)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
break; break;
} }
/* discard this data; no longer used */ /* discard this data; no longer used */
FcStrFree (data); FcStrBufDestroy (&parse->pstack->str);
break; break;
case FcElementInclude: case FcElementInclude:
FcParseInclude (parse); FcParseInclude (parse);