YSTest: MemoryMapping.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

MemoryMapping.cpp
浏览该文件的文档.
1 /*
2 © 2012-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YCLib/YModules.h"
29 #include YFM_YCLib_MemoryMapping
30 #include YFM_YCLib_FileIO // for uopen, ThrowFileOperationFailure;
31 #include YFM_YCLib_NativeAPI
32 #include <stdexcept> // for std::runtime_error;
33 #if YCL_Linux || YCL_OS_X
34 # include <sys/mman.h>
35 # include <sys/stat.h>
36 #endif
37
38 namespace platform
39 {
40
41 #if !YCL_DS
42 void
43 UnmapDelete::operator()(pointer p) const ynothrow
44 {
45 // XXX: Error ignored.
46 #if YCL_Win32
47 ::UnmapViewOfFile(p);
48 #else
49 ::munmap(p, size);
50 #endif
51 }
52 #endif
53
54
55 MappedFile::MappedFile(const char* path)
56 : file(uopen(path, O_RDONLY, mode_t(Mode::UserReadWrite))), size([this]{
57 if(!file)
58 ThrowFileOperationFailure("Failed mapping file.");
59 return file->GetSize();
60 }()), addr(
61 #if YCL_DS
62 new byte[size]
63 #else
64 [this]{
65 # if YCL_Win32
66 auto p(reinterpret_cast<void*>(-1));
67
68 if(size != 0)
69 {
70 const auto h(::HANDLE(::_get_osfhandle(*file.get())));
71
72 if(h != INVALID_HANDLE_VALUE)
73 if(::HANDLE fm = ::CreateFileMapping(h, {}, PAGE_READONLY, 0,
74 static_cast<unsigned long>(size), {}))
75 {
76 p = ::MapViewOfFile(fm, FILE_MAP_READ, 0, 0, size);
77 ::CloseHandle(fm);
78 }
79 }
80 # else
81 const auto p(::mmap({}, size, PROT_READ, MAP_PRIVATE, *file.get(), 0));
82 # endif
83
84 if(p == reinterpret_cast<void*>(-1))
85 throw std::runtime_error("Mapping failed.");
86 // TODO: Create specific exception type.
87 return static_cast< byte*>(p);
88 }()
89 #endif
90 #if !(YCL_DS || YCL_Win32)
91 , size
92 #endif
93 )
94 {
95 #if YCL_DS
96 if(YB_UNLIKELY(::read(*file.get(), GetPtr(), size) < 0))
97 throw std::runtime_error("Mapping failed.");
98 #endif
99 }
100
101 } // namespace platform;
102
platform::p
while p p
Definition: YCLib/FileSystem.h:225
platform::mode_t
::mode_t mode_t
文件模式类型。
Definition: FileIO.h:108
platform::MappedFile::MappedFile
MappedFile(const char *)
Definition: MemoryMapping.cpp:55
platform::UnmapDelete::operator()
void operator()(pointer) const ynothrow
Definition: MemoryMapping.cpp:43
platform::uopen
int uopen(const char *filename, int oflag, mode_t pmode) ynothrowv
Definition: FileIO.cpp:804
platform::MappedFile::file
UniqueFile file
Definition: MemoryMapping.h:79
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:438
platform
默认平台命名空间。
Definition: Container.h:47
platform::UnmapDelete::pointer
byte * pointer
Definition: MemoryMapping.h:48
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:303
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
YCL_DS
#define YCL_DS
Definition: Platform.h:325
YSLib::path
YF_API const char * path
Definition: Initialization.h:74
YModules.h
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808