CASToR  3.2
Tomographic Reconstruction (PET/SPECT/CT)
include/datafile/oMemoryMapped.hh
Go to the documentation of this file.
1 
13 #ifndef OMEMORYMAPPED_HH
14 #define OMEMORYMAPPED_HH 1
15 
16 // define fixed size integer types
17 #if defined(_WIN32)
18 #include <inttypes.h>
19 typedef unsigned __int64 uint64_t;
20 #else
21 #include <stdint.h>
22 #endif
23 
24 #include "gVariables.hh"
25 #include "sOutputManager.hh"
26 
28 
29 class oMemoryMapped
30 {
31  public:
33  enum CacheHint
34  {
35  Normal,
38  };
39 
41  enum MapRange
42  {
43  WholeFile = 0
44  };
45 
47  oMemoryMapped();
49  oMemoryMapped(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
52 
54  int Open(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
56  void Close();
57 
59  unsigned char operator[](size_t offset) const;
61  unsigned char at (size_t offset) const;
62 
64  const unsigned char* GetData() const;
65 
67  bool IsValid() const;
68 
70  uint64_t size() const;
72  size_t mappedSize() const;
73 
75  int Remap(uint64_t offset, size_t mappedBytes);
76 
77  private:
82 
84  static int GetPageSize();
85 
87  std::string _filename;
89  uint64_t _filesize;
93  size_t _mappedBytes;
94 
96 #if defined(_WIN32) || defined(CASTOR_USE_MINGW)
97  typedef void* FileHandle;
99  void* _mappedFile;
100 #else
101  typedef int FileHandle;
102 #endif
103 
105  FileHandle _file;
107  void* _mappedView;
108 };
109 
110 #endif
read file only once with few seeks
everything ... be careful when file is larger than memory
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()
size_t mappedSize() const
get number of actually mapped bytes
uint64_t size() const
get file size
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() )
const unsigned char * GetData() const
raw access
Portable read-only memory mapping (Windows and Linux)
CacheHint _hint
caching strategy
MapRange
how much should be mappend