| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_READ_FILEDATA_BASE_BUFFERED_HPP |
| 4 |
#define MPT_IO_READ_FILEDATA_BASE_BUFFERED_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/memory.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/numeric.hpp" |
| 11 |
#include "mpt/io/base.hpp" |
| 12 |
#include "mpt/io_read/filedata.hpp" |
| 13 |
#include "mpt/io_read/filedata_base_seekable.hpp" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace IO { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class FileDataSeekableBuffered : public FileDataSeekable { |
| 32 |
|
| 33 |
private: |
| 34 |
enum : std::size_t { |
| 35 |
CHUNK_SIZE = mpt::IO::BUFFERSIZE_SMALL, |
| 36 |
BUFFER_SIZE = mpt::IO::BUFFERSIZE_NORMAL |
| 37 |
}; |
| 38 |
enum : std::size_t { |
| 39 |
NUM_CHUNKS = BUFFER_SIZE / CHUNK_SIZE |
| 40 |
}; |
| 41 |
struct chunk_info { |
| 42 |
pos_type ChunkOffset = 0; |
| 43 |
std::size_t ChunkLength = 0; |
| 44 |
bool ChunkValid = false; |
| 45 |
}; |
| 46 |
mutable std::vector<std::byte> m_Buffer = std::vector<std::byte>(BUFFER_SIZE); |
| 47 |
|
| 48 |
mpt::byte_span chunk_data(std::size_t chunkIndex) const { |
| 49 |
return mpt::byte_span(m_Buffer.data() + (chunkIndex * CHUNK_SIZE), CHUNK_SIZE); |
| 50 |
} |
| 51 |
|
| 52 |
mutable std::array<chunk_info, NUM_CHUNKS> m_ChunkInfo = {}; |
| 53 |
mutable std::array<std::size_t, NUM_CHUNKS> m_ChunkIndexLRU = {}; |
| 54 |
|
| 55 |
std::size_t InternalFillPageAndReturnIndex(pos_type pos) const { |
| 56 |
pos = mpt::align_down(pos, static_cast<pos_type>(CHUNK_SIZE)); |
| 57 |
for (std::size_t chunkLRUIndex = 0; chunkLRUIndex < NUM_CHUNKS; ++chunkLRUIndex) { |
| 58 |
std::size_t chunkIndex = m_ChunkIndexLRU[chunkLRUIndex]; |
| 59 |
if (m_ChunkInfo[chunkIndex].ChunkValid && (m_ChunkInfo[chunkIndex].ChunkOffset == pos)) { |
| 60 |
std::size_t chunk = std::move(m_ChunkIndexLRU[chunkLRUIndex]); |
| 61 |
std::move_backward(m_ChunkIndexLRU.begin(), m_ChunkIndexLRU.begin() + chunkLRUIndex, m_ChunkIndexLRU.begin() + (chunkLRUIndex + 1)); |
| 62 |
m_ChunkIndexLRU[0] = std::move(chunk); |
| 63 |
return chunkIndex; |
| 64 |
} |
| 65 |
} |
| 66 |
{ |
| 67 |
std::size_t chunk = std::move(m_ChunkIndexLRU[NUM_CHUNKS - 1]); |
| 68 |
std::move_backward(m_ChunkIndexLRU.begin(), m_ChunkIndexLRU.begin() + (NUM_CHUNKS - 1), m_ChunkIndexLRU.begin() + NUM_CHUNKS); |
| 69 |
m_ChunkIndexLRU[0] = std::move(chunk); |
| 70 |
} |
| 71 |
std::size_t chunkIndex = m_ChunkIndexLRU[0]; |
| 72 |
chunk_info & chunk = m_ChunkInfo[chunkIndex]; |
| 73 |
chunk.ChunkOffset = pos; |
| 74 |
chunk.ChunkLength = InternalReadBuffered(pos, chunk_data(chunkIndex)).size(); |
| 75 |
chunk.ChunkValid = true; |
| 76 |
return chunkIndex; |
| 77 |
} |
| 78 |
|
| 79 |
protected: |
| 80 |
FileDataSeekableBuffered(pos_type streamLength_) |
| 81 |
: FileDataSeekable(streamLength_) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
private: |
| 86 |
mpt::byte_span InternalReadSeekable(pos_type pos, mpt::byte_span dst) const override { |
| 87 |
pos_type totalRead = 0; |
| 88 |
std::byte * pdst = dst.data(); |
| 89 |
std::size_t count = dst.size(); |
| 90 |
while (count > 0) { |
| 91 |
std::size_t chunkIndex = InternalFillPageAndReturnIndex(pos); |
| 92 |
pos_type pageSkip = pos - m_ChunkInfo[chunkIndex].ChunkOffset; |
| 93 |
pos_type chunkWanted = std::min(static_cast<pos_type>(CHUNK_SIZE) - pageSkip, static_cast<pos_type>(count)); |
| 94 |
pos_type chunkGot = (static_cast<pos_type>(m_ChunkInfo[chunkIndex].ChunkLength) > pageSkip) ? (static_cast<pos_type>(m_ChunkInfo[chunkIndex].ChunkLength) - pageSkip) : 0; |
| 95 |
pos_type chunk = std::min(chunkWanted, chunkGot); |
| 96 |
std::copy(chunk_data(chunkIndex).data() + static_cast<std::size_t>(pageSkip), chunk_data(chunkIndex).data() + static_cast<std::size_t>(pageSkip + chunk), pdst); |
| 97 |
pos += chunk; |
| 98 |
pdst += chunk; |
| 99 |
totalRead += chunk; |
| 100 |
count -= static_cast<std::size_t>(chunk); |
| 101 |
if (chunkWanted > chunk) { |
| 102 |
return dst.first(static_cast<std::size_t>(totalRead)); |
| 103 |
} |
| 104 |
} |
| 105 |
return dst.first(static_cast<std::size_t>(totalRead)); |
| 106 |
} |
| 107 |
|
| 108 |
virtual mpt::byte_span InternalReadBuffered(pos_type pos, mpt::byte_span dst) const = 0; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
} // namespace IO |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
} // namespace MPT_INLINE_NS |
| 118 |
} // namespace mpt |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#endif // MPT_IO_READ_FILEDATA_BASE_BUFFERED_HPP |