commit f9d496bff63d513a265086a07601b186284907a5
parent 4271751d2116073b731b163800844e6094f107df
Author: Andrej Kacian <ticho@claws-mail.org>
Date: Fri, 8 Feb 2019 18:33:00 +0100
Switch Litehtml's image cache from std::map to std::list
This makes the cache ordered, so we are able to remove
oldest entries if we want to trim memory usage.
Diffstat:
2 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/src/plugins/litehtml_viewer/container_linux.cpp b/src/plugins/litehtml_viewer/container_linux.cpp
@@ -278,14 +278,25 @@ void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::
{
litehtml::tstring url;
make_url(src, baseurl, url);
- if(m_images.find(url.c_str()) == m_images.end())
+ bool found = false;
+
+ for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+ const image *i = &(*ii);
+
+ if (!strcmp(i->first.c_str(), url.c_str())) {
+ found = true;
+ break;
+ }
+ }
+
+ if(!found)
{
try
{
GdkPixbuf *img = get_image(url.c_str(), true);
if(img)
{
- m_images[url.c_str()] = img;
+ m_images.push_back(std::make_pair(url, img));
}
} catch(...)
{
@@ -299,9 +310,19 @@ void container_linux::get_image_size( const litehtml::tchar_t* src, const liteht
{
litehtml::tstring url;
make_url(src, baseurl, url);
+ bool found = false;
+ const image *img = NULL;
- images_map::iterator img = m_images.find(url.c_str());
- if(img != m_images.end())
+ for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+ const image *i = &(*ii);
+ if (i->first == url) {
+ img = i;
+ found = true;
+ break;
+ }
+ }
+
+ if(img != NULL)
{
sz.width = gdk_pixbuf_get_width(img->second);
sz.height = gdk_pixbuf_get_height(img->second);
@@ -334,8 +355,19 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
//lock_images_cache();
- images_map::iterator img_i = m_images.find(url.c_str());
- if(img_i != m_images.end() && img_i->second)
+ bool found = false;
+ const image *img_i = NULL;
+
+ for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+ const image *i = &(*ii);
+ if (i->first == url) {
+ img_i = i;
+ found = true;
+ break;
+ }
+ }
+
+ if(img_i != NULL && img_i->second)
{
GdkPixbuf *bgbmp = img_i->second;
diff --git a/src/plugins/litehtml_viewer/container_linux.h b/src/plugins/litehtml_viewer/container_linux.h
@@ -1,6 +1,7 @@
#pragma once
#include <vector>
+#include <list>
#include <string>
#include <cairo.h>
@@ -44,7 +45,8 @@ struct cairo_font
class container_linux : public litehtml::document_container
{
- typedef std::map<litehtml::tstring, GdkPixbuf* > images_map;
+ typedef std::pair<litehtml::tstring, GdkPixbuf*> image;
+ typedef std::list<image> images_map;
protected:
cairo_surface_t* m_temp_surface;