-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,966 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include "memorymappedfilestream.h" | ||
#include "../log.h" | ||
|
||
#include <cstring> | ||
#include <algorithm> | ||
#include <system_error> | ||
|
||
namespace Impacto { | ||
namespace Io { | ||
template <AccessMode M> | ||
IoError MemoryMappedFileStream<M>::Create(std::string const& fileName, | ||
Stream** out) { | ||
MemoryMappedFileStream* result = | ||
new MemoryMappedFileStream(GetSystemDependentPath(fileName)); | ||
result->Meta.Size = GetFileSize(result->SourceFileName); | ||
if (result->Meta.Size == IoError_Fail) { | ||
delete result; | ||
return IoError_Fail; | ||
} | ||
|
||
std::error_code ec; | ||
result->mmapFile.map(result->SourceFileName, 0, result->Meta.Size, ec); | ||
if (ec) { | ||
ImpLog(LL_Error, LC_IO, "Failed to open file \"%s\", error:\"%s\"\n", | ||
result->SourceFileName.c_str(), ec.message().c_str()); | ||
delete result; | ||
return ec == std::errc::no_such_file_or_directory ? IoError_Eof | ||
: IoError_Fail; | ||
} | ||
*out = (Stream*)result; | ||
return IoError_OK; | ||
} | ||
|
||
template <AccessMode M> | ||
int64_t MemoryMappedFileStream<M>::Read(void* buffer, int64_t sz) { | ||
if (sz < 0) return IoError_Fail; | ||
if (Position >= Meta.Size) return IoError_Eof; | ||
|
||
int bytesToRead = std::min(sz, Meta.Size - Position); | ||
assert(mmapFile.data()); | ||
// Lock only if we are in read/write mode | ||
if constexpr (M == AccessMode::write) { | ||
std::shared_lock lock(*mutexLock); // Lock for shared read access | ||
memcpy(buffer, mmapFile.data() + Position, bytesToRead); | ||
} else { | ||
memcpy(buffer, mmapFile.data() + Position, bytesToRead); | ||
} | ||
|
||
Position += bytesToRead; | ||
return bytesToRead; | ||
} | ||
|
||
template <AccessMode M> | ||
int64_t MemoryMappedFileStream<M>::Seek(int64_t offset, int origin) { | ||
int64_t absPos; | ||
switch (origin) { | ||
case RW_SEEK_SET: | ||
absPos = offset; | ||
break; | ||
case RW_SEEK_CUR: | ||
absPos = Position + offset; | ||
break; | ||
case RW_SEEK_END: | ||
absPos = Meta.Size - offset; | ||
break; | ||
} | ||
|
||
if (absPos < 0 || absPos > Meta.Size) return IoError_Fail; | ||
Position = absPos; | ||
return Position; | ||
} | ||
|
||
template <AccessMode M> | ||
IoError MemoryMappedFileStream<M>::Duplicate(Stream** outStream) { | ||
MemoryMappedFileStream* result = new MemoryMappedFileStream(*this); | ||
if (result->Seek(Position, RW_SEEK_SET) < 0) { | ||
ImpLog(LL_Error, LC_IO, "Seek failed for file \"%s\"\n", | ||
SourceFileName.c_str()); | ||
delete result; | ||
return IoError_Fail; | ||
} | ||
*outStream = (Stream*)result; | ||
return IoError_OK; | ||
} | ||
|
||
template <AccessMode M> | ||
int64_t MemoryMappedFileStream<M>::Write(void* buffer, int64_t sz, int cnt) { | ||
if constexpr (M == AccessMode::read) { | ||
assert(false); | ||
return 0; | ||
} else { | ||
std::lock_guard<std::shared_mutex> lock(*mutexLock); | ||
sz = std::min(Meta.Size - Position, sz); | ||
memcpy(mmapFile.data() + Position, buffer, sz); | ||
Position += sz; | ||
return sz; | ||
} | ||
} | ||
|
||
template class MemoryMappedFileStream<AccessMode::write>; | ||
template class MemoryMappedFileStream<AccessMode::read>; | ||
|
||
} // namespace Io | ||
} // namespace Impacto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include "stream.h" | ||
#include <fstream> | ||
#include <cstddef> | ||
#include <shared_mutex> | ||
#include "../../vendor/mio/mio.hpp" | ||
|
||
namespace Impacto { | ||
namespace Io { | ||
|
||
using AccessMode = mio::access_mode; | ||
|
||
template <AccessMode Access> | ||
class MemoryMappedFileStream : public Stream { | ||
public: | ||
AccessMode Mode = Access; | ||
static IoError Create(std::string const& fileName, Stream** out); | ||
int64_t Read(void* buffer, int64_t sz) override; | ||
int64_t Seek(int64_t offset, int origin) override; | ||
IoError Duplicate(Stream** outStream) override; | ||
int64_t Write(void* buffer, int64_t sz, int cnt = 1) override; | ||
|
||
protected: | ||
MemoryMappedFileStream(std::string filePath) | ||
: SourceFileName(std::move(filePath)) { | ||
Meta.FileName = SourceFileName; | ||
if constexpr (Access == AccessMode::write) { | ||
mutexLock = std::make_shared<std::shared_mutex>(); | ||
} | ||
} | ||
|
||
MemoryMappedFileStream(MemoryMappedFileStream const& other) | ||
: SourceFileName(other.SourceFileName), mmapFile(other.mmapFile) { | ||
Meta.FileName = SourceFileName; | ||
Meta.Size = other.Meta.Size; | ||
if constexpr (Access == AccessMode::write) { | ||
mutexLock = other.mutexLock; | ||
} | ||
} | ||
std::string SourceFileName; | ||
mio::basic_shared_mmap<Access, std::byte> mmapFile; | ||
std::shared_ptr<std::shared_mutex> mutexLock; | ||
}; | ||
} // namespace Io | ||
} // namespace Impacto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.