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

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