revert between 56095 -> 55830 in arch
[AROS.git] / arch / all-mingw32 / bootstrap / memory.c
blob043a9fed5ce458c25e427b4a0ab3877e392cf643
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <io.h>
7 #include <stdio.h>
8 #include <windows.h>
10 #include "sharedmem.h"
12 /* This is not included in my Mingw32 headers */
13 #ifndef FILE_MAP_EXECUTE
14 #define FILE_MAP_EXECUTE 0x0020
15 #endif
17 #define D(x)
18 #define ID_LEN 64
20 HANDLE RAM_Handle = NULL;
21 void *RAM_Address = NULL;
23 void *AllocateRO(size_t len)
25 return VirtualAlloc(NULL, len, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
29 * Commit executable and read-only state for kickstart's .code
31 int SetRO(void *addr, size_t len)
33 DWORD old;
35 return !VirtualProtect(addr, len, PAGE_EXECUTE_READ, &old);
38 void *AllocateRW(size_t len)
40 return VirtualAlloc(NULL, len, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
43 void *AllocateRAM32(size_t len)
45 SECURITY_ATTRIBUTES sa;
46 void *addr = NULL;
47 const char *var = getenv(SHARED_RAM_VAR);
49 if (var)
51 D(fprintf(stderr, "[AllocateRAM] Found RAM specification: %s\n", var));
52 if (sscanf(var, "%p:%p", &RAM_Handle, &addr) != 2) {
53 D(fprintf(stderr, "[AllocateRAM] Error parsing specification\n"));
54 RAM_Handle = NULL;
55 addr = NULL;
58 D(fprintf(stderr, stderr, "[AllocateRAM] Inherited memory handle 0x%p address 0x%p\n", RAM_Handle, addr));
60 if (!RAM_Handle)
62 sa.nLength = sizeof(sa);
63 sa.lpSecurityDescriptor = NULL;
64 sa.bInheritHandle = TRUE;
65 RAM_Handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_EXECUTE_READWRITE, 0, len, NULL);
67 if (!RAM_Handle)
69 D(fprintf(stderr, "[AllocateRAM] PAGE_EXECUTE_READWRITE failed, retrying with PAGE_READWRITE\n"));
70 RAM_Handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, len, NULL);
72 D(fprintf(stderr, "[AllocateRAM] Shared memory handle 0x%p\n", RAM_Handle));
73 if (!RAM_Handle)
74 return NULL;
76 RAM_Address = MapViewOfFileEx(RAM_Handle, FILE_MAP_ALL_ACCESS|FILE_MAP_EXECUTE, 0, 0, 0, addr);
77 if (!RAM_Address)
79 D(fprintf(stderr, "[AllocateRAM] FILE_MAP_EXECUTE failed, retrying without it\n"));
80 RAM_Address = MapViewOfFileEx(RAM_Handle, FILE_MAP_ALL_ACCESS, 0, 0, 0, addr);
82 D(fprintf(stderr, "[AllocateRAM] Mapped at 0x%p\n", RAM_Address));
84 if (!RAM_Address)
86 CloseHandle(RAM_Handle);
87 RAM_Handle = NULL;
90 return RAM_Address;