Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

MemoryMap.h (Maintainer: Chris Bielow)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Chris Bielow $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_SYSTEM_MEMORYMAP_H
00028 #define OPENMS_SYSTEM_MEMORYMAP_H
00029 
00030 #include <OpenMS/CONCEPT/Types.h>
00031 
00032 #define OPENMS_MUNMAP_FAILURE (-1)
00033 
00034 #ifdef OPENMS_WINDOWSPLATFORM
00035   #include <Windows.h>
00036   #define MAP_FAILED ((void *) -1) /* from mman.h (linux)      */
00037 #else
00038   //#include <unistd.h>
00039   #include <sys/mman.h>            /* WARNING: use #undef MAP_TYPE when done!! see bottom of file! */
00040 #endif  
00041 
00042 
00043 // Mac OS X does not provide lseek64 and open64, so we need to replace them with their normal counterparts
00044 #if defined __APPLE__ & defined __MACH__
00045 #define mmap64 mmap
00046 #endif
00047 
00048 namespace OpenMS
00049 {  
00050 
00051 
00057   class MemoryMap
00058   {
00059     public:
00060     // recreate the functions already offered by POSIX  
00061   
00062     static std::size_t OpenMS_getFileBlocksize (void)
00063     {
00064       static std::size_t g_pagesize = 0;
00065       if (! g_pagesize) 
00066       {
00067         #ifdef OPENMS_WINDOWSPLATFORM
00068           SYSTEM_INFO system_info;
00069           GetSystemInfo (&system_info);
00070           g_pagesize = system_info.dwAllocationGranularity;
00071         #else
00072           g_pagesize = getpagesize();
00073         #endif  
00074       }
00075       return g_pagesize;    
00076     }
00077     
00078   
00079     #ifdef OPENMS_WINDOWSPLATFORM
00080     /* mmap for windows */
00081       static void* OpenMS_mmap (const std::size_t& size, const HANDLE& handle, const Offset64Int& file_offset)
00082       {
00083         
00084         // set maximal mapping size
00085         // @note this will increase the swap file size automatically
00086         //       (in contrast to Linux where file extension has to be done manually before the mapping!)
00087         LARGE_INTEGER iTmp1;
00088         iTmp1.QuadPart = file_offset + (Offset64Int)size; 
00089         DWORD hi1 = iTmp1.HighPart;
00090         DWORD lo1 = iTmp1.LowPart;
00091       
00092         // warning: do not attempt to create a mapping for an empty file. it will fail on windows!
00093         HANDLE mmapHandle_ = CreateFileMapping(handle,
00094                                               NULL,
00095                                               PAGE_READWRITE,
00096                                               hi1,
00097                                               lo1,
00098                                               NULL
00099                                               );
00100         LPVOID map;                                             
00101         if (mmapHandle_ == NULL)                                 
00102         {
00103            map = MAP_FAILED;  // ((void *) -1)
00104            return map;
00105         }
00106         
00107         // start real mapping
00108             
00109         LARGE_INTEGER iTmp;
00110         iTmp.QuadPart = file_offset;
00111         DWORD hi = iTmp.HighPart;
00112         DWORD lo = iTmp.LowPart;
00113     
00114         //std::cout << "in mmap: " << size << " " << file_offset << "\n";
00115     
00116         map = MapViewOfFile(
00117                             mmapHandle_,            // A file handle
00118                             FILE_MAP_ALL_ACCESS,    // A read/write view of the file is mapped
00119                             hi,
00120                             lo, 
00121                             size
00122                            );
00123       
00124         // file mapping handle not needed any longer
00125         CloseHandle(mmapHandle_);
00126         
00127         if (map == NULL) //FAILED
00128         { // convert to UNIX return value
00129           map = MAP_FAILED; // ((void *) -1)
00130         }
00131         return map;
00132     
00133       }
00134     #else
00135       static void* OpenMS_mmap (const std::size_t& size, const int& fileHandle, const Offset64Int& file_offset)
00136       {
00137         
00138         void* map =  mmap64(0,
00139                             size,
00140                             PROT_READ | PROT_WRITE,
00141                             MAP_SHARED,
00142                             fileHandle,
00143                             file_offset
00144                           );
00145         return map;
00146       }
00147     #endif
00148   
00149   
00152     static int OpenMS_unmap (void* p, const std::size_t& bytes)
00153     {
00154       #ifdef OPENMS_WINDOWSPLATFORM
00155         int result = UnmapViewOfFile(p);  
00156         if (result == 0) result = OPENMS_MUNMAP_FAILURE; // 0 indicates failure in Windows
00157       #else
00158         int result = munmap(p, bytes);
00159       #endif
00160       return result;
00161     }
00162   
00163   
00164   
00165   }; // end MemoryMap class
00166 
00167 
00168 } //namespace OpenMS
00169 
00170 #undef MAP_TYPE //this is a MACRO defined in mmap.h and conflicts with CGAL, which uses "MAP_TYPE" in an internal class as TEMPLATE parameter!  
00171 
00172 
00173 #endif // OPENMS_SYSTEM_MEMORYMAP_H

Generated Tue Apr 1 15:36:36 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1