9 #if ((XTD_OS_LINUX | XTD_OS_CYGWIN | XTD_OS_MSYS) & XTD_OS)
24 template <
size_t _PageSize>
27 static size_t page_size(){
return _PageSize; }
34 static size_t page_size(){
35 static size_t iRet = xtd::memory::page_size();
41 #if ((XTD_OS_CYGWIN|XTD_OS_MSYS|XTD_OS_LINUX) & XTD_OS)
44 template <
typename _Ty>
class mapped_page :
public std::shared_ptr <_Ty>{
46 using _super_t = std::shared_ptr<_Ty>;
48 explicit mapped_page(
void * addr) : _super_t(reinterpret_cast<_Ty*>(addr), [](
void*addr) { munmap(addr, 1);}) {}
50 template <
typename ... _ArgTs> mapped_page(_ArgTs&&...oArgs) : _super_t(std::forward<_ArgTs>(oArgs)...){}
52 mapped_page& operator=(
const mapped_page& src){
53 _super_t::operator =(src);
56 mapped_page& operator=(mapped_page&& rhs){
57 _super_t::operator =(std::move(rhs));
67 template <
size_t _page_size>
83 auto iPageSize = _super_t::page_size();
86 off_t iLastByte = (pageNum * iPageSize) + iPageSize;
87 if (oStat.st_size < iLastByte){
93 mmap(
nullptr, iPageSize, PROT_READ|PROT_WRITE, MAP_SHARED, _FileNum, (pageNum * iPageSize)),
94 [](
void*addr){
return nullptr==addr || MAP_FAILED==addr; }));
98 auto iPageSize = _super_t::page_size();
101 newpage = oStat.st_size / iPageSize;
107 mmap(
nullptr, iPageSize, PROT_READ|PROT_WRITE, MAP_SHARED, _FileNum, oStat.st_size),
108 [](
void*addr){
return nullptr==addr || MAP_FAILED==addr; }),
112 #elif ((XTD_OS_MINGW|XTD_OS_WINDOWS) & XTD_OS)
116 template <
typename _Ty>
class mapped_page :
public std::shared_ptr <_Ty>{
117 using _super_t = std::shared_ptr<_Ty>;
118 template <
size_t>
friend class mapped_file;
119 explicit mapped_page(
void * addr) : _super_t(reinterpret_cast<_Ty*>(addr), &UnmapViewOfFile){}
122 template <
typename ... _ArgTs> mapped_page(_ArgTs&&...oArgs) : _super_t(std::forward<_ArgTs>(oArgs)...){}
124 mapped_page& operator=(
const mapped_page& src){
125 _super_t::operator =(src);
128 mapped_page& operator=(mapped_page&& rhs){
129 _super_t::operator =(std::move(rhs));
139 template <
size_t _PageSize>
140 class mapped_file : _::mapped_file_base<_PageSize>{
141 using _super_t = _::mapped_file_base<_PageSize>;
146 if (_hMap && INVALID_HANDLE_VALUE != _hMap) CloseHandle(_hMap);
147 if (_hFile && INVALID_HANDLE_VALUE != _hFile) CloseHandle(_hFile);
149 explicit mapped_file(
const filesystem::path& Path)
150 : _hFile(xtd::windows::exception::
throw_if(CreateFileA(Path.string().c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr), [](HANDLE h){
return nullptr==h || INVALID_HANDLE_VALUE==h; }))
151 , _hMap(
xtd::windows::exception::throw_if(CreateFileMapping(_hFile,
nullptr, PAGE_READWRITE, 0, static_cast<DWORD>(_super_t::page_size()),
nullptr), [](HANDLE h){
return nullptr == h || INVALID_HANDLE_VALUE == h; }))
156 template <
typename _Ty> mapped_page<_Ty>
get(
size_t pageNum){
157 size_t newSize = (pageNum * _super_t::page_size()) + _super_t::page_size();
160 if (static_cast<size_t>(iSize.QuadPart) < newSize){
162 iSize.QuadPart = newSize;
163 _hMap =
xtd::windows::exception::throw_if(CreateFileMapping(_hFile,
nullptr, PAGE_READWRITE, iSize.HighPart, iSize.LowPart,
nullptr), [](HANDLE h){
return nullptr == h || INVALID_HANDLE_VALUE == h; });
165 iSize.QuadPart = newSize - _super_t::page_size();
166 return mapped_page<_Ty>(
168 MapViewOfFile(_hMap, FILE_MAP_READ|FILE_MAP_WRITE , iSize.HighPart, iSize.LowPart, _super_t::page_size()),
169 [](
void*addr){
return nullptr == addr; }));
172 template <
typename _Ty> mapped_page<_Ty> append(
size_t& newpage){
175 newpage =
static_cast<size_t>((iSize.QuadPart + _super_t::page_size()) / _super_t::page_size());
176 return get<_Ty>(newpage);
182 template<
typename _OtherT,
typename _ThisT>
183 mapped_page<_OtherT> static_page_cast(_ThisT ptr){
184 return (mapped_page<_OtherT>(ptr, static_cast<_OtherT*>(ptr.get())));
host, target and build configurations and settings Various components are purpose built for specific ...
#define throw_if(_test, _expression)
Simplifies use of exception::_throw_if.
generic and special purpose exceptions
handle necessary filesystem and path functionality until C++17 is finalized