images::PagesCache Class Reference

The pages cache can be used to cache ImagePages. More...

#include <images_pages_cache.h>

Public Member Functions

 PagesCache (void)
 ~PagesCache (void)
bool set_memory_limit (const int bytes)
 Reset the size of pages cache.
bool make_enough_memory (const gint64 length)
bool add_page (ImagePage *p)
 Add a new page.
ImagePageget_page (const size_t idx)
 Get a page.
void lock (void)
 Lock the whole PagesCache. Notes, Don't call these two functions around APIs of PagesCache. For inside of these APIs, already use lock() and unlock () to guarantee they are thread-safe.
void unlock (void)
 Unlock the whole PagesCache. Notes, See lock().

Detailed Description

The pages cache can be used to cache ImagePages.

Definition at line 48 of file images_pages_cache.h.


Constructor & Destructor Documentation

images::PagesCache::PagesCache ( void   ) 

Definition at line 33 of file images_pages_cache.cpp.

00034 : total_length(DEFAULT_MEMORY_LIMIT)
00035 , used_length(0)
00036 , pages()
00037 , cache_mutex(0)
00038 {
00039     cache_mutex = g_mutex_new();
00040 }

images::PagesCache::~PagesCache ( void   ) 

Definition at line 42 of file images_pages_cache.cpp.

00043 {
00044     g_mutex_lock(cache_mutex);
00045     clear();
00046     g_mutex_unlock(cache_mutex);
00047     g_mutex_free(cache_mutex);
00048 }


Member Function Documentation

bool images::PagesCache::add_page ( ImagePage p  ) 

Add a new page.

Definition at line 95 of file images_pages_cache.cpp.

References images::ImagePage::length(), and make_enough_memory().

Referenced by images::ImagesRenderer::add_page().

00096 {
00097     if (!p) { return false; }
00098 
00099     const int length = p->length();
00100 
00101     // make room in cache, if needed
00102     make_enough_memory( static_cast<gint64>(length) );
00103 
00104     // insert the new page into cache
00105     g_mutex_lock(cache_mutex);
00106     pages[(*p)()] = p;
00107     used_length += length;
00108     g_mutex_unlock(cache_mutex);
00109 
00110     return true;
00111 }

Here is the call graph for this function:

Here is the caller graph for this function:

ImagePage * images::PagesCache::get_page ( const size_t  idx  ) 

Get a page.

Definition at line 113 of file images_pages_cache.cpp.

Referenced by images::ImagesRenderer::get_page().

00114 {
00115     g_mutex_lock(cache_mutex);
00116     
00117     ImagePage * p = 0;
00118     PagesIter iter = pages.find(idx);
00119     if (iter != pages.end())
00120     {
00121         p = iter->second;
00122     }
00123 
00124     g_mutex_unlock(cache_mutex);
00125     return p;
00126 }

Here is the caller graph for this function:

void images::PagesCache::lock ( void   ) 

Lock the whole PagesCache. Notes, Don't call these two functions around APIs of PagesCache. For inside of these APIs, already use lock() and unlock () to guarantee they are thread-safe.

Definition at line 128 of file images_pages_cache.cpp.

Referenced by images::ImagesRenderer::notify_page_ready().

00129 {
00130     g_mutex_lock(cache_mutex);
00131 }

Here is the caller graph for this function:

bool images::PagesCache::make_enough_memory ( const gint64  length  ) 

Remove the old pages to make sure the memory is enough NOTE: length might be less than 0

Definition at line 73 of file images_pages_cache.cpp.

References LOGPRINTF.

Referenced by add_page(), and images::ImagesRenderer::can_render().

00074 {
00075     bool ok = true;
00076 
00077     g_mutex_lock(cache_mutex);
00078     LOGPRINTF("used [%d] total [%d] request [%lld]", used_length, total_length, length);
00079     
00080     while ( ok && length > static_cast<gint64>(total_length - used_length) )
00081     {
00082         if (!remove_page())
00083         {
00084             // Remove fails, because:
00085             // 1. There is no any cached images any more.
00086             // 2. The image is in use and can't be removed.
00087             ok = false;
00088         }
00089     }
00090 
00091     g_mutex_unlock(cache_mutex);
00092     return ok;
00093 }

Here is the caller graph for this function:

bool images::PagesCache::set_memory_limit ( const int  bytes  ) 

Reset the size of pages cache.

Definition at line 50 of file images_pages_cache.cpp.

Referenced by images::ImagesRenderer::set_memory_limit().

00051 {
00052     g_mutex_lock(cache_mutex);
00053 
00054     if (total_length > static_cast<unsigned int>(bytes))
00055     {
00056         //remove the redundant pages
00057         while (used_length > static_cast<unsigned int>(bytes))
00058         {
00059             if (!remove_page())
00060             {
00061                 g_mutex_unlock(cache_mutex);
00062                 return false;
00063             }
00064         }
00065     }
00066 
00067     total_length = static_cast<unsigned int>(bytes);
00068     
00069     g_mutex_unlock(cache_mutex);
00070     return true;
00071 }

Here is the caller graph for this function:

void images::PagesCache::unlock ( void   ) 

Unlock the whole PagesCache. Notes, See lock().

Definition at line 133 of file images_pages_cache.cpp.

Referenced by images::ImagesRenderer::notify_page_ready().

00134 {
00135     g_mutex_unlock(cache_mutex);
00136 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files:
Generated by  doxygen 1.6.2-20100208