src/platform/windows/memory.c (view raw)
1#include "memory.h"
2
3#include <io.h>
4#include <Windows.h>
5
6void* anonymousMemoryMap(size_t size) {
7 HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size & 0xFFFFFFFF, 0);
8 return MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, size);
9}
10
11void* fileMemoryMap(int fd, size_t size, int flags) {
12 int createFlags = PAGE_READONLY;
13 int mapFiles = FILE_MAP_READ;
14 if (flags & MEMORY_WRITE) {
15 createFlags = PAGE_READWRITE;
16 mapFiles = FILE_MAP_WRITE;
17 }
18 size_t location = lseek(fd, 0, SEEK_CUR);
19 size_t fileSize = lseek(fd, 0, SEEK_END);
20 lseek(fd, location, SEEK_SET);
21 if (size > fileSize) {
22 size = fileSize;
23 }
24 HANDLE hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
25 return MapViewOfFile(hMap, mapFiles, 0, 0, size);
26}
27
28void mappedMemoryFree(void* memory, size_t size) {
29 // TODO fill in
30}