42 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 48 #ifndef _LARGEFILE64_SOURCE 49 #define _LARGEFILE64_SOURCE 51 #ifdef _FILE_OFFSET_BITS 52 #undef _FILE_OFFSET_BITS 54 #define _FILE_OFFSET_BITS 64 74 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
93 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
99 Open(filename, mappedBytes, hint);
124 Cerr(
"***** oMemoryMapped::Open() -> File is already open !" << endl);
131 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 136 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 145 case Normal: winHint = FILE_ATTRIBUTE_NORMAL;
break;
147 case RandomAccess: winHint = FILE_FLAG_RANDOM_ACCESS;
break;
152 _file = ::CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, winHint, NULL);
155 Cerr(
"***** oMemoryMapped::Open() -> Failed to create windows file from function CreateFileA() !" << endl);
160 LARGE_INTEGER result;
161 if (!GetFileSizeEx(
_file, &result))
163 Cerr(
"***** oMemoryMapped::Open() -> Failed to get file size from windows function GetFileSizeEx() !" << endl);
166 _filesize =
static_cast<uint64_t
>(result.QuadPart);
169 _mappedFile = ::CreateFileMapping(
_file, NULL, PAGE_READONLY, 0, 0, NULL);
172 Cerr(
"***** oMemoryMapped::Open() -> Failed to convert file to mapped mode from windows function CreateFileMapping() !" << endl);
184 _file = ::open(filename.c_str(), O_RDONLY);
189 Cerr(
"***** oMemoryMapped::Open() -> Failed to open file from unix function open() !" << endl);
194 struct stat statInfo;
195 if (fstat(
_file, &statInfo) < 0)
197 Cerr(
"***** oMemoryMapped::Open() -> Failed to get correct file size from unix function fstat() !" << endl);
205 if (
Remap(0, mappedBytes))
207 Cerr(
"***** oMemoryMapped::Open() -> A problem occurred while calling the oMemoryMapped::Remap() function !" << endl);
214 Cerr(
"***** oMemoryMapped::Open() -> Failed to get a correct mapped view after calling the oMemoryMapped::Remap() function !" << endl);
233 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 241 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 244 ::CloseHandle(_mappedFile);
252 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 253 ::CloseHandle(
_file);
284 throw std::invalid_argument(
"No view mapped");
286 throw std::out_of_range(
"View is not large enough");
345 Cerr(
"***** oMemoryMapped::Remap() -> Cannot remap a file that has not been created !" << endl);
355 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 366 Cerr(
"***** oMemoryMapped::Remap() -> Provided offset is after the end of file !" << endl);
370 mappedBytes = size_t(
_filesize - offset);
372 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 378 DWORD offsetLow = DWORD(offset & 0xFFFFFFFF);
379 DWORD offsetHigh = DWORD(offset >> 32);
383 _mappedView = ::MapViewOfFile(_mappedFile, FILE_MAP_READ, offsetHigh, offsetLow, mappedBytes);
389 Cerr(
"***** oMemoryMapped::Remap() -> Mapped view is null after calling windows function MapViewOfFile() !" << endl);
401 _mappedView = ::mmap(NULL, mappedBytes, PROT_READ, MAP_SHARED,
_file, offset);
407 Cerr(
"***** oMemoryMapped::Remap() -> Mapping failed after calling the unix function mmap64() !" << endl);
417 case Normal: linuxHint = MADV_NORMAL;
break;
444 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 446 GetSystemInfo(&sysInfo);
447 return sysInfo.dwAllocationGranularity;
449 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 ...
void * _mappedView
pointer to the file contents mapped into memory
bool IsValid() const
true, if file successfully opened
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
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
uint64_t size() const
get file size
unsigned char at(size_t offset) const
access position, including range checking
static int GetPageSize()
get OS page size (for remap)
unsigned char operator[](size_t offset) const
access position, no range checking (faster)
Implementation of file to memory mapping.
~oMemoryMapped()
close file (see close() )
size_t _mappedBytes
mapped size
std::string _filename
file name
const unsigned char * GetData() const
raw access
FileHandle _file
file handle
CacheHint _hint
caching strategy