summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src/MappedFile.cpp
blob: e22e12172253cd7df663962e864192234ca81547 (plain)
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
// Copyright (C) 2003-2008 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif

#include "Common.h"
#include "MappedFile.h"

namespace Common
{
class CMappedFile
	: public IMappedFile
{
	public:

		CMappedFile(void);
		~CMappedFile(void);
		bool Open(const char* _szFilename);
		bool IsOpen(void);
		void Close(void);
		u64 GetSize(void);
		u8* Lock(u64 _offset, u64 _size);
		void Unlock(u8* ptr);


	private:

		u64 size;

		typedef std::map<u8*, u8*>Lockmap;
		Lockmap lockMap;
#ifdef _WIN32
		HANDLE hFile;
		HANDLE hFileMapping;
#elif POSIX
		int fd;
		typedef std::map<u8*, size_t>Sizemap;
		Sizemap sizeMap;
#endif

		int granularity;
};


CMappedFile::CMappedFile()
{
#ifdef _WIN32
	hFile = INVALID_HANDLE_VALUE;
	SYSTEM_INFO info;
	GetSystemInfo(&info);
	granularity = (int)info.dwAllocationGranularity;
#elif POSIX
	fd = -1;
	granularity = getpagesize(); //sysconf(_SC_PAGE_SIZE);
#endif
}


CMappedFile::~CMappedFile()
{
	Close();
}


bool CMappedFile::Open(const char* filename)
{
	Close();
#ifdef _WIN32
	hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0,   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		return(false);
	}

	hFileMapping = CreateFileMapping(hFile, 0, PAGE_READONLY, 0, 0, NULL);

	if (hFileMapping == NULL)
	{
		CloseHandle(hFile);
		hFile = 0;
		return(false);
	}

	u32 high = 0;
	u32 low = GetFileSize(hFile, (LPDWORD)&high);
	size = (u64)low | ((u64)high << 32);
#elif POSIX
	fd   = open(filename, O_RDONLY);
	size = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);
#endif

	return(true);
}


bool CMappedFile::IsOpen()
{
#ifdef _WIN32
	return(hFile != INVALID_HANDLE_VALUE);

#elif POSIX
	return(fd != -1);
#endif

}


u64 CMappedFile::GetSize()
{
	return(size);
}


void CMappedFile::Close()
{
#ifdef _WIN32
	if (hFile != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFileMapping);
		CloseHandle(hFile);
		lockMap.clear();
		hFile = INVALID_HANDLE_VALUE;
	}

#elif POSIX
	if (fd != -1)
	{
		lockMap.clear();
		sizeMap.clear();
		close(fd);
	}

	fd = -1;
#endif
}


u8* CMappedFile::Lock(u64 offset, u64 _size)
{
#ifdef _WIN32
	if (hFile != INVALID_HANDLE_VALUE)
#elif POSIX
	if (fd != -1)
#endif
	{
		u64 realOffset = offset & ~(granularity - 1);
		s64 difference = offset - realOffset;
		u64 fake_size = (difference + _size + granularity - 1) & ~(granularity - 1);
#ifdef _WIN32
		u8* realPtr = (u8*)MapViewOfFile(hFileMapping, FILE_MAP_READ, (DWORD)(realOffset >> 32), (DWORD)realOffset, (SIZE_T)(_size));

		if (realPtr == NULL)
		{
			return(NULL);
		}

#elif POSIX
		// TODO
		u8* realPtr = (u8*)mmap(0, fake_size, PROT_READ, MAP_PRIVATE, fd, (off_t)realOffset);

		if (!realPtr)
		{
			PanicAlert("Map Failed");
			exit(0);
		}
#endif

		u8* fakePtr = realPtr + difference;
		//add to map
		lockMap[fakePtr] = realPtr;
#ifndef _WIN32
		sizeMap[fakePtr] = _size + difference;
#endif
		return(fakePtr);
	}
	else
	{
		return(0);
	}
}


void CMappedFile::Unlock(u8* ptr)
{
	if (ptr != 0)
	{
		Lockmap::iterator iter = lockMap.find(ptr);

		if (iter != lockMap.end())
		{
#ifdef _WIN32
			UnmapViewOfFile((*iter).second);
#else
			munmap((*iter).second, sizeMap[ptr]);
#endif
			lockMap.erase(iter);
		}
		else
		{
			PanicAlert("CMappedFile : Unlock failed");
		}
	}
}


IMappedFile* IMappedFile::CreateMappedFile(void)
{
	return(new CMappedFile);
}
} // end of namespace Common