📄 memory_mapped_file.hpp
字号:
maxSizeHi = static_cast<DWORD>(maxSize >> 32);
maxSizeLo = static_cast<DWORD>(maxSize);
}
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
scoped_handle<HANDLE> hmap( ::CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, maxSizeHi
, maxSizeLo
, NULL)
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200
, (void (STLSOFT_STDCALL *)(HANDLE))&::CloseHandle
#else /* ? compiler */
, ::CloseHandle
#endif /* compiler */
, NULL);
if(hmap.empty())
{
on_error_("Failed to open file mapping");
}
else
{
void *memory = ::MapViewOfFile( hmap.get()
, FILE_MAP_READ
, static_cast<ws_uint32_t>(offset >> 32)
, static_cast<ws_uint32_t>(offset)
, requestSize);
if(NULL == memory)
{
on_error_("Failed to map view of file");
}
else
{
m_memory = memory;
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
m_cb = (0 == requestSize) ? (size_type(fileSizeHigh) << 32) | fileSizeLow : requestSize;
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
m_cb = fileSizeLow;
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
}
}
}
}
}
/// @}
/// \name Construction
/// @{
public:
/// Maps an entire file into memory
ss_explicit_k memory_mapped_file(ws_char_a_t const* fileName)
: m_cb(0)
, m_memory(NULL)
{
open_(fileName, 0, 0);
}
/// Maps an entire file into memory
ss_explicit_k memory_mapped_file(ws_char_w_t const* fileName)
: m_cb(0)
, m_memory(NULL)
{
open_(fileName, 0, 0);
}
/// Maps an entire file into memory
template <ss_typename_param_k S>
ss_explicit_k memory_mapped_file(S const& fileName)
: m_cb(0)
, m_memory(NULL)
{
open_(stlsoft_ns_qual(c_str_ptr)(fileName), 0, 0);
}
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
/// Maps a portion of a file into memory
///
/// \param fileName The name of the file to map into memory
/// \param offset The offset into the file where the mapping
/// begins. Must be a multiple of the system allocation
/// granularity
/// \param requestSize The size of the portion of the file
/// to map into memory. If 0, all (of the remaining portion)
/// of the file is loaded
memory_mapped_file( ws_char_a_t const* fileName
, offset_type offset
, ws_uint32_t requestSize)
: m_cb(0)
, m_memory(NULL)
{
open_(fileName, offset, requestSize);
}
/// Maps a portion of a file into memory
///
/// \param fileName The name of the file to map into memory
/// \param offset The offset into the file where the mapping
/// begins. Must be a multiple of the system allocation
/// granularity
/// \param requestSize The size of the portion of the file
/// to map into memory. If 0, all (of the remaining portion)
/// of the file is loaded
memory_mapped_file( ws_char_w_t const* fileName
, offset_type offset
, ws_uint32_t requestSize)
: m_cb(0)
, m_memory(NULL)
{
open_(fileName, offset, requestSize);
}
/// Maps a portion of a file into memory
///
/// \param fileName The name of the file to map into memory
/// \param offset The offset into the file where the mapping
/// begins. Must be a multiple of the system allocation
/// granularity
/// \param requestSize The size of the portion of the file
/// to map into memory. If 0, all (of the remaining portion)
/// of the file is loaded
template <ss_typename_param_k S>
memory_mapped_file( S const& fileName
, offset_type offset
, ws_uint32_t requestSize)
: m_cb(0)
, m_memory(NULL)
{
open_(stlsoft_ns_qual(c_str_ptr)(fileName), offset, requestSize);
}
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
~memory_mapped_file() stlsoft_throw_0()
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
WINSTL_ASSERT(NULL != m_memory || 0 == m_cb);
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
if(NULL != m_memory)
{
::UnmapViewOfFile(m_memory);
}
}
/// @}
/// \name Accessors
/// @{
public:
/// \brief Non-mutating (const) pointer to the start of the mapped
/// region.
void const *memory() const
{
return m_memory;
}
/// \brief The number of bytes in the mapped region
size_type size() const
{
return m_cb;
}
#ifndef STLSOFT_CF_EXCEPTION_SUPPORT
error_type lastError() const
{
return m_lastError;
}
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
/// @}
/// \name Implementation
/// @{
private:
void on_error_(char const* message, error_type error = ::GetLastError())
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
// The exception policy is used because VC++ 5 has a cow when it is
// asked to throw a windows_exception, and this special case is
// handled by windows_exception_policy
windows_exception_policy xp;
xp(message, error);
#else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
m_lastError = error;
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
/// @}
/// \name Members
/// @{
private:
size_type m_cb;
void *m_memory;
#ifndef STLSOFT_CF_EXCEPTION_SUPPORT
error_type m_lastError;
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
/// @}
/// \name Not to be implemented
/// @{
private:
memory_mapped_file(class_type const&);
class_type& operator =(class_type const&);
/// @}
};
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/memory_mapped_file_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -