diff --git a/CMakeLists.txt b/CMakeLists.txt index c945c5a0c..38a140660 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ set(sources stat stat_cache storage + storage_piece_set storage_utils time timestamp_history diff --git a/Jamfile b/Jamfile index 6a4f792e0..5837da7f2 100644 --- a/Jamfile +++ b/Jamfile @@ -617,6 +617,7 @@ SOURCES = socks5_stream stat storage + storage_piece_set storage_utils torrent torrent_handle diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 9cdd2020e..5af5c2ef8 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -176,6 +176,7 @@ nobase_include_HEADERS = \ aux_/proxy_settings.hpp \ aux_/session_interface.hpp \ aux_/suggest_piece.hpp \ + aux_/storage_piece_set.hpp \ aux_/time.hpp \ aux_/file_progress.hpp \ aux_/openssl.hpp \ diff --git a/include/libtorrent/aux_/storage_piece_set.hpp b/include/libtorrent/aux_/storage_piece_set.hpp new file mode 100644 index 000000000..862aa3d9e --- /dev/null +++ b/include/libtorrent/aux_/storage_piece_set.hpp @@ -0,0 +1,65 @@ +/* + +Copyright (c) 2003-2016, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_STORAGE_PIECE_SET_HPP_INCLUDE +#define TORRENT_STORAGE_PIECE_SET_HPP_INCLUDE + +#include + +#include "libtorrent/export.hpp" + +namespace libtorrent { + +struct cached_piece_entry; + +namespace aux { + + // this class keeps track of which pieces, belonging to + // a specific storage, are in the cache right now. It's + // used for quickly being able to evict all pieces for a + // specific torrent + struct TORRENT_EXPORT storage_piece_set + { + void add_piece(cached_piece_entry* p); + void remove_piece(cached_piece_entry* p); + bool has_piece(cached_piece_entry const* p) const; + int num_pieces() const { return int(m_cached_pieces.size()); } + std::unordered_set const& cached_pieces() const + { return m_cached_pieces; } + private: + // these are cached pieces belonging to this storage + std::unordered_set m_cached_pieces; + }; +}} + +#endif + diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index cc9691514..4696d1e35 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -38,24 +38,16 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include "libtorrent/aux_/disk_job_fence.hpp" -#include "libtorrent/piece_picker.hpp" -#include "libtorrent/peer_request.hpp" -#include "libtorrent/file.hpp" -#include "libtorrent/disk_buffer_holder.hpp" +#include "libtorrent/aux_/storage_piece_set.hpp" #include "libtorrent/storage_defs.hpp" #include "libtorrent/allocator.hpp" -#include "libtorrent/file_pool.hpp" // pool_file_status #include "libtorrent/part_file.hpp" #include "libtorrent/stat_cache.hpp" -#include "libtorrent/bdecode.hpp" #include "libtorrent/bitfield.hpp" -#include "libtorrent/performance_counters.hpp" #include "libtorrent/span.hpp" -#include "libtorrent/tailqueue.hpp" #include "libtorrent/aux_/vector.hpp" // OVERVIEW @@ -139,34 +131,13 @@ namespace libtorrent { class session; struct file_pool; - struct disk_io_job; - struct disk_buffer_pool; - struct cache_status; namespace aux { struct session_settings; } - struct cached_piece_entry; struct add_torrent_params; TORRENT_EXTRA_EXPORT void clear_bufs(span bufs); struct disk_io_thread; - // this class keeps track of which pieces, belonging to - // a specific storage, are in the cache right now. It's - // used for quickly being able to evict all pieces for a - // specific torrent - struct TORRENT_EXPORT storage_piece_set - { - void add_piece(cached_piece_entry* p); - void remove_piece(cached_piece_entry* p); - bool has_piece(cached_piece_entry const* p) const; - int num_pieces() const { return int(m_cached_pieces.size()); } - std::unordered_set const& cached_pieces() const - { return m_cached_pieces; } - private: - // these are cached pieces belonging to this storage - std::unordered_set m_cached_pieces; - }; - // The storage interface is a pure virtual class that can be implemented to // customize how and where data for a torrent is stored. The default storage // implementation uses regular files in the filesystem, mapping the files in @@ -195,7 +166,7 @@ namespace libtorrent struct TORRENT_EXPORT storage_interface : public std::enable_shared_from_this , public aux::disk_job_fence - , public storage_piece_set + , public aux::storage_piece_set , boost::noncopyable { diff --git a/src/Makefile.am b/src/Makefile.am index a35e961bf..35b7f9898 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -131,6 +131,7 @@ libtorrent_rasterbar_la_SOURCES = \ stat.cpp \ stat_cache.cpp \ storage.cpp \ + storage_piece_set.cpp \ storage_utils.cpp \ session_stats.cpp \ string_util.cpp \ diff --git a/src/storage.cpp b/src/storage.cpp index 1e14091eb..2d4eb885c 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -847,30 +847,4 @@ namespace libtorrent return new zero_storage; } - void storage_piece_set::add_piece(cached_piece_entry* p) - { - TORRENT_ASSERT(p->in_storage == false); - TORRENT_ASSERT(p->storage.get() == this); - TORRENT_ASSERT(m_cached_pieces.count(p) == 0); - m_cached_pieces.insert(p); -#if TORRENT_USE_ASSERTS - p->in_storage = true; -#endif - } - - bool storage_piece_set::has_piece(cached_piece_entry const* p) const - { - return m_cached_pieces.count(const_cast(p)) > 0; - } - - void storage_piece_set::remove_piece(cached_piece_entry* p) - { - TORRENT_ASSERT(p->in_storage == true); - TORRENT_ASSERT(m_cached_pieces.count(p) == 1); - m_cached_pieces.erase(p); -#if TORRENT_USE_ASSERTS - p->in_storage = false; -#endif - } - } // namespace libtorrent diff --git a/src/storage_piece_set.cpp b/src/storage_piece_set.cpp new file mode 100644 index 000000000..98f521cb0 --- /dev/null +++ b/src/storage_piece_set.cpp @@ -0,0 +1,68 @@ +/* + +Copyright (c) 2003-2016, Arvid Norberg, Daniel Wallin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "libtorrent/aux_/storage_piece_set.hpp" +#include "libtorrent/assert.hpp" +#include "libtorrent/block_cache.hpp" +#include "libtorrent/storage.hpp" // for storage_interface + +namespace libtorrent { namespace aux +{ + void storage_piece_set::add_piece(cached_piece_entry* p) + { + TORRENT_ASSERT(p->in_storage == false); + TORRENT_ASSERT(p->storage.get() == this); + TORRENT_ASSERT(m_cached_pieces.count(p) == 0); + m_cached_pieces.insert(p); +#if TORRENT_USE_ASSERTS + p->in_storage = true; +#endif + } + + bool storage_piece_set::has_piece(cached_piece_entry const* p) const + { + return m_cached_pieces.count(const_cast(p)) > 0; + } + + void storage_piece_set::remove_piece(cached_piece_entry* p) + { + TORRENT_ASSERT(p->in_storage == true); + TORRENT_ASSERT(m_cached_pieces.count(p) == 1); + m_cached_pieces.erase(p); +#if TORRENT_USE_ASSERTS + p->in_storage = false; +#endif + } + + +}} + diff --git a/test/test_fence.cpp b/test/test_fence.cpp index f1f4ce80d..e78f085af 100644 --- a/test/test_fence.cpp +++ b/test/test_fence.cpp @@ -1,5 +1,38 @@ +/* + +Copyright (c) 2003-2016, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + #include "libtorrent/storage.hpp" #include "libtorrent/disk_io_job.hpp" +#include "libtorrent/performance_counters.hpp" #include "test.hpp" #include