diff --git a/CMakeLists.txt b/CMakeLists.txt index 57804452a..daace9ba0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(libtorrent) set(sources alert + allocator assert connection_queue create_torrent diff --git a/Jamfile b/Jamfile index fdf8bdaab..584217632 100755 --- a/Jamfile +++ b/Jamfile @@ -302,6 +302,7 @@ lib ws2_32 : : ws2_32 ; SOURCES = alert + allocator assert connection_queue create_torrent diff --git a/include/Makefile.am b/include/Makefile.am index 332c20f9e..565fdfba8 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,4 +1,5 @@ nobase_include_HEADERS = libtorrent/alert.hpp \ +libtorrent/allocator.hpp \ libtorrent/alert_types.hpp \ libtorrent/assert.hpp \ libtorrent/alloca.hpp \ diff --git a/include/libtorrent/allocator.hpp b/include/libtorrent/allocator.hpp new file mode 100644 index 000000000..af9772547 --- /dev/null +++ b/include/libtorrent/allocator.hpp @@ -0,0 +1,48 @@ +/* + +Copyright (c) 2009, 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 + +namespace libtorrent +{ + + struct page_aligned_allocator + { + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + static char* malloc(const size_type bytes); + static void free(char* const block); + }; + +} + diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 722970e93..a52373ccb 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/storage.hpp" #include "libtorrent/hasher.hpp" #include "libtorrent/utf8.hpp" +#include "libtorrent/allocator.hpp" #include #include @@ -207,6 +208,15 @@ namespace libtorrent #endif } + struct piece_holder + { + piece_holder(int bytes): m_piece(page_aligned_allocator::malloc(bytes)) {} + ~piece_holder() { page_aligned_allocator::free(m_piece); } + char* bytes() { return m_piece; } + private: + char* m_piece; + }; + template void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f) { @@ -216,13 +226,13 @@ namespace libtorrent // calculate the hash for all pieces int num = t.num_pieces(); - std::vector buf(t.piece_length()); + piece_holder buf(t.piece_length()); for (int i = 0; i < num; ++i) { // read hits the disk and will block. Progress should // be updated in between reads - st->read(&buf[0], i, 0, t.piece_size(i)); - hasher h(&buf[0], t.piece_size(i)); + st->read(buf.bytes(), i, 0, t.piece_size(i)); + hasher h(buf.bytes(), t.piece_size(i)); t.set_hash(i, h.final()); f(i); } diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index c8e3f217f..32870bfb7 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #endif #include "libtorrent/storage.hpp" +#include "libtorrent/allocator.hpp" #include #include #include @@ -53,15 +54,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - struct page_aligned_allocator - { - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - - static char* malloc(const size_type bytes); - static void free(char* const block); - }; - struct cached_piece_info { int piece; diff --git a/src/Makefile.am b/src/Makefile.am index 6a363c57c..af6f92db6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,10 +25,13 @@ logger.cpp file_pool.cpp ut_pex.cpp lsd.cpp upnp.cpp instantiate_connection.cpp socks5_stream.cpp socks4_stream.cpp http_stream.cpp connection_queue.cpp \ disk_io_thread.cpp ut_metadata.cpp lt_trackers.cpp magnet_uri.cpp udp_socket.cpp smart_ban.cpp \ http_parser.cpp gzip.cpp disk_buffer_holder.cpp create_torrent.cpp GeoIP.c \ -parse_url.cpp file_storage.cpp error_code.cpp ConvertUTF.cpp $(kademlia_sources) +parse_url.cpp file_storage.cpp error_code.cpp ConvertUTF.cpp \ +allocator.cpp \ +$(kademlia_sources) noinst_HEADERS = \ $(top_srcdir)/include/libtorrent/alert.hpp \ +$(top_srcdir)/include/libtorrent/allocator.hpp \ $(top_srcdir)/include/libtorrent/alert_types.hpp \ $(top_srcdir)/include/libtorrent/assert.hpp \ $(top_srcdir)/include/libtorrent/alloca.hpp \ diff --git a/src/allocator.cpp b/src/allocator.cpp new file mode 100644 index 000000000..b48a54307 --- /dev/null +++ b/src/allocator.cpp @@ -0,0 +1,65 @@ +/* + +Copyright (c) 2009, 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/allocator.hpp" +#include "libtorrent/config.hpp" + +#ifdef TORRENT_WINDOWS +#include +#else +#include +#endif + + +namespace libtorrent +{ + char* page_aligned_allocator::malloc(const size_type bytes) + { +#ifdef TORRENT_WINDOWS + return reinterpret_cast(VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); +#else + return reinterpret_cast(valloc(bytes)); +#endif + } + + void page_aligned_allocator::free(char* const block) + { +#ifdef TORRENT_WINDOWS + VirtualFree(block, 0, MEM_RELEASE); +#else + free(block); +#endif + } + + +} + diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 95b846069..522c89214 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -38,12 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/invariant_check.hpp" #include -#ifdef TORRENT_WINDOWS -#include -#else -#include -#endif - #ifdef TORRENT_DISK_STATS #include "libtorrent/time.hpp" #endif @@ -51,24 +45,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - char* page_aligned_allocator::malloc(const size_type bytes) - { -#ifdef TORRENT_WINDOWS - return reinterpret_cast(VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); -#else - return reinterpret_cast(valloc(bytes)); -#endif - } - - void page_aligned_allocator::free(char* const block) - { -#ifdef TORRENT_WINDOWS - VirtualFree(block, 0, MEM_RELEASE); -#else - std::free(block); -#endif - } - disk_io_thread::disk_io_thread(asio::io_service& ios, int block_size) : m_abort(false) , m_queue_buffer_size(0)