2023-11-01 22:11:11 +01:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 13:05:39 -05:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 12:27:56 -06: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 23:21:28 +03:00
|
|
|
#pragma once
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 15:04:45 +03:00
|
|
|
#error only libtransmission should #include this header.
|
2010-06-19 14:25:11 +00:00
|
|
|
#endif
|
|
|
|
|
2023-06-20 22:50:36 -05:00
|
|
|
#include <cstddef> // for size_t
|
2022-08-17 11:08:36 -05:00
|
|
|
#include <cstdint> // for intX_t, uintX_t
|
|
|
|
#include <memory> // for std::unique_ptr
|
|
|
|
#include <utility> // for std::pair
|
2022-06-12 02:46:30 +02:00
|
|
|
#include <vector>
|
2020-08-11 13:11:55 -05:00
|
|
|
|
2023-06-20 22:50:36 -05:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2023-11-04 01:03:26 +08:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-02-19 07:45:19 -06:00
|
|
|
|
2023-11-04 01:03:26 +08:00
|
|
|
#include "libtransmission/block-info.h"
|
2023-11-14 17:23:51 -06:00
|
|
|
#include "libtransmission/values.h"
|
2022-02-19 07:45:19 -06:00
|
|
|
|
2022-06-12 02:46:30 +02:00
|
|
|
class tr_torrents;
|
2021-12-15 15:25:42 -06:00
|
|
|
struct tr_torrent;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 02:46:30 +02:00
|
|
|
class Cache
|
|
|
|
{
|
|
|
|
public:
|
2023-06-20 22:50:36 -05:00
|
|
|
using BlockData = small::max_size_vector<uint8_t, tr_block_info::BlockSize>;
|
2023-11-14 17:23:51 -06:00
|
|
|
using Memory = libtransmission::Values::Memory;
|
2023-05-13 14:16:00 -05:00
|
|
|
|
2023-11-14 17:23:51 -06:00
|
|
|
Cache(tr_torrents const& torrents, Memory max_size);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-11-14 17:23:51 -06:00
|
|
|
int set_limit(Memory max_size);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-10-24 16:57:07 -05:00
|
|
|
// @return any error code from cacheTrim()
|
2023-05-13 14:16:00 -05:00
|
|
|
int write_block(tr_torrent_id_t tor, tr_block_index_t block, std::unique_ptr<BlockData> writeme);
|
2022-10-24 16:57:07 -05:00
|
|
|
|
2023-12-02 14:16:36 -06:00
|
|
|
int read_block(tr_torrent const& tor, tr_block_info::Location const& loc, size_t len, uint8_t* setme);
|
|
|
|
int flush_torrent(tr_torrent_id_t tor_id);
|
|
|
|
int flush_file(tr_torrent const& tor, tr_file_index_t file);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 02:46:30 +02: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 02:46:30 +02:00
|
|
|
struct CacheBlock
|
|
|
|
{
|
|
|
|
Key key;
|
2023-05-13 14:16:00 -05:00
|
|
|
std::unique_ptr<BlockData> buf;
|
2022-06-12 02:46:30 +02:00
|
|
|
};
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 02:46:30 +02:00
|
|
|
using Blocks = std::vector<CacheBlock>;
|
|
|
|
using CIter = Blocks::const_iterator;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-12-02 14:16:36 -06:00
|
|
|
[[nodiscard]] static Key make_key(tr_torrent const& tor, tr_block_info::Location loc) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-06-27 12:08:29 -05: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 02:46:30 +02:00
|
|
|
// @return any error code from tr_ioWrite()
|
2023-06-27 12:08:29 -05:00
|
|
|
[[nodiscard]] int write_contiguous(CIter begin, CIter end) const;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 02:46:30 +02:00
|
|
|
// @return any error code from writeContiguous()
|
2023-06-27 12:08:29 -05:00
|
|
|
[[nodiscard]] int flush_span(CIter begin, CIter end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 02:46:30 +02:00
|
|
|
// @return any error code from writeContiguous()
|
2023-06-27 12:08:29 -05:00
|
|
|
[[nodiscard]] int flush_biggest();
|
2022-06-12 02:46:30 +02:00
|
|
|
|
|
|
|
// @return any error code from writeContiguous()
|
2023-05-05 23:11:05 -05:00
|
|
|
[[nodiscard]] int cache_trim();
|
2022-06-12 02:46:30 +02:00
|
|
|
|
2023-11-14 17:23:51 -06:00
|
|
|
[[nodiscard]] static constexpr size_t get_max_blocks(Memory const max_size) noexcept
|
|
|
|
{
|
|
|
|
return max_size.base_quantity() / tr_block_info::BlockSize;
|
|
|
|
}
|
2022-06-12 02:46:30 +02:00
|
|
|
|
2023-12-02 14:16:36 -06:00
|
|
|
[[nodiscard]] CIter get_block(tr_torrent const& tor, tr_block_info::Location const& loc) noexcept;
|
2022-06-12 02:46:30 +02:00
|
|
|
|
2023-11-13 01:53:04 +08:00
|
|
|
tr_torrents const& torrents_;
|
2022-06-12 02:46:30 +02:00
|
|
|
|
2024-11-17 20:04:55 -06:00
|
|
|
Blocks blocks_;
|
2022-06-12 02:46:30 +02:00
|
|
|
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 23:23:38 +08:00
|
|
|
|
|
|
|
static constexpr struct
|
|
|
|
{
|
2024-02-17 22:43:24 -06:00
|
|
|
[[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block) const
|
2023-06-28 23:23:38 +08:00
|
|
|
{
|
|
|
|
return key < block.key;
|
|
|
|
}
|
2024-02-17 22:43:24 -06:00
|
|
|
[[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key) const
|
2023-06-28 23:23:38 +08:00
|
|
|
{
|
|
|
|
return block.key < key;
|
|
|
|
}
|
|
|
|
} CompareCacheBlockByKey{};
|
2022-06-12 02:46:30 +02:00
|
|
|
};
|