casacore
|
Memory-mapped IO on a file. More...
#include <MMapfdIO.h>
Public Member Functions | |
MMapfdIO () | |
Default constructor. More... | |
MMapfdIO (int fd, const String &fileName) | |
Map the given file descriptor entirely into memory with read access. More... | |
~MMapfdIO () | |
Destructor. More... | |
void | map (int fd, const String &fileName) |
Map the given file descriptor entirely into memory with read access. More... | |
void | mapFile () |
Map or remap the entire file. More... | |
void | flush () |
Flush changed mapped data to the file. More... | |
virtual void | write (Int64 size, const void *buf) |
Write the number of bytes from the seek position on. More... | |
virtual Int64 | read (Int64 size, void *buf, Bool throwException=True) |
Read size bytes from the File. More... | |
const void * | getReadPointer (Int64 offset) const |
Get a read or write pointer to the given position in the mapped file. More... | |
void * | getWritePointer (Int64 offset) |
Int64 | getFileSize () const |
Get the file size. More... | |
Public Member Functions inherited from casacore::FiledesIO | |
FiledesIO () | |
Default constructor. More... | |
FiledesIO (int fd, const String &fileName=String()) | |
Construct from the given file descriptor. More... | |
void | attach (int fd, const String &fileName) |
Attach to the given file descriptor. More... | |
void | detach () |
Detach from the file descriptor. More... | |
virtual | ~FiledesIO () |
The destructor detaches, but does not close the file. More... | |
virtual void | pwrite (Int64 size, Int64 offset, const void *buf) |
Write the number of bytes at offset from start of the file. More... | |
virtual Int64 | pread (Int64 size, Int64 offset, void *buf, Bool throwException=True) |
Like read except reads from offset of the start of the file. More... | |
virtual Int64 | length () |
Get the length of the byte stream. More... | |
virtual Bool | isReadable () const |
Is the IO stream readable? More... | |
virtual Bool | isWritable () const |
Is the IO stream writable? More... | |
virtual Bool | isSeekable () const |
Is the IO stream seekable? More... | |
void | setWritable () |
Set that the IO stream is writable. More... | |
virtual String | fileName () const |
Get the file name of the file attached. More... | |
virtual void | fsync () |
Fsync the file (i.e. More... | |
Public Member Functions inherited from casacore::ByteIO | |
ByteIO () | |
The constructor does nothing. More... | |
virtual | ~ByteIO () |
virtual void | reopenRW () |
Reopen the underlying IO stream for read/write access. More... | |
Int64 | seek (Int offset, ByteIO::SeekOption=ByteIO::Begin) |
This function sets the position on the given offset. More... | |
Int64 | seek (Int64 offset, ByteIO::SeekOption=ByteIO::Begin) |
virtual void | resync () |
Resync the file (i.e. More... | |
Protected Member Functions | |
virtual Int64 | doSeek (Int64 offset, ByteIO::SeekOption) |
Reset the position pointer to the given value. More... | |
void | unmapFile () |
Unmap the file. More... | |
Protected Member Functions inherited from casacore::FiledesIO | |
int | fd () const |
Get the file descriptor. More... | |
void | fillRWFlags (int fd) |
Determine if the file descriptor is readable and/or writable. More... | |
void | fillSeekable () |
Determine if the file is seekable. More... | |
Protected Member Functions inherited from casacore::ByteIO | |
ByteIO (const ByteIO &byteIO) | |
Make copy constructor and assignment protected, so a user cannot use them (but a derived class can). More... | |
ByteIO & | operator= (const ByteIO &byteIO) |
Private Member Functions | |
MMapfdIO (const MMapfdIO &) | |
Forbid copy constructor and assignment. More... | |
MMapfdIO & | operator= (const MMapfdIO &) |
Private Attributes | |
Int64 | itsFileSize |
Int64 | itsPosition |
char * | itsPtr |
Bool | itsIsWritable |
Additional Inherited Members | |
Public Types inherited from casacore::ByteIO | |
enum | OpenOption { Old, Update, Append, New, NewNoReplace, Scratch, Delete } |
Define the possible ByteIO open options. More... | |
enum | SeekOption { Begin, Current, End } |
Define the possible seek options. More... | |
Static Public Member Functions inherited from casacore::FiledesIO | |
static int | create (const Char *name, int mode=0666) |
Some static convenience functions for file create/open/close. More... | |
static int | open (const Char *name, Bool writable=False, Bool throwExcp=True) |
static void | close (int fd) |
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 | ||
) |
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.
casacore::MMapfdIO::~MMapfdIO | ( | ) |
Destructor.
If needed, it will flush and unmap the file, but not close it.
|
private |
Forbid copy constructor and assignment.
|
protectedvirtual |
Reset the position pointer to the given value.
It returns the new position.
Reimplemented from casacore::FiledesIO.
|
virtual |
Flush changed mapped data to the file.
Nothing is done if the file is readonly.
Reimplemented from casacore::ByteIO.
|
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 | ||
) |
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 casacore::MMapfdIO::mapFile | ( | ) |
Map or remap the entire file.
Remapping is needed if the file has grown elsewhere.
|
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.
|
protected |
Unmap the file.
|
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.
|
private |
Definition at line 137 of file MMapfdIO.h.
Referenced by getFileSize().
|
private |
Definition at line 140 of file MMapfdIO.h.
|
private |
Definition at line 138 of file MMapfdIO.h.
|
private |
Definition at line 139 of file MMapfdIO.h.