From 318c6984baba0ea02147dc72a97e331234b6baf7 Mon Sep 17 00:00:00 2001 From: "Thomas E. Dickey" Date: Mon, 4 Jul 2022 07:41:12 -0400 Subject: refactor _XftFontUncacheGlyph, separate the two types of loop for readability Signed-off-by: Thomas E. Dickey --- src/xftglyphs.c | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/xftglyphs.c b/src/xftglyphs.c index 7e238b3..6edcf22 100644 --- a/src/xftglyphs.c +++ b/src/xftglyphs.c @@ -838,6 +838,9 @@ XftFontUnloadGlyphs (Display *dpy, continue; if (xftg->glyph_memory) { + if (XftDebug() & XFT_DBG_CACHEV) + printf ("Uncaching glyph 0x%x size %ld\n", + glyphindex, xftg->glyph_memory); if (font->format) { if (font->glyphset) @@ -1004,7 +1007,7 @@ XftCharIndex (Display *dpy, } /* - * Pick a random glyph from the font and remove it from the cache + * Remove glyph(s) from the font to reduce memory-usage. */ _X_HIDDEN void _XftFontUncacheGlyph (Display *dpy, XftFont *pub) @@ -1016,40 +1019,53 @@ _XftFontUncacheGlyph (Display *dpy, XftFont *pub) if (!font->glyph_memory) return; + + if (XftDebug() & XFT_DBG_CACHE) + _XftFontValidateMemory (dpy, pub); + if (font->use_free_glyphs) { + /* + * Pick a random glyph from the font and remove it from the cache + */ glyph_memory = ((unsigned long)rand() % font->glyph_memory); + for (glyphindex = 0; glyphindex < font->num_glyphs; glyphindex++) + { + xftg = font->glyphs[glyphindex]; + if (xftg) + { + if (xftg->glyph_memory > glyph_memory) + { + XftFontUnloadGlyphs (dpy, pub, &glyphindex, 1); + break; + } + glyph_memory -= xftg->glyph_memory; + } + } } else { + /* + * Free all glyphs, since they are part of a set. + */ if (font->glyphset) { XRenderFreeGlyphSet (dpy, font->glyphset); font->glyphset = 0; } - glyph_memory = 0; - } - - if (XftDebug() & XFT_DBG_CACHE) - _XftFontValidateMemory (dpy, pub); - for (glyphindex = 0; glyphindex < font->num_glyphs; glyphindex++) - { - xftg = font->glyphs[glyphindex]; - if (xftg) + for (glyphindex = 0; glyphindex < font->num_glyphs; glyphindex++) { - if (xftg->glyph_memory > glyph_memory) + xftg = font->glyphs[glyphindex]; + if (xftg) { - if (XftDebug() & XFT_DBG_CACHEV) - printf ("Uncaching glyph 0x%x size %ld\n", - glyphindex, xftg->glyph_memory); - XftFontUnloadGlyphs (dpy, pub, &glyphindex, 1); - if (!font->use_free_glyphs) - continue; - break; + if (xftg->glyph_memory > 0) + { + XftFontUnloadGlyphs (dpy, pub, &glyphindex, 1); + } } - glyph_memory -= xftg->glyph_memory; } } + if (XftDebug() & XFT_DBG_CACHE) _XftFontValidateMemory (dpy, pub); } -- cgit v1.2.1