| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_READ_FILEDATA_MEMORY_HPP |
| 4 |
#define MPT_IO_READ_FILEDATA_MEMORY_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/memory.hpp" |
| 10 |
#include "mpt/base/namespace.hpp" |
| 11 |
#include "mpt/io_read/filedata.hpp" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
#if MPT_GCC_AT_LEAST(12, 0, 0) |
| 17 |
#include <cstring> |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace IO { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class FileDataMemory |
| 32 |
: public IFileData { |
| 33 |
|
| 34 |
private: |
| 35 |
const std::byte * streamData; // Pointer to memory-mapped file |
| 36 |
pos_type streamLength; // Size of memory-mapped file in bytes |
| 37 |
|
| 38 |
public: |
| 39 |
FileDataMemory() |
| 40 |
: streamData(nullptr) |
| 41 |
, streamLength(0) { } |
| 42 |
FileDataMemory(mpt::const_byte_span data) |
| 43 |
: streamData(data.data()) |
| 44 |
, streamLength(data.size()) { } |
| 45 |
|
| 46 |
public: |
| 47 |
bool IsValid() const override { |
| 48 |
return streamData != nullptr; |
| 49 |
} |
| 50 |
|
| 51 |
bool HasFastGetLength() const override { |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
bool HasPinnedView() const override { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
const std::byte * GetRawData() const override { |
| 60 |
return streamData; |
| 61 |
} |
| 62 |
|
| 63 |
pos_type GetLength() const override { |
| 64 |
return streamLength; |
| 65 |
} |
| 66 |
|
| 67 |
mpt::byte_span Read(pos_type pos, mpt::byte_span dst) const override { |
| 68 |
if (pos >= streamLength) { |
| 69 |
return dst.first(0); |
| 70 |
} |
| 71 |
pos_type avail = std::min(streamLength - pos, static_cast<pos_type>(dst.size())); |
| 72 |
const std::byte * src = streamData + pos; |
| 73 |
#if MPT_GCC_AT_LEAST(12, 0, 0) |
| 74 |
// work-around bogus -Warray-bounds |
| 75 |
// work-around bogus -Wstringop-overflow |
| 76 |
// See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121165>. |
| 77 |
std::memcpy(dst.data(), src, avail); |
| 78 |
#else |
| 79 |
std::copy(src, src + avail, dst.data()); |
| 80 |
#endif |
| 81 |
return dst.first(static_cast<std::size_t>(avail)); |
| 82 |
} |
| 83 |
|
| 84 |
bool CanRead(pos_type pos, pos_type length) const override { |
| 85 |
if ((pos == streamLength) && (length == 0)) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
if (pos >= streamLength) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
return (length <= streamLength - pos); |
| 92 |
} |
| 93 |
|
| 94 |
pos_type GetReadableLength(pos_type pos, pos_type length) const override { |
| 95 |
if (pos >= streamLength) { |
| 96 |
return 0; |
| 97 |
} |
| 98 |
return std::min(length, streamLength - pos); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
} // namespace IO |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
} // namespace MPT_INLINE_NS |
| 109 |
} // namespace mpt |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#endif // MPT_IO_READ_FILEDATA_MEMORY_HPP |