Fix the wrong estimation for the memory usage information in fontconfig

This commit is contained in:
Akira TAGOH 2012-12-07 19:09:36 +09:00
parent 959442bca1
commit e7954674eb
5 changed files with 55 additions and 24 deletions

View File

@ -1743,6 +1743,8 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
#else #else
if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/') if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
strcat ((char *) path, "/"); strcat ((char *) path, "/");
else
osize--;
#endif #endif
strcat ((char *) path, (char *) file); strcat ((char *) path, (char *) file);

View File

@ -72,7 +72,7 @@ FcInitLoadConfig (void)
if (config->cacheDirs && config->cacheDirs->num == 0) if (config->cacheDirs && config->cacheDirs->num == 0)
{ {
FcChar8 *prefix; FcChar8 *prefix, *p;
size_t plen; size_t plen;
fprintf (stderr, fprintf (stderr,
@ -81,12 +81,15 @@ FcInitLoadConfig (void)
"Fontconfig warning: adding <cachedir>%s</cachedir>\n", "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
FC_CACHEDIR); FC_CACHEDIR);
prefix = FcConfigXdgCacheHome (); prefix = FcConfigXdgCacheHome ();
if (!prefix)
goto bail;
plen = prefix ? strlen ((const char *)prefix) : 0; plen = prefix ? strlen ((const char *)prefix) : 0;
if (!prefix) p = realloc (prefix, plen + 12);
goto bail; if (!p)
prefix = realloc (prefix, plen + 12);
if (!prefix)
goto bail; goto bail;
prefix = p;
FcMemFree (FC_MEM_STRING, plen + 1);
FcMemAlloc (FC_MEM_STRING, plen + 12);
memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11); memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
prefix[plen + 11] = 0; prefix[plen + 11] = 0;
fprintf (stderr, fprintf (stderr,
@ -98,11 +101,12 @@ FcInitLoadConfig (void)
bail: bail:
fprintf (stderr, fprintf (stderr,
"Fontconfig error: out of memory"); "Fontconfig error: out of memory");
free (prefix); if (prefix)
FcStrFree (prefix);
FcConfigDestroy (config); FcConfigDestroy (config);
return FcInitFallbackConfig (); return FcInitFallbackConfig ();
} }
free (prefix); FcStrFree (prefix);
} }
return config; return config;

View File

@ -182,7 +182,7 @@ FcLangNormalize (const FcChar8 *lang)
{ {
FcChar8 *result = NULL, *s, *orig; FcChar8 *result = NULL, *s, *orig;
char *territory, *encoding, *modifier; char *territory, *encoding, *modifier;
size_t llen, tlen = 0, mlen = 0; size_t llen, tlen = 0, mlen = 0, ssize;
if (!lang || !*lang) if (!lang || !*lang)
return NULL; return NULL;
@ -197,6 +197,10 @@ FcLangNormalize (const FcChar8 *lang)
s = FcStrCopy (lang); s = FcStrCopy (lang);
if (!s) if (!s)
goto bail; goto bail;
/* store the original length of 's' here to let FcMemFree know
* the correct size since we breaks 's' from now on.
*/
ssize = strlen ((const char *)s) + 1;
/* from the comments in glibc: /* from the comments in glibc:
* *
@ -282,6 +286,11 @@ FcLangNormalize (const FcChar8 *lang)
else else
{ {
result = s; result = s;
/* we'll miss the opportunity to reduce the correct size
* of the allocated memory for the string after that.
*/
FcMemFree (FC_MEM_STRING, ssize);
FcMemAlloc (FC_MEM_STRING, strlen((const char *)s) + 1);
s = NULL; s = NULL;
goto bail1; goto bail1;
} }
@ -295,6 +304,11 @@ FcLangNormalize (const FcChar8 *lang)
else else
{ {
result = s; result = s;
/* we'll miss the opportunity to reduce the correct size
* of the allocated memory for the string after that.
*/
FcMemFree (FC_MEM_STRING, ssize);
FcMemAlloc (FC_MEM_STRING, strlen((const char *)s) + 1);
s = NULL; s = NULL;
goto bail1; goto bail1;
} }
@ -312,14 +326,22 @@ FcLangNormalize (const FcChar8 *lang)
else else
{ {
result = s; result = s;
/* we'll miss the opportunity to reduce the correct size
* of the allocated memory for the string after that.
*/
FcMemFree (FC_MEM_STRING, ssize);
FcMemAlloc (FC_MEM_STRING, strlen((const char *)s) + 1);
s = NULL; s = NULL;
} }
bail1: bail1:
if (orig) if (orig)
free (orig); FcStrFree (orig);
bail0: bail0:
if (s) if (s)
{
free (s); free (s);
FcMemFree (FC_MEM_STRING, ssize);
}
bail: bail:
if (FcDebug () & FC_DBG_LANGSET) if (FcDebug () & FC_DBG_LANGSET)
{ {

View File

@ -38,7 +38,6 @@ FcStrCopy (const FcChar8 *s)
{ {
int len; int len;
FcChar8 *r; FcChar8 *r;
if (!s) if (!s)
return 0; return 0;
len = strlen ((char *) s) + 1; len = strlen ((char *) s) + 1;
@ -1204,7 +1203,7 @@ FcStrSetAddLangs (FcStrSet *strs, const char *languages)
if (normalized_lang) if (normalized_lang)
{ {
FcStrSetAdd (strs, normalized_lang); FcStrSetAdd (strs, normalized_lang);
free (normalized_lang); FcStrFree (normalized_lang);
ret = FcTrue; ret = FcTrue;
} }
} }
@ -1216,7 +1215,7 @@ FcStrSetAddLangs (FcStrSet *strs, const char *languages)
if (normalized_lang) if (normalized_lang)
{ {
FcStrSetAdd (strs, normalized_lang); FcStrSetAdd (strs, normalized_lang);
free (normalized_lang); FcStrFree (normalized_lang);
ret = FcTrue; ret = FcTrue;
} }
} }

