Memory-mapped IO on a file. More...
#include <MMapfdIO.h>
Public Member Functions | |
MMapfdIO () | |
Default constructor. | |
MMapfdIO (int fd, const String &fileName) | |
Map the given file descriptor entirely into memory with read access. | |
~MMapfdIO () | |
Destructor. | |
void | map (int fd, const String &fileName) |
Map the given file descriptor entirely into memory with read access. | |
void | mapFile () |
Map or remap the entire file. | |
void | flush () |
Flush changed mapped data to the file. | |
virtual void | write (Int64 size, const void *buf) |
Write the number of bytes from the seek position on. | |
virtual Int64 | read (Int64 size, void *buf, Bool throwException=True) |
Read size bytes from the File. | |
const void * | getReadPointer (Int64 offset) const |
Get a read or write pointer to the given position in the mapped file. | |
void * | getWritePointer (Int64 offset) |
Int64 | getFileSize () const |
Get the file size. | |
Protected Member Functions | |
virtual Int64 | doSeek (Int64 offset, ByteIO::SeekOption) |
Reset the position pointer to the given value. | |
void | unmapFile () |
Unmap the file. | |
Private Member Functions | |
MMapfdIO (const MMapfdIO &) | |
Forbid copy constructor and assignment. | |
MMapfdIO & | operator= (const MMapfdIO &) |
Assignment, should not be used. | |
Private Attributes | |
Int64 | itsFileSize |
Int64 | itsPosition |
char * | itsPtr |
Bool | itsIsWritable |
Memory-mapped IO on a file.
Memory-mapped IO lets the OS take care of caching file segments. This is particularly useful for the Tiled Storage Manager which keeps a cache of tiles. When using memory-mapped IO it does not need to do that anymore.
On 32-bit systems its use is limited because for large files the 4 GB memory space is insufficient. However, for 64-bit systems the memory space is large enough to make use of it.
In the general case there is direct access to the mapped file space. The read and write methods copies the data into/from a buffer. However, to avoid the copying it is possible to get a direct pointer to the mapped data. This should be used with care, because writing to it will cause a segmentation if the file is readonly. If the file is writable, writing into the mapped data segment means changing the file contents.
Definition at line 63 of file MMapfdIO.h.
casacore::MMapfdIO::MMapfdIO | ( | ) |
Default constructor.
A file can be memory-mapped using the map function.
casacore::MMapfdIO::MMapfdIO | ( | int | fd, | |
const String & | fileName | |||
) |
casacore::MMapfdIO::~MMapfdIO | ( | ) |
Destructor.
If needed, it will flush and unmap the file, but not close it.
casacore::MMapfdIO::MMapfdIO | ( | const MMapfdIO & | ) | [private] |
Forbid copy constructor and assignment.
virtual Int64 casacore::MMapfdIO::doSeek | ( | Int64 | offset, | |
ByteIO::SeekOption | ||||
) | [protected, virtual] |
Reset the position pointer to the given value.
It returns the new position.
Reimplemented from casacore::FiledesIO.
void casacore::MMapfdIO::flush | ( | ) | [virtual] |
Flush changed mapped data to the file.
Nothing is done if the file is readonly.
Reimplemented from casacore::ByteIO.
Int64 casacore::MMapfdIO::getFileSize | ( | ) | const [inline] |
const void* casacore::MMapfdIO::getReadPointer | ( | Int64 | offset | ) | const |
Get a read or write pointer to the given position in the mapped file.
An exception is thrown if beyond end-of-file or it not writable. These functions should be used with care. If the pointer is used to access data beyond the file size, a segmentation fault will occur. So it means that the write pointer can only be used to update the file, not to extend it. The seek
and write
functions should be used to extend a file.
void* casacore::MMapfdIO::getWritePointer | ( | Int64 | offset | ) |
void casacore::MMapfdIO::map | ( | int | fd, | |
const String & | fileName | |||
) |
void casacore::MMapfdIO::mapFile | ( | ) |
Assignment, should not be used.
Reimplemented from casacore::FiledesIO.
Reimplemented in casacore::MMapIO.
virtual Int64 casacore::MMapfdIO::read | ( | Int64 | size, | |
void * | buf, | |||
Bool | throwException = True | |||
) | [virtual] |
Read size
bytes from the File.
Returns the number of bytes actually read. Will throw an exception (AipsError) if the requested number of bytes could not be read unless throwException is set to False. Will always throw an exception if the file is not readable or the system call returns an undocumented value.
Reimplemented from casacore::FiledesIO.
void casacore::MMapfdIO::unmapFile | ( | ) | [protected] |
Unmap the file.
virtual void casacore::MMapfdIO::write | ( | Int64 | size, | |
const void * | buf | |||
) | [virtual] |
Write the number of bytes from the seek position on.
The file will be extended and remapped if writing beyond end-of-file. In that case possible pointers obtained using getXXPointer
are not valid anymore.
Reimplemented from casacore::FiledesIO.
Int64 casacore::MMapfdIO::itsFileSize [private] |
Definition at line 137 of file MMapfdIO.h.
Referenced by getFileSize().
Bool casacore::MMapfdIO::itsIsWritable [private] |
Definition at line 140 of file MMapfdIO.h.
Int64 casacore::MMapfdIO::itsPosition [private] |
Definition at line 138 of file MMapfdIO.h.
char* casacore::MMapfdIO::itsPtr [private] |
Definition at line 139 of file MMapfdIO.h.