2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2010-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2010-06-19 14:25:11 +00:00
|
|
|
#endif
|
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
#include <cstddef> // for size_t
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint> // for intX_t, uintX_t
|
|
|
|
#include <memory> // for std::unique_ptr
|
|
|
|
#include <utility> // for std::pair
|
2022-06-12 00:46:30 +00:00
|
|
|
#include <vector>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2022-02-19 13:45:19 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "block-info.h"
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
class tr_torrents;
|
2021-12-15 21:25:42 +00:00
|
|
|
struct tr_torrent;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
class Cache
|
|
|
|
{
|
|
|
|
public:
|
2023-06-21 03:50:36 +00:00
|
|
|
using BlockData = small::max_size_vector<uint8_t, tr_block_info::BlockSize>;
|
2023-05-13 19:16:00 +00:00
|
|
|
|
2023-06-25 18:29:58 +00:00
|
|
|
Cache(tr_torrents& torrents, size_t max_bytes);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-06-25 18:29:58 +00:00
|
|
|
int set_limit(size_t new_limit);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
// @return any error code from cacheTrim()
|
2023-05-13 19:16:00 +00:00
|
|
|
int write_block(tr_torrent_id_t tor, tr_block_index_t block, std::unique_ptr<BlockData> writeme);
|
2022-10-24 21:57:07 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
int read_block(tr_torrent* torrent, tr_block_info::Location const& loc, uint32_t len, uint8_t* setme);
|
|
|
|
int prefetch_block(tr_torrent* torrent, tr_block_info::Location const& loc, uint32_t len);
|
|
|
|
int flush_torrent(tr_torrent const* torrent);
|
|
|
|
int flush_file(tr_torrent const* torrent, tr_file_index_t file);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
private:
|
|
|
|
using Key = std::pair<tr_torrent_id_t, tr_block_index_t>;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
struct CacheBlock
|
|
|
|
{
|
|
|
|
Key key;
|
2023-05-13 19:16:00 +00:00
|
|
|
std::unique_ptr<BlockData> buf;
|
2022-06-12 00:46:30 +00:00
|
|
|
};
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
using Blocks = std::vector<CacheBlock>;
|
|
|
|
using CIter = Blocks::const_iterator;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] static Key make_key(tr_torrent const* torrent, tr_block_info::Location loc) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] static std::pair<CIter, CIter> find_biggest_span(CIter begin, CIter end) noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] static CIter find_span_end(CIter span_begin, CIter end) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from tr_ioWrite()
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int write_contiguous(CIter begin, CIter end) const;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from writeContiguous()
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int flush_span(CIter begin, CIter end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from writeContiguous()
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int flush_biggest();
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
// @return any error code from writeContiguous()
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] int cache_trim();
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-06-25 18:29:58 +00:00
|
|
|
[[nodiscard]] static size_t get_max_blocks(size_t max_bytes) noexcept;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] CIter get_block(tr_torrent const* torrent, tr_block_info::Location const& loc) noexcept;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
tr_torrents& torrents_;
|
|
|
|
|
|
|
|
Blocks blocks_ = {};
|
|
|
|
size_t max_blocks_ = 0;
|
|
|
|
|
|
|
|
mutable size_t disk_writes_ = 0;
|
|
|
|
mutable size_t disk_write_bytes_ = 0;
|
|
|
|
mutable size_t cache_writes_ = 0;
|
|
|
|
mutable size_t cache_write_bytes_ = 0;
|
2023-06-28 15:23:38 +00:00
|
|
|
|
|
|
|
static constexpr struct
|
|
|
|
{
|
|
|
|
[[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block)
|
|
|
|
{
|
|
|
|
return key < block.key;
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key)
|
|
|
|
{
|
|
|
|
return block.key < key;
|
|
|
|
}
|
|
|
|
} CompareCacheBlockByKey{};
|
2022-06-12 00:46:30 +00:00
|
|
|
};
|