Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MappedFile.cpp
Go to the documentation of this file.
2
3#include <fcntl.h>
4#include <sys/mman.h>
5#include <boost/integer/integer_mask.hpp>
6#include <cstring>
7#include <fstream>
8
10#include "storm/io/file.h"
12
13namespace storm {
14namespace parser {
15
16MappedFile::MappedFile(const char* filename) {
17 STORM_LOG_THROW(storm::io::fileExistsAndIsReadable(filename), storm::exceptions::FileIoException,
18 "Error while reading " << filename << ": The file does not exist or is not readable.");
19
20 // Do file mapping for reasonable systems.
21 // stat64(), open(), mmap()
22
23#if defined MACOS || !defined __GLIBC__
24 if (stat(filename, &(this->st)) != 0) {
25#else
26 if (stat64(filename, &(this->st)) != 0) {
27#endif
28 STORM_LOG_THROW(false, storm::exceptions::FileIoException, "Error in stat(" << filename << "): Probably, this file does not exist.");
29 }
30 this->file = open(filename, O_RDONLY);
31
32 STORM_LOG_THROW(this->file >= 0, storm::exceptions::FileIoException, "Error in open(" << filename << "): Probably, we may not read this file.");
33
34 this->data = static_cast<char*>(mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE, this->file, 0));
35 if (this->data == MAP_FAILED) {
36 close(this->file);
37 STORM_LOG_ERROR("Error in mmap(" << filename << "): " << std::strerror(errno));
38 throw exceptions::FileIoException() << "MappedFile Error in mmap(): " << std::strerror(errno);
39 }
40 this->dataEnd = this->data + this->st.st_size;
41}
42
44 munmap(this->data, this->st.st_size);
45 close(this->file);
46}
47
48char const* MappedFile::getData() const {
49 return data;
50}
51
52char const* MappedFile::getDataEnd() const {
53 return dataEnd;
54}
55
56std::size_t MappedFile::getDataSize() const {
57 return this->getDataEnd() - this->getData();
58}
59
60} // namespace parser
61} // namespace storm
char const * getData() const
Returns a pointer to the beginning of the mapped file data.
~MappedFile()
Destructs a MappedFile.
char const * getDataEnd() const
Returns a pointer to the end of the mapped file data.
std::size_t getDataSize() const
Returns the size of the mapped file data.
MappedFile(const char *filename)
Constructs a MappedFile.
#define STORM_LOG_ERROR(message)
Definition logging.h:26
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool fileExistsAndIsReadable(std::string const &filename)
Tests whether the given file exists and is readable.
Definition file.h:66