CASToR  2.0
Tomographic Reconstruction (PET/SPECT/CT)
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oMemoryMapped.hh
Go to the documentation of this file.
1 /*
2 This file is part of CASToR.
3 
4  CASToR is free software: you can redistribute it and/or modify it under the
5  terms of the GNU General Public License as published by the Free Software
6  Foundation, either version 3 of the License, or (at your option) any later
7  version.
8 
9  CASToR is distributed in the hope that it will be useful, but WITHOUT ANY
10  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  details.
13 
14  You should have received a copy of the GNU General Public License along with
15  CASToR (in file GNU_GPL.TXT). If not, see <http://www.gnu.org/licenses/>.
16 
17 Copyright 2017-2018 all CASToR contributors listed below:
18 
19  --> current contributors: Thibaut MERLIN, Simon STUTE, Didier BENOIT, Claude COMTAT, Marina FILIPOVIC, Mael MILLARDET
20  --> past contributors: Valentin VIELZEUF
21 
22 This is CASToR version 2.0.
23 */
24 
36 #ifndef OMEMORYMAPPED_HH
37 #define OMEMORYMAPPED_HH 1
38 
39 // define fixed size integer types
40 #if defined(_WIN32)
41 #include <inttypes.h>
42 typedef unsigned __int64 uint64_t;
43 #else
44 #include <stdint.h>
45 #endif
46 
47 #include "gVariables.hh"
48 #include "sOutputManager.hh"
49 
51 
53 {
54  public:
56  enum CacheHint
57  {
61  };
62 
64  enum MapRange
65  {
66  WholeFile = 0
67  };
68 
70  oMemoryMapped();
72  oMemoryMapped(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
75 
77  int Open(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
79  void Close();
80 
82  unsigned char operator[](size_t offset) const;
84  unsigned char at (size_t offset) const;
85 
87  const unsigned char* GetData() const;
88 
90  bool IsValid() const;
91 
93  uint64_t size() const;
95  size_t mappedSize() const;
96 
98  int Remap(uint64_t offset, size_t mappedBytes);
99 
100  private:
105 
107  static int GetPageSize();
108 
110  std::string _filename;
112  uint64_t _filesize;
116  size_t _mappedBytes;
117 
119 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
120  typedef void* FileHandle;
122  void* _mappedFile;
123 #else
124  typedef int FileHandle;
125 #endif
126 
130  void* _mappedView;
131 };
132 
133 #endif
This header file is mainly used to declare some macro definitions and all includes needed from the st...
oMemoryMapped & operator=(const oMemoryMapped &)
don't copy object
int Remap(uint64_t offset, size_t mappedBytes)
replace mapping by a new one of the same file, offset MUST be a multiple of the page size ...
bool IsValid() const
true, if file successfully opened
unsigned char at(size_t offset) const
access position, including range checking
void * _mappedView
pointer to the file contents mapped into memory
int Open(const std::string &filename, size_t mappedBytes=WholeFile, CacheHint hint=Normal)
open file, mappedBytes = 0 maps the whole file
oMemoryMapped()
do nothing, must use open()
CacheHint
tweak performance
unsigned char operator[](size_t offset) const
access position, no range checking (faster)
everything ... be careful when file is larger than memory
read file only once with few seeks
uint64_t _filesize
file size
size_t mappedSize() const
get number of actually mapped bytes
void Close()
close file
const unsigned char * GetData() const
raw access
static int GetPageSize()
get OS page size (for remap)
~oMemoryMapped()
close file (see close() )
size_t _mappedBytes
mapped size
uint64_t size() const
get file size
int FileHandle
define handle
Declaration of class sOutputManager.
std::string _filename
file name
Portable read-only memory mapping (Windows and Linux)
FileHandle _file
file handle
CacheHint _hint
caching strategy
good overall performance
MapRange
how much should be mappend