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
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_FILE_POOL_HPP
00034 #define TORRENT_FILE_POOL_HPP
00035
00036 #ifdef _MSC_VER
00037 #pragma warning(push, 1)
00038 #endif
00039
00040 #include <boost/filesystem/path.hpp>
00041 #include <boost/date_time/posix_time/posix_time_types.hpp>
00042 #include <boost/multi_index_container.hpp>
00043 #include <boost/multi_index/member.hpp>
00044 #include <boost/multi_index/ordered_index.hpp>
00045 #include <boost/shared_ptr.hpp>
00046 #include <boost/thread.hpp>
00047
00048 #ifdef _MSC_VER
00049 #pragma warning(pop)
00050 #endif
00051
00052 #include "libtorrent/file.hpp"
00053
00054 namespace libtorrent
00055 {
00056
00057 using boost::multi_index::multi_index_container;
00058 using boost::multi_index::ordered_non_unique;
00059 using boost::multi_index::ordered_unique;
00060 using boost::multi_index::indexed_by;
00061 using boost::multi_index::member;
00062 namespace pt = boost::posix_time;
00063 namespace fs = boost::filesystem;
00064
00065 struct TORRENT_EXPORT file_pool : boost::noncopyable
00066 {
00067 file_pool(int size = 40): m_size(size) {}
00068
00069 boost::shared_ptr<file> open_file(void* st, fs::path const& p, file::open_mode m);
00070 void release(void* st);
00071 void resize(int size);
00072
00073 private:
00074 int m_size;
00075
00076 struct lru_file_entry
00077 {
00078 lru_file_entry(boost::shared_ptr<file> const& f)
00079 : file_ptr(f)
00080 , last_use(pt::second_clock::universal_time()) {}
00081 mutable boost::shared_ptr<file> file_ptr;
00082 fs::path file_path;
00083 void* key;
00084 pt::ptime last_use;
00085 file::open_mode mode;
00086 };
00087
00088 typedef multi_index_container<
00089 lru_file_entry, indexed_by<
00090 ordered_unique<member<lru_file_entry, fs::path
00091 , &lru_file_entry::file_path> >
00092 , ordered_non_unique<member<lru_file_entry, pt::ptime
00093 , &lru_file_entry::last_use> >
00094 , ordered_non_unique<member<lru_file_entry, void*
00095 , &lru_file_entry::key> >
00096 >
00097 > file_set;
00098
00099 file_set m_files;
00100 boost::mutex m_mutex;
00101 };
00102 }
00103
00104 #endif