moved page_aligned_allocator to its own file and uses it in set_piece_hashes to not pass in unaligned buffers to storage read

This commit is contained in:
Arvid Norberg 2009-01-15 17:09:36 +00:00
parent 9edfd93603
commit 113d1f3557
9 changed files with 134 additions and 37 deletions

View File

@ -3,6 +3,7 @@ project(libtorrent)
set(sources
alert
allocator
assert
connection_queue
create_torrent

View File

@ -302,6 +302,7 @@ lib ws2_32 : : <name>ws2_32 ;
SOURCES =
alert
allocator
assert
connection_queue
create_torrent

View File

@ -1,4 +1,5 @@
nobase_include_HEADERS = libtorrent/alert.hpp \
libtorrent/allocator.hpp \
libtorrent/alert_types.hpp \
libtorrent/assert.hpp \
libtorrent/alloca.hpp \

View File

@ -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 <cstddef>
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);
};
}

View File

@ -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 <vector>
#include <string>
@ -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 <class Fun>
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<char> 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);
}

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include "libtorrent/storage.hpp"
#include "libtorrent/allocator.hpp"
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
@ -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;

View File

@ -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 \

65
src/allocator.cpp Normal file
View File

@ -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 <Windows.h>
#else
#include <stdlib.h>
#endif
namespace libtorrent
{
char* page_aligned_allocator::malloc(const size_type bytes)
{
#ifdef TORRENT_WINDOWS
return reinterpret_cast<char*>(VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
#else
return reinterpret_cast<char*>(valloc(bytes));
#endif
}
void page_aligned_allocator::free(char* const block)
{
#ifdef TORRENT_WINDOWS
VirtualFree(block, 0, MEM_RELEASE);
#else
free(block);
#endif
}
}

View File

@ -38,12 +38,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/invariant_check.hpp"
#include <boost/scoped_array.hpp>
#ifdef TORRENT_WINDOWS
#include <Windows.h>
#else
#include <stdlib.h>
#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<char*>(VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
#else
return reinterpret_cast<char*>(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)