4#include "Stroika/Foundation/StroikaPreComp.h"
9#if qStroika_Platform_Windows
11#elif qStroika_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_Platform_Windows
20#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
21#include "Stroika/Foundation/Execution/Platform/Windows/HRESULTErrorException.h"
26#include "MemoryMappedFileReader.h"
32using namespace Stroika::Foundation::IO;
35using namespace Stroika::Foundation::Memory;
37#if qStroika_Platform_Windows
46MemoryMappedFileReader::MemoryMappedFileReader (
const filesystem::path& fileName)
48 auto activity =
LazyEvalActivity ([&] () ->
String {
return "memory mapping {} for read access"_f(fileName); });
50#if qStroika_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_Platform_Windows
63 fFileHandle_ = ::CreateFile (fileName.c_str (), GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
64 if (fFileHandle_ == INVALID_HANDLE_VALUE) {
65 Execution::ThrowSystemErrNo ();
67 DWORD fileSize = ::GetFileSize (fFileHandle_,
nullptr);
69 fFileMapping_ = ::CreateFileMapping (fFileHandle_,
nullptr, PAGE_READONLY, 0, fileSize, 0);
72 fSpan_ = span{
reinterpret_cast<const byte*
> (::MapViewOfFile (fFileMapping_, FILE_MAP_READ, 0, 0, 0)), fileSize};
77 if (fFileMapping_ != INVALID_HANDLE_VALUE) {
78 ::CloseHandle (fFileMapping_);
80 if (fFileHandle_ != INVALID_HANDLE_VALUE) {
81 ::CloseHandle (fFileHandle_);
90MemoryMappedFileReader::~MemoryMappedFileReader ()
92#if qStroika_Platform_POSIX
93 if (::munmap (
const_cast<byte*
> (fSpan_.data ()), fSpan_.size ())) {
94 DbgTrace (
"munmap failed: Cannot throw in DTOR, so just DbgTrace log: errno={}"_f, errno);
96#elif qStroika_Platform_Windows
97 if (fSpan_.data () !=
nullptr) {
98 (void)::UnmapViewOfFile (fSpan_.data ());
100 if (fFileMapping_ != INVALID_HANDLE_VALUE) {
101 ::CloseHandle (fFileMapping_);
103 if (fFileHandle_ != INVALID_HANDLE_VALUE) {
104 ::CloseHandle (fFileHandle_);
#define AssertNotImplemented()
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Exception<> is a replacement (subclass) for any std c++ exception class (e.g. the default 'std::excep...
void Throw(T &&e2Throw)
like the builtin C++ 'throw', but stamps in the current Activity context and does helpful,...
INT_TYPE ThrowPOSIXErrNoIfNegative(INT_TYPE returnCode)