Add COPYING file
[ps3tools.git] / mingw_mmap.c
blob19995274fa703a7b21a0482a473a0079b4b22698
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!
12 #include <stdio.h>
13 #include "mingw_mmap.h"
15 extern int getpagesize();
17 /**
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) {
23 (void)nProt;
24 HANDLE hHandle;
26 if (pStart != NULL || !(nFlags & MAP_PRIVATE)) {
27 printf("Invalid usage of mingw_mmap");
28 return NULL;
31 if (oOffset % getpagesize() != 0) {
32 printf("Offset does not match the memory allocation granularity");
33 return NULL;
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);
41 return pStart;
44 /**
45 * Use UnmapViewOfFile to undo mmap() above.
46 * @param pStart
47 * @param length - Not used, kept for compatibility.
48 * @return boolean; no checks are performed.
50 int mingw_munmap(void *pStart, size_t sLength) {
51 (void)sLength;
53 if (UnmapViewOfFile(pStart) != 0)
54 return FALSE;
56 return TRUE;