24 #if !defined(BARE_METAL) 35 #include <sys/types.h> 52 std::pair<size_t, bool> get_file_size(
const std::string &filename)
55 memset(&st, 0,
sizeof(
struct stat));
56 if(stat(filename.c_str(), &st) == 0)
58 return std::make_pair(st.st_size,
true);
62 return std::make_pair(0,
false);
70 size_t get_page_size()
72 return sysconf(_SC_PAGESIZE);
77 : _filename(), _file_size(0), _map_size(0), _map_offset(0), _fp(nullptr), _data(nullptr)
82 : _filename(std::move(filename)), _file_size(0), _map_size(size), _map_offset(
offset), _fp(nullptr), _data(nullptr)
84 map(_filename, _map_size, _map_offset);
101 _fp = fopen(filename.c_str(),
"a+be");
108 int fd = fileno(_fp);
109 bool status = fd >= 0;
113 std::tie(_file_size, status) = get_file_size(_filename);
118 _map_size = (size == 0) ? _file_size : size;
122 if((_map_offset > _file_size) || (_map_offset % get_page_size() != 0))
129 if(_map_offset + _map_size > _file_size)
131 _map_size = _file_size - _map_offset;
135 _data = ::mmap(
nullptr, _map_size, PROT_WRITE, MAP_SHARED, fd, _map_offset);
153 ::munmap(_data, _file_size);
172 return static_cast<unsigned char *>(_data);
187 return _data !=
nullptr;
192 #endif // !defined(BARE_METAL) __global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
size_t map_size() const
Map size accessor.
bool map(const std::string &filename, size_t size, size_t offset)
Opens and maps a file.
void release()
Unmaps and closes file.
Copyright (c) 2017-2021 Arm Limited.
~MMappedFile()
Destructor.
bool is_mapped() const
Checks if file mapped.
unsigned char * data()
Mapped data accessor.
MMappedFile()
Constructor.
size_t file_size() const
File size accessor.