View File

@ -1849,7 +1849,7 @@ static void
FcParseDir (FcConfigParse *parse) FcParseDir (FcConfigParse *parse)
{ {
const FcChar8 *attr, *data; const FcChar8 *attr, *data;
FcChar8 *prefix = NULL; FcChar8 *prefix = NULL, *p;
#ifdef _WIN32 #ifdef _WIN32
FcChar8 buffer[1000]; FcChar8 buffer[1000];
#endif #endif
@ -1868,13 +1868,14 @@ FcParseDir (FcConfigParse *parse)
size_t plen = strlen ((const char *)prefix); size_t plen = strlen ((const char *)prefix);
size_t dlen = strlen ((const char *)data); size_t dlen = strlen ((const char *)data);
FcMemFree (FC_MEM_STRING, plen + 1); p = realloc (prefix, plen + 1 + dlen + 1);
prefix = realloc (prefix, plen + 1 + dlen + 1); if (!p)
if (!prefix)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
goto bail; goto bail;
} }
prefix = p;
FcMemFree (FC_MEM_STRING, plen + 1);
FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1); FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1);
prefix[plen] = FC_DIR_SEPARATOR; prefix[plen] = FC_DIR_SEPARATOR;
memcpy (&prefix[plen + 1], data, dlen); memcpy (&prefix[plen + 1], data, dlen);
@ -1947,7 +1948,7 @@ static void
FcParseCacheDir (FcConfigParse *parse) FcParseCacheDir (FcConfigParse *parse)
{ {
const FcChar8 *attr; const FcChar8 *attr;
FcChar8 *prefix = NULL, *data; FcChar8 *prefix = NULL, *p, *data;
attr = FcConfigGetAttribute (parse, "prefix"); attr = FcConfigGetAttribute (parse, "prefix");
if (attr && FcStrCmp (attr, (const FcChar8 *)"xdg") == 0) if (attr && FcStrCmp (attr, (const FcChar8 *)"xdg") == 0)
@ -1963,13 +1964,15 @@ FcParseCacheDir (FcConfigParse *parse)
size_t plen = strlen ((const char *)prefix); size_t plen = strlen ((const char *)prefix);
size_t dlen = strlen ((const char *)data); size_t dlen = strlen ((const char *)data);
FcMemFree (FC_MEM_STRING, plen + 1); p = realloc (prefix, plen + 1 + dlen + 1);
prefix = realloc (prefix, plen + 1 + dlen + 1); if (!p)
if (!prefix)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
data = prefix;
goto bail; goto bail;
} }
prefix = p;
FcMemFree (FC_MEM_STRING, plen + 1);
FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1); FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1);
prefix[plen] = FC_DIR_SEPARATOR; prefix[plen] = FC_DIR_SEPARATOR;
memcpy (&prefix[plen + 1], data, dlen); memcpy (&prefix[plen + 1], data, dlen);
@ -2043,7 +2046,7 @@ FcParseInclude (FcConfigParse *parse)
const FcChar8 *attr; const FcChar8 *attr;
FcBool ignore_missing = FcFalse; FcBool ignore_missing = FcFalse;
FcBool deprecated = FcFalse; FcBool deprecated = FcFalse;
FcChar8 *prefix = NULL; FcChar8 *prefix = NULL, *p;
s = FcStrBufDoneStatic (&parse->pstack->str); s = FcStrBufDoneStatic (&parse->pstack->str);
if (!s) if (!s)
@ -2065,13 +2068,14 @@ FcParseInclude (FcConfigParse *parse)
size_t plen = strlen ((const char *)prefix); size_t plen = strlen ((const char *)prefix);
size_t dlen = strlen ((const char *)s); size_t dlen = strlen ((const char *)s);
FcMemFree (FC_MEM_STRING, plen + 1); p = realloc (prefix, plen + 1 + dlen + 1);
prefix = realloc (prefix, plen + 1 + dlen + 1); if (!p)
if (!prefix)
{ {
FcConfigMessage (parse, FcSevereError, "out of memory"); FcConfigMessage (parse, FcSevereError, "out of memory");
goto bail; goto bail;
} }
prefix = p;
FcMemFree (FC_MEM_STRING, plen + 1);
FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1); FcMemAlloc (FC_MEM_STRING, plen + 1 + dlen + 1);
prefix[plen] = FC_DIR_SEPARATOR; prefix[plen] = FC_DIR_SEPARATOR;
memcpy (&prefix[plen + 1], s, dlen); memcpy (&prefix[plen + 1], s, dlen);