-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMemoryMapped.h
100 lines (82 loc) · 2.49 KB
/
MemoryMapped.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// //////////////////////////////////////////////////////////
// MemoryMapped.h
// Copyright (c) 2013 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//
#pragma once
// define fixed size integer types
#ifdef _MSC_VER
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
#include <string>
/// Portable read-only memory mapping (Windows and Linux)
/** Filesize limited by size_t, usually 2^32 or 2^64 */
class MemoryMapped
{
public:
/// tweak performance
enum CacheHint
{
Normal, ///< good overall performance
SequentialScan, ///< read file only once with few seeks
RandomAccess ///< jump around
};
/// how much should be mappend
enum MapRange
{
WholeFile = 0 ///< everything ... be careful when file is larger than memory
};
/// do nothing, must use open()
MemoryMapped();
/// open file, mappedBytes = 0 maps the whole file
MemoryMapped(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
/// close file (see close() )
~MemoryMapped();
/// open file, mappedBytes = 0 maps the whole file
bool open(const std::string& filename, size_t mappedBytes = WholeFile, CacheHint hint = Normal);
/// close file
void close();
/// access position, no range checking (faster)
unsigned char operator[](size_t offset) const;
/// access position, including range checking
unsigned char at (size_t offset) const;
/// raw access
const unsigned char* getData() const;
/// true, if file successfully opened
bool isValid() const;
/// get file size
uint64_t size() const;
/// get number of actually mapped bytes
size_t mappedSize() const;
/// replace mapping by a new one of the same file, offset MUST be a multiple of the page size
bool remap(uint64_t offset, size_t mappedBytes);
private:
/// don't copy object
MemoryMapped(const MemoryMapped&);
/// don't copy object
MemoryMapped& operator=(const MemoryMapped&);
/// get OS page size (for remap)
static int getpagesize();
/// file name
std::string _filename;
/// file size
uint64_t _filesize;
/// caching strategy
CacheHint _hint;
/// mapped size
size_t _mappedBytes;
/// define handle
#ifdef _MSC_VER
typedef void* FileHandle;
/// Windows handle to memory mapping of _file
void* _mappedFile;
#else
typedef int FileHandle;
#endif
/// file handle
FileHandle _file;
/// pointer to the file contents mapped into memory
void* _mappedView;
};