-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemorymapped.cpp
122 lines (108 loc) · 2.59 KB
/
memorymapped.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* memorymapped.cpp
* Original Copyright (c) 2013 Stephan Brumme. All rights reserved.
* see http://create.stephan-brumme.com/disclaimer.html
*/
#include <sstream>
#include <cstdio>
#include "memorymapped.h"
// OS-specific
#if defined(MEMMAPPED_USING_WINDOWS)
// Windows
#include <windows.h>
#else
// Linux
// enable large file support on 32 bit systems
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif
#define _FILE_OFFSET_BITS 64
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
// and include needed headers
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#endif
namespace MemoryMapped
{
File::File():
m_filename (),
m_filesize (0),
m_hint (Normal),
m_handle (0),
m_mappedBytes(0),
m_mappedView (NULL),
m_mappedFile (NULL)
{
}
File::File(const std::string& filename, size_t mappedBytes, CacheHint hint):
m_filename (filename),
m_filesize (0),
m_hint (hint),
m_handle (0),
m_mappedBytes(mappedBytes),
m_mappedView (NULL),
m_mappedFile (NULL)
{
open(filename, mappedBytes, hint);
}
File::~File()
{
close();
}
bool File::open(const std::string& filename, size_t mappedBytes, CacheHint hint)
{
m_filename = filename;
return openReal(mappedBytes, hint);
}
bool File::errOffset()
{
throw IOError(m_filename, "trying to read beyond file (offset > filesize)");
return false;
}
unsigned char File::operator[](size_t offset) const
{
return data()[offset];
}
unsigned char File::at(size_t offset) const
{
// checks
if(!m_mappedView)
{
throw IOError(m_filename, "no view mapped");
}
if (offset >= m_filesize)
{
throw IOError(m_filename, "mapped view is not large enough");
}
return operator[](offset);
}
const unsigned char* File::data() const
{
return (const unsigned char*)m_mappedView;
}
bool File::isValid() const
{
return (m_mappedView != NULL);
}
uint64_t File::size() const
{
return m_filesize;
}
size_t File::mappedSize() const
{
return m_mappedBytes;
}
}
#if defined(MEMMAPPED_USING_WINDOWS)
#include "impl.win32.cpp"
#else
#include "impl.linux.cpp"
#endif