Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXMemMap.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                      M e m o r y   M a p p e d   F i l e                      *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2004,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXMemMap.h,v 1.18 2009/01/06 13:07:25 fox Exp $                          *
00022 ********************************************************************************/
00023 #ifndef FXMEMMAP_H
00024 #define FXMEMMAP_H
00025 
00026 
00027 #ifndef FXFILE_H
00028 #include "FXFile.h"
00029 #endif
00030 
00031 namespace FX {
00032 
00033 
00034 /**
00035 * A Memory Map provides a view of a file as an array of memory;
00036 * this allows the file itself to be used as backing for the data
00037 * and very simplified file access results.
00038 * Moreover, mapped files may be shared by processes, resuling
00039 * in far less "real" memory being used than would otherwise be
00040 * the case.
00041 */
00042 class FXAPI FXMemMap : public FXFile {
00043 private:
00044   FXInputHandle maphandle;      // Handle for the map
00045   FXuchar*      mapbase;        // Memory base where it is mapped
00046   FXlong        mapoffset;      // Offset of the map
00047   FXlong        mapposition;    // Position in file
00048   FXival        maplength;      // Length of the map
00049 private:
00050   FXMemMap(const FXMemMap&);
00051   FXMemMap &operator=(const FXMemMap&);
00052 public:
00053 
00054   /// Construct a memory map
00055   FXMemMap();
00056 
00057   /// Open a file, and map a view of it into memory; the offset must be a multiple of the page size
00058   void* openMap(const FXString& filename,FXlong off=0,FXival len=-1L,FXuint m=FXIO::Reading,FXuint p=FXIO::AllReadWrite);
00059 
00060   /// Open map using existing file handle, and map a view of it into memory
00061   void* openMap(FXInputHandle h,FXlong off=0,FXival len=-1L,FXuint m=FXIO::Reading);
00062 
00063   /// Map a view of the already open file into memory
00064   void *map(FXlong off=0,FXival len=-1L);
00065 
00066   /// Return pointer to memory area
00067   void* base() const { return mapbase; }
00068 
00069   /// Obtain length of the map
00070   long length() const { return maplength; }
00071 
00072   /// Obtain offset of the map
00073   long offset() const { return mapoffset; }
00074 
00075   /// Get current file position
00076   virtual FXlong position() const;
00077 
00078   /// Change file position, returning new position from start
00079   virtual FXlong position(FXlong off,FXuint from=FXIO::Begin);
00080 
00081   /// Unmap the view of the file
00082   void* unmap();
00083 
00084   /// Read block of bytes, returning number of bytes read
00085   virtual FXival readBlock(void* data,FXival count);
00086 
00087   /// Write block of bytes, returning number of bytes written
00088   virtual FXival writeBlock(const void* data,FXival count);
00089 
00090   /// Flush to disk
00091   virtual FXbool flush();
00092 
00093   /// Close file, and also the map
00094   virtual FXbool close();
00095 
00096   /// Destroy the map
00097   ~FXMemMap();
00098   };
00099 
00100 
00101 }
00102 
00103 #endif

Copyright © 1997-2009 Jeroen van der Zijp