13 #include "oMemoryMapped.hh" 20 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 26 #ifndef _LARGEFILE64_SOURCE 27 #define _LARGEFILE64_SOURCE 29 #ifdef _FILE_OFFSET_BITS 30 #undef _FILE_OFFSET_BITS 32 #define _FILE_OFFSET_BITS 64 52 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
67 : _filename (filename),
70 _mappedBytes(mappedBytes),
71 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 77 Open(filename, mappedBytes, hint);
102 Cerr(
"***** oMemoryMapped::Open() -> File is already open !" << endl);
109 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 114 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 123 case Normal: winHint = FILE_ATTRIBUTE_NORMAL;
break;
125 case RandomAccess: winHint = FILE_FLAG_RANDOM_ACCESS;
break;
130 _file = ::CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, winHint, NULL);
133 Cerr(
"***** oMemoryMapped::Open() -> Failed to create windows file from function CreateFileA() !" << endl);
138 LARGE_INTEGER result;
139 if (!GetFileSizeEx(
_file, &result))
141 Cerr(
"***** oMemoryMapped::Open() -> Failed to get file size from windows function GetFileSizeEx() !" << endl);
144 _filesize =
static_cast<uint64_t
>(result.QuadPart);
147 _mappedFile = ::CreateFileMapping(
_file, NULL, PAGE_READONLY, 0, 0, NULL);
150 Cerr(
"***** oMemoryMapped::Open() -> Failed to convert file to mapped mode from windows function CreateFileMapping() !" << endl);
162 _file = ::open(filename.c_str(), O_RDONLY);
167 Cerr(
"***** oMemoryMapped::Open() -> Failed to open file from unix function open() !" << endl);
172 struct stat statInfo;
173 if (fstat(
_file, &statInfo) < 0)
175 Cerr(
"***** oMemoryMapped::Open() -> Failed to get correct file size from unix function fstat() !" << endl);
183 if (
Remap(0, mappedBytes))
185 Cerr(
"***** oMemoryMapped::Open() -> A problem occurred while calling the oMemoryMapped::Remap() function !" << endl);
192 Cerr(
"***** oMemoryMapped::Open() -> Failed to get a correct mapped view after calling the oMemoryMapped::Remap() function !" << endl);
211 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 219 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 222 ::CloseHandle(_mappedFile);
230 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 231 ::CloseHandle(
_file);
262 throw std::invalid_argument(
"No view mapped");
264 throw std::out_of_range(
"View is not large enough");
323 Cerr(
"***** oMemoryMapped::Remap() -> Cannot remap a file that has not been created !" << endl);
333 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 344 Cerr(
"***** oMemoryMapped::Remap() -> Provided offset is after the end of file !" << endl);
348 mappedBytes = size_t(
_filesize - offset);
350 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 356 DWORD offsetLow = DWORD(offset & 0xFFFFFFFF);
357 DWORD offsetHigh = DWORD(offset >> 32);
361 _mappedView = ::MapViewOfFile(_mappedFile, FILE_MAP_READ, offsetHigh, offsetLow, mappedBytes);
367 Cerr(
"***** oMemoryMapped::Remap() -> Mapped view is null after calling windows function MapViewOfFile() !" << endl);
379 _mappedView = ::mmap(NULL, mappedBytes, PROT_READ, MAP_SHARED,
_file, offset);
385 Cerr(
"***** oMemoryMapped::Remap() -> Mapping failed after calling the unix function mmap64() !" << endl);
395 case Normal: linuxHint = MADV_NORMAL;
break;
422 #if defined(_WIN32) || defined(CASTOR_USE_MINGW) 424 GetSystemInfo(&sysInfo);
425 return sysInfo.dwAllocationGranularity;
427 return sysconf(_SC_PAGESIZE);
read file only once with few seeks
everything ... be careful when file is larger than memory
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
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)
~oMemoryMapped()
close file (see close() )
size_t _mappedBytes
mapped size
const unsigned char * GetData() const
raw access
FileHandle _file
file handle
CacheHint _hint
caching strategy