Core/Stream: Add MemoryMapped stream options (allowing for direct memory access)

This commit is contained in:
SirLynix
2023-03-03 13:18:51 +01:00
parent 3000345eab
commit 34abeeb7bd
9 changed files with 103 additions and 72 deletions

View File

@@ -62,6 +62,11 @@ namespace Nz
// Nothing to flush
}
void* MemoryStream::GetMemoryMappedPointer() const
{
return m_buffer->GetBuffer();
}
/*!
* \brief Reads blocks
* \return Number of blocks read

View File

@@ -42,13 +42,26 @@ namespace Nz
*/
MemoryView::MemoryView(const void* ptr, UInt64 size) :
Stream(StreamOption::None, OpenMode::ReadOnly),
Stream(StreamOption::MemoryMapped, OpenMode::ReadOnly),
m_ptr(static_cast<UInt8*>(const_cast<void*>(ptr))), //< Okay, right, const_cast is bad, but this pointer is still read-only
m_pos(0),
m_size(size)
{
}
/*!
* \brief Flushes the stream
*/
void MemoryView::FlushStream()
{
// Nothing to do
}
void* MemoryView::GetMemoryMappedPointer() const
{
return m_ptr;
}
/*!
* \brief Gets the size of the raw memory
* \return Size of the memory
@@ -59,15 +72,6 @@ namespace Nz
return m_size;
}
/*!
* \brief Flushes the stream
*/
void MemoryView::FlushStream()
{
// Nothing to do
}
/*!
* \brief Reads blocks
* \return Number of blocks read
@@ -100,15 +104,6 @@ namespace Nz
return true;
}
/*!
* \brief Gets the position of the cursor
* \return Position of the cursor
*/
UInt64 MemoryView::TellStreamCursor() const
{
return m_pos;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if cursor is at the end of the stream
@@ -118,6 +113,15 @@ namespace Nz
return m_pos >= m_size;
}
/*!
* \brief Gets the position of the cursor
* \return Position of the cursor
*/
UInt64 MemoryView::TellStreamCursor() const
{
return m_pos;
}
/*!
* \brief Writes blocks
* \return Number of blocks written

View File

@@ -6,6 +6,7 @@
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <cstring>
#include <Nazara/Core/Debug.hpp>
@@ -34,26 +35,6 @@ namespace Nz
return TestStreamEnd();
}
/*!
* \brief Gets the directory of the stream
* \return Empty string (meant to be virtual)
*/
std::filesystem::path Stream::GetDirectory() const
{
return {};
}
/*!
* \brief Gets the path of the stream
* \return Empty string (meant to be virtual)
*/
std::filesystem::path Stream::GetPath() const
{
return {};
}
UInt64 Stream::GetCursorPos() const
{
if (m_bufferCapacity == 0)
@@ -65,6 +46,24 @@ namespace Nz
}
}
/*!
* \brief Gets the directory of the stream
* \return Empty string (meant to be virtual)
*/
std::filesystem::path Stream::GetDirectory() const
{
return {};
}
/*!
* \brief Gets the path of the stream
* \return Empty string (meant to be virtual)
*/
std::filesystem::path Stream::GetPath() const
{
return {};
}
/*!
* \brief Reads the stream and puts the result in a buffer
* \return Size of the read
@@ -264,17 +263,22 @@ namespace Nz
#if defined(NAZARA_PLATFORM_WINDOWS)
std::string temp(string);
ReplaceStr(temp, "\n", "\r\n");
#elif defined(NAZARA_PLATFORM_LINUX) || defined(NAZARA_PLATFORM_WEB)
std::string_view temp(string);
// Nothing to do
string = temp;
#elif defined(NAZARA_PLATFORM_MACOS)
std::string temp(string);
ReplaceStr(temp, "\n", "\r");
#endif
return Write(temp.data(), temp.size()) == temp.size();
string = temp;
#endif
}
else
return Write(string.data(), string.size()) == string.size();
return Write(string.data(), string.size()) == string.size();
}
void* Stream::GetMemoryMappedPointer() const
{
NazaraError("Stream set the MemoryMapped option but did not implement GetMemoryMappedPointer");
return nullptr;
}
}