Compute Library
 21.11
MMappedFile Class Reference

Memory mapped file class. More...

#include <MMappedFile.h>

Public Member Functions

 MMappedFile ()
 Constructor. More...
 
 MMappedFile (std::string filename, size_t size, size_t offset)
 Constructor. More...
 
 MMappedFile (const MMappedFile &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 MMappedFile (MMappedFile &&)=default
 Default move constructor. More...
 
MMappedFileoperator= (const MMappedFile &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
MMappedFileoperator= (MMappedFile &&)=default
 Default move assignment operator. More...
 
 ~MMappedFile ()
 Destructor. More...
 
bool map (const std::string &filename, size_t size, size_t offset)
 Opens and maps a file. More...
 
void release ()
 Unmaps and closes file. More...
 
unsigned char * data ()
 Mapped data accessor. More...
 
size_t file_size () const
 File size accessor. More...
 
size_t map_size () const
 Map size accessor. More...
 
bool is_mapped () const
 Checks if file mapped. More...
 

Detailed Description

Memory mapped file class.

Definition at line 39 of file MMappedFile.h.

Constructor & Destructor Documentation

◆ MMappedFile() [1/4]

Constructor.

Definition at line 76 of file MMappedFile.cpp.

77  : _filename(), _file_size(0), _map_size(0), _map_offset(0), _fp(nullptr), _data(nullptr)
78 {
79 }

◆ MMappedFile() [2/4]

MMappedFile ( std::string  filename,
size_t  size,
size_t  offset 
)

Constructor.

Note
file will be created if it doesn't exist.
Parameters
[in]filenameFile to be mapped, if doesn't exist will be created.
[in]sizeSize of file to map
[in]offsetOffset to mapping point, should be multiple of page size

Definition at line 81 of file MMappedFile.cpp.

References MMappedFile::map().

82  : _filename(std::move(filename)), _file_size(0), _map_size(size), _map_offset(offset), _fp(nullptr), _data(nullptr)
83 {
84  map(_filename, _map_size, _map_offset);
85 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1069
bool map(const std::string &filename, size_t size, size_t offset)
Opens and maps a file.
Definition: MMappedFile.cpp:92

◆ MMappedFile() [3/4]

MMappedFile ( const MMappedFile )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ MMappedFile() [4/4]

MMappedFile ( MMappedFile &&  )
default

Default move constructor.

◆ ~MMappedFile()

Destructor.

Definition at line 87 of file MMappedFile.cpp.

References MMappedFile::release().

88 {
89  release();
90 }
void release()
Unmaps and closes file.

Member Function Documentation

◆ data()

unsigned char * data ( )

Mapped data accessor.

Returns
Pointer to the mapped data, nullptr if not mapped

Definition at line 170 of file MMappedFile.cpp.

Referenced by arm_compute::test::validation::TEST_CASE().

171 {
172  return static_cast<unsigned char *>(_data);
173 }

◆ file_size()

size_t file_size ( ) const

File size accessor.

Returns
Size of file

Definition at line 175 of file MMappedFile.cpp.

176 {
177  return _file_size;
178 }

◆ is_mapped()

bool is_mapped ( ) const

Checks if file mapped.

Returns
True if file is mapped else false

Definition at line 185 of file MMappedFile.cpp.

Referenced by MMappedFile::map(), and arm_compute::test::validation::TEST_CASE().

186 {
187  return _data != nullptr;
188 }

◆ map()

bool map ( const std::string &  filename,
size_t  size,
size_t  offset 
)

Opens and maps a file.

Note
file will be created if it doesn't exist.
Parameters
[in]filenameFile to be mapped, if doesn't exist will be created.
[in]sizeSize of file to map. If 0 all the file will be mapped.
[in]offsetOffset to mapping point, should be multiple of page size.
Returns
True if operation was successful else false

Definition at line 92 of file MMappedFile.cpp.

References MMappedFile::is_mapped(), and offset().

Referenced by MMappedFile::MMappedFile().

93 {
94  // Check if file is mapped
95  if(is_mapped())
96  {
97  return false;
98  }
99 
100  // Open file
101  _fp = fopen(filename.c_str(), "a+be");
102  if(_fp == nullptr)
103  {
104  return false;
105  }
106 
107  // Extract file descriptor
108  int fd = fileno(_fp);
109  bool status = fd >= 0;
110  if(status)
111  {
112  // Get file size
113  std::tie(_file_size, status) = get_file_size(_filename);
114 
115  if(status)
116  {
117  // Map all file from offset if map size is 0
118  _map_size = (size == 0) ? _file_size : size;
119  _map_offset = offset;
120 
121  // Check offset mapping
122  if((_map_offset > _file_size) || (_map_offset % get_page_size() != 0))
123  {
124  status = false;
125  }
126  else
127  {
128  // Truncate to file size
129  if(_map_offset + _map_size > _file_size)
130  {
131  _map_size = _file_size - _map_offset;
132  }
133 
134  // Perform mapping
135  _data = ::mmap(nullptr, _map_size, PROT_WRITE, MAP_SHARED, fd, _map_offset);
136  }
137  }
138  }
139 
140  if(!status)
141  {
142  fclose(_fp);
143  }
144 
145  return status;
146 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1069
bool is_mapped() const
Checks if file mapped.

◆ map_size()

size_t map_size ( ) const

Map size accessor.

Returns
Mapping size

Definition at line 180 of file MMappedFile.cpp.

181 {
182  return _map_size;
183 }

◆ operator=() [1/2]

MMappedFile& operator= ( const MMappedFile )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

MMappedFile& operator= ( MMappedFile &&  )
default

Default move assignment operator.

◆ release()

void release ( )

Unmaps and closes file.

Definition at line 148 of file MMappedFile.cpp.

Referenced by MMappedFile::~MMappedFile().

149 {
150  // Unmap file
151  if(_data != nullptr)
152  {
153  ::munmap(_data, _file_size);
154  _data = nullptr;
155  }
156 
157  // Close file
158  if(_fp != nullptr)
159  {
160  fclose(_fp);
161  _fp = nullptr;
162  }
163 
164  // Clear variables
165  _file_size = 0;
166  _map_size = 0;
167  _map_offset = 0;
168 }

The documentation for this class was generated from the following files: