casacore
Loading...
Searching...
No Matches
MMapfdIO.h
Go to the documentation of this file.
1// # MMapfdIO.h: Memory-mapped IO on a file descriptor
2// #
3// # Copyright (C) 2009
4// # Associated Universities, Inc. Washington DC, USA.
5// #
6// # This library is free software; you can redistribute it and/or modify it
7// # under the terms of the GNU Library General Public License as published by
8// # the Free Software Foundation; either version 2 of the License, or (at your
9// # option) any later version.
10// #
11// # This library is distributed in the hope that it will be useful, but WITHOUT
12// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14// # License for more details.
15// #
16// # You should have received a copy of the GNU Library General Public License
17// # along with this library; if not, write to the Free Software Foundation,
18// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19// #
20// # Correspondence concerning AIPS++ should be addressed as follows:
21// # Internet email: casa-feedback@nrao.edu.
22// # Postal address: AIPS++ Project Office
23// # National Radio Astronomy Observatory
24// # 520 Edgemont Road
25// # Charlottesville, VA 22903-2475 USA
26
27#ifndef CASA_MMAPFDIO_H
28#define CASA_MMAPFDIO_H
29
30// # Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/IO/FiledesIO.h>
33#include <casacore/casa/OS/RegularFile.h>
34
35namespace casacore {
36
37// <summary>
38// Memory-mapped IO on a file.
39// </summary>
40
41// <synopsis>
42// Memory-mapped IO lets the OS take care of caching file segments.
43// This is particularly useful for the Tiled Storage Manager which keeps a
44// cache of tiles. When using memory-mapped IO it does not need to do that
45// anymore.
46//
47// On 32-bit systems its use is limited because for large files the 4 GB
48// memory space is insufficient. However, for 64-bit systems the memory
49// space is large enough to make use of it.
50//
51// In the general case there is direct access to the mapped file space.
52// The read and write methods copies the data into/from a buffer.
53// However, to avoid the copying it is possible to get a direct pointer
54// to the mapped data. This should be used with care, because writing to
55// it will cause a segmentation if the file is readonly. If the file is
56// writable, writing into the mapped data segment means changing the file
57// contents.
58// </synopsis>
59
60class MMapfdIO : public FiledesIO {
61 public:
62 // Default constructor.
63 // A file can be memory-mapped using the map function.
65
66 // Map the given file descriptor entirely into memory with read access.
67 // The map has also write access if the file is opened for write.
68 // The file name is only used in possible error messages.
69 MMapfdIO(int fd, const String& fileName);
70
71 // Destructor.
72 // If needed, it will flush and unmap the file, but not close it.
74
75 // Forbid copy constructor and assignment
76 // <group>
77 MMapfdIO(const MMapfdIO&) = delete;
78 MMapfdIO& operator=(const MMapfdIO&) = delete;
79 // </group>
80
81 // Map the given file descriptor entirely into memory with read access.
82 // The map has also write access if the file is opened for write.
83 // An exception is thrown if a file descriptor was already attached.
84 // The file name is only used in possible error messages.
85 void map(int fd, const String& fileName);
86
87 // Map or remap the entire file.
88 // Remapping is needed if the file has grown elsewhere.
89 void mapFile();
90
91 // Flush changed mapped data to the file.
92 // Nothing is done if the file is readonly.
93 void flush();
94
95 // Write the number of bytes from the seek position on.
96 // The file will be extended and remapped if writing beyond end-of-file.
97 // In that case possible pointers obtained using <src>getXXPointer</src>
98 // are not valid anymore.
99 virtual void write(Int64 size, const void* buf);
100
101 // Read <src>size</src> bytes from the File. Returns the number of bytes
102 // actually read. Will throw an exception (AipsError) if the requested
103 // number of bytes could not be read unless throwException is set to
104 // False. Will always throw an exception if the file is not readable or
105 // the system call returns an undocumented value.
106 virtual Int64 read(Int64 size, void* buf, Bool throwException = True);
107
108 // Get a read or write pointer to the given position in the mapped file.
109 // An exception is thrown if beyond end-of-file or it not writable.
110 // These functions should be used with care. If the pointer is used to
111 // access data beyond the file size, a segmentation fault will occur.
112 // So it means that the write pointer can only be used to update the file,
113 // not to extend it. The <src>seek</src> and <src>write</src> functions
114 // should be used to extend a file.
115 // <group>
116 const void* getReadPointer(Int64 offset) const;
118 // </group>
119
120 // Get the file size.
121 Int64 getFileSize() const { return itsFileSize; }
122
123 protected:
124 // Reset the position pointer to the given value. It returns the
125 // new position.
127
128 // Unmap the file.
129 void unmapFile();
130
131 private:
132 Int64 itsFileSize; // # File size
133 Int64 itsPosition; // # Current seek position
134 char* itsPtr; // # Pointer to memory map
136};
137
138} // namespace casacore
139
140#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:77
int fd() const
Get the file descriptor.
Definition FiledesIO.h:159
FiledesIO()
Default constructor.
virtual String fileName() const
Get the file name of the file attached.
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)
Read size bytes from the File.
Int64 getFileSize() const
Get the file size.
Definition MMapfdIO.h:121
MMapfdIO(int fd, const String &fileName)
Map the given file descriptor entirely into memory with read access.
virtual void write(Int64 size, const void *buf)
Write the number of bytes from the seek position on.
const void * getReadPointer(Int64 offset) const
Get a read or write pointer to the given position in the mapped file.
void map(int fd, const String &fileName)
Map the given file descriptor entirely into memory with read access.
MMapfdIO()
Default constructor.
MMapfdIO & operator=(const MMapfdIO &)=delete
void unmapFile()
Unmap the file.
~MMapfdIO()
Destructor.
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)
Reset the position pointer to the given value.
void flush()
Flush changed mapped data to the file.
MMapfdIO(const MMapfdIO &)=delete
Forbid copy constructor and assignment.
void mapFile()
Map or remap the entire file.
void * getWritePointer(Int64 offset)
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
int offset(int, int) const
compute a linear offset from array indicies
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
size_t size() const
Definition Block.h:566
const Bool True
Definition aipstype.h:41