pdf::PagesCache Class Reference

#include <pdf_pages_cache.h>

Collaboration diagram for pdf::PagesCache:
Collaboration graph
[legend]

Public Member Functions

 PagesCache (void)
 ~PagesCache (void)
bool reset (const unsigned int size)
 Reset the size of pages cache.
unsigned int size ()
 Get the size of pages cache.
void clear ()
 Clear the pages cache.
void add_page (PagePtr p)
 Add a new page.
bool make_enough_memory (const int page_num, const int length)
void clear_cached_bitmaps ()
 Clear the cached bitmaps but locked page.
PagePtr get_page (const size_t idx)
 Get a page.
void update_mem_usage (const int length)
 Increase total length by adding the page length.
Mutexget_mutex ()
 Get the mutex, for externally locking the cache.

Detailed Description

Definition at line 41 of file pdf_pages_cache.h.


Constructor & Destructor Documentation

pdf::PagesCache::PagesCache ( void   ) 

Definition at line 33 of file pdf_pages_cache.cpp.

00034 : size_limit(0)
00035 , total_length(0)
00036 , pages()
00037 , cache_mutex()
00038 {
00039 }

pdf::PagesCache::~PagesCache ( void   ) 

Definition at line 41 of file pdf_pages_cache.cpp.

References clear().

00042 {
00043     clear();
00044 }

Here is the call graph for this function:


Member Function Documentation

void pdf::PagesCache::add_page ( PagePtr  p  ) 

Add a new page.

Definition at line 91 of file pdf_pages_cache.cpp.

Referenced by pdf::PDFRenderer::gen_page().

00092 {
00093     ScopeMutex m(&cache_mutex);
00094     // insert the new page into cache
00095     pages[(*p)()] = p;
00096 }

Here is the caller graph for this function:

void pdf::PagesCache::clear ( void   ) 

Clear the pages cache.

Definition at line 75 of file pdf_pages_cache.cpp.

Referenced by ~PagesCache().

00076 {
00077     ScopeMutex m(&cache_mutex);
00078 
00079     PagesIter begin = pages.begin();
00080     PagesIter end = pages.end();
00081     PagesIter iter = begin;
00082     for(; iter != end; ++iter)
00083     {
00084         delete iter->second;
00085     }
00086     pages.clear();
00087     total_length = 0;
00088 }

Here is the caller graph for this function:

void pdf::PagesCache::clear_cached_bitmaps (  ) 

Clear the cached bitmaps but locked page.

Definition at line 136 of file pdf_pages_cache.cpp.

References pdf::PDFPage::destroy(), pdf::PDFPage::get_bitmap(), pdf::PDFPage::locked(), and LOGPRINTF.

00137 {
00138     ScopeMutex m(&cache_mutex);
00139     // clear all cached bitmaps
00140     LOGPRINTF("Clear cached bitmaps due to out of memory\n\n");
00141     PagePtr page = 0;
00142     PagesIter iter = pages.begin();
00143     for (; iter != pages.end(); ++iter)
00144     {
00145         page = iter->second;
00146         if (page->get_bitmap() && !page->locked())
00147         {
00148             int delta = static_cast<int>(page->destroy());
00149 
00150             // update the total length
00151             total_length -= delta;
00152         }
00153     }
00154 }

Here is the call graph for this function:

Mutex& pdf::PagesCache::get_mutex (  )  [inline]

Get the mutex, for externally locking the cache.

Definition at line 73 of file pdf_pages_cache.h.

Referenced by pdf::PDFRenderer::post_render_task().

00073 { return cache_mutex; }

Here is the caller graph for this function:

PagePtr pdf::PagesCache::get_page ( const size_t  idx  ) 

Get a page.

Definition at line 156 of file pdf_pages_cache.cpp.

Referenced by pdf::PDFController::get_page().

00157 {
00158     ScopeMutex m(&cache_mutex);
00159 
00160     PagePtr page = 0;
00161     PagesIter iter = pages.find(idx);
00162     if (iter != pages.end())
00163     {
00164         page = iter->second;
00165     }
00166     return page;
00167 }

Here is the caller graph for this function:

bool pdf::PagesCache::make_enough_memory ( const int  page_num,
const int  length 
)

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

Definition at line 100 of file pdf_pages_cache.cpp.

References LOGPRINTF, and size().

00101 {
00102     ScopeMutex m(&cache_mutex);
00103 
00104     int sum = total_length + length;
00105     if (sum <= static_cast<int>(size_limit))
00106     {
00107         return true;
00108     }
00109 
00110     // remove the most useless pages until the sum is less than
00111     // a quarter of the size limitation
00112     unsigned int size = (size_limit >> 1);
00113     while (sum > static_cast<int>(size))
00114     {
00115         if (!remove_page(page_num))
00116         {
00117             // remove fails, because:
00118             // 1. there is no any cached images any more
00119             // 2. the image is locked
00120             if ((total_length + length) <= static_cast<int>(size_limit))
00121             {
00122                 // if the total length plus with the length is less
00123                 // than the size limitation, it means there is enough
00124                 // space left, otherwise return false.
00125                 return true;
00126             }
00127             LOGPRINTF("Skip Page:%d", page_num);
00128             return false;
00129         }
00130         sum = total_length + length;
00131     }
00132 
00133     return true;
00134 }

Here is the call graph for this function:

bool pdf::PagesCache::reset ( const unsigned int  size  ) 

Reset the size of pages cache.

Definition at line 46 of file pdf_pages_cache.cpp.

Referenced by pdf::PDFController::set_memory_limit().

00047 {
00048     ScopeMutex m(&cache_mutex);
00049 
00050     int real_size = static_cast<int>(size);
00051 
00052     if (size_limit > static_cast<unsigned int>(real_size))
00053     {
00054         //remove the redundant pages
00055         while (total_length > real_size)
00056         {
00057             if (!remove_page())
00058             {
00059                 // cannot remove the needed page
00060                 return false;
00061             }
00062         }
00063     }
00064 
00065     size_limit = static_cast<unsigned int>(real_size);
00066     return true;
00067 }

Here is the caller graph for this function:

unsigned int pdf::PagesCache::size (  ) 

Get the size of pages cache.

Definition at line 69 of file pdf_pages_cache.cpp.

Referenced by pdf::PDFController::get_memory_limit(), and make_enough_memory().

00070 {
00071     ScopeMutex m(&cache_mutex);
00072     return size_limit;
00073 }

Here is the caller graph for this function:

void pdf::PagesCache::update_mem_usage ( const int  length  ) 

Increase total length by adding the page length.

Definition at line 169 of file pdf_pages_cache.cpp.

00170 {
00171     ScopeMutex m(&cache_mutex);
00172     total_length += length;
00173     /*LOGPRINTF("Add memory usage:%d, total length:%d\n"
00174             , length
00175             , total_length);*/
00176 }


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