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>
Tue, 12 Feb 2019 18:38:10 +0000 (19:38 +0100)
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 26845fb..f24add4 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 94eafab..8c6eb6d 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;