1 /** This code is adapted from:
2 * https://kerneltrap.org/mailarchive/git/2008/11/21/4186494
3 * Original code by Vasyl Vavrychuk.
5 * This file is part of Rockstars.
6 * Coded in Hungarian Notation so it looks stupid. :D
8 * If you haven't seen the header file, call mmap() and munmap() not the
9 * function names below!
13 #include "mingw_mmap.h"
15 extern int getpagesize();
18 * Use CreateFileMapping and MapViewOfFile to simulate POSIX mmap().
19 * Why Microsoft won't just implement these is beyond everyone's comprehension.
20 * @return pointer or NULL
22 void *mingw_mmap(void *pStart
, size_t sLength
, int nProt
, int nFlags
, int nFd
, off_t oOffset
) {
26 if (pStart
!= NULL
|| !(nFlags
& MAP_PRIVATE
)) {
27 printf("Invalid usage of mingw_mmap");
31 if (oOffset
% getpagesize() != 0) {
32 printf("Offset does not match the memory allocation granularity");
36 hHandle
= CreateFileMapping((HANDLE
)_get_osfhandle(nFd
), NULL
, PAGE_WRITECOPY
, 0, 0, NULL
);
37 if (hHandle
!= NULL
) {
38 pStart
= MapViewOfFile(hHandle
, FILE_MAP_COPY
, 0, oOffset
, sLength
);
45 * Use UnmapViewOfFile to undo mmap() above.
47 * @param length - Not used, kept for compatibility.
48 * @return boolean; no checks are performed.
50 int mingw_munmap(void *pStart
, size_t sLength
) {
53 if (UnmapViewOfFile(pStart
) != 0)