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
38// <summary>
39// Memory-mapped IO on a file.
40// </summary>
41
42// <synopsis>
43// Memory-mapped IO lets the OS take care of caching file segments.
44// This is particularly useful for the Tiled Storage Manager which keeps a
45// cache of tiles. When using memory-mapped IO it does not need to do that
46// anymore.
47//
48// On 32-bit systems its use is limited because for large files the 4 GB
49// memory space is insufficient. However, for 64-bit systems the memory
50// space is large enough to make use of it.
51//
52// In the general case there is direct access to the mapped file space.
53// The read and write methods copies the data into/from a buffer.
54// However, to avoid the copying it is possible to get a direct pointer
55// to the mapped data. This should be used with care, because writing to
56// it will cause a segmentation if the file is readonly. If the file is
57// writable, writing into the mapped data segment means changing the file
58// contents.
59// </synopsis>
60
61class MMapfdIO: public FiledesIO
62{
63public:
64 // Default constructor.
65 // A file can be memory-mapped using the map function.
67
68 // Map the given file descriptor entirely into memory with read access.
69 // The map has also write access if the file is opened for write.
70 // The file name is only used in possible error messages.
71 MMapfdIO (int fd, const String& fileName);
72
73 // Destructor.
74 // If needed, it will flush and unmap the file, but not close it.
76
77 // Forbid copy constructor and assignment
78 // <group>
79 MMapfdIO (const MMapfdIO&) = delete;
80 MMapfdIO& operator= (const MMapfdIO&) = delete;
81 // </group>
82
83 // Map the given file descriptor entirely into memory with read access.
84 // The map has also write access if the file is opened for write.
85 // An exception is thrown if a file descriptor was already attached.
86 // The file name is only used in possible error messages.
87 void map (int fd, const String& fileName);
88
89 // Map or remap the entire file.
90 // Remapping is needed if the file has grown elsewhere.
91 void mapFile();
92
93 // Flush changed mapped data to the file.
94 // Nothing is done if the file is readonly.
95 void flush();
96
97 // Write the number of bytes from the seek position on.
98 // The file will be extended and remapped if writing beyond end-of-file.
99 // In that case possible pointers obtained using <src>getXXPointer</src>
100 // are not valid anymore.
101 virtual void write (Int64 size, const void* buf);
102
103 // Read <src>size</src> bytes from the File. Returns the number of bytes
104 // actually read. Will throw an exception (AipsError) if the requested
105 // number of bytes could not be read unless throwException is set to
106 // False. Will always throw an exception if the file is not readable or
107 // the system call returns an undocumented value.
108 virtual Int64 read (Int64 size, void* buf, Bool throwException=True);
109
110 // Get a read or write pointer to the given position in the mapped file.
111 // An exception is thrown if beyond end-of-file or it not writable.
112 // These functions should be used with care. If the pointer is used to
113 // access data beyond the file size, a segmentation fault will occur.
114 // So it means that the write pointer can only be used to update the file,
115 // not to extend it. The <src>seek</src> and <src>write</src> functions
116 // should be used to extend a file.
117 // <group>
118 const void* getReadPointer (Int64 offset) const;
119 void* getWritePointer (Int64 offset);
120 // </group>
121
122 // Get the file size.
124 { return itsFileSize; }
125
126protected:
127 // Reset the position pointer to the given value. It returns the
128 // new position.
130
131 // Unmap the file.
132 void unmapFile();
133
134private:
135 Int64 itsFileSize; //# File size
136 Int64 itsPosition; //# Current seek position
137 char* itsPtr; //# Pointer to memory map
139};
140
141} // end namespace
142
143#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:80
int fd() const
Get the file descriptor.
Definition FiledesIO.h:164
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:123
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
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
const Bool True
Definition aipstype.h:41