gtkfilesystem: bound the shared file icon cache

This commit is contained in:
Daemonratte 2026-10-13 14:19:38 +02:00
commit 9a491e585a

View file

@ -1663,18 +1663,105 @@ file_icon_cache_key_free (FileIconCacheKey *key)
g_slice_free (FileIconCacheKey, key);
}
static void
file_icon_cache_theme_changed_cb (GtkIconTheme *icon_theme,
GHashTable *cache)
/* The cache is bounded (least recently used entries go first) so zooming
* through many icon sizes does not keep every rendered size alive. */
#define FILE_ICON_CACHE_MAX_ENTRIES 64
#define FILE_ICON_CACHE_MAX_BYTES (4 * 1024 * 1024)
typedef struct
{
g_hash_table_remove_all (cache);
GHashTable *table; /* FileIconCacheKey -> FileIconCacheEntry */
GQueue lru; /* keys, most recently used first */
gsize bytes;
} FileIconCache;
typedef struct
{
GdkPixbuf *pixbuf;
GList *link; /* in FileIconCache.lru */
gsize bytes;
} FileIconCacheEntry;
static void
file_icon_cache_entry_free (FileIconCacheEntry *entry)
{
g_object_unref (entry->pixbuf);
g_slice_free (FileIconCacheEntry, entry);
}
static GHashTable *
static void
file_icon_cache_free (FileIconCache *cache)
{
g_queue_clear (&cache->lru);
g_hash_table_destroy (cache->table);
g_slice_free (FileIconCache, cache);
}
static void
file_icon_cache_theme_changed_cb (GtkIconTheme *icon_theme,
FileIconCache *cache)
{
g_queue_clear (&cache->lru);
g_hash_table_remove_all (cache->table);
cache->bytes = 0;
}
static GdkPixbuf *
file_icon_cache_lookup (FileIconCache *cache,
const FileIconCacheKey *key)
{
FileIconCacheEntry *entry = g_hash_table_lookup (cache->table, key);
if (entry == NULL)
return NULL;
/* Mark as most recently used. */
g_queue_unlink (&cache->lru, entry->link);
g_queue_push_head_link (&cache->lru, entry->link);
return entry->pixbuf;
}
static void
file_icon_cache_insert (FileIconCache *cache,
FileIconCacheKey *key,
GdkPixbuf *pixbuf)
{
FileIconCacheEntry *entry;
/* g_hash_table_insert() would free @key and keep the old one, leaving a
* dangling pointer in the LRU queue; keep the existing entry instead. */
if (g_hash_table_lookup (cache->table, key) != NULL)
{
file_icon_cache_key_free (key);
return;
}
entry = g_slice_new (FileIconCacheEntry);
entry->pixbuf = g_object_ref (pixbuf);
entry->bytes = (gsize) gdk_pixbuf_get_rowstride (pixbuf) * gdk_pixbuf_get_height (pixbuf);
g_queue_push_head (&cache->lru, key);
entry->link = cache->lru.head;
g_hash_table_insert (cache->table, key, entry);
cache->bytes += entry->bytes;
while (cache->lru.length > 1 &&
(cache->lru.length > FILE_ICON_CACHE_MAX_ENTRIES ||
cache->bytes > FILE_ICON_CACHE_MAX_BYTES))
{
FileIconCacheKey *old_key = g_queue_pop_tail (&cache->lru);
FileIconCacheEntry *old = g_hash_table_lookup (cache->table, old_key);
cache->bytes -= old->bytes;
g_hash_table_remove (cache->table, old_key);
}
}
static FileIconCache *
file_icon_cache_get_for_icon_theme (GtkIconTheme *icon_theme)
{
static GQuark quark_file_icon_cache = 0;
GHashTable *cache;
FileIconCache *cache;
if (G_UNLIKELY (quark_file_icon_cache == 0))
quark_file_icon_cache = g_quark_from_static_string ("gtk-file-icon-cache");
@ -1682,14 +1769,16 @@ file_icon_cache_get_for_icon_theme (GtkIconTheme *icon_theme)
cache = g_object_get_qdata (G_OBJECT (icon_theme), quark_file_icon_cache);
if (!cache)
{
cache = g_hash_table_new_full (file_icon_cache_key_hash,
file_icon_cache_key_equal,
(GDestroyNotify) file_icon_cache_key_free,
g_object_unref);
cache = g_slice_new0 (FileIconCache);
cache->table = g_hash_table_new_full (file_icon_cache_key_hash,
file_icon_cache_key_equal,
(GDestroyNotify) file_icon_cache_key_free,
(GDestroyNotify) file_icon_cache_entry_free);
g_queue_init (&cache->lru);
g_object_set_qdata_full (G_OBJECT (icon_theme),
quark_file_icon_cache,
cache,
(GDestroyNotify) g_hash_table_destroy);
(GDestroyNotify) file_icon_cache_free);
g_signal_connect (icon_theme, "changed",
G_CALLBACK (file_icon_cache_theme_changed_cb),
@ -1708,7 +1797,7 @@ get_pixbuf_from_gicon (GIcon *icon,
GdkScreen *screen;
GtkIconTheme *icon_theme;
GtkIconInfo *icon_info;
GHashTable *cache;
FileIconCache *cache;
FileIconCacheKey lookup_key;
FileIconCacheKey *cache_key;
GdkPixbuf *pixbuf;
@ -1732,7 +1821,7 @@ get_pixbuf_from_gicon (GIcon *icon,
lookup_key.icon = icon;
lookup_key.icon_size = icon_size;
pixbuf = g_hash_table_lookup (cache, &lookup_key);
pixbuf = file_icon_cache_lookup (cache, &lookup_key);
if (pixbuf)
return g_object_ref (pixbuf);
}
@ -1758,7 +1847,7 @@ get_pixbuf_from_gicon (GIcon *icon,
if (cache)
{
cache_key = file_icon_cache_key_new (screen, icon, icon_size);
g_hash_table_insert (cache, cache_key, g_object_ref (pixbuf));
file_icon_cache_insert (cache, cache_key, pixbuf);
}
return pixbuf;