CASToR  3.0
Tomographic Reconstruction (PET/SPECT/CT)
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-2019 all CASToR contributors listed below:
18 
19  --> Didier BENOIT, Claude COMTAT, Marina FILIPOVIC, Thibaut MERLIN, Mael MILLARDET, Simon STUTE, Valentin VIELZEUF
20 
21 This is CASToR version 3.0.
22 */
23 
35 #ifndef OMEMORYMAPPED_HH
36 #define OMEMORYMAPPED_HH 1
37 
38 // define fixed size integer types
39 #if defined(_WIN32)
40 #include <inttypes.h>
41 typedef unsigned __int64 uint64_t;
42 #else
43 #include <stdint.h>
44 #endif
45 
46 #include "gVariables.hh"
47 #include "sOutputManager.hh"
48 
50 
52 {
53  public:
55  enum CacheHint
56  {
60  };
61 
63  enum MapRange
64  {
65  WholeFile = 0
66  };
67 
69  oMemoryMapped();
71  oMemoryMapped(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
74 
76  int Open(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
78  void Close();
79 
81  unsigned char operator[](size_t offset) const;
83  unsigned char at (size_t offset) const;
84 
86  const unsigned char* GetData() const;
87 
89  bool IsValid() const;
90 
92  uint64_t size() const;
94  size_t mappedSize() const;
95 
97  int Remap(uint64_t offset, size_t mappedBytes);
98 
99  private:
104 
106  static int GetPageSize();
107 
109  std::string _filename;
111  uint64_t _filesize;
115  size_t _mappedBytes;
116 
118 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
119  typedef void* FileHandle;
121  void* _mappedFile;
122 #else
123  typedef int FileHandle;
124 #endif
125 
127  FileHandle _file;
129  void* _mappedView;
130 };
131 
132 #endif
This header file is mainly used to declare some macro definitions and all includes needed from the st...
oMemoryMapped & operator=(const oMemoryMapped &)
don&#39;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 ...
void * _mappedView
pointer to the file contents mapped into memory
bool IsValid() const
true, if file successfully opened
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
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
uint64_t size() const
get file size
void Close()
close file
unsigned char at(size_t offset) const
access position, including range checking
static int GetPageSize()
get OS page size (for remap)
unsigned char operator[](size_t offset) const
access position, no range checking (faster)
~oMemoryMapped()
close file (see close() )
size_t _mappedBytes
mapped size
int FileHandle
define handle
Declaration of class sOutputManager.
std::string _filename
file name
const unsigned char * GetData() const
raw access
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