#include <pdf_pages_cache.h>
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. | |
Mutex & | get_mutex () |
Get the mutex, for externally locking the cache. |
Definition at line 41 of file pdf_pages_cache.h.
pdf::PagesCache::PagesCache | ( | void | ) |
Definition at line 33 of file pdf_pages_cache.cpp.
pdf::PagesCache::~PagesCache | ( | void | ) |
Definition at line 41 of file pdf_pages_cache.cpp.
References clear().
00042 { 00043 clear(); 00044 }
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 }
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 }
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 }
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().
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 }
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 }
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 }
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().
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.