3 Copyright (c) 2006, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include <boost/version.hpp>
34 #include "libtorrent/pch.hpp"
35 #include "libtorrent/file_pool.hpp"
36 #include "libtorrent/error_code.hpp"
42 using boost::multi_index::nth_index
;
43 using boost::multi_index::get
;
45 boost::shared_ptr
<file
> file_pool::open_file(void* st
, fs::path
const& p
46 , file::open_mode m
, error_code
& ec
)
48 TORRENT_ASSERT(st
!= 0);
49 TORRENT_ASSERT(p
.is_complete());
50 TORRENT_ASSERT(m
== file::in
|| m
== (file::in
| file::out
));
51 boost::mutex::scoped_lock
l(m_mutex
);
52 typedef nth_index
<file_set
, 0>::type path_view
;
53 path_view
& pt
= get
<0>(m_files
);
54 path_view::iterator i
= pt
.find(p
);
57 lru_file_entry e
= *i
;
58 e
.last_use
= time_now();
62 // this means that another instance of the storage
63 // is using the exact same file.
64 #if BOOST_VERSION >= 103500
65 ec
= error_code(errors::file_collision
, libtorrent_category
);
67 return boost::shared_ptr
<file
>();
71 if ((e
.mode
& m
) != m
)
73 // close the file before we open it with
74 // the new read/write privilages
76 TORRENT_ASSERT(e
.file_ptr
.unique());
78 if (!e
.file_ptr
->open(p
, m
, ec
))
81 return boost::shared_ptr
<file
>();
83 TORRENT_ASSERT(e
.file_ptr
->is_open());
89 // the file is not in our cache
90 if ((int)m_files
.size() >= m_size
)
92 // the file cache is at its maximum size, close
93 // the least recently used (lru) file from it
94 typedef nth_index
<file_set
, 1>::type lru_view
;
95 lru_view
& lt
= get
<1>(m_files
);
96 lru_view::iterator i
= lt
.begin();
97 // the first entry in this view is the least recently used
98 TORRENT_ASSERT(lt
.size() == 1 || (i
->last_use
<= boost::next(i
)->last_use
));
102 e
.file_ptr
.reset(new (std::nothrow
)file
);
105 ec
= error_code(ENOMEM
, get_posix_category());
108 if (!e
.file_ptr
->open(p
, m
, ec
))
109 return boost::shared_ptr
<file
>();
114 TORRENT_ASSERT(e
.file_ptr
->is_open());
118 void file_pool::release(fs::path
const& p
)
120 boost::mutex::scoped_lock
l(m_mutex
);
122 typedef nth_index
<file_set
, 0>::type path_view
;
123 path_view
& pt
= get
<0>(m_files
);
124 path_view::iterator i
= pt
.find(p
);
125 if (i
!= pt
.end()) pt
.erase(i
);
128 void file_pool::release(void* st
)
130 boost::mutex::scoped_lock
l(m_mutex
);
131 TORRENT_ASSERT(st
!= 0);
134 typedef nth_index
<file_set
, 2>::type key_view
;
135 key_view
& kt
= get
<2>(m_files
);
137 key_view::iterator start
, end
;
138 tie(start
, end
) = kt
.equal_range(st
);
139 kt
.erase(start
, end
);
142 void file_pool::resize(int size
)
144 TORRENT_ASSERT(size
> 0);
145 if (size
== m_size
) return;
146 boost::mutex::scoped_lock
l(m_mutex
);
148 if (int(m_files
.size()) <= m_size
) return;
150 // close the least recently used files
151 typedef nth_index
<file_set
, 1>::type lru_view
;
152 lru_view
& lt
= get
<1>(m_files
);
153 lru_view::iterator i
= lt
.begin();
154 while (int(m_files
.size()) > m_size
)
156 // the first entry in this view is the least recently used
157 TORRENT_ASSERT(lt
.size() == 1 || (i
->last_use
<= boost::next(i
)->last_use
));