casa
$Rev:20696$
|
00001 //# MMapfdIO.h: Memory-mapped IO on a file descriptor 00002 //# 00003 //# Copyright (C) 2009 00004 //# Associated Universities, Inc. Washington DC, USA. 00005 //# 00006 //# This library is free software; you can redistribute it and/or modify it 00007 //# under the terms of the GNU Library General Public License as published by 00008 //# the Free Software Foundation; either version 2 of the License, or (at your 00009 //# option) any later version. 00010 //# 00011 //# This library is distributed in the hope that it will be useful, but WITHOUT 00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 //# License for more details. 00015 //# 00016 //# You should have received a copy of the GNU Library General Public License 00017 //# along with this library; if not, write to the Free Software Foundation, 00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00019 //# 00020 //# Correspondence concerning AIPS++ should be addressed as follows: 00021 //# Internet email: aips2-request@nrao.edu. 00022 //# Postal address: AIPS++ Project Office 00023 //# National Radio Astronomy Observatory 00024 //# 520 Edgemont Road 00025 //# Charlottesville, VA 22903-2475 USA 00026 //# 00027 //# $Id: MMapfdIO.h 20859 2010-02-03 13:14:15Z gervandiepen $ 00028 00029 #ifndef CASA_MMAPFDIO_H 00030 #define CASA_MMAPFDIO_H 00031 00032 //# Includes 00033 #include <casa/IO/LargeFiledesIO.h> 00034 #include <casa/OS/RegularFile.h> 00035 00036 namespace casa 00037 { 00038 00039 // <summary> 00040 // Memory-mapped IO on a file. 00041 // </summary> 00042 00043 // <synopsis> 00044 // Memory-mapped IO lets the OS take care of caching file segments. 00045 // This is particularly useful for the Tiled Storage Manager which keeps a 00046 // cache of tiles. When using memory-mapped IO it does not need to do that 00047 // anymore. 00048 // 00049 // On 32-bit systems its use is limited because for large files the 4 GB 00050 // memory space is insufficient. However, for 64-bit systems the memory 00051 // space is large enough to make use of it. 00052 // 00053 // In the general case there is direct access to the mapped file space. 00054 // The read and write methods copies the data into/from a buffer. 00055 // However, to avoid the copying it is possible to get a direct pointer 00056 // to the mapped data. This should be used with care, because writing to 00057 // it will cause a segmentation if the file is readonly. If the file is 00058 // writable, writing into the mapped data segment means changing the file 00059 // contents. 00060 // </synopsis> 00061 00062 class MMapfdIO: public LargeFiledesIO 00063 { 00064 public: 00065 // Default constructor. 00066 // A file can be memory-mapped using the map function. 00067 MMapfdIO(); 00068 00069 // Map the given file descriptor entirely into memory with read access. 00070 // The map has also write access if the file is opened for write. 00071 // The file name is only used in possible error messages. 00072 MMapfdIO (int fd, const String& fileName); 00073 00074 // Destructor. 00075 // If needed, it will flush and unmap the file, but not close it. 00076 ~MMapfdIO(); 00077 00078 // Map the given file descriptor entirely into memory with read access. 00079 // The map has also write access if the file is opened for write. 00080 // An exception is thrown if a file descriptor was already attached. 00081 // The file name is only used in possible error messages. 00082 void map (int fd, const String& fileName); 00083 00084 // Map or remap the entire file. 00085 // Remapping is needed if the file has grown elsewhere. 00086 void mapFile(); 00087 00088 // Flush changed mapped data to the file. 00089 // Nothing is done if the file is readonly. 00090 void flush(); 00091 00092 // Write the number of bytes from the seek position on. 00093 // The file will be extended and remapped if writing beyond end-of-file. 00094 // In that case possible pointers obtained using <src>getXXPointer</src> 00095 // are not valid anymore. 00096 virtual void write (uInt size, const void* buf); 00097 00098 // Read <src>size</src> bytes from the File. Returns the number of bytes 00099 // actually read. Will throw an exception (AipsError) if the requested 00100 // number of bytes could not be read unless throwException is set to 00101 // False. Will always throw an exception if the file is not readable or 00102 // the system call returns an undocumented value. 00103 virtual Int read (uInt size, void* buf, Bool throwException=True); 00104 00105 // Get a read or write pointer to the given position in the mapped file. 00106 // An exception is thrown if beyond end-of-file or it not writable. 00107 // These functions should be used with care. If the pointer is used to 00108 // access data beyond the file size, a segmentation fault will occur. 00109 // So it means that the write pointer can only be used to update the file, 00110 // not to extend it. The <src>seek</src> and <src>write</src> functions 00111 // should be used to extend a file. 00112 // <group> 00113 const void* getReadPointer (Int64 offset) const; 00114 void* getWritePointer (Int64 offset); 00115 // </group> 00116 00117 // Get the file size. 00118 Int64 getFileSize() const 00119 { return itsFileSize; } 00120 00121 protected: 00122 // Reset the position pointer to the given value. It returns the 00123 // new position. 00124 virtual Int64 doSeek (Int64 offset, ByteIO::SeekOption); 00125 00126 // Unmap the file. 00127 void unmapFile(); 00128 00129 private: 00130 // Forbid copy constructor and assignment 00131 // <group> 00132 MMapfdIO (const MMapfdIO&); 00133 MMapfdIO& operator= (const MMapfdIO&); 00134 // </group> 00135 00136 Int64 itsFileSize; //# File size 00137 Int64 itsPosition; //# Current seek position 00138 char* itsPtr; //# Pointer to memory map 00139 Bool itsIsWritable; 00140 }; 00141 00142 } // end namespace 00143 00144 #endif