pdf_library.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #include "pdf_library.h"
00028 #include "pdf_doc_controller.h"
00029 
00030 #ifdef _WIN32
00031 #include <windows.h>
00032 #else
00033 #include <sys/sysinfo.h>
00034 #endif
00035 
00036 namespace pdf
00037 {
00038 
00039 unsigned long get_system_free_memory()
00040 {
00041 #ifdef _WIN32
00042     MEMORYSTATUS info;
00043     GlobalMemoryStatus(&info);
00044     return info.dwAvailPhys;
00045 #else
00046     struct sysinfo info;
00047     sysinfo(&info);
00048     return info.freeram * info.mem_unit;
00049 #endif
00050 }
00051 
00052 PDFLibrary::PDFLibrary()
00053 : docs()
00054 , thread()
00055 , size_limit(DEFAULT_SIZE_LIMIT)
00056 {
00057     
00058     thread.start();
00059 }
00060 
00061 PDFLibrary::~PDFLibrary()
00062 {
00063     
00064     thread.stop();
00065 }
00066 
00067 PDFLibrary& PDFLibrary::instance()
00068 {
00069     static PDFLibrary lib;
00070     return lib;
00071 }
00072 
00073 void PDFLibrary::try_start_thread()
00074 {
00075     thread.start();
00076 }
00077 
00078 void PDFLibrary::try_stop_thread()
00079 {
00080     if (docs.empty())
00081     {
00082         thread.stop();
00083     }
00084 }
00085 
00086 bool PDFLibrary::make_enough_memory(PDFController *doc_ptr,
00087                                     const int page_num,
00088                                     const int length)
00089 {
00090     if (length <= 0)
00091     {
00092         WARNPRINTF("Should NOT make enough memory by length:%d", length);
00093         return false;
00094     }
00095 
00096     
00097     unsigned long free_mem = get_system_free_memory();
00098     if (free_mem < static_cast<unsigned long>(length << 1))
00099     {
00100         
00101         DocumentsIter idx = docs.begin();
00102         for (; idx != docs.end(); ++idx)
00103         {
00104             (*idx)->clear_cached_bitmaps();
00105         }
00106     }
00107 
00108     return doc_ptr->make_enough_memory(page_num, length);
00109 }
00110 
00111 void PDFLibrary::add_document(PDFController *doc_ptr)
00112 {
00113     try_start_thread();
00114     docs.push_back(doc_ptr);
00115 }
00116 
00117 void PDFLibrary::remove_tasks_by_document(PDFController *doc)
00118 {
00119     thread.cancel_tasks(doc);
00120 }
00121 
00122 void PDFLibrary::remove_document(PDFController *doc_ptr)
00123 {
00124     DocumentsIter idx = std::find(docs.begin(), docs.end(), doc_ptr);
00125     if (idx != docs.end())
00126     {
00127         docs.erase(idx);
00128     }
00129     try_stop_thread();
00130 }
00131 
00132 void PDFLibrary::thread_add_render_task(Task *task, bool prerender, bool abort_current)
00133 {
00134     if (prerender)
00135     {
00136         get_thread().append_task(task);
00137     }
00138     else
00139     {
00140         
00141         get_thread().prepend_task(task, abort_current);
00142     }
00143 }
00144 
00145 void PDFLibrary::thread_add_search_task(Task *task)
00146 {
00147     get_thread().prepend_task(task, true);
00148 }
00149 
00150 void PDFLibrary::thread_cancel_render_tasks(void *user_data)
00151 {
00152     get_thread().clear_all(user_data, TASK_RENDER);
00153 }
00154 
00155 }
00156 
00157