Skip to content

Commit 749d0da

Browse files
committed
make iovec_t an alias for span<char>, in preparation for removing it altogether in favour of span
1 parent a693565 commit 749d0da

17 files changed

+191
-191
lines changed

examples/connection_tester.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ using namespace lt::detail; // for write_* and read_*
6363

6464
using namespace std::placeholders;
6565

66+
// TODO: this should take a span
6667
void generate_block(std::uint32_t* buffer, piece_index_t const piece, int start, int length)
6768
{
6869
std::uint32_t fill = (static_cast<int>(piece) << 8) | ((start / 0x4000) & 0xff);
@@ -867,7 +868,8 @@ void generate_data(char const* path, torrent_info const& ti)
867868
{
868869
generate_block(piece, i, j, 0x4000);
869870
int const left_in_piece = ti.piece_size(i) - j;
870-
iovec_t const b = { piece, size_t(std::min(left_in_piece, 0x4000))};
871+
iovec_t const b = { reinterpret_cast<char*>(piece)
872+
, size_t(std::min(left_in_piece, 0x4000))};
871873
storage_error error;
872874
st->writev(b, i, j, 0, error);
873875
if (error)

include/libtorrent/aux_/storage_utils.hpp

+5-14
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ POSSIBILITY OF SUCH DAMAGE.
4040
#include "libtorrent/units.hpp"
4141
#include "libtorrent/storage_defs.hpp" // for status_t
4242

43-
#ifndef TORRENT_WINDOWS
44-
#include <sys/uio.h> // for iovec
45-
#endif
46-
4743
namespace libtorrent {
4844

4945
class file_storage;
@@ -52,19 +48,14 @@ namespace libtorrent {
5248
struct stat_cache;
5349
struct add_torrent_params;
5450

55-
#ifdef TORRENT_WINDOWS
56-
struct iovec_t
57-
{
58-
void* iov_base;
59-
size_t iov_len;
60-
};
61-
#else
62-
using iovec_t = ::iovec;
63-
#endif
51+
// TODO: 3 remove this typedef, and use span<char const> for disk write
52+
// operations
53+
using iovec_t = span<char>;
6454

6555
namespace aux {
6656

67-
TORRENT_EXTRA_EXPORT int copy_bufs(span<iovec_t const> bufs, int bytes, span<iovec_t> target);
57+
TORRENT_EXTRA_EXPORT int copy_bufs(span<iovec_t const> bufs
58+
, int bytes, span<iovec_t> target);
6859
TORRENT_EXTRA_EXPORT span<iovec_t> advance_bufs(span<iovec_t> bufs, int bytes);
6960

7061
// this identifies a read or write operation so that readwritev() knows

include/libtorrent/file.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ namespace libtorrent {
214214
static bool has_manage_volume_privs;
215215
#endif
216216
};
217-
218-
TORRENT_EXTRA_EXPORT int bufs_size(span<iovec_t const> bufs);
219-
220217
}
221218

222219
#endif // TORRENT_FILE_HPP_INCLUDED

src/block_cache.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1272,14 +1272,14 @@ void block_cache::insert_blocks(cached_piece_entry* pe, int block, span<iovec_t
12721272
for (auto const& buf : iov)
12731273
{
12741274
// each iovec buffer has to be the size of a block (or the size of the last block)
1275-
TORRENT_PIECE_ASSERT(int(buf.iov_len) == std::min(block_size()
1275+
TORRENT_PIECE_ASSERT(int(buf.size()) == std::min(block_size()
12761276
, pe->storage->files().piece_size(pe->piece) - block * block_size()), pe);
12771277

12781278
// no nullptrs allowed
1279-
TORRENT_ASSERT(buf.iov_base != nullptr);
1279+
TORRENT_ASSERT(buf.data() != nullptr);
12801280

12811281
#ifdef TORRENT_DEBUG_BUFFERS
1282-
TORRENT_PIECE_ASSERT(is_disk_buffer(static_cast<char*>(buf.iov_base)), pe);
1282+
TORRENT_PIECE_ASSERT(is_disk_buffer(buf.data()), pe);
12831283
#endif
12841284

12851285
if (pe->blocks[block].buf && (flags & blocks_inc_refcount))
@@ -1290,13 +1290,13 @@ void block_cache::insert_blocks(cached_piece_entry* pe, int block, span<iovec_t
12901290
// either free the block or insert it. Never replace a block
12911291
if (pe->blocks[block].buf)
12921292
{
1293-
free_buffer(static_cast<char*>(buf.iov_base));
1293+
free_buffer(buf.data());
12941294
}
12951295
else
12961296
{
1297-
pe->blocks[block].buf = static_cast<char*>(buf.iov_base);
1297+
pe->blocks[block].buf = buf.data();
12981298

1299-
TORRENT_PIECE_ASSERT(buf.iov_base != nullptr, pe);
1299+
TORRENT_PIECE_ASSERT(buf.data() != nullptr, pe);
13001300
TORRENT_PIECE_ASSERT(pe->blocks[block].dirty == false, pe);
13011301
++pe->num_blocks;
13021302
++m_read_cache_size;

src/disk_buffer_pool.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,16 @@ namespace libtorrent {
195195
std::unique_lock<std::mutex> l(m_pool_mutex);
196196
for (auto& i : iov)
197197
{
198-
i.iov_base = allocate_buffer_impl(l, "pending read");
199-
i.iov_len = std::size_t(block_size());
200-
if (i.iov_base == nullptr)
198+
i = { allocate_buffer_impl(l, "pending read"), std::size_t(block_size())};
199+
if (i.data() == nullptr)
201200
{
202201
// uh oh. We failed to allocate the buffer!
203202
// we need to roll back and free all the buffers
204203
// we've already allocated
205204
for (auto j : iov)
206205
{
207-
if (j.iov_base == nullptr) break;
208-
char* buf = static_cast<char*>(j.iov_base);
206+
if (j.data() == nullptr) break;
207+
char* buf = j.data();
209208
TORRENT_ASSERT(is_disk_buffer(buf, l));
210209
free_buffer_impl(buf, l);
211210
remove_buffer_in_use(buf);
@@ -222,7 +221,7 @@ namespace libtorrent {
222221
std::unique_lock<std::mutex> l(m_pool_mutex);
223222
for (auto i : iov)
224223
{
225-
char* buf = static_cast<char*>(i.iov_base);
224+
char* buf = i.data();
226225
TORRENT_ASSERT(is_disk_buffer(buf, l));
227226
free_buffer_impl(buf, l);
228227
remove_buffer_in_use(buf);

src/disk_io_thread.cpp

+21-22
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,7 @@ namespace libtorrent {
631631
TORRENT_UNUSED(locked);
632632

633633
flushing[num_flushing++] = i + block_base_index;
634-
iov[iov_len].iov_base = pe->blocks[i].buf;
635-
iov[iov_len].iov_len = aux::numeric_cast<std::size_t>(std::min(block_size, size_left));
634+
iov[iov_len] = { pe->blocks[i].buf, aux::numeric_cast<std::size_t>(std::min(block_size, size_left)) };
636635
++iov_len;
637636
pe->blocks[i].pending = true;
638637

@@ -1302,9 +1301,10 @@ namespace libtorrent {
13021301

13031302
// if this is the last piece, adjust the size of the
13041303
// last buffer to match up
1305-
iov[iov_len - 1].iov_len = aux::numeric_cast<std::size_t>(std::min(int(piece_size - adjusted_offset)
1306-
- (iov_len - 1) * block_size, block_size));
1307-
TORRENT_ASSERT(iov[iov_len - 1].iov_len > 0);
1304+
iov[iov_len - 1] = iov[iov_len - 1].first(aux::numeric_cast<std::size_t>(
1305+
std::min(int(piece_size - adjusted_offset)
1306+
- (iov_len - 1) * block_size, block_size)));
1307+
TORRENT_ASSERT(iov[iov_len - 1].size() > 0);
13081308

13091309
// at this point, all the buffers are allocated and iov is initialized
13101310
// and the blocks have their refcounters incremented, so no other thread
@@ -2162,8 +2162,7 @@ namespace libtorrent {
21622162
std::uint32_t const file_flags = file_flags_for_job(j
21632163
, m_settings.get_bool(settings_pack::coalesce_reads));
21642164

2165-
iovec_t iov;
2166-
iov.iov_base = m_disk_cache.allocate_buffer("hashing");
2165+
iovec_t iov = { m_disk_cache.allocate_buffer("hashing"), 0x4000 };
21672166
hasher h;
21682167
int ret = 0;
21692168
int offset = 0;
@@ -2174,10 +2173,11 @@ namespace libtorrent {
21742173

21752174
time_point const start_time = clock_type::now();
21762175

2177-
iov.iov_len = aux::numeric_cast<std::size_t>(std::min(block_size, piece_size - offset));
2176+
iov = iov.first(aux::numeric_cast<std::size_t>(std::min(block_size, piece_size - offset)));
21782177
ret = j->storage->readv(iov, j->piece
21792178
, offset, file_flags, j->error);
21802179
if (ret < 0) break;
2180+
iov = iov.first(std::size_t(ret));
21812181

21822182
if (!j->error.ec)
21832183
{
@@ -2191,10 +2191,10 @@ namespace libtorrent {
21912191
}
21922192

21932193
offset += block_size;
2194-
h.update(static_cast<char const*>(iov.iov_base), int(iov.iov_len));
2194+
h.update(iov);
21952195
}
21962196

2197-
m_disk_cache.free_buffer(static_cast<char*>(iov.iov_base));
2197+
m_disk_cache.free_buffer(iov.data());
21982198

21992199
sha1_hash piece_hash = h.final();
22002200
std::memcpy(j->d.piece_hash, piece_hash.data(), 20);
@@ -2326,23 +2326,22 @@ namespace libtorrent {
23262326
int next_locked_block = 0;
23272327
for (int i = offset / block_size; i < blocks_in_piece; ++i)
23282328
{
2329-
iovec_t iov;
2330-
iov.iov_len = aux::numeric_cast<std::size_t>(std::min(block_size, piece_size - offset));
2331-
23322329
if (next_locked_block < num_locked_blocks
23332330
&& locked_blocks[next_locked_block] == i)
23342331
{
2332+
int const len = std::min(block_size, piece_size - offset);
23352333
++next_locked_block;
23362334
TORRENT_PIECE_ASSERT(pe->blocks[i].buf, pe);
23372335
TORRENT_PIECE_ASSERT(offset == i * block_size, pe);
2338-
offset += int(iov.iov_len);
2339-
ph->h.update({pe->blocks[i].buf, iov.iov_len});
2336+
offset += len;
2337+
ph->h.update({pe->blocks[i].buf, aux::numeric_cast<std::size_t>(len)});
23402338
}
23412339
else
23422340
{
2343-
iov.iov_base = m_disk_cache.allocate_buffer("hashing");
2341+
iovec_t const iov = { m_disk_cache.allocate_buffer("hashing")
2342+
, aux::numeric_cast<std::size_t>(std::min(block_size, piece_size - offset))};
23442343

2345-
if (iov.iov_base == nullptr)
2344+
if (iov.data() == nullptr)
23462345
{
23472346
l.lock();
23482347

@@ -2374,19 +2373,19 @@ namespace libtorrent {
23742373
{
23752374
ret = status_t::fatal_disk_error;
23762375
TORRENT_ASSERT(j->error.ec && j->error.operation != 0);
2377-
m_disk_cache.free_buffer(static_cast<char*>(iov.iov_base));
2376+
m_disk_cache.free_buffer(iov.data());
23782377
break;
23792378
}
23802379

23812380
// treat a short read as an error. The hash will be invalid, the
23822381
// block cannot be cached and the main thread should skip the rest
23832382
// of this file
2384-
if (read_ret != int(iov.iov_len))
2383+
if (read_ret != int(iov.size()))
23852384
{
23862385
ret = status_t::fatal_disk_error;
23872386
j->error.ec = boost::asio::error::eof;
23882387
j->error.operation = storage_error::read;
2389-
m_disk_cache.free_buffer(static_cast<char*>(iov.iov_base));
2388+
m_disk_cache.free_buffer(iov.data());
23902389
break;
23912390
}
23922391

@@ -2403,8 +2402,8 @@ namespace libtorrent {
24032402
}
24042403

24052404
TORRENT_PIECE_ASSERT(offset == i * block_size, pe);
2406-
offset += int(iov.iov_len);
2407-
ph->h.update({static_cast<char const*>(iov.iov_base), iov.iov_len});
2405+
offset += int(iov.size());
2406+
ph->h.update(iov);
24082407

24092408
l.lock();
24102409
m_disk_cache.insert_blocks(pe, i, iov, j);

0 commit comments

Comments
 (0)