-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMemoryMapFile.cpp
More file actions
285 lines (221 loc) · 5.75 KB
/
Copy pathMemoryMapFile.cpp
File metadata and controls
285 lines (221 loc) · 5.75 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// MemoryMapFile.cpp
// Test
//
// Created by 周贺伟 on 2017/8/3.
// Copyright © 2017年 zhouhewei. All rights reserved.
//
#include "MemoryMapFile.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string>
IMemoryMapFile* createMemoryFileInstance(const char* fileName, long initFileSize)
{
return new CMemoryMapFile(fileName, initFileSize);
}
CMemoryMapFile::CMemoryMapFile()
{
m_size = 0;
m_capacity = 0;
m_map_file_ptr = NULL;
m_file_write_ptr = NULL;
m_file_read_ptr = NULL;
m_fd = -1;
}
CMemoryMapFile::CMemoryMapFile(_IN_ const char* fileName, _IN_ long initFileSize)
{
m_size = 0;
m_capacity = 0;
m_map_file_ptr = NULL;
m_file_write_ptr = NULL;
m_file_read_ptr = NULL;
m_fd = -1;
MMOpen(fileName, initFileSize);
}
CMemoryMapFile::~CMemoryMapFile()
{
MMClose();
}
//write functions
long CMemoryMapFile::getWritePosition()
{
if (!m_file_write_ptr) {
return 0;
}
return m_file_write_ptr - (char*)m_map_file_ptr;
}
bool CMemoryMapFile::setWritePosition(_IN_ long pos)
{
if (!m_file_write_ptr) {
return false;
}
if (pos >= m_capacity) {
return false;
}
m_file_write_ptr = (char*)m_map_file_ptr + pos;
return true;
}
bool CMemoryMapFile::writeData(_IN_ const void* buffer, _IN_ long size)
{
if (!m_file_write_ptr) {
return false;
}
long writeSpace = m_capacity - (m_file_write_ptr - (char*)m_map_file_ptr);
if (writeSpace < size) {
if(!reSizeFile(size - writeSpace + m_capacity)){
return false;
}
}
memcpy(m_file_write_ptr, buffer, size);
m_file_write_ptr += size;
if (m_file_write_ptr > (char*)m_map_file_ptr + m_size) {
m_size = m_file_write_ptr - (char*)m_map_file_ptr;
}
return true;
}
bool CMemoryMapFile::writeData(_IN_ long startPosition, _IN_ const void* buffer, _IN_ long size)
{
if (!m_file_write_ptr) {
return false;
}
if (setWritePosition(startPosition)) {
return writeData(buffer, size);
}
return false;
}
//read functions
long CMemoryMapFile::getReadPosition()
{
if (!m_file_read_ptr) {
return 0;
}
return m_file_read_ptr - (char*)m_map_file_ptr;
}
bool CMemoryMapFile::setReadPosition(_IN_ long pos)
{
if (!m_file_read_ptr) {
return false;
}
if (pos >= m_size) {
return false;
}
m_file_read_ptr = (char*)m_map_file_ptr + pos;
return true;
}
bool CMemoryMapFile::readData(_OUT_ void* buffer, _IN_ long size)
{
if (!m_file_read_ptr) {
return false;
}
long readSpace = m_size - (m_file_read_ptr - (char*)m_map_file_ptr);
if (readSpace < size) {
return false;
}
memcpy(buffer, m_file_read_ptr, size);
m_file_read_ptr += size;
return true;
}
bool CMemoryMapFile::readData(_IN_ long startPosition, _OUT_ void* buffer, _IN_ long size)
{
if (!m_file_read_ptr) {
return false;
}
if (setReadPosition(startPosition)) {
return readData(buffer, size);
}
return false;
}
//get property functions
const void* CMemoryMapFile::getRawMemory()
{
return m_map_file_ptr;
}
long CMemoryMapFile::getCapacity()
{
return m_capacity;
}
long CMemoryMapFile::getFileSize()
{
return m_size;
}
//control functions
bool CMemoryMapFile::MMOpen(_IN_ const char* fileName, _IN_ long initFileSize)
{
std::string file_name = fileName;
if (file_name.length() == 0) {
return false;
}
m_fd = open(file_name.c_str() ,O_RDWR | O_CREAT | O_TRUNC, 0666);
if(m_fd == -1){
printf("open file %s failed!:%m\n", file_name.c_str());
return false;
}
lseek(m_fd, initFileSize - 1, SEEK_SET);
write(m_fd, "z", 1);
struct stat st;
int re = fstat(m_fd, &st);
if(re == -1){
printf("get file %s 's size failed!:%m\n", file_name.c_str());
close(m_fd);
return false;
}
if (st.st_size != initFileSize) {
close(m_fd);
return false;
}
m_map_file_ptr = mmap(NULL, initFileSize, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (!m_map_file_ptr) {
close(m_fd);
return false;
}
m_size = 0;
m_capacity = initFileSize;
m_file_write_ptr = (char*)m_map_file_ptr;
m_file_read_ptr = (char*)m_map_file_ptr;
return true;
}
bool CMemoryMapFile::MMClose()
{
if (!m_map_file_ptr) {
return true;
}
int ret = munmap(m_map_file_ptr, m_capacity);
if (ret) {
printf("close file failed!:%m\n");
return false;
}
m_map_file_ptr = NULL;
m_size = 0;
m_capacity = 0;
m_file_write_ptr = NULL;
m_file_read_ptr = NULL;
return true;
}
//private funtcions
bool CMemoryMapFile::reSizeFile(long new_size)
{
if (new_size <= m_capacity) {
return false;
}
long current_write_pos = m_file_write_ptr - (char*)m_map_file_ptr;
long current_read_pos = m_file_read_ptr - (char*)m_map_file_ptr;
int ret = munmap(m_map_file_ptr, m_capacity);
if (ret) {
printf("close file failed!:%m\n");
return false;
}
lseek(m_fd, new_size - 1, SEEK_SET);
write(m_fd, "z", 1);
m_map_file_ptr = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (!m_map_file_ptr) {
printf("resize file failed, file will be closed!:%m\n");
close(m_fd);
return false;
}
m_file_write_ptr = (char*)m_map_file_ptr + current_write_pos;
m_file_read_ptr = (char*)m_map_file_ptr + current_read_pos;
m_capacity = new_size;
return true;
}