mappedfile.h

Go to the documentation of this file.
00001 /** @file rutz/mappedfile.h c++ wrapper for mmap() for fast file i/o */
00002 
00003 ///////////////////////////////////////////////////////////////////////
00004 //
00005 // Copyright (c) 2004-2007 University of Southern California
00006 // Rob Peters <rjpeters at usc dot edu>
00007 //
00008 // created: Fri Oct  8 13:50:14 2004
00009 // commit: $Id: mappedfile.h 8249 2007-04-12 06:03:40Z rjpeters $
00010 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mappedfile.h $
00011 //
00012 // --------------------------------------------------------------------
00013 //
00014 // This file is part of GroovX.
00015 //   [http://ilab.usc.edu/rjpeters/groovx/]
00016 //
00017 // GroovX is free software; you can redistribute it and/or modify it
00018 // under the terms of the GNU General Public License as published by
00019 // the Free Software Foundation; either version 2 of the License, or
00020 // (at your option) any later version.
00021 //
00022 // GroovX is distributed in the hope that it will be useful, but
00023 // WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025 // General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License
00028 // along with GroovX; if not, write to the Free Software Foundation,
00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00030 //
00031 ///////////////////////////////////////////////////////////////////////
00032 
00033 #ifndef GROOVX_RUTZ_MAPPEDFILE_H_UTC20050626084020_DEFINED
00034 #define GROOVX_RUTZ_MAPPEDFILE_H_UTC20050626084020_DEFINED
00035 
00036 #include <sys/stat.h>  // for stat()
00037 
00038 namespace rutz
00039 {
00040   //! An mmap()/munmap() wrapper class for fast input file reading.
00041   /*! In cases where an entire file would eventually be read into
00042       memory anyway, it is likely to be significantly faster (2-3x
00043       faster at least) to use mmap-ing instead of C-style
00044       fopen()/fgetc()/fread()/fclose() or C++-style iostreams. This is
00045       because both the C and C++ libraries are likely implemented
00046       internally with mmap anyway, and those libraries introduce
00047       another layer of memory buffering on top of the raw OS mmap. */
00048   class mapped_infile
00049   {
00050   public:
00051     //! Open the named file using mmap().
00052     /*! The contents of the file become accessible as if the file were
00053         laid out continously in memory. */
00054     mapped_infile(const char* filename);
00055 
00056     //! Destructor closes and munmap()'s the file.
00057     ~mapped_infile();
00058 
00059     //! Get a pointer to the memory representing the file contents.
00060     const void* memory() const { return m_mem; }
00061 
00062     //! Get the length of the file, and of its in-memory representation.
00063     off_t length() const { return m_statbuf.st_size; }
00064 
00065     //! Get the last-modification time of the file.
00066     time_t mtime() const { return m_statbuf.st_mtime; }
00067 
00068   private:
00069     mapped_infile(const mapped_infile&);
00070     mapped_infile& operator=(const mapped_infile&);
00071 
00072     struct stat m_statbuf;
00073     int         m_fileno;
00074     void*       m_mem;
00075   };
00076 }
00077 
00078 static const char __attribute__((used)) vcid_groovx_rutz_mappedfile_h_utc20050626084020[] = "$Id: mappedfile.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mappedfile.h $";
00079 #endif // !GROOVX_RUTZ_MAPPEDFILE_H_UTC20050626084020_DEFINED
Generated on Sun May 8 08:42:12 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3