-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmemory_mapped_file.hpp
86 lines (80 loc) · 2.2 KB
/
memory_mapped_file.hpp
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
#ifndef MEMORY_MAPPED_FILE_HPP
#define MEMORY_MAPPED_FILE_HPP
#include <cstddef> // for size_t
namespace memory_mapped_file
{
unsigned int mmf_granularity();
class base_mmf
{
public:
explicit base_mmf();
~base_mmf();
size_t offset() const { return offset_; }
size_t mapped_size() const { return mapped_size_; }
size_t file_size() const { return file_size_; }
void unmap();
void close();
bool is_open() const
{
return file_handle_ !=
#if defined(_WIN32)
(void*)
#endif
-1;
}
#if defined(_WIN32)
typedef void* HANDLE;
#else
typedef int HANDLE;
#endif
HANDLE file_handle() const
{
return file_handle_;
}
protected:
size_t query_file_size_();
char* data_;
size_t offset_;
size_t mapped_size_;
size_t file_size_;
int granularity_;
HANDLE file_handle_;
#if defined(_WIN32)
HANDLE file_mapping_handle_;
#endif
};
class read_only_mmf: public base_mmf
{
public:
explicit read_only_mmf(char const* pathname = 0, bool map_all = true);
void open(char const* pathname, bool map_all = true);
char const* data() const { return data_; }
void map(size_t offset = 0, size_t size = 0);
};
enum mmf_exists_mode
{
if_exists_fail,
if_exists_just_open,
if_exists_map_all,
if_exists_truncate,
};
enum mmf_doesnt_exist_mode
{
if_doesnt_exist_fail,
if_doesnt_exist_create,
};
class writable_mmf: public base_mmf
{
public:
explicit writable_mmf(char const* pathname = 0,
mmf_exists_mode exists_mode = if_exists_fail,
mmf_doesnt_exist_mode doesnt_exist_mode = if_doesnt_exist_create);
void open(char const* pathname,
mmf_exists_mode exists_mode = if_exists_fail,
mmf_doesnt_exist_mode doesnt_exist_mode = if_doesnt_exist_create);
char* data() { return data_; }
void map(size_t offset = 0, size_t size = 0);
bool flush();
};
}
#endif // MEMORY_MAPPED_FILE_HPP