4#include "Stroika/Foundation/StroikaPreComp.h"
9#if qStroika_Foundation_Common_Platform_Windows
11#elif qStroika_Foundation_Common_Platform_POSIX
16#include "Stroika/Foundation/Execution/Activity.h"
17#include "Stroika/Foundation/Execution/Exceptions.h"
18#include "Stroika/Foundation/Execution/Throw.h"
19#if qStroika_Foundation_Common_Platform_Windows
20#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
21#include "Stroika/Foundation/Execution/Platform/Windows/HRESULTErrorException.h"
23#include "FileSystem.h"
26#include "MemoryMappedFileReader.h"
32using namespace Stroika::Foundation::IO;
35using namespace Stroika::Foundation::Memory;
37#if qStroika_Foundation_Common_Platform_Windows
46MemoryMappedFileReader::MemoryMappedFileReader (
const filesystem::path& fileName)
48 auto activity =
LazyEvalActivity ([&] () ->
String {
return "memory mapping {} for read access"_f(fileName); });
50#if qStroika_Foundation_Common_Platform_POSIX
53 auto fileLength = filesystem::file_size (fileName);
56 if (fileLength >= numeric_limits<size_t>::max ()) {
59 fSpan_ = span{
reinterpret_cast<const byte*
> (::mmap (
nullptr, fileLength, PROT_READ, MAP_PRIVATE, fd, 0)),
static_cast<size_t> (fileLength)};
61#elif qStroika_Foundation_Common_Platform_Windows
64 fFileHandle_ = ::CreateFile (fileName.c_str (), GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
65 if (fFileHandle_ == INVALID_HANDLE_VALUE) {
66 Execution::ThrowSystemErrNo ();
68 DWORD fileSize = ::GetFileSize (fFileHandle_,
nullptr);
70 fFileMapping_ = ::CreateFileMapping (fFileHandle_,
nullptr, PAGE_READONLY, 0, fileSize, 0);
73 fSpan_ = span{
reinterpret_cast<const byte*
> (::MapViewOfFile (fFileMapping_, FILE_MAP_READ, 0, 0, 0)), fileSize};
78 if (fFileMapping_ != INVALID_HANDLE_VALUE) {
79 ::CloseHandle (fFileMapping_);
81 if (fFileHandle_ != INVALID_HANDLE_VALUE) {
82 ::CloseHandle (fFileHandle_);
91MemoryMappedFileReader::~MemoryMappedFileReader ()
93#if qStroika_Foundation_Common_Platform_POSIX
94 if (::munmap (
const_cast<byte*
> (fSpan_.data ()), fSpan_.size ())) {
95 DbgTrace (
"munmap failed: Cannot throw in DTOR, so just DbgTrace log: errno={}"_f, errno);
97#elif qStroika_Foundation_Common_Platform_Windows
98 if (fSpan_.data () !=
nullptr) {
99 (void)::UnmapViewOfFile (fSpan_.data ());
101 if (fFileMapping_ != INVALID_HANDLE_VALUE) {
102 ::CloseHandle (fFileMapping_);
104 if (fFileHandle_ != INVALID_HANDLE_VALUE) {
105 ::CloseHandle (fFileHandle_);
#define AssertNotImplemented()
String is like std::u32string, except it is much easier to use, often much more space efficient,...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
INT_TYPE ThrowPOSIXErrNoIfNegative(INT_TYPE returnCode)