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
if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
strcat ((char *) path, "/");
else
osize--;
#endif
strcat ((char *) path, (char *) file);

View File

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

View File

@ -182,7 +182,7 @@ FcLangNormalize (const FcChar8 *lang)
{
FcChar8 *result = NULL, *s, *orig;
char *territory, *encoding, *modifier;
size_t llen, tlen = 0, mlen = 0;
size_t llen, tlen = 0, mlen = 0, ssize;
if (!lang || !*lang)
return NULL;
@ -197,6 +197,10 @@ FcLangNormalize (const FcChar8 *lang)
s = FcStrCopy (lang);
if (!s)
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:
*
@ -282,6 +286,11 @@ FcLangNormalize (const FcChar8 *lang)
else
{
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;
goto bail1;
}
@ -295,6 +304,11 @@ FcLangNormalize (const FcChar8 *lang)
else
{
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;
goto bail1;
}
@ -312,14 +326,22 @@ FcLangNormalize (const FcChar8 *lang)
else
{
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;
}
bail1:
if (orig)
free (orig);
FcStrFree (orig);
bail0:
if (s)
{
free (s);
FcMemFree (FC_MEM_STRING, ssize);
}
bail:
if (FcDebug () & FC_DBG_LANGSET)
{

View File

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

View File

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