memoryMap.cpp

00001 
00002 /* Copyright (c) 2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published 
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "memoryMap.h"
00019 
00020 #include "debug.h"
00021 
00022 #include <fcntl.h>
00023 #include <sys/stat.h>
00024 #ifndef WIN32
00025 #   include <sys/mman.h>
00026 #endif
00027 
00028 namespace eq
00029 {
00030 namespace base
00031 {
00032 
00033 MemoryMap::MemoryMap()
00034 #ifdef WIN32
00035         : _map( 0 )
00036 #else
00037         : _fd( 0 )
00038 #endif
00039         , _ptr( 0 )
00040         , _size( 0 )
00041 {}
00042 
00043 MemoryMap::~MemoryMap()
00044 {
00045     if( _ptr )
00046         unmap();
00047 }
00048 
00049 const void* MemoryMap::map( const std::string& filename )
00050 {
00051     if( _ptr )
00052     {
00053         EQWARN << "File already mapped" << std::endl;
00054         return 0;
00055     }
00056 
00057 #ifdef WIN32
00058     // try to open binary file
00059     HANDLE file = CreateFile( filename.c_str(), GENERIC_READ, FILE_SHARE_READ,
00060                               0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
00061     if( file == INVALID_HANDLE_VALUE )
00062     {
00063         EQWARN << "Can't open " << filename << std::endl;
00064         return 0;
00065     }
00066 
00067     // create a file mapping
00068     _map = CreateFileMapping( file, 0, PAGE_READONLY, 0, 0, filename.c_str( ));
00069     if( !_map )
00070     {
00071         EQWARN << "File mapping failed." << std::endl;
00072         return 0;
00073     }
00074     
00075     // get a view of the mapping
00076     _ptr = MapViewOfFile( _map, FILE_MAP_READ, 0, 0, 0 );
00077     
00078     // get size
00079     DWORD highSize;
00080     const DWORD lowSize = GetFileSize( file, &highSize );
00081     _size = lowSize | ( static_cast< uint64_t >( highSize ) << 32 );
00082 
00083     CloseHandle( file );
00084 #else // POSIX
00085 
00086     // try to open binary file
00087     _fd = open( filename.c_str(), O_RDONLY );
00088     if( _fd < 0 )
00089     {
00090         EQWARN << "Can't open " << filename << std::endl;
00091         return 0;
00092     }
00093     
00094     // retrieving file information
00095     struct stat status;
00096     fstat( _fd, &status );
00097     
00098     // create memory mapped file
00099     _size = status.st_size;
00100     _ptr = mmap( 0, _size, PROT_READ, MAP_SHARED, _fd, 0 );
00101 
00102     if( _ptr == MAP_FAILED )
00103     {
00104         close( _fd );
00105         _ptr = 0;
00106         _size = 0;
00107         _fd = 0;
00108     }
00109 #endif
00110 
00111     return _ptr;
00112 }
00113 
00114 void MemoryMap::unmap()
00115 {
00116     if( !_ptr )
00117     {
00118         EQWARN << "File not mapped" << std::endl;
00119         return;
00120     }
00121 
00122 #ifdef WIN32
00123     UnmapViewOfFile( _ptr );
00124     CloseHandle( _map );
00125     
00126     _ptr = 0;
00127     _size = 0;
00128     _map = 0;
00129 #else
00130     munmap( _ptr, _size );
00131     close( _fd );
00132     
00133     _ptr = 0;
00134     _size = 0;
00135     _fd = 0;
00136 #endif
00137 }
00138 
00139 }
00140 }
Generated on Mon Aug 10 18:58:40 2009 for Equalizer 0.9 by  doxygen 1.5.8