Switch Litehtml's image cache from std::map to std::list
authorAndrej Kacian <ticho@claws-mail.org>
Fri, 8 Feb 2019 17:33:00 +0000 (18:33 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Sat, 4 May 2019 14:51:20 +0000 (16:51 +0200)
This makes the cache ordered, so we are able to remove
oldest entries if we want to trim memory usage.

src/plugins/litehtml_viewer/container_linux.cpp
src/plugins/litehtml_viewer/container_linux.h

index 26845fbfb4cb4b3c03ceca1b18a2099823ff9ad8..f24add4bb64d707a48b776141c76033d78944eb4 100644 (file)
@@ -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;
 
index 94eafaba8d5399a8e9ae2984afa3e3e94cb92ec6..8c6eb6d669cea6283a2a260b7c0c8b3449abc643 100644 (file)
@@ -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;