00001 /* 00002 * File Name: images_pages_cache.h 00003 */ 00004 00005 /* 00006 * This file is part of uds-plugin-images. 00007 * 00008 * uds-plugin-images is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * uds-plugin-images is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 /** 00023 * Copyright (C) 2008 iRex Technologies B.V. 00024 * All rights reserved. 00025 */ 00026 00027 #ifndef PAGES_CACHE_H_ 00028 #define PAGES_CACHE_H_ 00029 00030 #include <map> 00031 #include <string> 00032 #include <glib.h> 00033 00034 #ifdef _WIN32 00035 #include <unordered_map> 00036 #else 00037 #include <tr1/unordered_map> 00038 #endif 00039 00040 #include "image_page.h" 00041 00042 namespace images 00043 { 00044 00045 #define DEFAULT_MEMORY_LIMIT 16777216 // 16M 00046 00047 ///@brief The pages cache can be used to cache ImagePages. 00048 class PagesCache 00049 { 00050 public: 00051 PagesCache(void); 00052 ~PagesCache(void); 00053 00054 /// @brief Reset the size of pages cache 00055 bool set_memory_limit(const int bytes); 00056 00057 /// Remove the old pages to make sure the memory is enough 00058 /// NOTE: length might be less than 0 00059 bool make_enough_memory(const gint64 length); 00060 00061 /// @brief Add a new page 00062 bool add_page(ImagePage * p); 00063 00064 /// @brief Get a page 00065 ImagePage * get_page(const size_t idx); 00066 00067 // TODO Maybe should add a lock for a page 00068 // instead of using the lock of PagesCache? 00069 // 00070 /// @brief Lock the whole PagesCache. 00071 /// Notes, Don't call these two functions around APIs of PagesCache. 00072 /// For inside of these APIs, already use lock() and unlock () 00073 /// to guarantee they are thread-safe. 00074 void lock(void); 00075 00076 /// @brief Unlock the whole PagesCache. 00077 /// Notes, See lock(). 00078 void unlock(void); 00079 00080 private: 00081 /// @brief Clear the pages cache 00082 void clear(void); 00083 00084 // remove a page: return true when a page is sucessfully deleted; 00085 // otherwise there is only one page in cache and it is locked. 00086 bool remove_page(); 00087 00088 private: 00089 typedef std::tr1::unordered_map<size_t, ImagePage *> Pages; 00090 typedef Pages::iterator PagesIter; 00091 00092 private: 00093 // the size limit 00094 unsigned int total_length; 00095 00096 // the memory cost of current cached pages 00097 unsigned int used_length; 00098 00099 // the pages list 00100 Pages pages; 00101 00102 // the mutext of the cached pages 00103 GMutex *cache_mutex; 00104 }; 00105 00106 };//namespace common 00107 00108 #endif //PAGES_CACHE_H_ 00109 00110