43 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
49 #ifndef _LARGEFILE64_SOURCE
50 #define _LARGEFILE64_SOURCE
52 #ifdef _FILE_OFFSET_BITS
53 #undef _FILE_OFFSET_BITS
55 #define _FILE_OFFSET_BITS 64
75 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
90 : _filename (filename),
93 _mappedBytes(mappedBytes),
94 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
100 Open(filename, mappedBytes, hint);
125 Cerr(
"***** oMemoryMapped::Open() -> File is already open !" << endl);
132 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
137 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
146 case Normal: winHint = FILE_ATTRIBUTE_NORMAL;
break;
148 case RandomAccess: winHint = FILE_FLAG_RANDOM_ACCESS;
break;
153 _file = ::CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, winHint, NULL);
156 Cerr(
"***** oMemoryMapped::Open() -> Failed to create windows file from function CreateFileA() !" << endl);
161 LARGE_INTEGER result;
162 if (!GetFileSizeEx(
_file, &result))
164 Cerr(
"***** oMemoryMapped::Open() -> Failed to get file size from windows function GetFileSizeEx() !" << endl);
167 _filesize =
static_cast<uint64_t
>(result.QuadPart);
170 _mappedFile = ::CreateFileMapping(
_file, NULL, PAGE_READONLY, 0, 0, NULL);
173 Cerr(
"***** oMemoryMapped::Open() -> Failed to convert file to mapped mode from windows function CreateFileMapping() !" << endl);
184 _file = ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
188 Cerr(
"***** oMemoryMapped::Open() -> Failed to open file from unix function open() !" << endl);
193 struct stat64 statInfo;
194 if (fstat64(
_file, &statInfo) < 0)
196 Cerr(
"***** oMemoryMapped::Open() -> Failed to get correct file size from unix function fstat64() !" << endl);
204 if (
Remap(0, mappedBytes))
206 Cerr(
"***** oMemoryMapped::Open() -> A problem occured while calling the oMemoryMapped::Remap() function !" << endl);
213 Cerr(
"***** oMemoryMapped::Open() -> Failed to get a correct mapped view after calling the oMemoryMapped::Remap() function !" << endl);
232 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
240 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
243 ::CloseHandle(_mappedFile);
251 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
252 ::CloseHandle(
_file);
283 throw std::invalid_argument(
"No view mapped");
285 throw std::out_of_range(
"View is not large enough");
344 Cerr(
"***** oMemoryMapped::Remap() -> Cannot remap a file that has not been created !" << endl);
354 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
365 Cerr(
"***** oMemoryMapped::Remap() -> Provided offset is after the end of file !" << endl);
369 mappedBytes = size_t(
_filesize - offset);
371 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
377 DWORD offsetLow = DWORD(offset & 0xFFFFFFFF);
378 DWORD offsetHigh = DWORD(offset >> 32);
382 _mappedView = ::MapViewOfFile(_mappedFile, FILE_MAP_READ, offsetHigh, offsetLow, mappedBytes);
388 Cerr(
"***** oMemoryMapped::Remap() -> Mapped view is null after calling windows function MapViewOfFile() !" << endl);
399 _mappedView = ::mmap64(NULL, mappedBytes, PROT_READ, MAP_SHARED,
_file, offset);
404 Cerr(
"***** oMemoryMapped::Remap() -> Mapping failed after calling the unix function mmap64() !" << endl);
414 case Normal: linuxHint = MADV_NORMAL;
break;
441 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
443 GetSystemInfo(&sysInfo);
444 return sysInfo.dwAllocationGranularity;
446 return sysconf(_SC_PAGESIZE);
int Remap(uint64_t offset, size_t mappedBytes)
replace mapping by a new one of the same file, offset MUST be a multiple of the page size ...
bool IsValid() const
true, if file successfully opened
unsigned char at(size_t offset) const
access position, including range checking
void * _mappedView
pointer to the file contents mapped into memory
int Open(const std::string &filename, size_t mappedBytes=WholeFile, CacheHint hint=Normal)
open file, mappedBytes = 0 maps the whole file
oMemoryMapped()
do nothing, must use open()
CacheHint
tweak performance
unsigned char operator[](size_t offset) const
access position, no range checking (faster)
everything ... be careful when file is larger than memory
read file only once with few seeks
uint64_t _filesize
file size
size_t mappedSize() const
get number of actually mapped bytes
const unsigned char * GetData() const
raw access
static int GetPageSize()
get OS page size (for remap)
Implementation of file to memory mapping.
~oMemoryMapped()
close file (see close() )
size_t _mappedBytes
mapped size
uint64_t size() const
get file size
FileHandle _file
file handle
CacheHint _hint
caching strategy