CARMA C++
carma::ui::rtd::MemoryMappedFile Class Reference

Memory Mapped file is a class that provides a C++ interface to the mmap(2) system call. More...

#include <carma/ui/rtd/common/MemoryMappedFile.h>

Public Types

enum  Protection_t { NoAccess = PROT_NONE, Read = PROT_READ, Write = PROT_WRITE, Exec = PROT_EXEC }
 File protection flags. More...
 

Public Member Functions

void * getClientArea ()
 Get a pointer to the start of the data area. More...
 
int getFileDescriptor () const
 Get the file descriptor. More...
 
const char * getFileName ()
 Get the filename. More...
 
bool lockRead ()
 Lock the file for read access. More...
 
bool lockWrite ()
 Lock the file for write access. More...
 
 MemoryMappedFile (const char *map_file_name, size_t client_size, bool reuse, Protection_t p_mask=Protection_t(Read|Write), bool persistent=true)
 
 MemoryMappedFile (const std::string &map_file_name, size_t client_size, bool reuse, Protection_t p_mask=Protection_t(Read|Write), bool persistent=true)
 
bool unlock ()
 Unlock the file. More...
 
 ~MemoryMappedFile ()
 Destructor. More...
 

Detailed Description

Memory Mapped file is a class that provides a C++ interface to the mmap(2) system call.

Usefulness of the class is increased with the inclusion of interprocess synchronization primatives (locks) based on the descriptor of the mmaped file. The mmaped region and thus the mmaped file itself is divided into two parts called the header and client regions. The client region is intended to be read and written by users of the class and is of arbitrary length. The header portion consists of the first page of memory in the mmaped region, it is intended to provide a reserved space for private and protected object data.

The name of the file to be used as backing store is passed as the first argument to the constructor. If a file of that name cannot be opened then MemoryMappedFileException is thrown. A pointer to the start of the client region which extends for 'client_size' bytes should be retrieved using the 'getClientArea' method.

A value of B_TRUE for the 'reuse' argument in the constructor will allow a previously mapped file to be reread if a file of the name 'map_file_name' is found to exist. In this case the 'client_size' argument must correspond to the 'client_size' of the previous use, otherwise MemoryMappedFileException is thrown. If 'reuse' is B_FALSE the previous contents and size are discarded.

A MemoryMappedFile object provides for mutually exlusive access through the 'lockRead', 'lockWrite', and 'unlock' methods. All of these methods provide wrappers around the fcntl(2) file descriptor locking services F_SETLKW. The descriptor operated on in these locking calls is that for the backing store file specified in the constructor.


Definition at line 73 of file MemoryMappedFile.h.

Member Enumeration Documentation

File protection flags.

Definition at line 78 of file MemoryMappedFile.h.

Constructor & Destructor Documentation

carma::ui::rtd::MemoryMappedFile::MemoryMappedFile ( const char *  map_file_name,
size_t  client_size,
bool  reuse,
Protection_t  p_mask = Protection_t(Read|Write),
bool  persistent = true 
)
Deprecated:
Trying to eliminate constructor with char* type name
Parameters
map_file_nameFile name for the memory mapped file
client_sizeSize of the data area in bytes
reuseFlag for treating file as empty and overwriting the header
p_maskFile protection mask for reading and or writing
persistentFlag to use a persistent disk file as opposed to pure shared memory
carma::ui::rtd::MemoryMappedFile::MemoryMappedFile ( const std::string &  map_file_name,
size_t  client_size,
bool  reuse,
Protection_t  p_mask = Protection_t(Read|Write),
bool  persistent = true 
)
Parameters
map_file_nameFile name for the memory mapped file
client_sizeSize of the data area in bytes
reuseFlag for treating file as empty and overwriting the header
p_maskFile protection mask for reading and or writing
persistentFlag to use a persistent disk file as opposed to pure shared memory
carma::ui::rtd::MemoryMappedFile::~MemoryMappedFile ( )

Destructor.

Member Function Documentation

void* carma::ui::rtd::MemoryMappedFile::getClientArea ( )

Get a pointer to the start of the data area.

int carma::ui::rtd::MemoryMappedFile::getFileDescriptor ( ) const

Get the file descriptor.

const char* carma::ui::rtd::MemoryMappedFile::getFileName ( )

Get the filename.

bool carma::ui::rtd::MemoryMappedFile::lockRead ( )

Lock the file for read access.

bool carma::ui::rtd::MemoryMappedFile::lockWrite ( )

Lock the file for write access.

bool carma::ui::rtd::MemoryMappedFile::unlock ( )

Unlock the file.


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