From 3ed70071cdc8a03229c009f5565c23948264a5e0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2009 14:07:15 -0400 Subject: [PATCH] [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. --- src/fcint.h | 3 +++ src/fcstr.c | 13 +++++++++++++ src/fcxml.c | 28 ++++++++++++++-------------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/fcint.h b/src/fcint.h index be520e7..ec6a01c 100644 --- a/src/fcint.h +++ b/src/fcint.h @@ -1018,6 +1018,9 @@ FcStrBufDestroy (FcStrBuf *buf); FcPrivate FcChar8 * FcStrBufDone (FcStrBuf *buf); +FcPrivate FcChar8 * +FcStrBufDoneStatic (FcStrBuf *buf); + FcPrivate FcBool FcStrBufChar (FcStrBuf *buf, FcChar8 c); diff --git a/src/fcstr.c b/src/fcstr.c index 4fbb1a2..c683c80 100644 --- a/src/fcstr.c +++ b/src/fcstr.c @@ -754,6 +754,19 @@ FcStrBufDone (FcStrBuf *buf) return ret; } +FcChar8 * +FcStrBufDoneStatic (FcStrBuf *buf) +{ + FcChar8 *ret; + + FcStrBufChar (buf, '\0'); + + if (buf->failed) + return NULL; + + return buf->buf; +} + FcBool FcStrBufChar (FcStrBuf *buf, FcChar8 c) { diff --git a/src/fcxml.c b/src/fcxml.c index 14da713..f60e248 100644 --- a/src/fcxml.c +++ b/src/fcxml.c @@ -1114,7 +1114,7 @@ FcParseInt (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1126,7 +1126,7 @@ FcParseInt (FcConfigParse *parse) FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s); else FcVStackPushInteger (parse, l); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } /* @@ -1195,7 +1195,7 @@ FcParseDouble (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1207,7 +1207,7 @@ FcParseDouble (FcConfigParse *parse) FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s); else FcVStackPushDouble (parse, d); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } static void @@ -1283,14 +1283,14 @@ FcParseBool (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); return; } FcVStackPushBool (parse, FcConfigLexBool (parse, s)); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } static FcBool @@ -1370,14 +1370,14 @@ FcParseFamily (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); return; } expr = FcExprCreateString (s); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); if (expr) FcVStackPushExpr (parse, FcVStackFamily, expr); } @@ -1636,7 +1636,7 @@ FcParseInclude (FcConfigParse *parse) const FcChar8 *i; FcBool ignore_missing = FcFalse; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1647,7 +1647,7 @@ FcParseInclude (FcConfigParse *parse) ignore_missing = FcTrue; if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing)) parse->error = FcTrue; - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } typedef struct _FcOpMap { @@ -2053,7 +2053,7 @@ FcEndElement(void *userData, const XML_Char *name) case FcElementFontconfig: break; case FcElementDir: - data = FcStrBufDone (&parse->pstack->str); + data = FcStrBufDoneStatic (&parse->pstack->str); if (!data) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -2132,7 +2132,7 @@ FcEndElement(void *userData, const XML_Char *name) if (!FcConfigAddDir (parse->config, data)) FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data); } - FcStrFree (data); + FcStrBufDestroy (&parse->pstack->str); break; case FcElementCacheDir: data = FcStrBufDone (&parse->pstack->str); @@ -2174,14 +2174,14 @@ FcEndElement(void *userData, const XML_Char *name) break; case FcElementCache: - data = FcStrBufDone (&parse->pstack->str); + data = FcStrBufDoneStatic (&parse->pstack->str); if (!data) { FcConfigMessage (parse, FcSevereError, "out of memory"); break; } /* discard this data; no longer used */ - FcStrFree (data); + FcStrBufDestroy (&parse->pstack->str); break; case FcElementInclude: FcParseInclude (parse);