MemoryMap.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __MEMORYMAP_H
00019 #define __MEMORYMAP_H
00020 #include <sys/types.h>
00021 #include <fcntl.h>
00022 
00023 #if defined(WIN32)
00024 #include <windows.h>
00025 #endif
00026 
00155 class MemoryMap
00156 {
00157 #if defined(WIN32)
00158     static  SYSTEM_INFO system_info;    // for page size information
00159     HANDLE  file_handle;
00160     HANDLE  map_handle;
00161 #else
00162     int fd;
00163 #endif
00164     off_t offset;
00165     size_t mapped_length;
00166     size_t total_length;
00167     size_t page_size;
00168     bool    useMemoryMapFlag;
00169 public:
00170 
00171     void *data;
00172 
00173     MemoryMap();
00174 
00175     virtual ~MemoryMap();
00176 
00177     void debug_print();
00178 
00179     void constructor_clear();
00180 
00181     void destructor_clear();
00182 
00188     virtual bool open(const char * file, int flags = O_RDONLY);
00189 
00195     //
00196     virtual bool create(const char * file, size_t size);
00197 
00204     //
00205     virtual bool create(size_t size);
00206 
00207     bool close();
00208     void test();
00209     size_t length()
00210     {
00211         return mapped_length;
00212     }
00213 
00214     char operator[](unsigned int index)
00215     {
00216         return ((char *)data)[index];
00217     };
00218     int prefetch();     // force pages into RAM
00219 
00220     //
00221     // set or unset use of mmap() call in ::open().
00222     // This flag must be set before ::open() is called,
00223     // if it is called afterwards, it has no effect.
00224     //
00225     void useMemoryMap(bool flag=true)
00226     {
00227         useMemoryMapFlag = flag;
00228     }
00229 };
00230 
00231 #endif
Generated on Tue Nov 9 16:11:49 2010 for StatGen Software by  doxygen 1.6.3