📄 memory_mapped_file.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/filesystem/memory_mapped_file.hpp (based on MMFile.h, ::SynesisWin)
*
* Purpose: Memory mapped file class.
*
* Created: 15th December 1996
* Updated: 17th August 2007
*
* Thanks to: Pablo Aguilar for requesting multibyte / Unicode ambivalence.
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1996-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/filesystem/memory_mapped_file.hpp
*
* \brief [C++ only] Definition of the winstl::memory_mapped_file class
* (\ref group__library__filesystem "File System" Library).
*/
#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE
#define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_MAJOR 4
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_MINOR 4
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_REVISION 1
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_EDIT 78
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# ifndef WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS
# include <winstl/error/exceptions.hpp>
# endif /* !WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS */
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
#ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE
# include <stlsoft/smartptr/scoped_handle.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
# include <stlsoft/shims/access/string/fwd.h>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
#ifdef STLSOFT_UNITTEST
# include <winstl/filesystem/file_path_buffer.hpp>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/** \brief Facade over the Win32 memory mapped file API.
*
* \ingroup group__library__filesystem
*/
class memory_mapped_file
{
/// \name Member Types
/// @{
public:
/// \brief This type
typedef memory_mapped_file class_type;
/// \brief The size type
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
typedef ws_uint64_t size_type;
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
typedef ws_uint32_t size_type;
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
/// \brief The error type
typedef ws_dword_t error_type;
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
/// \brief The offset type
typedef ws_uint64_t offset_type;
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
/// @}
/// \name Implementation
/// @{
private:
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
void open_(ws_char_a_t const* fileName, offset_type offset, ws_uint32_t requestSize)
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
void open_(ws_char_a_t const* fileName)
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
{
scoped_handle<HANDLE> hfile( ::CreateFileA( fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, NULL)
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200
, (void (STLSOFT_STDCALL *)(HANDLE))&::CloseHandle
#else /* ? compiler */
, ::CloseHandle
#endif /* compiler */
, INVALID_HANDLE_VALUE);
open_helper_(hfile.get(), offset, requestSize);
}
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
void open_(ws_char_w_t const* fileName, offset_type offset, ws_uint32_t requestSize)
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
void open_(ws_char_w_t const* fileName)
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
{
scoped_handle<HANDLE> hfile( ::CreateFileW( fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, NULL)
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200
, (void (STLSOFT_STDCALL *)(HANDLE))&::CloseHandle
#else /* ? compiler */
, ::CloseHandle
#endif /* compiler */
, INVALID_HANDLE_VALUE);
open_helper_(hfile.get(), offset, requestSize);
}
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
void open_helper_(HANDLE hFile, offset_type offset, ws_uint32_t requestSize)
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
void open_helper_(HANDLE hFile)
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
{
if(INVALID_HANDLE_VALUE == hFile)
{
on_error_("Failed to open file for mapping");
}
else
{
DWORD fileSizeHigh;
DWORD fileSizeLow = ::GetFileSize(hFile, &fileSizeHigh);
DWORD error = ::GetLastError();
if( INVALID_FILE_SIZE == fileSizeLow &&
ERROR_SUCCESS != error)
{
on_error_("Failed to determine mapped file size", error);
}
#ifndef STLSOFT_CF_64BIT_INT_SUPPORT
else if(0 != fileSizeHigh)
{
on_error_("Cannot map files with sizes larger than 4GB with compilers that do not support 64-bit integers", ERROR_SUCCESS);
}
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
else if(0 == fileSizeHigh &&
0 == fileSizeLow)
{
m_memory = NULL;
m_cb = 0;
}
else
{
DWORD maxSizeHi = 0;
DWORD maxSizeLo = 0;
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
if( /* 0 != offset && \
*/0 != requestSize)
{
offset_type maxSize = offset + requestSize;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -