diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 8879a9232..139458405 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -158,6 +158,7 @@ nobase_include_HEADERS = \ \ aux_/allocating_handler.hpp \ aux_/bind_to_device.hpp \ + aux_/block_cache_reference.hpp \ aux_/cpuid.hpp \ aux_/disable_warnings_push.hpp \ aux_/disable_warnings_pop.hpp \ diff --git a/include/libtorrent/aux_/block_cache_reference.hpp b/include/libtorrent/aux_/block_cache_reference.hpp new file mode 100644 index 000000000..dac5f5955 --- /dev/null +++ b/include/libtorrent/aux_/block_cache_reference.hpp @@ -0,0 +1,50 @@ +/* + +Copyright (c) 2010-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_BLOCK_CACHE_REFERENCE_HPP +#define TORRENT_BLOCK_CACHE_REFERENCE_HPP + +namespace libtorrent { +namespace aux { + + struct block_cache_reference + { + void* storage; + int piece; + int block; + }; + +} +} + +#endif + diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index 93462c558..ff97800fc 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -39,14 +39,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io_service.hpp" #include "libtorrent/time.hpp" #include "libtorrent/disk_buffer_holder.hpp" +#include "libtorrent/error_code.hpp" #include #include -#ifndef TORRENT_DISABLE_DHT -#include "libtorrent/socket.hpp" -#endif - #include "libtorrent/socket.hpp" // for tcp::endpoint #include "libtorrent/aux_/disable_warnings_push.hpp" diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index 41e4e2b26..fc3a169a0 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -54,9 +54,12 @@ namespace libtorrent struct disk_io_job; struct storage_interface; struct cache_status; - struct block_cache_reference; struct counters; - namespace aux { struct session_settings; } + namespace aux + { + struct session_settings; + struct block_cache_reference; + } #if TORRENT_USE_ASSERTS class file_storage; #endif @@ -346,7 +349,7 @@ namespace libtorrent int pad_job(disk_io_job const* j, int blocks_in_piece , int read_ahead) const; - void reclaim_block(block_cache_reference const& ref); + void reclaim_block(aux::block_cache_reference const& ref); // returns a range of all pieces. This might be a very // long list, use carefully @@ -408,7 +411,7 @@ namespace libtorrent // looks for this piece in the cache. If it's there, returns a pointer // to it, otherwise 0. - cached_piece_entry* find_piece(block_cache_reference const& ref); + cached_piece_entry* find_piece(aux::block_cache_reference const& ref); cached_piece_entry* find_piece(disk_io_job const* j); cached_piece_entry* find_piece(storage_interface* st, int piece); diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 098090ac4..74f5c5046 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -315,8 +315,8 @@ public: // peer_connection functions of the same names virtual void append_const_send_buffer(char const* buffer, int size , chained_buffer::free_buffer_fun destructor = &nop - , void* userdata = nullptr, block_cache_reference ref - = block_cache_reference()) override; + , void* userdata = nullptr, aux::block_cache_reference ref + = aux::block_cache_reference()) override; private: diff --git a/include/libtorrent/chained_buffer.hpp b/include/libtorrent/chained_buffer.hpp index 39147f441..4a1fbb223 100644 --- a/include/libtorrent/chained_buffer.hpp +++ b/include/libtorrent/chained_buffer.hpp @@ -34,7 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_CHAINED_BUFFER_HPP_INCLUDED #include "libtorrent/config.hpp" -#include "libtorrent/disk_io_job.hpp" // for block_cache_reference +#include "libtorrent/aux_/block_cache_reference.hpp" #include "libtorrent/debug.hpp" #include "libtorrent/buffer.hpp" @@ -60,7 +60,7 @@ namespace libtorrent // destructs/frees the buffer (1st arg) with // 2nd argument as userdata - typedef void (*free_buffer_fun)(char*, void*, block_cache_reference ref); + typedef void (*free_buffer_fun)(char*, void*, aux::block_cache_reference ref); struct buffer_t { @@ -73,7 +73,7 @@ namespace libtorrent char* start; // the first byte to send/receive in the buffer int size; // the total size of the buffer int used_size; // this is the number of bytes to send/receive - block_cache_reference ref; + aux::block_cache_reference ref; }; bool empty() const { return m_bytes == 0; } @@ -85,11 +85,11 @@ namespace libtorrent //TODO: 3 use span<> instead of (buffer,s) void append_buffer(char* buffer, int s, int used_size , free_buffer_fun destructor, void* userdata - , block_cache_reference ref = block_cache_reference()); + , aux::block_cache_reference ref = aux::block_cache_reference()); void prepend_buffer(char* buffer, int s, int used_size , free_buffer_fun destructor, void* userdata - , block_cache_reference ref = block_cache_reference()); + , aux::block_cache_reference ref = aux::block_cache_reference()); // returns the number of bytes available at the // end of the last chained buffer. diff --git a/include/libtorrent/disk_buffer_holder.hpp b/include/libtorrent/disk_buffer_holder.hpp index 7335c5e25..130b0e7e2 100644 --- a/include/libtorrent/disk_buffer_holder.hpp +++ b/include/libtorrent/disk_buffer_holder.hpp @@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" -#include "libtorrent/disk_io_job.hpp" // for block_cache_reference +#include "libtorrent/aux_/block_cache_reference.hpp" #include "libtorrent/span.hpp" #include @@ -49,7 +49,7 @@ namespace libtorrent struct TORRENT_EXTRA_EXPORT buffer_allocator_interface { virtual void free_disk_buffer(char* b) = 0; - virtual void reclaim_blocks(span refs) = 0; + virtual void reclaim_blocks(span refs) = 0; virtual disk_buffer_holder allocate_disk_buffer(char const* category) = 0; virtual disk_buffer_holder allocate_disk_buffer(bool& exceeded , std::shared_ptr o @@ -79,7 +79,7 @@ namespace libtorrent // using a disk buffer pool directly (there's only one // disk_buffer_pool per session) disk_buffer_holder(buffer_allocator_interface& alloc - , block_cache_reference const& ref, char* buf) noexcept; + , aux::block_cache_reference const& ref, char* buf) noexcept; // frees any unreleased disk buffer held by this object ~disk_buffer_holder(); @@ -96,7 +96,7 @@ namespace libtorrent // (or nullptr by default). If it's already holding a // disk buffer, it will first be freed. void reset(char* buf = 0); - void reset(block_cache_reference const& ref, char* buf); + void reset(aux::block_cache_reference const& ref, char* buf); // swap pointers of two disk buffer holders. void swap(disk_buffer_holder& h) noexcept @@ -106,7 +106,7 @@ namespace libtorrent std::swap(h.m_ref, m_ref); } - block_cache_reference ref() const noexcept { return m_ref; } + aux::block_cache_reference ref() const noexcept { return m_ref; } // implicitly convertible to true if the object is currently holding a // buffer @@ -116,7 +116,7 @@ namespace libtorrent buffer_allocator_interface* m_allocator; char* m_buf; - block_cache_reference m_ref; + aux::block_cache_reference m_ref; }; } diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index 3221f767f..26647fd6f 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/tailqueue.hpp" +#include "libtorrent/aux_/block_cache_reference.hpp" #include #include @@ -48,13 +49,6 @@ namespace libtorrent class torrent_info; struct add_torrent_params; - struct block_cache_reference - { - void* storage; - int piece; - int block; - }; - // disk_io_jobs are allocated in a pool allocator in disk_io_thread // they are always allocated from the network thread, posted // (as pointers) to the disk I/O thread, and then passed back @@ -178,7 +172,7 @@ namespace libtorrent // is set in a response to a read, the buffer needs to // be de-referenced by sending a reclaim_block message // back to the disk thread - block_cache_reference ref; + aux::block_cache_reference ref; // for read and write, the offset into the piece // the read or write should start diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index a46182a0f..c2a7812df 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -60,6 +60,11 @@ namespace libtorrent struct counters; class alert_manager; + namespace aux + { + struct block_cache_reference; + } + struct cached_piece_info { storage_interface* storage; @@ -327,7 +332,7 @@ namespace libtorrent void clear_piece(storage_interface* storage, int index) override; // implements buffer_allocator_interface - void reclaim_blocks(span ref) override; + void reclaim_blocks(span ref) override; void free_disk_buffer(char* buf) override { m_disk_cache.free_buffer(buf); } disk_buffer_holder allocate_disk_buffer(char const* category) override { diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 3c6640fbb..6699918f9 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -164,6 +164,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/sha1_hash.hpp" #include "libtorrent/string_view.hpp" #include "libtorrent/socket.hpp" +#include "libtorrent/error_code.hpp" namespace libtorrent { diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index a34dff3b4..be37a3910 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -148,7 +148,7 @@ namespace libtorrent }; // internal - inline void nop(char*, void*, block_cache_reference) {} + inline void nop(char*, void*, aux::block_cache_reference) {} struct TORRENT_EXTRA_EXPORT peer_connection_hot_members { @@ -630,13 +630,13 @@ namespace libtorrent void append_send_buffer(char* buffer, int size , chained_buffer::free_buffer_fun destructor = &nop - , void* userdata = nullptr, block_cache_reference ref - = block_cache_reference()); + , void* userdata = nullptr, aux::block_cache_reference ref + = aux::block_cache_reference()); virtual void append_const_send_buffer(char const* buffer, int size , chained_buffer::free_buffer_fun destructor = &nop - , void* userdata = nullptr, block_cache_reference ref - = block_cache_reference()); + , void* userdata = nullptr, aux::block_cache_reference ref + = aux::block_cache_reference()); int outstanding_bytes() const { return m_outstanding_bytes; } diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 4e8761501..65d7873ca 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -54,6 +54,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bitfield.hpp" #include "libtorrent/performance_counters.hpp" #include "libtorrent/span.hpp" +#include "libtorrent/tailqueue.hpp" +#include "libtorrent/disk_io_job.hpp" // OVERVIEW // diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index fa8f954a7..633555a8d 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -92,6 +92,7 @@ namespace libtorrent struct storage_interface; class bt_peer_connection; struct listen_socket_t; + struct disk_io_job; enum class waste_reason { diff --git a/src/block_cache.cpp b/src/block_cache.cpp index 6db414bf4..63a7b1c12 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alloca.hpp" #include "libtorrent/performance_counters.hpp" #include "libtorrent/aux_/time.hpp" +#include "libtorrent/aux_/block_cache_reference.hpp" /* @@ -1779,7 +1780,7 @@ int block_cache::copy_from_piece(cached_piece_entry* const pe return j->d.io.buffer_size; } -void block_cache::reclaim_block(block_cache_reference const& ref) +void block_cache::reclaim_block(aux::block_cache_reference const& ref) { cached_piece_entry* pe = find_piece(ref); TORRENT_ASSERT(pe); @@ -1817,7 +1818,7 @@ bool block_cache::maybe_free_piece(cached_piece_entry* pe) return true; } -cached_piece_entry* block_cache::find_piece(block_cache_reference const& ref) +cached_piece_entry* block_cache::find_piece(aux::block_cache_reference const& ref) { return find_piece(static_cast(ref.storage), ref.piece); } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 46bb77d4e..b6074e39d 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -725,7 +725,7 @@ namespace libtorrent namespace { void regular_c_free(char* buf, void* /* userdata */ - , block_cache_reference /* ref */) + , aux::block_cache_reference /* ref */) { std::free(buf); } @@ -735,7 +735,7 @@ namespace libtorrent void bt_peer_connection::append_const_send_buffer(char const* buffer, int size , chained_buffer::free_buffer_fun destructor, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) if (!m_enc_handler.is_send_plaintext()) @@ -2433,14 +2433,14 @@ namespace libtorrent namespace { void buffer_reclaim_block(char* /* buffer */, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { buffer_allocator_interface* buf = static_cast(userdata); buf->reclaim_blocks(ref); } void buffer_free_disk_buf(char* buffer, void* userdata - , block_cache_reference /* ref */) + , aux::block_cache_reference /* ref */) { buffer_allocator_interface* buf = static_cast(userdata); buf->free_disk_buffer(buffer); diff --git a/src/chained_buffer.cpp b/src/chained_buffer.cpp index de1f2b945..85382a3ac 100644 --- a/src/chained_buffer.cpp +++ b/src/chained_buffer.cpp @@ -68,7 +68,7 @@ namespace libtorrent void chained_buffer::append_buffer(char* buffer, int s, int used_size , free_buffer_fun destructor, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(s >= used_size); @@ -89,7 +89,7 @@ namespace libtorrent void chained_buffer::prepend_buffer(char* buffer, int s, int used_size , free_buffer_fun destructor, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { TORRENT_ASSERT(s >= used_size); buffer_t b; diff --git a/src/disk_buffer_holder.cpp b/src/disk_buffer_holder.cpp index a7408556d..6cf5110eb 100644 --- a/src/disk_buffer_holder.cpp +++ b/src/disk_buffer_holder.cpp @@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_buffer_holder.hpp" #include "libtorrent/storage.hpp" // for storage_interface +#include "libtorrent/aux_/block_cache_reference.hpp" namespace libtorrent { @@ -58,7 +59,7 @@ namespace libtorrent } disk_buffer_holder::disk_buffer_holder(buffer_allocator_interface& alloc - , block_cache_reference const& ref, char* buf) noexcept + , aux::block_cache_reference const& ref, char* buf) noexcept : m_allocator(&alloc), m_buf(buf), m_ref(ref) { TORRENT_ASSERT(m_ref.storage == nullptr || m_ref.piece >= 0); @@ -69,7 +70,7 @@ namespace libtorrent || m_ref.block <= static_cast(m_ref.storage)->files()->piece_length() / 0x4000); } - void disk_buffer_holder::reset(block_cache_reference const& ref, char* buf) + void disk_buffer_holder::reset(aux::block_cache_reference const& ref, char* buf) { if (m_ref.storage) m_allocator->reclaim_blocks(m_ref); else if (m_buf) m_allocator->free_disk_buffer(m_buf); diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 4bdeac6c8..7a1cc087f 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -239,7 +239,7 @@ namespace libtorrent m_hash_threads.abort(wait); } - void disk_io_thread::reclaim_blocks(span refs) + void disk_io_thread::reclaim_blocks(span refs) { TORRENT_ASSERT(m_magic == 0x1337); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 9d59497ca..1b3058f95 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/close_reason.hpp" +#include "libtorrent/disk_io_job.hpp" #include "libtorrent/aux_/has_block.hpp" #include "libtorrent/aux_/time.hpp" @@ -5676,7 +5677,7 @@ namespace libtorrent void peer_connection::append_send_buffer(char* buffer, int size , chained_buffer::free_buffer_fun destructor, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { TORRENT_ASSERT(is_single_thread()); m_send_buffer.append_buffer(buffer, size, size, destructor @@ -5685,7 +5686,7 @@ namespace libtorrent void peer_connection::append_const_send_buffer(char const* buffer, int size , chained_buffer::free_buffer_fun destructor, void* userdata - , block_cache_reference ref) + , aux::block_cache_reference ref) { TORRENT_ASSERT(is_single_thread()); m_send_buffer.append_buffer(const_cast(buffer), size, size, destructor @@ -5702,7 +5703,7 @@ namespace libtorrent } namespace { - void session_free_buffer(char* buffer, void* userdata, block_cache_reference) + void session_free_buffer(char* buffer, void* userdata, aux::block_cache_reference) { aux::session_interface* ses = static_cast(userdata); ses->free_buffer(buffer); diff --git a/test/test_buffer.cpp b/test/test_buffer.cpp index 6d1e561ea..fbc90bc4e 100644 --- a/test/test_buffer.cpp +++ b/test/test_buffer.cpp @@ -140,7 +140,7 @@ TORRENT_TEST(buffer_move_assign) std::set buffer_list; -void free_buffer(char* m, void* userdata, block_cache_reference ref) +void free_buffer(char* m, void* userdata, aux::block_cache_reference ref) { TEST_CHECK(userdata == (void*)0x1337); std::set::iterator i = buffer_list.find(m);