mappedfile.cc

Go to the documentation of this file.
00001 /** @file rutz/mappedfile.cc 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 14:11:31 2004
00009 // commit: $Id: mappedfile.cc 8249 2007-04-12 06:03:40Z rjpeters $
00010 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mappedfile.cc $
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_CC_UTC20050626084020_DEFINED
00034 #define GROOVX_RUTZ_MAPPEDFILE_CC_UTC20050626084020_DEFINED
00035 
00036 #include "mappedfile.h"
00037 
00038 #include "rutz/error.h"
00039 #include "rutz/sfmt.h"
00040 
00041 #include <cerrno>
00042 #include <cstring>     // for strerror()
00043 #include <fcntl.h>     // for open(), O_RDONLY
00044 #include <sys/mman.h>  // for mmap()
00045 #include <unistd.h>    // for close()
00046 
00047 rutz::mapped_infile::mapped_infile(const char* filename)
00048   :
00049   m_statbuf(),
00050   m_fileno(0),
00051   m_mem(0)
00052 {
00053   errno = 0;
00054 
00055   if (stat(filename, &m_statbuf) == -1)
00056     {
00057       throw rutz::error(rutz::sfmt("stat() failed for file '%s':\n"
00058                                    "%s\n", filename, strerror(errno)),
00059                         SRC_POS);
00060     }
00061 
00062   m_fileno = open(filename, O_RDONLY);
00063   if (m_fileno == -1)
00064     {
00065       throw rutz::error(rutz::sfmt("open() failed for file '%s':\n"
00066                                    "%s\n", filename, strerror(errno)),
00067                         SRC_POS);
00068     }
00069 
00070   m_mem = mmap(0, m_statbuf.st_size,
00071                PROT_READ, MAP_PRIVATE, m_fileno, 0);
00072 
00073   if (m_mem == (void*)-1)
00074     {
00075       throw rutz::error(rutz::sfmt("mmap() failed for file '%s':\n"
00076                                    "%s\n", filename, strerror(errno)),
00077                         SRC_POS);
00078     }
00079 }
00080 
00081 rutz::mapped_infile::~mapped_infile()
00082 {
00083   munmap(m_mem, m_statbuf.st_size);
00084   close(m_fileno);
00085 }
00086 
00087 static const char __attribute__((used)) vcid_groovx_rutz_mappedfile_cc_utc20050626084020[] = "$Id: mappedfile.cc 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mappedfile.cc $";
00088 #endif // !GROOVX_RUTZ_MAPPEDFILE_CC_UTC20050626084020_DEFINED
Generated on Sun May 8 08:06:43 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3