-
Notifications
You must be signed in to change notification settings - Fork 14
/
mingw_mmap.c
61 lines (49 loc) · 1.49 KB
/
mingw_mmap.c
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
/** This code is adapted from:
* https://kerneltrap.org/mailarchive/git/2008/11/21/4186494
* Original code by Vasyl Vavrychuk.
*
* This file is part of Rockstars.
* Coded in Hungarian Notation so it looks stupid. :D
*
* If you haven't seen the header file, call mmap() and munmap() not the
* function names below!
*/
#ifdef __MINGW32__
#include <stdio.h>
#include "mingw_mmap.h"
extern int getpagesize(void);
/**
* Use CreateFileMapping and MapViewOfFile to simulate POSIX mmap().
* Why Microsoft won't just implement these is beyond everyone's comprehension.
* @return pointer or NULL
*/
void *mingw_mmap(void *pStart, size_t sLength, int nProt, int nFlags, int nFd, off_t oOffset) {
(void)nProt;
HANDLE hHandle;
if (pStart != NULL || !(nFlags & MAP_PRIVATE)) {
printf("Invalid usage of mingw_mmap");
return NULL;
}
if (oOffset % getpagesize() != 0) {
printf("Offset does not match the memory allocation granularity");
return NULL;
}
hHandle = CreateFileMapping((HANDLE)_get_osfhandle(nFd), NULL, PAGE_WRITECOPY, 0, 0, NULL);
if (hHandle != NULL) {
pStart = MapViewOfFile(hHandle, FILE_MAP_COPY, 0, oOffset, sLength);
}
return pStart;
}
/**
* Use UnmapViewOfFile to undo mmap() above.
* @param pStart
* @param length - Not used, kept for compatibility.
* @return boolean; no checks are performed.
*/
int mingw_munmap(void *pStart, size_t sLength) {
(void)sLength;
if (UnmapViewOfFile(pStart) != 0)
return FALSE;
return TRUE;
}
#endif