-
Notifications
You must be signed in to change notification settings - Fork 9
/
MemoryMap.cpp
152 lines (133 loc) · 3.01 KB
/
MemoryMap.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
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
#include "MemoryMap.h"
#ifdef _MSC_VER
#include <stdio.h>
#include <malloc.h>
#include <Windows.h>
#pragma warning(disable:4456)
class MemoryMapImpl :public MemoryMap
{
public:
MemoryMapImpl(const char *mappingObject,uint64_t size,bool createOk)
{
mData = NULL;
mMapFile = NULL;
mMapHandle = NULL;
bool createFile = true;
bool fileOk = false;
HANDLE h = CreateFileA(mappingObject, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if ( h != NULL )
{
size = getFileSize(h);
fileOk = true;
createFile = false;
printf("Found previous existing mapping file '%s' and using it.\r\n", mappingObject );
CloseHandle(h);
}
if ( createFile && createOk )
{
printf("Creating memory map file: %s\r\n", mappingObject);
HANDLE h = CreateFileA(mappingObject, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ( h != NULL )
{
#define ONEMB (1024*1024)
char *temp = (char *)malloc(ONEMB);
if ( temp )
{
memset(temp,0,ONEMB);
uint64_t writeSize = size;
while ( writeSize > 0 )
{
DWORD w = writeSize >= ONEMB ? ONEMB : (DWORD)writeSize;
DWORD bytesWritten=0;
WriteFile(h,temp,w,&bytesWritten,NULL);
if ( bytesWritten != w )
{
printf("Failed to write to file '%s', out of disk space?\r\n", mappingObject );
break;
}
writeSize-=w;
}
if ( writeSize == 0)
{
fileOk = true;
printf("Finished creating mapping file '%s'\r\n", mappingObject );
}
}
CloseHandle(h);
}
}
if ( fileOk )
{
mMapSize = size;
mMapFile = CreateFileA(mappingObject, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (mMapFile != INVALID_HANDLE_VALUE)
{
mMapHandle = CreateFileMappingA(mMapFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (mMapHandle == INVALID_HANDLE_VALUE)
{
CloseHandle(mMapFile);
mMapFile = NULL;
}
else
{
mData = MapViewOfFile(mMapHandle, FILE_MAP_WRITE, 0, 0, 0);
if ( mData == NULL)
{
CloseHandle(mMapHandle);
CloseHandle(mMapFile);
mMapHandle = NULL;
mMapFile = NULL;
}
}
}
}
}
~MemoryMapImpl(void)
{
if ( mMapHandle )
{
CloseHandle(mMapHandle);
}
if ( mMapFile )
{
CloseHandle(mMapFile);
}
}
virtual void *getBaseAddress(void)
{
return mData;
}
virtual void release(void)
{
delete this;
}
virtual uint64_t getFileSize(void) override final
{
return mMapSize;
}
uint64_t getFileSize(HANDLE h)
{
DWORD highSize=0;
DWORD lowSize = GetFileSize(h,&highSize);
uint64_t ret;
DWORD *d = (DWORD *)&ret;
d[0] = lowSize;
d[1] = highSize;
return ret;
}
uint64_t mMapSize;
HANDLE mMapFile;
HANDLE mMapHandle;
void *mData;
};
MemoryMap * createMemoryMap(const char *fileName,uint64_t size,bool createOk)
{
MemoryMapImpl *m = new MemoryMapImpl(fileName,size,createOk);
if ( m->getBaseAddress() == NULL )
{
m->release();
m = NULL;
}
return static_cast< MemoryMap *>(m);
}
#endif