casa
$Rev:20696$
|
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 (uInt size, const void *buf) |
Write the number of bytes from the seek position on. | |
virtual Int | read (uInt 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 &) |
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 62 of file MMapfdIO.h.
Default constructor.
A file can be memory-mapped using the map function.
casa::MMapfdIO::MMapfdIO | ( | int | fd, |
const String & | fileName | ||
) |
Map the given file descriptor entirely into memory with read access.
The map has also write access if the file is opened for write. The file name is only used in possible error messages.
Destructor.
If needed, it will flush and unmap the file, but not close it.
casa::MMapfdIO::MMapfdIO | ( | const MMapfdIO & | ) | [private] |
Forbid copy constructor and assignment.
virtual Int64 casa::MMapfdIO::doSeek | ( | Int64 | offset, |
ByteIO::SeekOption | |||
) | [protected, virtual] |
Reset the position pointer to the given value.
It returns the new position.
Reimplemented from casa::LargeFiledesIO.
void casa::MMapfdIO::flush | ( | ) |
Flush changed mapped data to the file.
Nothing is done if the file is readonly.
Int64 casa::MMapfdIO::getFileSize | ( | ) | const [inline] |
const void* casa::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* casa::MMapfdIO::getWritePointer | ( | Int64 | offset | ) |
void casa::MMapfdIO::map | ( | int | fd, |
const String & | fileName | ||
) |
Map the given file descriptor entirely into memory with read access.
The map has also write access if the file is opened for write. An exception is thrown if a file descriptor was already attached. The file name is only used in possible error messages.
void casa::MMapfdIO::mapFile | ( | ) |
Map or remap the entire file.
Remapping is needed if the file has grown elsewhere.
virtual Int casa::MMapfdIO::read | ( | uInt | 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 casa::LargeFiledesIO.
void casa::MMapfdIO::unmapFile | ( | ) | [protected] |
Unmap the file.
virtual void casa::MMapfdIO::write | ( | uInt | 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 casa::LargeFiledesIO.
Int64 casa::MMapfdIO::itsFileSize [private] |
Bool casa::MMapfdIO::itsIsWritable [private] |
Definition at line 139 of file MMapfdIO.h.
Int64 casa::MMapfdIO::itsPosition [private] |
Definition at line 137 of file MMapfdIO.h.
char* casa::MMapfdIO::itsPtr [private] |
Definition at line 138 of file MMapfdIO.h.