-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMmFile.h
More file actions
102 lines (83 loc) · 3.39 KB
/
MmFile.h
File metadata and controls
102 lines (83 loc) · 3.39 KB
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
101
102
// --------------------------------------------------------------
// Workfile : MmFile.h
// Author : Johannes Posch
// Date : 20.10.2017
// Description : This class is a wrapper for the linux systemcalls
// to memorymap a file for read / write.
// --------------------------------------------------------------
#ifndef MMFILE_H
#define MMFILE_H
#include <string>
#ifdef _WIN32
#include <Windows.h>
#endif //_WIN32
class MmFile {
public:
MmFile();
~MmFile();
// --------------------------------------------------------------
// fastMap:
// This function opens a file does a resize if the size argument
// is greater than 0 and maps the file. The function does a
// cleanup on error
// --------------------------------------------------------------
bool fastMap(std::string const &filepath, bool const write = false,
unsigned long const size = 0);
// --------------------------------------------------------------
// openFile:
// This function opens a file from the given path to map it later
// Mode specifies if the file should be truncated on opening
// --------------------------------------------------------------
bool openFile(std::string const &filepath);
// --------------------------------------------------------------
// resize:
// This function does a resize on a previously opened file
// The file also gets truncated on resize
// --------------------------------------------------------------
bool resize(unsigned long const size);
// --------------------------------------------------------------
// closeFile:
// This function unmaps the file from memory if it was mapped
// before and also releases the file resources.
// explicitSync triggers a synchronisation with the filesystem
// before the file get's closed
// --------------------------------------------------------------
bool closeFile(bool const explicitSync = false);
// --------------------------------------------------------------
// map:
// This function does the mapping of the previously opened file
// into memory, this function must get called after opening a
// file
// --------------------------------------------------------------
bool map(bool const write = false);
// --------------------------------------------------------------
// getMemoryMappedAddress:
// This function returns the startaddress of the mapped file
// nullptr will be returned if the file is not mapped
// --------------------------------------------------------------
void* getMemoryMappedAddress() const;
// --------------------------------------------------------------
// syncMemoryMapped:
// This function does a synchronisation on the filesystem
// --------------------------------------------------------------
bool syncMemoryMapped();
private:
// --------------------------------------------------------------
// unmapMemoryMapped:
// This function unmaps the file from memory
// --------------------------------------------------------------
bool unmapMemoryMapped();
#ifdef _WIN32
HANDLE m_fileDescriptor;
HANDLE m_memoryMapped;
HANDLE m_memoryBaseHandle;
_off_t m_filesize;
#else
void* m_memoryMapped;
int m_fileDescriptor;
__off_t m_filesize;
#endif //_WIN32
bool m_mapped;
bool m_opened;
};
#endif //MMFILE_H